-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
SveltePlugin.ts
231 lines (205 loc) · 7.52 KB
/
SveltePlugin.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 {
CancellationToken,
CodeAction,
CodeActionContext,
CompletionContext,
CompletionList,
Diagnostic,
FormattingOptions,
Hover,
Position,
Range,
SelectionRange,
TextEdit,
WorkspaceEdit
} from 'vscode-languageserver';
import { getPackageInfo, importPrettier } from '../../importPackage';
import { Document } from '../../lib/documents';
import { Logger } from '../../logger';
import { LSConfigManager, LSSvelteConfig } from '../../ls-config';
import {
CodeActionsProvider,
CompletionsProvider,
DiagnosticsProvider,
FormattingProvider,
HoverProvider,
SelectionRangeProvider
} from '../interfaces';
import { executeCommand, getCodeActions } from './features/getCodeActions';
import { getCompletions } from './features/getCompletions';
import { getDiagnostics } from './features/getDiagnostics';
import { getHoverInfo } from './features/getHoverInfo';
import { getSelectionRange } from './features/getSelectionRanges';
import { SvelteCompileResult, SvelteDocument } from './SvelteDocument';
export class SveltePlugin
implements
DiagnosticsProvider,
FormattingProvider,
CompletionsProvider,
HoverProvider,
CodeActionsProvider,
SelectionRangeProvider
{
__name = 'svelte';
private docManager = new Map<Document, SvelteDocument>();
constructor(private configManager: LSConfigManager) {}
async getDiagnostics(document: Document): Promise<Diagnostic[]> {
if (!this.featureEnabled('diagnostics') || !this.configManager.getIsTrusted()) {
return [];
}
return getDiagnostics(
document,
await this.getSvelteDoc(document),
this.configManager.getConfig().svelte.compilerWarnings
);
}
async getCompiledResult(document: Document): Promise<SvelteCompileResult | null> {
try {
const svelteDoc = await this.getSvelteDoc(document);
return svelteDoc.getCompiledWith({ generate: 'dom' });
} catch (error) {
return null;
}
}
async formatDocument(document: Document, options: FormattingOptions): Promise<TextEdit[]> {
if (!this.featureEnabled('format')) {
return [];
}
const filePath = document.getFilePath()!;
const prettier = importPrettier(filePath);
// Try resolving the config through prettier and fall back to possible editor config
const config = this.configManager.getMergedPrettierConfig(
await prettier.resolveConfig(filePath, { editorconfig: true }),
// Be defensive here because IDEs other than VSCode might not have these settings
options && {
tabWidth: options.tabSize,
useTabs: !options.insertSpaces
}
);
// If user has prettier-plugin-svelte 1.x, then remove `options` from the sort
// order or else it will throw a config error (`options` was not present back then).
if (
config?.svelteSortOrder &&
getPackageInfo('prettier-plugin-svelte', filePath)?.version.major < 2
) {
config.svelteSortOrder = config.svelteSortOrder
.replace('-options', '')
.replace('options-', '');
}
// Take .prettierignore into account
const fileInfo = await prettier.getFileInfo(filePath, {
ignorePath: this.configManager.getPrettierConfig()?.ignorePath ?? '.prettierignore',
// Sapper places stuff within src/node_modules, we want to format that, too
withNodeModules: true
});
if (fileInfo.ignored) {
Logger.debug('File is ignored, formatting skipped');
return [];
}
const formattedCode = prettier.format(document.getText(), {
...config,
plugins: [...(config.plugins ?? []), ...getSveltePlugin()],
parser: 'svelte' as any
});
return document.getText() === formattedCode
? []
: [
TextEdit.replace(
Range.create(
document.positionAt(0),
document.positionAt(document.getTextLength())
),
formattedCode
)
];
function getSveltePlugin() {
// Only provide our version of the svelte plugin if the user doesn't have one in
// the workspace already. If we did it, Prettier would - for some reason - use
// the workspace version for parsing and the extension version for printing,
// which could crash if the contract of the parser output changed.
const hasPluginLoadedAlready = prettier
.getSupportInfo()
.languages.some((l) => l.name === 'svelte');
return hasPluginLoadedAlready ? [] : [require.resolve('prettier-plugin-svelte')];
}
}
async getCompletions(
document: Document,
position: Position,
_?: CompletionContext,
cancellationToken?: CancellationToken
): Promise<CompletionList | null> {
if (!this.featureEnabled('completions')) {
return null;
}
const svelteDoc = await this.getSvelteDoc(document);
if (cancellationToken?.isCancellationRequested) {
return null;
}
return getCompletions(document, svelteDoc, position);
}
async doHover(document: Document, position: Position): Promise<Hover | null> {
if (!this.featureEnabled('hover')) {
return null;
}
return getHoverInfo(document, await this.getSvelteDoc(document), position);
}
async getCodeActions(
document: Document,
range: Range,
context: CodeActionContext,
cancellationToken?: CancellationToken
): Promise<CodeAction[]> {
if (!this.featureEnabled('codeActions')) {
return [];
}
const svelteDoc = await this.getSvelteDoc(document);
if (cancellationToken?.isCancellationRequested) {
return [];
}
try {
return getCodeActions(svelteDoc, range, context);
} catch (error) {
return [];
}
}
async executeCommand(
document: Document,
command: string,
args?: any[]
): Promise<WorkspaceEdit | string | null> {
if (!this.featureEnabled('codeActions')) {
return null;
}
const svelteDoc = await this.getSvelteDoc(document);
try {
return executeCommand(svelteDoc, command, args);
} catch (error) {
return null;
}
}
async getSelectionRange(
document: Document,
position: Position
): Promise<SelectionRange | null> {
if (!this.featureEnabled('selectionRange')) {
return null;
}
const svelteDoc = await this.getSvelteDoc(document);
return getSelectionRange(svelteDoc, position);
}
private featureEnabled(feature: keyof LSSvelteConfig) {
return (
this.configManager.enabled('svelte.enable') &&
this.configManager.enabled(`svelte.${feature}.enable`)
);
}
private async getSvelteDoc(document: Document) {
let svelteDoc = this.docManager.get(document);
if (!svelteDoc || svelteDoc.version !== document.version) {
svelteDoc = new SvelteDocument(document);
this.docManager.set(document, svelteDoc);
}
return svelteDoc;
}
}