-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
151 lines (130 loc) · 4.45 KB
/
index.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
import {
CancellationToken,
CompletionContext,
CompletionItem,
CompletionList,
ExtensionContext,
ForkOptions,
InitializeParams,
LanguageClient,
LanguageClientOptions,
LinesTextDocument,
Position,
ProvideCompletionItemsSignature,
ServerOptions,
Thenable,
TransportKind,
extensions,
services,
window,
workspace,
} from 'coc.nvim';
import * as fs from 'fs';
import * as path from 'path';
import { activate as commonActivate, deactivate as commonDeactivate } from './common';
import {
config,
getConfigDisableProgressNotifications,
getConfigMiddlewareProvideCompletionItemEnable,
getConfigVolarEnable,
getDisabledFeatures,
} from './config';
let serverModule: string;
export async function activate(context: ExtensionContext): Promise<void> {
if (!getConfigVolarEnable()) return;
const cocTypeScriptVuePlugin = extensions.getExtensionById('@yaegassy/coc-typescript-vue-plugin');
if (cocTypeScriptVuePlugin) {
window.showWarningMessage(
'Please uninstall @yaegassy/coc-typescript-vue-plugin as it has been deprecated. :CocUninstall @yaegassy/coc-typescript-vue-plugin',
);
return;
}
let tsExtension = extensions.getExtensionById('coc-tsserver');
if (!tsExtension) {
// https://github.com/neoclide/coc-tsserver/pull/445#issuecomment-1976305468
tsExtension = extensions.getExtensionById('coc-tsserver-dev');
}
if (tsExtension) {
if (!tsExtension.isActive) await tsExtension.activate();
const tsService = services.getService('tsserver');
if (tsService) await tsService.start();
}
return commonActivate(context, (id, name, documentSelector, initOptions, port, outputChannel) => {
class _LanguageClient extends LanguageClient {
fillInitializeParams(params: InitializeParams) {
(params as any).locale = workspace.getConfiguration('volar').get<string>('tsLocale', 'en');
}
}
const vueServerPath = config.server.path ? workspace.expand(config.server.path) : null;
if (vueServerPath != null && fs.existsSync(vueServerPath)) {
serverModule = vueServerPath;
} else {
serverModule = context.asAbsolutePath(
path.join('node_modules', '@vue', 'language-server', 'bin', 'vue-language-server.js'),
);
}
const runOptions: ForkOptions = {};
if (config.server.maxOldSpaceSize) {
runOptions.execArgv ??= [];
runOptions.execArgv.push('--max-old-space-size=' + config.server.maxOldSpaceSize);
}
const debugOptions = { execArgv: ['--nolazy', '--inspect=' + port] };
const serverOptions: ServerOptions = {
run: {
module: serverModule,
transport: TransportKind.ipc,
options: runOptions,
},
debug: {
module: serverModule,
transport: TransportKind.ipc,
options: debugOptions,
},
};
const clientOptions: LanguageClientOptions = {
documentSelector: documentSelector,
initializationOptions: initOptions,
progressOnInitialization: !getConfigDisableProgressNotifications(),
disabledFeatures: getDisabledFeatures(),
middleware: {
provideCompletionItem: getConfigMiddlewareProvideCompletionItemEnable()
? id === 'vue'
? handleProvideCompletionItem
: undefined
: undefined,
},
outputChannel,
};
const client = new _LanguageClient(id, name, serverOptions, clientOptions);
// **Memo**
//
// There is a registerLanguageClient, but it is not available for the
// slightly older coc.nvim, so an error is possible.
//
// Therefore, we will use the old registLanguageClient
context.subscriptions.push(services.registLanguageClient(client));
return client;
});
}
export function deactivate(): Thenable<any> | undefined {
return commonDeactivate();
}
async function handleProvideCompletionItem(
document: LinesTextDocument,
position: Position,
context: CompletionContext,
token: CancellationToken,
next: ProvideCompletionItemsSignature,
) {
const res = await Promise.resolve(next(document, position, context, token));
const doc = workspace.getDocument(document.uri);
if (!doc || !res) return [];
let items: CompletionItem[] = res.hasOwnProperty('isIncomplete')
? (res as CompletionList).items
: (res as CompletionItem[]);
const pre = doc.getline(position.line).slice(0, position.character);
if (context.triggerCharacter === '@' || /@\w*$/.test(pre)) {
items = items.filter((o) => o.label.startsWith('@'));
}
return items;
}