From f8888195d0adabead361ca744bf20ceb8aaa1195 Mon Sep 17 00:00:00 2001 From: Joe Hermaszewski Date: Thu, 27 Aug 2020 09:49:00 +0800 Subject: [PATCH] Add hlint:applyAll command --- src/commands/HLintApply.ts | 84 ++++++++++++++++++++++++++++++++++++++ src/commands/constants.ts | 1 + src/extension.ts | 4 ++ 3 files changed, 89 insertions(+) create mode 100644 src/commands/HLintApply.ts diff --git a/src/commands/HLintApply.ts b/src/commands/HLintApply.ts new file mode 100644 index 00000000..89d60507 --- /dev/null +++ b/src/commands/HLintApply.ts @@ -0,0 +1,84 @@ +'use strict'; +import { + commands, + ExtensionContext, + window, + workspace, +} from 'vscode'; +import { + LanguageClient, +} from 'vscode-languageclient'; +import { CommandNames } from './constants'; + +export namespace HLintApply { + 'use strict'; + + export function registerCommands( + clients: Map, + context: ExtensionContext) { + registerHieFileCommand(clients, CommandNames.HlintApplyAllCommandName, 'hlint:applyAll', context); + } +} + +/* + * Create an editor command that calls an action on the active LSP server. + */ +async function registerHieCommand( + clients: Map, + name: string, + command: string, + context: ExtensionContext, + getArgs: () => Promise +) { + const editorCmd = commands.registerCommand(name, async () => { + const editor = window.activeTextEditor; + if (!editor) { + return; + } + + const document = editor.document; + + const args = await getArgs(); + 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); +} + +/* + * Create an editor command that calls an action on the active LSP server for a file + */ +async function registerHieFileCommand( + clients: Map, + name: string, + command: string, + context: ExtensionContext +) { + registerHieCommand(clients, name, command, context, async () => { + const editor = window.activeTextEditor; + if (!editor) { + return []; + } + const document = editor.document; + return [document.uri]; + }); +} diff --git a/src/commands/constants.ts b/src/commands/constants.ts index e91017a6..a80a687c 100644 --- a/src/commands/constants.ts +++ b/src/commands/constants.ts @@ -1,4 +1,5 @@ export namespace CommandNames { export const ImportIdentifierCommandName = 'haskell.commands.importIdentifier'; export const RestartServerCommandName = 'haskell.commands.restartServer'; + export const HlintApplyAllCommandName = 'haskell.commands.applyAll'; } diff --git a/src/extension.ts b/src/extension.ts index ef1c8e0a..a7e56b84 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -20,6 +20,7 @@ import { TransportKind, } from 'vscode-languageclient'; import { CommandNames } from './commands/constants'; +import { HLintApply } from './commands/HLintApply'; import { ImportIdentifier } from './commands/importIdentifier'; import { DocsBrowser } from './docsBrowser'; import { downloadHaskellLanguageServer } from './hlsBinaries'; @@ -65,6 +66,9 @@ export async function activate(context: ExtensionContext) { // Set up the documentation browser. const docsDisposable = DocsBrowser.registerDocsBrowser(); context.subscriptions.push(docsDisposable); + + // Add the HLint commands + HLintApply.registerCommands(clients, context); } function findManualExecutable(uri: Uri, folder?: WorkspaceFolder): string | null {