-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Separate Client into own class (#35)
- Loading branch information
Showing
4 changed files
with
131 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { workspace, WorkspaceFolder, Uri, window, OutputChannel } from "vscode"; | ||
|
||
import { | ||
LanguageClient, | ||
LanguageClientOptions, | ||
ServerOptions, | ||
TextDocumentFilter, | ||
} from "vscode-languageclient/node"; | ||
|
||
import { extensionName, languageId } from "./constants"; | ||
import findNargo from "./find-nargo"; | ||
|
||
function globFromUri(uri: Uri, glob: string) { | ||
// globs always need to use `/` | ||
return `${uri.fsPath}${glob}`.replaceAll("\\", "/"); | ||
} | ||
|
||
function getLspCommand(uri: Uri) { | ||
let config = workspace.getConfiguration("noir", uri); | ||
|
||
let lspEnabled = config.get<boolean>("enableLSP"); | ||
|
||
if (!lspEnabled) { | ||
return; | ||
} | ||
|
||
let command = config.get<string | undefined>("nargoPath") || findNargo(); | ||
|
||
let flags = config.get<string | undefined>("nargoFlags") || ""; | ||
|
||
// Remove empty strings from the flags list | ||
let args = ["lsp", ...flags.split(" ")].filter((arg) => arg !== ""); | ||
|
||
return [command, args] as const; | ||
} | ||
|
||
export default class Client extends LanguageClient { | ||
#uri: Uri; | ||
#command: string; | ||
#args: string[]; | ||
#output: OutputChannel; | ||
|
||
constructor(uri: Uri, workspaceFolder?: WorkspaceFolder) { | ||
let outputChannel = window.createOutputChannel(extensionName, languageId); | ||
|
||
let [command, args] = getLspCommand(uri); | ||
|
||
let documentSelector: TextDocumentFilter[] = []; | ||
if (workspaceFolder) { | ||
documentSelector.push({ | ||
scheme: "file", | ||
language: languageId, | ||
// Glob starts with `/` because it just appends both segments | ||
pattern: `${globFromUri(uri, "/**/*")}`, | ||
}); | ||
} else { | ||
documentSelector.push({ | ||
scheme: uri.scheme, | ||
language: languageId, | ||
pattern: `${globFromUri(uri, "")}`, | ||
}); | ||
} | ||
|
||
let clientOptions: LanguageClientOptions = { | ||
documentSelector, | ||
workspaceFolder, | ||
outputChannel, | ||
}; | ||
|
||
let serverOptions: ServerOptions = { | ||
command, | ||
args, | ||
}; | ||
|
||
super(languageId, extensionName, serverOptions, clientOptions); | ||
|
||
this.#uri = uri; | ||
this.#command = command; | ||
this.#args = args; | ||
this.#output = outputChannel; | ||
} | ||
|
||
async start(): Promise<void> { | ||
let command = this.#command; | ||
let args = this.#args.join(" "); | ||
this.info(`Starting LSP client using command: ${command} ${args}`); | ||
|
||
await super.start(); | ||
} | ||
|
||
async dispose(timeout?: number): Promise<void> { | ||
await super.dispose(timeout); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export let extensionName = "Noir Language Server"; | ||
|
||
export let languageId = "noir"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import which from "which"; | ||
|
||
const nargoBinaries = ["nargo"]; | ||
|
||
export default function findNargo() { | ||
for (const bin of nargoBinaries) { | ||
try { | ||
const nargo = which.sync(bin); | ||
// If it didn't throw, we found a nargo binary | ||
return nargo; | ||
} catch (err) { | ||
// Not found | ||
} | ||
} | ||
throw new Error("Unable to locate any nargo binary. Did you install it?"); | ||
} |