Skip to content

Commit

Permalink
Fix ast viewer not working with synthetic files
Browse files Browse the repository at this point in the history
  • Loading branch information
kralicky committed Oct 27, 2023
1 parent 3a4ba5a commit b0e6957
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
41 changes: 30 additions & 11 deletions editors/vscode/client/src/astviewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ export class ASTViewer implements vscode.TextDocumentContentProvider {
this.virtualUrisByFile.delete(doc.uri.toString())
break
}
case "protoast2":
case "protoast": {
this.virtualUrisByFile.delete(fromProtoAstURi(doc.uri).toString())
this.virtualUrisByFile.delete(fromProtoAstUri(doc.uri).toString())
break
}
}
Expand Down Expand Up @@ -57,7 +58,7 @@ export class ASTViewer implements vscode.TextDocumentContentProvider {
uri: vscode.Uri,
token: vscode.CancellationToken,
): vscode.ProviderResult<string> {
const fileUri = fromProtoAstURi(uri)
const fileUri = fromProtoAstUri(uri)
if (!this.virtualUrisByFile.has(fileUri.toString())) {
return ""
}
Expand All @@ -71,15 +72,33 @@ export class ASTViewer implements vscode.TextDocumentContentProvider {
}

export function toProtoAstUri(uri: vscode.Uri): vscode.Uri {
return uri.with({
scheme: "protoast",
path: uri.path + " [AST]",
})
if (uri.scheme === "file") {
return uri.with({
scheme: "protoast",
path: uri.path + " [AST]",
})
} else if (uri.scheme === "proto") {
return uri.with({
scheme: "protoast2",
path: uri.path + " [AST]",
})
} else {
return uri
}
}

export function fromProtoAstURi(uri: vscode.Uri): vscode.Uri {
return uri.with({
scheme: "file",
path: uri.path.replace(/ \[AST\]$/, ""),
})
export function fromProtoAstUri(uri: vscode.Uri): vscode.Uri {
if (uri.scheme === "protoast") {
return uri.with({
scheme: "file",
path: uri.path.replace(/ \[AST\]$/, ""),
})
} else if (uri.scheme === "protoast2") {
return uri.with({
scheme: "proto",
path: uri.path.replace(/ \[AST\]$/, ""),
})
} else {
return uri
}
}
11 changes: 9 additions & 2 deletions editors/vscode/client/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from "vscode"
import { LanguageClient } from "vscode-languageclient/node"
import { ASTViewer, fromProtoAstURi } from "./astviewer"
import { ASTViewer, fromProtoAstUri } from "./astviewer"
import { buildLanguageClient } from "./client"
import { initCommands } from "./commands"

Expand All @@ -13,12 +13,19 @@ export async function activate(context: vscode.ExtensionContext) {
client.start()

const astViewer = new ASTViewer((uri) => {
return client.sendRequest("protols/ast", fromProtoAstURi(uri).toString())
return client.sendRequest("protols/ast", fromProtoAstUri(uri).toString())
})
context.subscriptions.push(
astViewer,
vscode.workspace.registerTextDocumentContentProvider("protoast", astViewer),
)
context.subscriptions.push(
astViewer,
vscode.workspace.registerTextDocumentContentProvider(
"protoast2",
astViewer,
),
)

context.subscriptions.push(
vscode.commands.registerCommand("protols.restart", async () => {
Expand Down

0 comments on commit b0e6957

Please sign in to comment.