From 6c1d055df8feba39c5932ba1ea299d4243fef8f7 Mon Sep 17 00:00:00 2001 From: Joe Hermaszewski Date: Thu, 27 Aug 2020 00:15:35 +0800 Subject: [PATCH] Add hlint commands --- src/extension.ts | 68 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/src/extension.ts b/src/extension.ts index 2ca56600..b5a1fb76 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -58,6 +58,8 @@ export async function activate(context: ExtensionContext) { }); context.subscriptions.push(restartCmd); + registerHiePointCommand(CommandNames.HlintApplyOneCommandName, "hlint:applyOne", context); + registerHieFileCommand(CommandNames.HlintApplyAllCommandName, "hlint:applyAll", context); } function findManualExecutable(uri: Uri, folder?: WorkspaceFolder): string | null { @@ -247,5 +249,69 @@ function showNotInstalledErrorMessage(uri: Uri) { } const notInstalledMsg: string = variant + ' executable missing, please make sure it is installed, see https://github.com' + projectUrl + '.'; - window.showErrorMessage(notInstalledMsg); + workspace.showMessage(notInstalledMsg, 'error'); +} + + +/* + * Create an editor command that calls an action on the active LSP server. + */ +async function registerHiePointCommand(name: string, command: string, context: ExtensionContext) { + registerHieCommand(name, command, context, async () => { + const { document, position } = await workspace.getCurrentState(); + return [ + { + file: document.uri.toString(), + pos: position + } + ]; + }); +} + +/* + * Create an editor command that calls an action on the active LSP server for a file + */ +async function registerHieFileCommand(name: string, command: string, context: ExtensionContext) { + registerHieCommand(name, command, context, async () => { + const { document } = await workspace.getCurrentState(); + return [document.uri.toString()]; + }); +} + +/* + * Create an editor command that calls an action on the active LSP server. + */ +async function registerHieCommand( + name: string, + command: string, + context: ExtensionContext, + getArgs: () => Promise +) { + const editorCmd = commands.registerCommand(name, async () => { + const { document } = await workspace.getCurrentState(); + const args = await getArgs(); + workspace.showMessage(`hie: ${JSON.stringify(args)}`); + const cmd = { + command, + arguments: args + }; + // Get the current file and workspace folder. + const uri = document.uri; + const folder = workspace.getWorkspaceFolder(uri); + // If there is a client registered for this workspace, use that client. + if (folder !== undefined && folder !== null && clients.has(folder.uri.toString())) { + const client = clients.get(folder.uri.toString()); + if (client !== undefined && client !== null) { + client.sendRequest('workspace/executeCommand', cmd).then( + hints => { + return true; + }, + e => { + console.error(e); + } + ); + } + } + }); + context.subscriptions.push(editorCmd); }