-
-
Notifications
You must be signed in to change notification settings - Fork 417
/
index.ts
231 lines (220 loc) · 8.41 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
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
import type * as ts from 'typescript';
import { decorateLanguageService } from '@volar/typescript/lib/node/decorateLanguageService';
import { decorateLanguageServiceHost, searchExternalFiles } from '@volar/typescript/lib/node/decorateLanguageServiceHost';
import { createFileRegistry, resolveCommonLanguageId } from '@vue/language-core';
import { projects } from './lib/utils';
import * as vue from '@vue/language-core';
import { startNamedPipeServer } from './lib/server';
import { _getComponentNames } from './lib/requests/componentInfos';
import { capitalize } from '@vue/shared';
const windowsPathReg = /\\/g;
const externalFiles = new WeakMap<ts.server.Project, string[]>();
const projectExternalFileExtensions = new WeakMap<ts.server.Project, string[]>();
const decoratedLanguageServices = new WeakSet<ts.LanguageService>();
const decoratedLanguageServiceHosts = new WeakSet<ts.LanguageServiceHost>();
export = createLanguageServicePlugin();
function createLanguageServicePlugin(): ts.server.PluginModuleFactory {
return modules => {
const { typescript: ts } = modules;
const pluginModule: ts.server.PluginModule = {
create(info) {
if (
!decoratedLanguageServices.has(info.languageService)
&& !decoratedLanguageServiceHosts.has(info.languageServiceHost)
) {
decoratedLanguageServices.add(info.languageService);
decoratedLanguageServiceHosts.add(info.languageServiceHost);
const vueOptions = vue.resolveVueCompilerOptions(getVueCompilerOptions());
const languagePlugin = vue.createVueLanguagePlugin(
ts,
id => id,
info.languageServiceHost.getCompilationSettings(),
vueOptions,
);
const extensions = languagePlugin.typescript?.extraFileExtensions.map(ext => '.' + ext.extension) ?? [];
const getScriptSnapshot = info.languageServiceHost.getScriptSnapshot.bind(info.languageServiceHost);
const files = createFileRegistry(
[languagePlugin],
ts.sys.useCaseSensitiveFileNames,
fileName => {
const snapshot = getScriptSnapshot(fileName);
if (snapshot) {
files.set(fileName, resolveCommonLanguageId(fileName), snapshot);
}
else {
files.delete(fileName);
}
}
);
projectExternalFileExtensions.set(info.project, extensions);
projects.set(info.project, {
info,
files,
ts,
vueOptions,
});
decorateLanguageService(files, info.languageService);
decorateLanguageServiceHost(files, info.languageServiceHost, ts);
startNamedPipeServer(info.project.projectKind, info.project.getCurrentDirectory());
const getCompletionsAtPosition = info.languageService.getCompletionsAtPosition;
const getCompletionEntryDetails = info.languageService.getCompletionEntryDetails;
const getCodeFixesAtPosition = info.languageService.getCodeFixesAtPosition;
const getEncodedSemanticClassifications = info.languageService.getEncodedSemanticClassifications;
info.languageService.getCompletionsAtPosition = (fileName, position, options) => {
const result = getCompletionsAtPosition(fileName, position, options);
if (result) {
// filter __VLS_
result.entries = result.entries.filter(
entry => entry.name.indexOf('__VLS_') === -1
&& (!entry.labelDetails?.description || entry.labelDetails.description.indexOf('__VLS_') === -1)
);
// modify label
for (const item of result.entries) {
if (item.source) {
const originalName = item.name;
for (const ext of vueOptions.extensions) {
const suffix = capitalize(ext.substring('.'.length)); // .vue -> Vue
if (item.source.endsWith(ext) && item.name.endsWith(suffix)) {
item.name = item.name.slice(0, -suffix.length);
if (item.insertText) {
// #2286
item.insertText = item.insertText.replace(`${suffix}$1`, '$1');
}
if (item.data) {
// @ts-expect-error
item.data.__isComponentAutoImport = {
ext,
suffix,
originalName,
newName: item.insertText,
};
}
break;
}
}
}
}
}
return result;
};
info.languageService.getCompletionEntryDetails = (...args) => {
const details = getCompletionEntryDetails(...args);
// modify import statement
// @ts-expect-error
if (args[6]?.__isComponentAutoImport) {
// @ts-expect-error
const { ext, suffix, originalName, newName } = args[6]?.__isComponentAutoImport;
for (const codeAction of details?.codeActions ?? []) {
for (const change of codeAction.changes) {
for (const textChange of change.textChanges) {
textChange.newText = textChange.newText.replace('import ' + originalName + ' from ', 'import ' + newName + ' from ');
}
}
}
}
return details;
};
info.languageService.getCodeFixesAtPosition = (...args) => {
let result = getCodeFixesAtPosition(...args);
// filter __VLS_
result = result.filter(entry => entry.description.indexOf('__VLS_') === -1);
return result;
};
info.languageService.getEncodedSemanticClassifications = (fileName, span, format) => {
const result = getEncodedSemanticClassifications(fileName, span, format);
const file = files.get(fileName);
if (
file?.generated?.code instanceof vue.VueGeneratedCode
&& file.generated.code.sfc.template
) {
const validComponentNames = _getComponentNames(ts, info.languageService, file.generated.code, vueOptions);
const components = new Set([
...validComponentNames,
...validComponentNames.map(vue.hyphenateTag),
]);
const { template } = file.generated.code.sfc;
const spanTemplateRange = [
span.start - template.startTagEnd,
span.start + span.length - template.startTagEnd,
] as const;
template.ast?.children.forEach(function visit(node) {
if (node.loc.end.offset <= spanTemplateRange[0] || node.loc.start.offset >= spanTemplateRange[1]) {
return;
}
if (node.type === 1 satisfies vue.CompilerDOM.NodeTypes.ELEMENT) {
if (components.has(node.tag)) {
result.spans.push(
node.loc.start.offset + node.loc.source.indexOf(node.tag) + template.startTagEnd,
node.tag.length,
256, // class
);
if (template.lang === 'html' && !node.isSelfClosing) {
result.spans.push(
node.loc.start.offset + node.loc.source.lastIndexOf(node.tag) + template.startTagEnd,
node.tag.length,
256, // class
);
}
}
for (const child of node.children) {
visit(child);
}
}
else if (node.type === 9 satisfies vue.CompilerDOM.NodeTypes.IF) {
for (const branch of node.branches) {
for (const child of branch.children) {
visit(child);
}
}
}
else if (node.type === 11 satisfies vue.CompilerDOM.NodeTypes.FOR) {
for (const child of node.children) {
visit(child);
}
}
});
}
return result;
};
}
return info.languageService;
function getVueCompilerOptions() {
if (info.project.projectKind === ts.server.ProjectKind.Configured) {
const tsconfig = info.project.getProjectName();
return vue.createParsedCommandLine(ts, ts.sys, tsconfig.replace(windowsPathReg, '/')).vueOptions;
}
else {
return vue.createParsedCommandLineByJson(ts, ts.sys, info.languageServiceHost.getCurrentDirectory(), {}).vueOptions;
}
}
},
getExternalFiles(project, updateLevel = 0) {
if (
updateLevel >= (1 satisfies ts.ProgramUpdateLevel.RootNamesAndUpdate)
|| !externalFiles.has(project)
) {
const oldFiles = externalFiles.get(project);
const newFiles = searchExternalFiles(ts, project, projectExternalFileExtensions.get(project)!);
externalFiles.set(project, newFiles);
if (oldFiles && !arrayItemsEqual(oldFiles, newFiles)) {
project.refreshDiagnostics();
}
}
return externalFiles.get(project)!;
},
};
return pluginModule;
};
}
function arrayItemsEqual(a: string[], b: string[]) {
if (a.length !== b.length) {
return false;
}
const set = new Set(a);
for (const file of b) {
if (!set.has(file)) {
return false;
}
}
return true;
}