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(aztec): search for aztec-nargo on top of nargo bin #67

Merged
merged 6 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ export default class Client extends LanguageClient {
});
}

get command(): string {
return this.#command;
}

async refreshProfileInfo() {
const response = await this.sendRequest<NargoProfileRunResult>('nargo/profile/run', { package: '' });

Expand Down
123 changes: 67 additions & 56 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
import { languageId } from './constants';
import Client from './client';
import findNargo from './find-nargo';
import { lspClients, editorLineDecorationManager } from './noir';
import { lspClients, editorLineDecorationManager, getNoirStatusBarItem, handleClientStartError } from './noir';

const activeCommands: Map<string, Disposable> = new Map();

Expand Down Expand Up @@ -246,67 +246,78 @@ async function didOpenTextDocument(document: TextDocument): Promise<Disposable>
const uri = document.uri;
let folder = workspace.getWorkspaceFolder(uri);
let configHandler;
if (folder) {
// If we have nested workspace folders we only start a server on the outer most workspace folder.
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);
try {
if (folder) {
// If we have nested workspace folders we only start a server on the outer most workspace folder.
folder = getOuterMostWorkspaceFolder(folder);

await addWorkspaceClient(folder);

const currentLspClient = lspClients.get(folder.uri.toString());
const statusBarItem = getNoirStatusBarItem();
statusBarItem.tooltip = currentLspClient.command;
statusBarItem.show();

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)) {
await removeWorkspaceClient(folder);
await addWorkspaceClient(folder);
}
});
} else {
// We only want to handle `file:` and `untitled:` schemes because
// vscode sends `output:` schemes for markdown responses from our LSP
if (uri.scheme !== 'file' && uri.scheme !== 'untitled') {
return Disposable.from();
}

if (e.affectsConfiguration('noir.enableLSP', folder.uri)) {
await removeWorkspaceClient(folder);
await addWorkspaceClient(folder);
}
});
} else {
// We only want to handle `file:` and `untitled:` schemes because
// vscode sends `output:` schemes for markdown responses from our LSP
if (uri.scheme !== 'file' && uri.scheme !== 'untitled') {
return Disposable.from();
// 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)) {
await removeFileClient(uri);
await addFileClient(uri);
}
});
}

// 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)) {
await removeFileClient(uri);
await addFileClient(uri);
}
});
return workspace.onDidChangeConfiguration(configHandler);
} catch (e) {
handleClientStartError(e);
}

return workspace.onDidChangeConfiguration(configHandler);
}

async function didChangeWorkspaceFolders(event: WorkspaceFoldersChangeEvent) {
Expand Down
38 changes: 36 additions & 2 deletions src/find-nargo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import os from 'os';
import path from 'path';
import fs from 'fs';
import which from 'which';
import { NargoNotFoundError } from './noir';
import { MarkdownString } from 'vscode';

const nargoBinaries = ['nargo'];
// List of possible nargo binaries to find on Path
// We prioritize 'aztec-nargo' as more specialised case
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
const nargoBinaries = ['aztec-nargo', 'nargo'];
// List of possible default installations in users folder
const nargoInstallLocationPostix = ['.aztec/bin/aztec-nargo', '.nargo/bin/nargo'];
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved

export default function findNargo() {
for (const bin of nargoBinaries) {
Expand All @@ -12,5 +21,30 @@ export default function findNargo() {
// Not found
}
}
throw new Error('Unable to locate any nargo binary. Did you install it?');

const homeDir = os.homedir();
// So far we have not found installations on path
// Let's check default installation locations
for (const postfix of nargoInstallLocationPostix) {
const filePath = path.join(homeDir, postfix);
if (fs.existsSync(filePath)) {
return filePath;
}
}

const message = new MarkdownString();
message.appendText(`Could not locate any of\n`);
for (const nargoBinary of nargoBinaries) {
message.appendMarkdown(`\`${nargoBinary}\``);
message.appendText(`\n`);
}

message.appendText(`on \`$PATH\`, or one of default installation locations\n`);
for (const postfix of nargoInstallLocationPostix) {
const filePath = path.join(homeDir, postfix);
message.appendMarkdown(`\`${filePath}\``);
message.appendText(`\n`);
}

throw new NargoNotFoundError(message);
}
40 changes: 40 additions & 0 deletions src/noir.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
import { MarkdownString, StatusBarAlignment, StatusBarItem, ThemeColor, window } from 'vscode';
import { EditorLineDecorationManager } from './EditorLineDecorationManager';
import Client from './client';

export const lspClients: Map<string, Client> = new Map();

export const editorLineDecorationManager = new EditorLineDecorationManager(lspClients);

let noirStatusBarItem: StatusBarItem;

export function getNoirStatusBarItem() {
if (noirStatusBarItem) {
return noirStatusBarItem;
}
// Create Nargo Status bar item, we only need one for lifetime of plugin
// we will show/update it depending on file user is working with
noirStatusBarItem = window.createStatusBarItem(StatusBarAlignment.Right, 100);
noirStatusBarItem.text = 'Nargo';

return noirStatusBarItem;
}

export class NargoNotFoundError extends Error {
constructor(markdownMessage?: MarkdownString) {
super(markdownMessage.value);

this.markdownMessage = markdownMessage;
}

markdownMessage: MarkdownString;
}

export function handleClientStartError(err: Error) {
if (err instanceof NargoNotFoundError) {
const backgroundColor = new ThemeColor('statusBarItem.errorBackground');
const foregroundColor = new ThemeColor('statusBarItem.errorForeground');

const statusBarItem = getNoirStatusBarItem();
statusBarItem.backgroundColor = backgroundColor;
statusBarItem.color = foregroundColor;
statusBarItem.tooltip = err.markdownMessage;
statusBarItem.show();
} else {
throw err;
}
}
Loading