Skip to content

Commit

Permalink
feat: autodiscover Nargo project
Browse files Browse the repository at this point in the history
  • Loading branch information
mverzilli committed Jan 16, 2024
1 parent f97fd8e commit 476ca90
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import {
window,
workspace,
commands,
debug,
Expand All @@ -37,6 +38,9 @@ import {
window,
ProgressLocation,
DebugAdapterDescriptorFactory,
DebugConfigurationProvider,
CancellationToken,
DebugConfiguration,
DebugAdapterDescriptor,
DebugAdapterExecutable,
DebugSession,
Expand All @@ -46,6 +50,7 @@ import {
import { languageId } from './constants';
import Client from './client';
import findNargo from './find-nargo';
import findNearestPackageFrom from './find-nearest-package';
import { lspClients, editorLineDecorationManager } from './noir';

const activeCommands: Map<string, Disposable> = new Map();
Expand All @@ -56,7 +61,10 @@ class NoirDebugAdapterDescriptorFactory implements DebugAdapterDescriptorFactory
_executable: DebugAdapterExecutable,
): ProviderResult<DebugAdapterDescriptor> {
const config = workspace.getConfiguration('noir');
const nargoPath = config.get<string | undefined>('nargoPath') || findNargo();

const configuredNargoPath = config.get<string | undefined>('nargoPath');
const nargoPath = configuredNargoPath || findNargo();

return new DebugAdapterExecutable(nargoPath, ['dap']);
}
}
Expand Down Expand Up @@ -354,10 +362,37 @@ export async function activate(context: ExtensionContext): Promise<void> {
context.subscriptions.push(
debug.registerDebugAdapterDescriptorFactory('noir', new NoirDebugAdapterDescriptorFactory()),
);

context.subscriptions.push(
debug.registerDebugConfigurationProvider('noir', new NoirDebugConfigurationProvider()),
);
}

export async function deactivate(): Promise<void> {
for (const client of lspClients.values()) {
await client.stop();
}
}

class NoirDebugConfigurationProvider implements DebugConfigurationProvider {
resolveDebugConfiguration(folder: WorkspaceFolder | undefined, config: DebugConfiguration, token?: CancellationToken): ProviderResult<DebugConfiguration> {
if (config.program || config.request == 'attach')
return config;

if (window.activeTextEditor?.document.languageId != 'noir')
return window.showInformationMessage("Select a Noir file to debug").then(_ => {
return null;
});

const currentFilePath = window.activeTextEditor.document.uri.fsPath;
let currentProject = findNearestPackageFrom(currentFilePath);

return {
type: 'noir',
name: 'Noir binary package',
request: 'launch',
program: currentFilePath,
projectFolder: currentProject,
}
}
}
41 changes: 41 additions & 0 deletions src/find-nearest-package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';

/**
* Given a program file path, walk up the file system until
* finding the nearest a Nargo.toml in a directory that contains
* the program.
*
* To reduce the odds of accidentally choosing the wrong Nargo package,
* end the walk at the root of the current VS Code open files.
*/
export default function findNearestPackageFolder(program: string): string {
const workspaceFolders = vscode.workspace.workspaceFolders;
if (!workspaceFolders) {
throw new Error("No workspace is currently open in VS Code.");
}

const workspaceRoots = workspaceFolders.map(wf => wf.uri.fsPath);

let currentFolder = path.dirname(program);

try {
while (
currentFolder !== path.dirname(currentFolder) &&
!workspaceRoots.includes(currentFolder)
) {
const maybeNargoProject = path.join(currentFolder, 'Nargo.toml');

if (fs.existsSync(maybeNargoProject)) {
return currentFolder;
}

currentFolder = path.dirname(currentFolder);
}
} catch (error) {
throw new Error("Could not find a Nargo package associated to this file.");
}

throw new Error("Could not find a Nargo package associated to this file.");
}

0 comments on commit 476ca90

Please sign in to comment.