-
-
Notifications
You must be signed in to change notification settings - Fork 366
/
codeLens.ts
63 lines (55 loc) · 1.66 KB
/
codeLens.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
import * as shared from '@volar/shared';
import * as vscode from 'vscode-languageserver-protocol';
import type { LanguageServiceRuntimeContext } from '../types';
import { languageFeatureWorker } from '../utils/featureWorkers';
import { executePluginCommand, ExecutePluginCommandArgs } from './executeCommand';
export interface PluginCodeLensData {
uri: string,
originalItem: vscode.CodeLens,
pluginId: number,
}
export function register(context: LanguageServiceRuntimeContext) {
return async (uri: string) => {
return await languageFeatureWorker(
context,
uri,
undefined,
(arg, sourceMap) => [arg],
async (plugin, document, arg, sourceMap) => {
const codeLens = await plugin.codeLens?.on?.(document);
if (codeLens) {
return codeLens.map(item => {
const data: PluginCodeLensData = {
uri,
originalItem: item,
pluginId: context.getPluginId(plugin),
};
const commandArgs: ExecutePluginCommandArgs | undefined = item.command ? [uri, context.getPluginId(plugin), item.command] : undefined;
const codeLens: vscode.CodeLens = {
...item,
command: item.command && commandArgs ? {
...item.command,
command: executePluginCommand,
arguments: commandArgs as any,
} : undefined,
data: data,
};
return codeLens;
});
}
},
(data, sourceMap) => data.map(codeLens => {
if (!sourceMap)
return codeLens;
const range = sourceMap.getSourceRange(codeLens.range.start, codeLens.range.end)?.[0];
if (range) {
return {
...codeLens,
range,
};
}
}).filter(shared.notEmpty),
arr => arr.flat(),
) ?? [];
};
}