-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathindex.ts
More file actions
116 lines (108 loc) · 3.54 KB
/
index.ts
File metadata and controls
116 lines (108 loc) · 3.54 KB
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
import * as vscode from 'vscode';
import * as protocol from './protocol.js';
export { activate as activateAutoInsertion } from './lib/features/autoInsertion';
export { activate as activateDocumentDropEdit } from './lib/features/documentDropEdits';
export { activate as activateFindFileReferences } from './lib/features/fileReferences';
export { activate as activateReloadProjects } from './lib/features/reloadProject';
export { activate as activateTsConfigStatusItem } from './lib/features/tsconfig';
export { activate as activateTsVersionStatusItem, getTsdk } from './lib/features/tsVersion';
export * from 'vscode-languageclient';
import type { BaseLanguageClient, Middleware } from 'vscode-languageclient';
export const middleware: Middleware = {
async provideCodeActions(document, range, context, token, next) {
let actions = await next(document, range, context, token);
actions = actions?.map(action => {
if (!(action instanceof vscode.CodeAction)) {
return parseServerCommand(action);
}
if (action.command) {
action.command = parseServerCommand(action.command);
}
return action;
});
return actions;
},
async resolveCodeAction(item, token, next) {
const action = await next(item, token);
if (action?.command) {
action.command = parseServerCommand(action.command);
}
return action;
},
async provideCodeLenses(document, token, next) {
let codeLenses = await next(document, token);
codeLenses = codeLenses?.map(action => {
if (action.command) {
action.command = parseServerCommand(action.command);
}
return action;
});
return codeLenses;
},
async resolveCodeLens(item, token, next) {
const codeLens = await next(item, token);
if (codeLens?.command) {
codeLens.command = parseServerCommand(codeLens.command);
}
return codeLens;
},
};
export function parseServerCommand(command: vscode.Command) {
if (command.command === 'editor.action.rename' && command.arguments) {
return {
...command,
arguments: [[
vscode.Uri.parse(command.arguments[0]),
new vscode.Position(command.arguments[1].line, command.arguments[1].character),
]],
};
}
else if (command.command === 'editor.action.showReferences' && command.arguments) {
return {
...command,
arguments: [
vscode.Uri.parse(command.arguments[0]),
new vscode.Position(command.arguments[1].line, command.arguments[1].character),
command.arguments[2].map((ref: any) =>
new vscode.Location(
vscode.Uri.parse(ref.uri),
new vscode.Range(
ref.range.start.line,
ref.range.start.character,
ref.range.end.line,
ref.range.end.character,
),
)
),
],
};
}
return command;
}
export const currentLabsVersion = '2.3.1';
export function createLabsInfo(_?: typeof import('@volar/language-server/protocol')) {
const onDidAddLanguageClientEmitter = new vscode.EventEmitter<BaseLanguageClient>();
const extensionExports: LabsInfo = {
volarLabs: {
version: currentLabsVersion,
languageClients: [] as BaseLanguageClient[],
languageServerProtocol: protocol,
onDidAddLanguageClient: onDidAddLanguageClientEmitter.event,
},
};
return {
extensionExports,
addLanguageClient(languageClient: BaseLanguageClient) {
extensionExports.volarLabs.languageClients.push(languageClient);
onDidAddLanguageClientEmitter.fire(languageClient);
},
};
}
export interface LabsInfo {
volarLabs: {
version: typeof currentLabsVersion;
languageClients: BaseLanguageClient[];
onDidAddLanguageClient: vscode.Event<BaseLanguageClient>;
languageServerProtocol: typeof import('./protocol.js');
};
}