From 616b058e049e69c9a9d15a80de0f4c59c05bd6fa Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Wed, 28 Jun 2023 15:35:29 -0700 Subject: [PATCH 1/3] feat: Handle nargo.test command when executed by codelens --- src/extension.ts | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/src/extension.ts b/src/extension.ts index 783a70d..4a2d719 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -27,6 +27,13 @@ import { ConfigurationChangeEvent, Uri, window, + tasks, + Task, + TaskScope, + TaskRevealKind, + TaskPanelKind, + TaskGroup, + ProcessExecution, } from "vscode"; import { @@ -43,6 +50,7 @@ let languageId = "noir"; let outputChannel = window.createOutputChannel(extensionName, languageId); +let activeCommands: Map = new Map(); let fileClients: Map = new Map(); let workspaceClients: Map = new Map(); @@ -139,6 +147,72 @@ function getLspCommand(uri: Uri) { return [command, args] as const; } +let INTERNAL_COMMANDS = [ + { type: "nargo", command: "test", group: TaskGroup.Test }, +]; + +function registerCommands(uri: Uri) { + let file = uri.toString(); + let config = workspace.getConfiguration("noir", uri); + + let nargoPath = config.get("nargoPath") || findNargo(); + + let nargoFlags = config.get("nargoFlags") || []; + + let commands$: Disposable[] = []; + for (let { type, command, group } of INTERNAL_COMMANDS) { + let internalName = `${type}.${command}`; + let displayName = `${type} ${command}`; + let command$ = commands.registerCommand(internalName, async (args) => { + let task = new Task( + { type, command }, + TaskScope.Workspace, + displayName, + languageId, + new ProcessExecution(nargoPath, [command].concat(nargoFlags).concat(args)), + [] + ); + task.group = group; + // We set `isBackground` to `true` to avoid showing the internal task as "recently used" + task.isBackground = true; + // However, we still want to show the terminal when you run a test + task.presentationOptions = { + reveal: TaskRevealKind.Always, + panel: TaskPanelKind.Dedicated, + clear: true, + }; + + return tasks.executeTask(task); + }); + + commands$.push(command$); + } + + activeCommands.set(file, Disposable.from(...commands$)); +} + +function disposeCommands(uri: Uri) { + let file = uri.toString(); + let commands$ = activeCommands.get(file); + commands$.dispose(); +} + +function registerFileCommands(uri: Uri) { + registerCommands(uri); +} + +function disposeFileCommands(uri: Uri) { + disposeCommands(uri); +} + +function registerWorkspaceCommands(workspaceFolder: WorkspaceFolder) { + registerCommands(workspaceFolder.uri); +} + +function disposeWorkspaceCommands(workspaceFolder: WorkspaceFolder) { + disposeCommands(workspaceFolder.uri); +} + async function startFileClient(uri: Uri) { let [command, args] = getLspCommand(uri); @@ -272,18 +346,23 @@ async function didOpenTextDocument( folder = getOuterMostWorkspaceFolder(folder); await addWorkspaceClient(folder); + registerWorkspaceCommands(folder); configHandler = mutex( folder.uri.toString(), async (e: ConfigurationChangeEvent) => { if (e.affectsConfiguration("noir.nargoFlags", folder.uri)) { + disposeWorkspaceCommands(folder); await removeWorkspaceClient(folder); await addWorkspaceClient(folder); + registerWorkspaceCommands(folder); } if (e.affectsConfiguration("noir.nargoPath", folder.uri)) { + disposeWorkspaceCommands(folder); await removeWorkspaceClient(folder); await addWorkspaceClient(folder); + registerWorkspaceCommands(folder); } if (e.affectsConfiguration("noir.enableLSP", folder.uri)) { @@ -301,18 +380,23 @@ async function didOpenTextDocument( // Each file outside of a workspace gets it's own client await addFileClient(uri); + registerFileCommands(uri); configHandler = mutex( uri.toString(), async (e: ConfigurationChangeEvent) => { if (e.affectsConfiguration("noir.nargoFlags", uri)) { + disposeFileCommands(uri); await removeFileClient(uri); await addFileClient(uri); + registerFileCommands(uri); } if (e.affectsConfiguration("noir.nargoPath", uri)) { + disposeFileCommands(uri); await removeFileClient(uri); await addFileClient(uri); + registerFileCommands(uri); } if (e.affectsConfiguration("noir.enableLSP", uri)) { From 1847a4ff18be17e15ebe57f2828596debcd7fb71 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Wed, 28 Jun 2023 15:35:57 -0700 Subject: [PATCH 2/3] format --- src/extension.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/extension.ts b/src/extension.ts index 4a2d719..c81972f 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -169,7 +169,10 @@ function registerCommands(uri: Uri) { TaskScope.Workspace, displayName, languageId, - new ProcessExecution(nargoPath, [command].concat(nargoFlags).concat(args)), + new ProcessExecution( + nargoPath, + [command].concat(nargoFlags).concat(args) + ), [] ); task.group = group; From e91becfdb926524a9953bec50ff18f62f2ad82bb Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Wed, 28 Jun 2023 15:56:36 -0700 Subject: [PATCH 3/3] add feature --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9dd4c34..1225fd0 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ This extension helps developers write, understand, and improve Noir code by prov - Syntax highlighting - Compile errors and warnings on file save +- Run tests via codelens above each test - Useful snippets for common code patterns ## Requirements