-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
main.ts
350 lines (326 loc) · 13.7 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import * as vscode from 'vscode';
import { LanguageClient, ServerOptions, LanguageClientOptions, DocumentSymbol, Position } from 'vscode-languageclient';
import * as util from '../utilities'
import * as config from '../config';
import { provideClojureDefinition } from '../providers/definition';
import { setStateValue, getStateValue } from '../../out/cljs-lib/cljs-lib';
import { downloadClojureLsp } from './download';
import { readVersionFile, getClojureLspPath } from './utilities';
import * as os from 'os';
import * as path from 'path';
import * as fs from 'fs';
const LSP_CLIENT_KEY = 'lspClient';
const RESOLVE_MACRO_AS_COMMAND = 'resolve-macro-as';
function createClient(clojureLspPath: string): LanguageClient {
const serverOptions: ServerOptions = {
run: { command: clojureLspPath },
debug: { command: clojureLspPath },
};
const clientOptions: LanguageClientOptions = {
documentSelector: [{ scheme: 'file', language: 'clojure' }],
synchronize: {
configurationSection: 'clojure-lsp',
fileEvents: vscode.workspace.createFileSystemWatcher('**/.clientrc')
},
initializationOptions: {
"dependency-scheme": "jar",
"auto-add-ns-to-new-files?": true,
"document-formatting?": false,
"document-range-formatting?": false,
"keep-require-at-start?": true,
},
middleware: {
handleDiagnostics(uri, diagnostics, next) {
if (uri.path.endsWith(config.REPL_FILE_EXT)) {
return;
}
return next(uri, diagnostics);
},
provideCodeActions(document, range, context, token, next) {
return next(document, range, context, token);
},
provideCodeLenses: async (document, token, next): Promise<vscode.CodeLens[]> => {
if (config.getConfig().referencesCodeLensEnabled) {
return await next(document, token);
}
return [];
},
resolveCodeLens: async (codeLens, token, next) => {
if (config.getConfig().referencesCodeLensEnabled) {
return await next(codeLens, token);
}
return null;
},
provideHover(document, position, token, next) {
if (util.getConnectedState()) {
return null;
} else {
return next(document, position, token);
}
},
async provideDefinition(document, position, token, next) {
const nReplDefinition = await provideClojureDefinition(document, position, token);
if (nReplDefinition) {
return null;
} else {
return next(document, position, token);
}
},
provideCompletionItem(document, position, context, token, next) {
if (util.getConnectedState()) {
return null;
} else {
return next(document, position, context, token);
}
},
provideSignatureHelp(document, position, context, token, next) {
if (util.getConnectedState()) {
return null;
} else {
return next(document, position, context, token);
}
}
}
};
return new LanguageClient(
'clojure',
'Clojure Language Client',
serverOptions,
clientOptions
);
}
type ClojureLspCommand = {
command: string,
extraParamFn?: () => Thenable<string>,
category?: string;
}
function makePromptForInput(placeHolder: string) {
return async () => {
return await vscode.window.showInputBox({
value: '',
placeHolder: placeHolder,
validateInput: (input => input.trim() === '' ? 'Empty input' : null)
})
}
}
const clojureLspCommands: ClojureLspCommand[] = [
{
command: 'clean-ns'
},
{
command: 'add-missing-libspec'
},
// This seems to be similar to Calva's rewrap commands
//{
// command: 'cycle-coll'
//},
{
command: 'cycle-privacy'
},
{
command: 'expand-let'
},
{
command: 'thread-first'
},
{
command: 'thread-first-all'
},
{
command: 'thread-last'
},
{
command: 'thread-last-all'
},
{
command: 'inline-symbol'
},
{
command: 'unwind-all'
},
{
command: 'unwind-thread'
},
{
command: 'introduce-let',
extraParamFn: makePromptForInput('Bind to')
},
{
command: 'move-to-let',
extraParamFn: makePromptForInput('Bind to')
},
{
command: 'extract-function',
extraParamFn: makePromptForInput('Function name')
}
];
function sendCommandRequest(command: string, args: (number | string)[]): void {
const client = getStateValue(LSP_CLIENT_KEY);
if (client) {
client.sendRequest('workspace/executeCommand', {
command,
arguments: args
}).catch(e => {
console.error(e);
});
}
}
function registerLspCommand(client: LanguageClient, command: ClojureLspCommand): vscode.Disposable {
const category = command.category ? command.category : 'calva.refactor';
const vscodeCommand = `${category}.${command.command.replace(/-[a-z]/g, (m) => m.substring(1).toUpperCase())}`;
return vscode.commands.registerCommand(vscodeCommand, async () => {
const editor = vscode.window.activeTextEditor;
const document = util.getDocument(editor.document);
if (document && document.languageId === 'clojure') {
const line = editor.selection.start.line;
const column = editor.selection.start.character;
const docUri = `${document.uri.scheme}://${document.uri.path}`;
const params = [docUri, line, column];
const extraParam = command.extraParamFn ? await command.extraParamFn() : undefined;
if (!command.extraParamFn || command.extraParamFn && extraParam) {
sendCommandRequest(command.command, (extraParam ? [...params, extraParam] : params));
}
}
});
}
async function codeLensReferencesHandler(_, line, character): Promise<void> {
vscode.window.activeTextEditor.selection = new vscode.Selection(line - 1, character - 1, line - 1, character - 1);
await vscode.commands.executeCommand('editor.action.referenceSearch.trigger');
}
async function resolveMacroAsCodeActionCommandHandler(document: string, line: number, character: number): Promise<void> {
const macroToResolveAs = await vscode.window.showQuickPick([
'clojure.core/def',
'clojure.core/defn',
'clojure.core/let',
'clojure.core/for',
'clojure.core/->',
'clojure.core/->>',
'clj-kondo.lint-as/def-catch-all'
]);
const workspaceFolders = vscode.workspace.workspaceFolders;
const rootWorkspaceFolder = workspaceFolders && workspaceFolders[0];
const homeDirectory = os.homedir();
const cljKondoUserConfig = path.join(homeDirectory, '.config', 'clj-kondo', 'config.edn');
const configPaths = [cljKondoUserConfig];
if (rootWorkspaceFolder) {
const cljKondoProjectConfig = path.join(rootWorkspaceFolder.uri.fsPath, '.clj-kondo', 'config.edn');
configPaths.push(cljKondoProjectConfig);
}
const cljKondoConfigPath = await vscode.window.showQuickPick(configPaths, { placeHolder: 'Select where this setting should be saved:' });
if (macroToResolveAs && cljKondoConfigPath) {
const args = [document, line, character, macroToResolveAs, cljKondoConfigPath];
sendCommandRequest(RESOLVE_MACRO_AS_COMMAND, args);
}
}
function resolveMacroAsCommandHandler(): void {
const activeTextEditor = vscode.window.activeTextEditor;
if (activeTextEditor && activeTextEditor.document && activeTextEditor.document.languageId === 'clojure') {
const documentUri = decodeURIComponent(activeTextEditor.document.uri.toString());
const { line, character } = activeTextEditor.selection.active;
resolveMacroAsCodeActionCommandHandler(documentUri, line + 1, character + 1);
}
}
function registerCommands(context: vscode.ExtensionContext, client: LanguageClient) {
// The title of this command is dictated by clojure-lsp and is executed when the user clicks the references code lens for a symbol
context.subscriptions.push(vscode.commands.registerCommand('code-lens-references', codeLensReferencesHandler));
// The title of this command is dictated by clojure-lsp and is executed when the user executes the Resolve Macro As code action
context.subscriptions.push(vscode.commands.registerCommand(RESOLVE_MACRO_AS_COMMAND, resolveMacroAsCodeActionCommandHandler));
context.subscriptions.push(vscode.commands.registerCommand('calva.linting.resolveMacroAs', resolveMacroAsCommandHandler));
context.subscriptions.push(
...clojureLspCommands.map(command => registerLspCommand(client, command))
);
}
function registerEventHandlers(context: vscode.ExtensionContext, client: LanguageClient) {
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(async event => {
if (event.affectsConfiguration('calva.referencesCodeLens.enabled')) {
const visibleFileEditors = vscode.window.visibleTextEditors.filter(editor => {
return editor.document.uri.scheme === 'file';
});
for (let editor of visibleFileEditors) {
// Hacky solution for triggering codeLens refresh
// Could not find a better way, aside from possibly changes to clojure-lsp
// https://github.com/microsoft/vscode-languageserver-node/issues/705
const edit1 = new vscode.WorkspaceEdit();
edit1.insert(editor.document.uri, new vscode.Position(0, 0), '\n');
await vscode.workspace.applyEdit(edit1);
const edit2 = new vscode.WorkspaceEdit();
edit2.delete(editor.document.uri, new vscode.Range(0, 0, 1, 0));
await vscode.workspace.applyEdit(edit2);
}
}
}));
}
async function startClient(clojureLspPath: string, context: vscode.ExtensionContext): Promise<void> {
const client = createClient(clojureLspPath);
console.log('Starting clojure-lsp at', clojureLspPath);
const onReadyPromise = client.onReady();
vscode.window.setStatusBarMessage('$(sync~spin) Initializing Clojure language features via clojure-lsp', onReadyPromise);
client.start();
await onReadyPromise;
setStateValue(LSP_CLIENT_KEY, client);
registerCommands(context, client);
registerEventHandlers(context, client);
}
async function serverInfoCommandHandler(): Promise<void> {
const client = getStateValue(LSP_CLIENT_KEY);
if (client) {
sendCommandRequest('server-info', []);
} else {
vscode.window.showInformationMessage('There is no clojure-lsp server running.')
}
}
async function activate(context: vscode.ExtensionContext): Promise<void> {
vscode.commands.registerCommand('calva.diagnostics.clojureLspServerInfo', serverInfoCommandHandler);
const extensionPath = context.extensionPath;
const currentVersion = readVersionFile(extensionPath);
const userConfiguredClojureLspPath = config.getConfig().clojureLspPath;
let clojureLspPath = userConfiguredClojureLspPath === '' ? getClojureLspPath(extensionPath, util.isWindows) : userConfiguredClojureLspPath;
if (userConfiguredClojureLspPath === '') {
const configuredVersion: string = config.getConfig().clojureLspVersion;
if (configuredVersion === '') {
// This should never be an empty string and can cause issues with clojure-lsp starting, particularly if there is no version file yet from a previous download and no custom clojure-lsp path set. Inform the user.
vscode.window.showWarningMessage('The calva.clojureLspVersion setting is blank and calva.clojureLspPath is also blank, so clojure-lsp will not be started. Please reset the calva.clojureLspVersion setting to use the default version of clojure-lsp, or set it to a clojure-lsp version you want to use. Alternatively, you can set the calva.clojureLspPath to use a downloaded binary of clojure-lsp.');
return;
}
if (currentVersion !== configuredVersion) {
const downloadPromise = downloadClojureLsp(context.extensionPath, configuredVersion);
vscode.window.setStatusBarMessage('$(sync~spin) Downloading clojure-lsp', downloadPromise);
clojureLspPath = await downloadPromise;
}
}
await startClient(clojureLspPath, context);
}
function deactivate(): Promise<void> {
const client = getStateValue(LSP_CLIENT_KEY);
if (client) {
return client.stop();
}
return Promise.resolve();
}
async function getReferences(lspClient: LanguageClient, uri: string, position: Position, includeDeclaration: boolean = true): Promise<Location[] | null> {
const result: Location[] = await lspClient.sendRequest('textDocument/references', {
textDocument: {
uri,
},
position,
context: {
includeDeclaration
}
});
return result;
}
async function getDocumentSymbols(lspClient: LanguageClient, uri: string): Promise<DocumentSymbol[]> {
const result: DocumentSymbol[] = await lspClient.sendRequest('textDocument/documentSymbol', {
textDocument: {
uri
}
});
return result;
}
export default {
activate,
deactivate,
LSP_CLIENT_KEY,
getReferences,
getDocumentSymbols
}