|
| 1 | +'use strict'; |
| 2 | +import { |
| 3 | + commands, |
| 4 | + ExtensionContext, |
| 5 | + window, |
| 6 | + workspace, |
| 7 | +} from 'vscode'; |
| 8 | +import { |
| 9 | + LanguageClient, |
| 10 | +} from 'vscode-languageclient'; |
| 11 | +import { CommandNames } from './constants'; |
| 12 | + |
| 13 | +export namespace HLintApply { |
| 14 | + 'use strict'; |
| 15 | + |
| 16 | + export function registerCommands( |
| 17 | + clients: Map<string, LanguageClient | null>, |
| 18 | + context: ExtensionContext) { |
| 19 | + registerHieFileCommand(clients, CommandNames.HlintApplyAllCommandName, 'hlint:applyAll', context); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +/* |
| 24 | + * Create an editor command that calls an action on the active LSP server. |
| 25 | + */ |
| 26 | +async function registerHieCommand( |
| 27 | + clients: Map<string, LanguageClient | null>, |
| 28 | + name: string, |
| 29 | + command: string, |
| 30 | + context: ExtensionContext, |
| 31 | + getArgs: () => Promise<any[]> |
| 32 | +) { |
| 33 | + const editorCmd = commands.registerCommand(name, async () => { |
| 34 | + const editor = window.activeTextEditor; |
| 35 | + if (!editor) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + const document = editor.document; |
| 40 | + |
| 41 | + const args = await getArgs(); |
| 42 | + const cmd = { |
| 43 | + command, |
| 44 | + arguments: args |
| 45 | + }; |
| 46 | + // Get the current file and workspace folder. |
| 47 | + const uri = document.uri; |
| 48 | + const folder = workspace.getWorkspaceFolder(uri); |
| 49 | + // If there is a client registered for this workspace, use that client. |
| 50 | + if (folder !== undefined && folder !== null && clients.has(folder.uri.toString())) { |
| 51 | + const client = clients.get(folder.uri.toString()); |
| 52 | + if (client !== undefined && client !== null) { |
| 53 | + client.sendRequest('workspace/executeCommand', cmd).then( |
| 54 | + hints => { |
| 55 | + return true; |
| 56 | + }, |
| 57 | + e => { |
| 58 | + console.error(e); |
| 59 | + } |
| 60 | + ); |
| 61 | + } |
| 62 | + } |
| 63 | + }); |
| 64 | + context.subscriptions.push(editorCmd); |
| 65 | +} |
| 66 | + |
| 67 | +/* |
| 68 | + * Create an editor command that calls an action on the active LSP server for a file |
| 69 | + */ |
| 70 | +async function registerHieFileCommand( |
| 71 | + clients: Map<string, LanguageClient | null>, |
| 72 | + name: string, |
| 73 | + command: string, |
| 74 | + context: ExtensionContext |
| 75 | +) { |
| 76 | + registerHieCommand(clients, name, command, context, async () => { |
| 77 | + const editor = window.activeTextEditor; |
| 78 | + if (!editor) { |
| 79 | + return []; |
| 80 | + } |
| 81 | + const document = editor.document; |
| 82 | + return [document.uri]; |
| 83 | + }); |
| 84 | +} |
0 commit comments