Skip to content

Commit

Permalink
LS: expose view analyzed crates command (#6266)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotmag769 authored Aug 26, 2024
1 parent 536bcff commit e28e48a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
5 changes: 5 additions & 0 deletions vscode-cairo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
"command": "cairo.expandMacro",
"title": "Recursively expand macros for item at caret",
"category": "Cairo"
},
{
"command": "cairo.viewAnalyzedCrates",
"title": "View currently analyzed crates",
"category": "Cairo"
}
],
"configuration": [
Expand Down
7 changes: 6 additions & 1 deletion vscode-cairo/src/cairols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { Context } from "./context";
import { Scarb } from "./scarb";
import { isScarbProject } from "./scarbProject";
import { StandaloneLS } from "./standalonels";
import { registerMacroExpandProvider, registerVfsProvider } from "./textDocumentProviders";
import {
registerMacroExpandProvider,
registerVfsProvider,
registerViewAnalyzedCratesProvider,
} from "./textDocumentProviders";

export interface LanguageServerExecutableProvider {
languageServerExecutable(): lc.Executable;
Expand Down Expand Up @@ -67,6 +71,7 @@ export async function setupLanguageServer(ctx: Context): Promise<lc.LanguageClie

registerVfsProvider(client, ctx);
registerMacroExpandProvider(client, ctx);
registerViewAnalyzedCratesProvider(client, ctx);

client.onNotification("scarb/could-not-find-scarb-executable", () => notifyScarbMissing(ctx));

Expand Down
5 changes: 5 additions & 0 deletions vscode-cairo/src/lspRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ export const vfsProvide = new lc.RequestType<
ProvideVirtualFileResponse,
void
>("vfs/provide");

export type ViewAnalyzedCratesResponse = string;
export const viewAnalyzedCrates = new lc.RequestType0<ViewAnalyzedCratesResponse, void>(
"cairo/viewAnalyzedCrates",
);
42 changes: 41 additions & 1 deletion vscode-cairo/src/textDocumentProviders.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as lc from "vscode-languageclient/node";
import * as vscode from "vscode";
import { Context } from "./context";
import { expandMacro, vfsProvide } from "./lspRequests";
import { expandMacro, vfsProvide, viewAnalyzedCrates } from "./lspRequests";

export const registerVfsProvider = (client: lc.LanguageClient, ctx: Context) => {
const eventEmitter = new vscode.EventEmitter<vscode.Uri>();
Expand Down Expand Up @@ -61,3 +61,43 @@ export const registerMacroExpandProvider = (client: lc.LanguageClient, ctx: Cont
}),
);
};

export const registerViewAnalyzedCratesProvider = (client: lc.LanguageClient, ctx: Context) => {
const uri = vscode.Uri.parse(
"cairo-view-analyzed-crates://viewAnalyzedCrates/[ANALYZED_CRATES].txt",
);
const eventEmitter = new vscode.EventEmitter<vscode.Uri>();

const tdcp: vscode.TextDocumentContentProvider = {
provideTextDocumentContent: () => client.sendRequest(viewAnalyzedCrates),
onDidChange: eventEmitter.event,
};

ctx.extension.subscriptions.push(
vscode.workspace.registerTextDocumentContentProvider("cairo-view-analyzed-crates", tdcp),
);

ctx.extension.subscriptions.push(
vscode.commands.registerCommand("cairo.viewAnalyzedCrates", async () => {
const document = await vscode.workspace.openTextDocument(uri);

eventEmitter.fire(uri);

return vscode.window.showTextDocument(document, vscode.ViewColumn.Two, true);
}),
);

ctx.extension.subscriptions.push(
vscode.workspace.onDidChangeTextDocument((e) => {
const path = e.document.uri.path;
const relevant_suffixes = [".cairo", "Scarb.toml", "Scarb.lock", "cairo_project.toml"];

for (const suffix of relevant_suffixes) {
if (path.endsWith(suffix)) {
eventEmitter.fire(uri);
break;
}
}
}),
);
};

0 comments on commit e28e48a

Please sign in to comment.