-
-
Notifications
You must be signed in to change notification settings - Fork 414
/
index.ts
134 lines (123 loc) · 4.83 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
import { decorateLanguageService } from '@volar/typescript/lib/node/decorateLanguageService';
import { decorateLanguageServiceHost, searchExternalFiles } from '@volar/typescript/lib/node/decorateLanguageServiceHost';
import * as vue from '@vue/language-core';
import { createLanguage } from '@vue/language-core';
import type * as ts from 'typescript';
import { decorateLanguageServiceForVue } from './lib/common';
import { startNamedPipeServer, projects } from './lib/server';
import { resolveFileLanguageId } from '@volar/typescript';
const windowsPathReg = /\\/g;
const externalFiles = new WeakMap<ts.server.Project, Set<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 = getVueCompilerOptions();
const languagePlugin = vue.createVueLanguagePlugin<string>(
ts,
id => id,
info.languageServiceHost.useCaseSensitiveFileNames?.() ?? false,
() => info.languageServiceHost.getProjectVersion?.() ?? '',
() => externalFiles.get(info.project) ?? [],
info.languageServiceHost.getCompilationSettings(),
vueOptions
);
const extensions = languagePlugin.typescript?.extraFileExtensions.map(ext => '.' + ext.extension) ?? [];
const getScriptSnapshot = info.languageServiceHost.getScriptSnapshot.bind(info.languageServiceHost);
const getScriptVersion = info.languageServiceHost.getScriptVersion.bind(info.languageServiceHost);
const syncedScriptVersions = new vue.FileMap<string>(ts.sys.useCaseSensitiveFileNames);
const language = createLanguage<string>(
[
languagePlugin,
{
getLanguageId(fileName) {
return resolveFileLanguageId(fileName);
},
},
],
new vue.FileMap(ts.sys.useCaseSensitiveFileNames),
fileName => {
const version = getScriptVersion(fileName);
if (syncedScriptVersions.get(fileName) === version) {
return;
}
syncedScriptVersions.set(fileName, version);
const snapshot = getScriptSnapshot(fileName);
if (snapshot) {
language.scripts.set(fileName, snapshot);
}
else {
language.scripts.delete(fileName);
}
}
);
projectExternalFileExtensions.set(info.project, extensions);
projects.set(info.project, { info, language, vueOptions });
decorateLanguageService(language, info.languageService);
decorateLanguageServiceForVue(language, info.languageService, vueOptions, ts, true, fileName => fileName);
decorateLanguageServiceHost(ts, language, info.languageServiceHost);
startNamedPipeServer(ts, info.project.projectKind, info.project.getCurrentDirectory());
// #3963
const timer = setInterval(() => {
if (info.project['program']) {
clearInterval(timer);
(info.project['program'] as any).__vue__ = { language };
}
}, 50);
}
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 = new Set(searchExternalFiles(ts, project, projectExternalFileExtensions.get(project)!));
externalFiles.set(project, newFiles);
if (oldFiles && !twoSetsEqual(oldFiles, newFiles)) {
for (const oldFile of oldFiles) {
if (!newFiles.has(oldFile)) {
projects.get(project)?.language.scripts.delete(oldFile);
}
}
project.refreshDiagnostics();
}
}
return [...externalFiles.get(project)!];
},
};
return pluginModule;
};
}
function twoSetsEqual(a: Set<string>, b: Set<string>) {
if (a.size !== b.size) {
return false;
}
for (const file of a) {
if (!b.has(file)) {
return false;
}
}
return true;
}