From 2f137f79350d3d99d68425e1e218539dd28c3e55 Mon Sep 17 00:00:00 2001 From: Piotr Magiera Date: Fri, 23 Aug 2024 17:59:35 +0200 Subject: [PATCH 1/3] LS: expose view analyzed crates command --- vscode-cairo/package.json | 6 ++++ vscode-cairo/src/cairols.ts | 7 +++- vscode-cairo/src/lspRequests.ts | 5 +++ vscode-cairo/src/textDocumentProviders.ts | 42 ++++++++++++++++++++++- 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/vscode-cairo/package.json b/vscode-cairo/package.json index 4ef112c1d98..c934d8a419e 100644 --- a/vscode-cairo/package.json +++ b/vscode-cairo/package.json @@ -73,6 +73,11 @@ "command": "cairo.expandMacro", "title": "Recursively expand macros for item at caret", "category": "Cairo" + }, + { + "command": "cairo.viewAnalyzedCrates", + "title": "View crates currently analyzed by LS", + "category": "Cairo" } ], "configuration": [ @@ -130,6 +135,7 @@ } ], "configurationDefaults": { + "explorer.fileNesting.enabled": true, "[cairo]": { "editor.tabSize": 4, "editor.insertSpaces": true, diff --git a/vscode-cairo/src/cairols.ts b/vscode-cairo/src/cairols.ts index 96953aba5f6..1179faae520 100644 --- a/vscode-cairo/src/cairols.ts +++ b/vscode-cairo/src/cairols.ts @@ -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"; import { NotificationType } from "vscode-jsonrpc/lib/common/messages"; import * as cp from "node:child_process"; @@ -64,6 +68,7 @@ export async function setupLanguageServer(ctx: Context): Promise notifyScarbMissing(ctx)); diff --git a/vscode-cairo/src/lspRequests.ts b/vscode-cairo/src/lspRequests.ts index 41b924316fa..f2691b8c867 100644 --- a/vscode-cairo/src/lspRequests.ts +++ b/vscode-cairo/src/lspRequests.ts @@ -20,3 +20,8 @@ export const vfsProvide = new lc.RequestType< ProvideVirtualFileResponse, void >("vfs/provide"); + +export type viewAnalyzedCratesResponse = string; +export const viewAnalyzedCrates = new lc.RequestType0( + "cairo/viewAnalyzedCrates", +); diff --git a/vscode-cairo/src/textDocumentProviders.ts b/vscode-cairo/src/textDocumentProviders.ts index ab3b3ddcfd1..270805151d0 100644 --- a/vscode-cairo/src/textDocumentProviders.ts +++ b/vscode-cairo/src/textDocumentProviders.ts @@ -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(); @@ -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(); + + 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; + } + } + }), + ); +}; From 4956a313e289bcd493469b06e65bc883bbe8097f Mon Sep 17 00:00:00 2001 From: Piotr Magiera Date: Mon, 26 Aug 2024 14:41:34 +0200 Subject: [PATCH 2/3] review --- vscode-cairo/package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vscode-cairo/package.json b/vscode-cairo/package.json index c934d8a419e..3c3df174328 100644 --- a/vscode-cairo/package.json +++ b/vscode-cairo/package.json @@ -76,7 +76,7 @@ }, { "command": "cairo.viewAnalyzedCrates", - "title": "View crates currently analyzed by LS", + "title": "View currently analyzed crates", "category": "Cairo" } ], @@ -135,7 +135,6 @@ } ], "configurationDefaults": { - "explorer.fileNesting.enabled": true, "[cairo]": { "editor.tabSize": 4, "editor.insertSpaces": true, From bb5949c772d0592033744fb9dd3c4cd93a56f199 Mon Sep 17 00:00:00 2001 From: Piotr Magiera Date: Mon, 26 Aug 2024 17:03:14 +0200 Subject: [PATCH 3/3] fix --- vscode-cairo/src/cairols.ts | 2 -- vscode-cairo/src/lspRequests.ts | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/vscode-cairo/src/cairols.ts b/vscode-cairo/src/cairols.ts index cf402fb6f04..d24596bd19e 100644 --- a/vscode-cairo/src/cairols.ts +++ b/vscode-cairo/src/cairols.ts @@ -11,8 +11,6 @@ import { registerVfsProvider, registerViewAnalyzedCratesProvider, } from "./textDocumentProviders"; -import { NotificationType } from "vscode-jsonrpc/lib/common/messages"; -import * as cp from "node:child_process"; export interface LanguageServerExecutableProvider { languageServerExecutable(): lc.Executable; diff --git a/vscode-cairo/src/lspRequests.ts b/vscode-cairo/src/lspRequests.ts index f2691b8c867..d7cc5f3d193 100644 --- a/vscode-cairo/src/lspRequests.ts +++ b/vscode-cairo/src/lspRequests.ts @@ -21,7 +21,7 @@ export const vfsProvide = new lc.RequestType< void >("vfs/provide"); -export type viewAnalyzedCratesResponse = string; -export const viewAnalyzedCrates = new lc.RequestType0( +export type ViewAnalyzedCratesResponse = string; +export const viewAnalyzedCrates = new lc.RequestType0( "cairo/viewAnalyzedCrates", );