-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
client.ts
154 lines (144 loc) · 6.06 KB
/
client.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
import { CodeActionKind, Command, Executable, LanguageClient, LanguageClientOptions, Position, Range, ServerOptions, StaticFeature, Uri, workspace } from 'coc.nvim';
import { existsSync } from 'fs';
import { join } from 'path';
import { CodeAction, CodeActionParams, CodeActionRequest } from 'vscode-languageserver-protocol';
import { Env } from './config';
import { isRustDocument } from './ctx';
import * as ra from './lsp_ext';
class ExperimentalFeatures implements StaticFeature {
fillClientCapabilities(capabilities: any): void {
const caps: any = capabilities.experimental ?? {};
caps.snippetTextEdit = true;
caps.resolveCodeAction = true;
caps.serverStatusNotification = true;
caps.commands = {
commands: [
'rust-analyzer.runSingle',
'rust-analyzer.debugSingle',
'rust-analyzer.showReferences',
'rust-analyzer.gotoLocation',
'editor.action.triggerParameterHints'],
};
capabilities.experimental = caps;
}
initialize(): void {}
dispose(): void {}
}
function isCodeActionWithoutEditsAndCommands(value: any): boolean {
const candidate: CodeAction = value;
return (
candidate &&
(candidate.diagnostics === void 0 || Array.isArray(candidate.diagnostics)) &&
(candidate.kind === void 0 || typeof candidate.kind === 'string') &&
candidate.edit === void 0 &&
candidate.command === void 0
);
}
export function createClient(bin: string, extra: Env): LanguageClient {
let folder = '.';
if (workspace.workspaceFolder?.uri.length) {
folder = Uri.parse(workspace.workspaceFolder.uri).fsPath;
}
const env = Object.assign(Object.assign({}, process.env), extra);
const run: Executable = {
command: bin,
options: { env, cwd: folder },
};
function standalone(root?: string) {
if (!root) return true;
if (existsSync(join(Uri.parse(root).fsPath, 'Cargo.toml'))) return false;
if (existsSync(join(Uri.parse(root).fsPath, 'rust-project.json'))) return false;
return true;
}
let initializationOptions = workspace.getConfiguration('rust-analyzer');
if (standalone(workspace.workspaceFolder?.uri)) {
const docs = workspace.documents.filter((doc) => isRustDocument(doc.textDocument));
if (docs.length) {
initializationOptions = { detachedFiles: docs.map((doc) => Uri.parse(doc.uri).fsPath), ...initializationOptions };
}
}
const serverOptions: ServerOptions = run;
const clientOptions: LanguageClientOptions = {
documentSelector: [{ language: 'rust' }],
initializationOptions,
middleware: {
async provideHover(document, position, token) {
let positionOrRange: Range | Position | null = null;
const mode = (await workspace.nvim.call('mode')) as string;
if (mode === 'v' || mode === 'V') {
const doc = workspace.getDocument(document.uri);
await workspace.nvim.call('eval', 'feedkeys("\\<esc>", "in")');
positionOrRange = await workspace.getSelectedRange(mode, doc);
}
if (!positionOrRange) positionOrRange = position;
const param: ra.HoverParams = {
position: positionOrRange || position,
textDocument: { uri: document.uri },
};
return await client.sendRequest(ra.hover, param, token);
},
async resolveCompletionItem(item, token, next) {
if (item.data && !item.data.position) {
// TODO: remove this if coc undefined item.data
// coc will set item.data to {} if undefined
// but RA will check item.data.position to resolve
// this hacks to delete item.data
delete item.data;
}
return await next(item, token);
},
async provideCodeActions(document, range, context, token) {
const params: CodeActionParams = {
textDocument: { uri: document.uri },
range,
context,
};
const values = await client.sendRequest(CodeActionRequest.type.method, params, token);
if (values === null) return undefined;
const result: (CodeAction | Command)[] = [];
for (const item of values as (Command | CodeAction)[]) {
if (CodeAction.is(item)) {
result.push(item);
continue;
}
if (!isCodeActionWithoutEditsAndCommands(item)) {
console.error('isCodeActionWithoutEditsAndCommands:', JSON.stringify(item));
continue;
}
const command: Command = {
command: 'rust-analyzer.resolveCodeAction',
title: item.title,
arguments: [item],
};
const kind: CodeActionKind = (item as any).kind;
result.push(CodeAction.create(item.title, command, kind));
}
return result;
},
},
};
const client = new LanguageClient('rust-analyzer', 'Rust Analyzer Language Server', serverOptions, clientOptions);
// HACK: This is an awful way of filtering out the decorations notifications
// However, pending proper support, this is the most effecitve approach
// Proper support for this would entail a change to vscode-languageclient to allow not notifying on certain messages
// Or the ability to disable the serverside component of highlighting (but this means that to do tracing we need to disable hihlighting)
// This also requires considering our settings strategy, which is work which needs doing
// @ts-ignore The tracer is private to vscode-languageclient, but we need access to it to not log publishDecorations requests
client._tracer = {
log: (messageOrDataObject: string | unknown, data?: string) => {
if (typeof messageOrDataObject === 'string') {
if (messageOrDataObject.includes('rust-analyzer/publishDecorations') || messageOrDataObject.includes('rust-analyzer/decorationsRequest')) {
// Don't log publish decorations requests
} else {
// @ts-ignore This is just a utility function
client.logTrace(messageOrDataObject, data);
}
} else {
// @ts-ignore
client.logObjectTrace(messageOrDataObject);
}
},
};
client.registerFeature(new ExperimentalFeatures());
return client;
}