Skip to content

Commit

Permalink
Cleaning up file access API
Browse files Browse the repository at this point in the history
  • Loading branch information
idavis committed Aug 15, 2023
1 parent 51bb079 commit 6e72294
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
6 changes: 2 additions & 4 deletions vscode/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ export const qsharpExtensionId = "qsharp-vscode";

export interface FileAccessor {
normalizePath(path: string): string;
convertToWindowsPathSeparator(path: string): string;
resolvePathToUri(path: string): Uri;
openFile(path: string): Promise<TextDocument>;
openPath(path: string): Promise<TextDocument>;
openUri(uri: Uri): Promise<TextDocument>;
readFile(path: string): Promise<Uint8Array>;
readFileAsString(path: string): Promise<string>;
writeFile(path: string, contents: Uint8Array): Promise<void>;
}
18 changes: 5 additions & 13 deletions vscode/src/debugger/activate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,33 +128,25 @@ export const workspaceFileAccessor: FileAccessor = {
normalizePath(path: string): string {
return path.replace(/\\/g, "/");
},
convertToWindowsPathSeparator(path: string): string {
return path.replace(/\//g, "\\");
},
resolvePathToUri(path: string): vscode.Uri {
const normalizedPath = this.normalizePath(path);
return vscode.Uri.parse(normalizedPath, false);
},
async openFile(path: string): Promise<vscode.TextDocument> {
async openPath(path: string): Promise<vscode.TextDocument> {
const uri: vscode.Uri = this.resolvePathToUri(path);
return this.openUri(uri);
},
async openUri(uri: vscode.Uri): Promise<vscode.TextDocument> {
try {
return await vscode.workspace.openTextDocument(uri);
} catch {
const path = uri.toString().replace(/\//g, "\\");
const path = this.convertToWindowsPathSeparator(uri.toString());
return await vscode.workspace.openTextDocument(vscode.Uri.file(path));
}
},
async readFile(path: string): Promise<Uint8Array> {
let uri: vscode.Uri = this.resolvePathToUri(path);
return await vscode.workspace.fs.readFile(uri);
},
async readFileAsString(path: string): Promise<string> {
const contents = await this.readFile(path);
return new TextDecoder().decode(contents);
},
async writeFile(path: string, contents: Uint8Array) {
await vscode.workspace.fs.writeFile(this.resolvePathToUri(path), contents);
},
};

class InlineDebugAdapterFactory
Expand Down
6 changes: 3 additions & 3 deletions vscode/src/debugger/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ export class QscDebugSession extends LoggingDebugSession {
};

const file = await this.fileAccessor
.openFile(args.source.path ?? "")
.openPath(args.source.path ?? "")
.catch((e) => {
log.error(`Failed to open file: ${e}`);
const fileUri = this.fileAccessor.resolvePathToUri(
Expand Down Expand Up @@ -444,7 +444,7 @@ export class QscDebugSession extends LoggingDebugSession {
log.trace(`setBreakPointsRequest: %O`, args);

const file = await this.fileAccessor
.openFile(args.source.path ?? "")
.openPath(args.source.path ?? "")
.catch((e) => {
log.error(`setBreakPointsRequest - Failed to open file: ${e}`);
const fileUri = this.fileAccessor.resolvePathToUri(
Expand Down Expand Up @@ -549,7 +549,7 @@ export class QscDebugSession extends LoggingDebugSession {
log.trace(`frames: path %O`, f.path);

const file = await this.fileAccessor
.openFile(f.path ?? "")
.openPath(f.path ?? "")
.catch((e) => {
log.error(`stackTraceRequest - Failed to open file: ${e}`);
const fileUri = this.fileAccessor.resolvePathToUri(f.path ?? "");
Expand Down

0 comments on commit 6e72294

Please sign in to comment.