Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/commands/HLintApply.ts
Original file line number Diff line number Diff line change
@@ -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<string, LanguageClient | null>,
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<string, LanguageClient | null>,
name: string,
command: string,
context: ExtensionContext,
getArgs: () => Promise<any[]>
) {
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<string, LanguageClient | null>,
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];
});
}
1 change: 1 addition & 0 deletions src/commands/constants.ts
Original file line number Diff line number Diff line change
@@ -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';
}
4 changes: 4 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand Down