-
-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathlsp.ts
254 lines (239 loc) · 8.97 KB
/
lsp.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
import * as vscode from 'vscode';
import { LanguageClient, ServerOptions, LanguageClientOptions } from 'vscode-languageclient';
import * as path from 'path';
import * as state from './state';
import * as util from './utilities'
import config from './config';
import { provideClojureDefinition } from './providers/definition';
const LSP_CLIENT_KEY = 'lspClient';
function createClient(jarPath: string): LanguageClient {
const serverOptions: ServerOptions = {
run: { command: 'java', args: ['-jar', jarPath] },
debug: { command: 'java', args: ['-jar', jarPath] },
};
const clientOptions: LanguageClientOptions = {
documentSelector: [{ scheme: 'file', language: 'clojure' }],
synchronize: {
configurationSection: 'clojure-lsp',
fileEvents: vscode.workspace.createFileSystemWatcher('**/.clientrc')
},
initializationOptions: {
"dependency-scheme": "jar",
// LSP-TODO: Use lsp's feature and remove Calva's feature for this
"auto-add-ns-to-new-files?": false,
"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 (state.config().referencesCodeLensEnabled) {
return await next(document, token);
}
return [];
},
resolveCodeLens: async (codeLens, token, next) => {
if (state.config().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')
},
{
command: 'server-info',
category: 'calva.diagnostics'
}
]
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.active.line;
const column = editor.selection.active.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) {
client.sendRequest('workspace/executeCommand', {
'command': command.command,
'arguments': extraParam ? [...params, extraParam] : params
}).catch(e => {
console.error(e);
});
}
}
});
}
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 above a symbol
context.subscriptions.push(vscode.commands.registerCommand('code-lens-references', async (_, line, character) => {
vscode.window.activeTextEditor.selection = new vscode.Selection(line - 1, character - 1, line - 1, character - 1);
await vscode.commands.executeCommand('editor.action.referenceSearch.trigger');
}));
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);
}
}
}));
}
function activate(context: vscode.ExtensionContext): Thenable<void> {
const jarPath = path.join(context.extensionPath, 'clojure-lsp.jar');
const client = createClient(jarPath);
registerCommands(context, client);
registerEventHandlers(context, client);
return new Promise((resolveLspActivation, _rejectLspActivation) => {
vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: "clojure-lsp starting. You don't need to wait for it to start using Calva. Please go ahead with Jack-in or Connect to the REPL. See https://calva.io/clojure-lsp for more info.",
cancellable: false
}, async (_progress, _token) => {
await client.onReady();
state.cursor.set(LSP_CLIENT_KEY, client);
resolveLspActivation();
});
client.start();
});
}
function deactivate(): Promise<void> {
const client = state.deref().get(LSP_CLIENT_KEY);
if (client) {
return client.stop();
}
return Promise.resolve();
}
export default {
activate,
deactivate,
LSP_CLIENT_KEY
}