-
-
Notifications
You must be signed in to change notification settings - Fork 192
/
service.ts
138 lines (120 loc) · 4.98 KB
/
service.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
import { dirname, resolve } from 'path';
import ts from 'typescript';
import { Document } from '../../api';
import { getSveltePackageInfo } from '../svelte/sveltePackage';
import { DocumentSnapshot } from './DocumentSnapshot';
import { createSvelteModuleLoader } from './module-loader';
import { ensureRealSvelteFilePath, getScriptKindFromFileName, isSvelteFilePath } from './utils';
export interface LanguageServiceContainer {
getService(): ts.LanguageService;
updateDocument(document: Document): ts.LanguageService;
}
const services = new Map<string, LanguageServiceContainer>();
export type CreateDocument = (fileName: string, content: string) => Document;
export function getLanguageServiceForDocument(
document: Document,
createDocument: CreateDocument,
): ts.LanguageService {
const searchDir = dirname(document.getFilePath()!);
const tsconfigPath =
ts.findConfigFile(searchDir, ts.sys.fileExists, 'tsconfig.json') ||
ts.findConfigFile(searchDir, ts.sys.fileExists, 'jsconfig.json') ||
'';
let service: LanguageServiceContainer;
if (services.has(tsconfigPath)) {
service = services.get(tsconfigPath)!;
} else {
service = createLanguageService(tsconfigPath, createDocument);
services.set(tsconfigPath, service);
}
return service.updateDocument(document);
}
export function createLanguageService(
tsconfigPath: string,
createDocument: CreateDocument,
): LanguageServiceContainer {
const workspacePath = tsconfigPath ? dirname(tsconfigPath) : '';
const documents = new Map<string, DocumentSnapshot>();
const sveltePkgInfo = getSveltePackageInfo(workspacePath);
let compilerOptions: ts.CompilerOptions = {
allowNonTsExtensions: true,
target: ts.ScriptTarget.Latest,
module: ts.ModuleKind.ESNext,
moduleResolution: ts.ModuleResolutionKind.NodeJs,
allowJs: true,
types: [resolve(sveltePkgInfo.path, 'types', 'runtime')],
};
const configJson = tsconfigPath && ts.readConfigFile(tsconfigPath, ts.sys.readFile).config;
let files: string[] = [];
if (configJson) {
const parsedConfig = ts.parseJsonConfigFileContent(
configJson,
ts.sys,
workspacePath,
compilerOptions,
tsconfigPath,
undefined,
[{ extension: 'svelte', isMixedContent: true }],
);
files = parsedConfig.fileNames;
compilerOptions = { ...compilerOptions, ...parsedConfig.options };
}
const svelteModuleLoader = createSvelteModuleLoader(getSvelteSnapshot, compilerOptions);
const host: ts.LanguageServiceHost = {
getCompilationSettings: () => compilerOptions,
getScriptFileNames: () => Array.from(new Set([...files, ...Array.from(documents.keys())])),
getScriptVersion(fileName: string) {
const doc = getSvelteSnapshot(fileName);
return doc ? String(doc.version) : '0';
},
getScriptSnapshot(fileName: string): ts.IScriptSnapshot | undefined {
const doc = getSvelteSnapshot(fileName);
if (doc) {
return doc;
}
return ts.ScriptSnapshot.fromString(this.readFile!(fileName) || '');
},
getCurrentDirectory: () => workspacePath,
getDefaultLibFileName: ts.getDefaultLibFilePath,
fileExists: svelteModuleLoader.fileExists,
readFile: svelteModuleLoader.readFile,
resolveModuleNames: svelteModuleLoader.resolveModuleNames,
readDirectory: ts.sys.readDirectory,
getScriptKind: (fileName: string) => {
const doc = getSvelteSnapshot(fileName);
if (doc) {
return doc.scriptKind;
}
return getScriptKindFromFileName(fileName);
},
};
let languageService = ts.createLanguageService(host);
return {
getService: () => languageService,
updateDocument,
};
function updateDocument(document: Document): ts.LanguageService {
const preSnapshot = documents.get(document.getFilePath()!);
const newSnapshot = DocumentSnapshot.fromDocument(document);
if (preSnapshot && preSnapshot.scriptKind !== newSnapshot.scriptKind) {
// Restart language service as it doesn't handle script kind changes.
languageService.dispose();
languageService = ts.createLanguageService(host);
}
documents.set(document.getFilePath()!, newSnapshot);
return languageService;
}
function getSvelteSnapshot(fileName: string): DocumentSnapshot | undefined {
fileName = ensureRealSvelteFilePath(fileName);
const doc = documents.get(fileName);
if (doc) {
return doc;
}
if (isSvelteFilePath(fileName)) {
const file = ts.sys.readFile(fileName) || '';
const doc = DocumentSnapshot.fromDocument(createDocument(fileName, file));
documents.set(fileName, doc);
return doc;
}
}
}