Skip to content

Commit f888819

Browse files
committed
Add hlint:applyAll command
1 parent 6e8e00c commit f888819

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

src/commands/HLintApply.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}

src/commands/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export namespace CommandNames {
22
export const ImportIdentifierCommandName = 'haskell.commands.importIdentifier';
33
export const RestartServerCommandName = 'haskell.commands.restartServer';
4+
export const HlintApplyAllCommandName = 'haskell.commands.applyAll';
45
}

src/extension.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
TransportKind,
2121
} from 'vscode-languageclient';
2222
import { CommandNames } from './commands/constants';
23+
import { HLintApply } from './commands/HLintApply';
2324
import { ImportIdentifier } from './commands/importIdentifier';
2425
import { DocsBrowser } from './docsBrowser';
2526
import { downloadHaskellLanguageServer } from './hlsBinaries';
@@ -65,6 +66,9 @@ export async function activate(context: ExtensionContext) {
6566
// Set up the documentation browser.
6667
const docsDisposable = DocsBrowser.registerDocsBrowser();
6768
context.subscriptions.push(docsDisposable);
69+
70+
// Add the HLint commands
71+
HLintApply.registerCommands(clients, context);
6872
}
6973

7074
function findManualExecutable(uri: Uri, folder?: WorkspaceFolder): string | null {

0 commit comments

Comments
 (0)