Skip to content

Commit

Permalink
feat: Allow overriding the path to forc-lsp executable (#174)
Browse files Browse the repository at this point in the history
* Add configuration to let users select their forc-lsp

Fixes #167

By default the field is empty and it is only useful to advanced users.

The runtime will use the field to try to boot the lsp but in case the
configuration is invalid it will fallback to finding the forc-lsp in $PATH

* Allow overriding the path to forc-lsp executable

* prettier

* update reload opts

* fix typo

---------

Co-authored-by: Cesar Rodas <cesar@rodasm.com.py>
  • Loading branch information
sdankel and crodas committed Apr 17, 2024
1 parent 140891e commit 41bb467
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"outFiles": ["${workspaceRoot}/client/out/**/*.js"],
"preLaunchTask": {
"type": "npm",
"script": "esbuild-watch"
"script": "esbuild"
}
}
],
Expand Down
6 changes: 5 additions & 1 deletion client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const getExtensionPath = () =>
vscode.extensions.getExtension(EXTENSION_ID)!.extensionPath;

export class Config {
private readonly requiresReloadOpts = ['debug'].map(
private readonly requiresReloadOpts = ['debug', 'diagnostic'].map(
opt => `${EXTENSION_ROOT}.${opt}`
);

Expand Down Expand Up @@ -87,6 +87,10 @@ export class Config {
return this.cfg.get<T>(path)!;
}

get binPath() {
return this.get<string>('diagnostic.binPath');
}

get traceExtension() {
return this.get<boolean>('trace.extension');
}
Expand Down
68 changes: 49 additions & 19 deletions client/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export async function activate(context: ExtensionContext) {
);

try {
const client = createClient(getClientOptions(), await getServerOptions());
const client = createClient(
getClientOptions(),
await getServerOptions(config)
);

// Start the client. This will also launch the server
await client.start();
Expand All @@ -53,35 +56,62 @@ export function deactivate(): Thenable<void> | undefined {
return client.stop();
}

async function getServerOptions(): Promise<lc.ServerOptions> {
// Look for the executable in FUELUP_HOME if it exists, otherwise look for it in the PATH.
const executable = process.env.FUELUP_HOME
async function getServerOptions(config: Config): Promise<lc.ServerOptions> {
// Look for the default executable in FUELUP_HOME if it exists, otherwise look for it in the PATH.
const defaultExecutable = process.env.FUELUP_HOME
? `${process.env.FUELUP_HOME}/bin/${LSP_EXECUTABLE_NAME}`
: LSP_EXECUTABLE_NAME;

// Use the settings override path if provided, otherwise use the default executable.
const settingsExecutable = config.binPath;
const executable = settingsExecutable || defaultExecutable;

// Check if the executable exists.
try {
let version = await promisify(exec)(`${executable} --version`);
log.info(`Server executable version: ${version.stdout.trim()}`);
} catch (error) {
window
.showErrorMessage(
'The Sway Language Server is not installed. Would you like to install it?',
'Yes',
'No'
)
.then(async selection => {
if (selection === 'Yes') {
window.showInformationMessage(
'Follow the instructions in the terminal to install the Sway Language Server, then reload the window.'
);
commands.executeCommand('sway.installServer');
}
});
throw Error(`Missing executable: ${LSP_EXECUTABLE_NAME}\
if (!!settingsExecutable) {
const updateMessage =
'Update the setting "sway-lsp.diagnostic.binPath" either to a valid path to a forc-lsp executable, or leave it empty to use the executable to which your $PATH resolves.';
window
.showErrorMessage(
'The Sway Language Server is not installed at the path defined in your Extension Settings.',
'Edit Setting',
'Later'
)
.then(async selection => {
if (selection === 'Edit Setting') {
window.showInformationMessage(updateMessage);
commands.executeCommand(
'workbench.action.openWorkspaceSettings',
'sway-lsp.diagnostic.binPath'
);
}
});
throw Error(`Missing executable: ${LSP_EXECUTABLE_NAME}\
\n\nThe VSCode setting "sway-lsp.diagnostic.binPath" is set to an invalid path: "${settingsExecutable}"
\n${updateMessage}`);
} else {
window
.showErrorMessage(
'The Sway Language Server is not installed. Would you like to install it?',
'Yes',
'No'
)
.then(async selection => {
if (selection === 'Yes') {
window.showInformationMessage(
'Follow the instructions in the terminal to install the Sway Language Server, then reload the window.'
);
commands.executeCommand('sway.installServer');
}
});
throw Error(`Missing executable: ${LSP_EXECUTABLE_NAME}\
\n\nYou may need to install the fuel toolchain or add ${process.env.HOME}/.fuelup/bin your path. Try running:\
\n\ncurl --proto '=https' --tlsv1.2 -sSf https://install.fuel.network/fuelup-init.sh | sh\
\n\nOr read about fuelup for more information: https://github.com/FuelLabs/fuelup\n`);
}
}

// Get the full path to the server executable.
Expand Down
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"format:fix": "prettier --write 'client/src' 'snippets' 'syntaxes'",
"fix-syntaxes": "vscode-tmgrammar-snap --updateSnapshot client/test/syntaxes/*sw",
"test-syntaxes": "vscode-tmgrammar-snap client/test/syntaxes/*sw",
"package": "vsce package -o sway-vscode-plugin-dev.vsix"
"package": "vsce package -o sway-vscode-plugin.vsix"
},
"engines": {
"vscode": "^1.66.0"
Expand Down Expand Up @@ -103,6 +103,13 @@
"type": "boolean",
"default": true
},
"sway-lsp.diagnostic.binPath": {
"description": "Optionally override the path to the Sway language server executable. If empty, the extension will use the forc-lsp executable to which your $PATH resolves (recommended for most users).",
"type": "string",
"default": "",
"pattern": "(^\/(.+\/)*forc-lsp$)|^$",
"patternErrorMessage": "Must be an absolute path to the `forc-lsp` executable, or left empty."
},
"sway-lsp.logging.level": {
"scope": "window",
"type": "string",
Expand Down Expand Up @@ -396,4 +403,4 @@
"vsce": "^2.5.3",
"vscode-tmgrammar-test": "^0.1.1"
}
}
}

0 comments on commit 41bb467

Please sign in to comment.