Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Handle nargo.test command when executed by codelens #25

Merged
merged 3 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
87 changes: 87 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ import {
ConfigurationChangeEvent,
Uri,
window,
tasks,
Task,
TaskScope,
TaskRevealKind,
TaskPanelKind,
TaskGroup,
ProcessExecution,
} from "vscode";

import {
Expand All @@ -43,6 +50,7 @@ let languageId = "noir";

let outputChannel = window.createOutputChannel(extensionName, languageId);

let activeCommands: Map<string, Disposable> = new Map();
let fileClients: Map<string, LanguageClient> = new Map();
let workspaceClients: Map<string, LanguageClient> = new Map();

Expand Down Expand Up @@ -139,6 +147,75 @@ 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<string | undefined>("nargoPath") || findNargo();

let nargoFlags = config.get<string | undefined>("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);

Expand Down Expand Up @@ -272,18 +349,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)) {
Expand All @@ -301,18 +383,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)) {
Expand Down