forked from sveltejs/language-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvelte-check.ts
113 lines (104 loc) · 3.75 KB
/
svelte-check.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
import { Document, DocumentManager } from './lib/documents';
import { LSConfigManager } from './ls-config';
import { CSSPlugin, PluginHost, SveltePlugin, TypeScriptPlugin } from './plugins';
import { Diagnostic, Position, Range } from 'vscode-languageserver';
import { Logger } from './logger';
import { urlToPath, pathToUrl } from './utils';
export type SvelteCheckDiagnosticSource = 'js' | 'css' | 'svelte';
export interface SvelteCheckOptions {
compilerWarnings?: Record<string, 'ignore' | 'error'>;
diagnosticSources?: SvelteCheckDiagnosticSource[];
}
/**
* Small wrapper around PluginHost's Diagnostic Capabilities
* for svelte-check, without the overhead of the lsp.
*/
export class SvelteCheck {
private docManager = new DocumentManager(
(textDocument) => new Document(textDocument.uri, textDocument.text)
);
private configManager = new LSConfigManager();
private pluginHost = new PluginHost(this.docManager);
constructor(workspacePath: string, options: SvelteCheckOptions = {}) {
Logger.setLogErrorsOnly(true);
this.initialize(workspacePath, options);
}
private initialize(workspacePath: string, options: SvelteCheckOptions) {
this.configManager.update({
svelte: {
compilerWarnings: options.compilerWarnings
}
});
// No HTMLPlugin, it does not provide diagnostics
if (shouldRegister('svelte')) {
this.pluginHost.register(new SveltePlugin(this.configManager));
}
if (shouldRegister('css')) {
this.pluginHost.register(new CSSPlugin(this.docManager, this.configManager));
}
if (shouldRegister('js')) {
this.pluginHost.register(
new TypeScriptPlugin(
this.docManager,
this.configManager,
[pathToUrl(workspacePath)],
/**isEditor */ false
)
);
}
function shouldRegister(source: SvelteCheckDiagnosticSource) {
return !options.diagnosticSources || options.diagnosticSources.includes(source);
}
}
/**
* Creates/updates given document
*
* @param doc Text and Uri of the document
*/
upsertDocument(doc: { text: string; uri: string }) {
if (doc.uri.endsWith('.ts') || doc.uri.endsWith('.js')) {
this.pluginHost.updateTsOrJsFile(urlToPath(doc.uri) || '', [
{
range: Range.create(
Position.create(0, 0),
Position.create(Number.MAX_VALUE, Number.MAX_VALUE)
),
text: doc.text
}
]);
} else {
this.docManager.openDocument({
text: doc.text,
uri: doc.uri
});
this.docManager.markAsOpenedInClient(doc.uri);
}
}
/**
* Removes/closes document
*
* @param uri Uri of the document
*/
removeDocument(uri: string) {
this.docManager.closeDocument(uri);
this.docManager.releaseDocument(uri);
}
/**
* Gets the diagnostics for all currently open files.
*/
async getDiagnostics(): Promise<
Array<{ filePath: string; text: string; diagnostics: Diagnostic[] }>
> {
return await Promise.all(
this.docManager.getAllOpenedByClient().map(async (doc) => {
const uri = doc[1].uri;
const diagnostics = await this.pluginHost.getDiagnostics({ uri });
return {
filePath: urlToPath(uri) || '',
text: this.docManager.get(uri)?.getText() || '',
diagnostics
};
})
);
}
}