-
-
Notifications
You must be signed in to change notification settings - Fork 200
/
CSSPlugin.ts
298 lines (260 loc) · 9.33 KB
/
CSSPlugin.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import { getEmmetCompletionParticipants, doComplete as doEmmetComplete } from 'vscode-emmet-helper';
import {
Color,
ColorInformation,
ColorPresentation,
CompletionContext,
CompletionList,
CompletionTriggerKind,
Diagnostic,
Hover,
Position,
Range,
SymbolInformation,
CompletionItem,
CompletionItemKind,
} from 'vscode-languageserver';
import {
Document,
DocumentManager,
mapColorPresentationToOriginal,
mapCompletionItemToOriginal,
mapRangeToGenerated,
mapSymbolInformationToOriginal,
mapObjWithRangeToOriginal,
mapHoverToParent,
} from '../../lib/documents';
import { LSConfigManager, LSCSSConfig } from '../../ls-config';
import {
ColorPresentationsProvider,
CompletionsProvider,
DiagnosticsProvider,
DocumentColorsProvider,
DocumentSymbolsProvider,
HoverProvider,
} from '../interfaces';
import { CSSDocument } from './CSSDocument';
import { getLanguage, getLanguageService } from './service';
import { GlobalVars } from './global-vars';
export class CSSPlugin
implements
HoverProvider,
CompletionsProvider,
DiagnosticsProvider,
DocumentColorsProvider,
ColorPresentationsProvider,
DocumentSymbolsProvider {
private configManager: LSConfigManager;
private cssDocuments = new WeakMap<Document, CSSDocument>();
private triggerCharacters = ['.', ':', '-', '/'];
private globalVars = new GlobalVars();
constructor(docManager: DocumentManager, configManager: LSConfigManager) {
this.configManager = configManager;
this.globalVars.watchFiles(this.configManager.get('css.globals'));
this.configManager.onChange((config) =>
this.globalVars.watchFiles(config.get('css.globals')),
);
docManager.on('documentChange', (document) =>
this.cssDocuments.set(document, new CSSDocument(document)),
);
docManager.on('documentClose', (document) => this.cssDocuments.delete(document));
}
getDiagnostics(document: Document): Diagnostic[] {
if (!this.featureEnabled('diagnostics')) {
return [];
}
const cssDocument = this.getCSSDoc(document);
if (isSASS(cssDocument)) {
return [];
}
const kind = extractLanguage(cssDocument);
if (shouldExcludeValidation(kind)) {
return [];
}
return getLanguageService(kind)
.doValidation(cssDocument, cssDocument.stylesheet)
.map((diagnostic) => ({ ...diagnostic, source: getLanguage(kind) }))
.map((diagnostic) => mapObjWithRangeToOriginal(cssDocument, diagnostic));
}
doHover(document: Document, position: Position): Hover | null {
if (!this.featureEnabled('hover')) {
return null;
}
const cssDocument = this.getCSSDoc(document);
if (!cssDocument.isInGenerated(position) || isSASS(cssDocument)) {
return null;
}
const hoverInfo = getLanguageService(extractLanguage(cssDocument)).doHover(
cssDocument,
cssDocument.getGeneratedPosition(position),
cssDocument.stylesheet,
);
return hoverInfo ? mapHoverToParent(cssDocument, hoverInfo) : hoverInfo;
}
getCompletions(
document: Document,
position: Position,
completionContext?: CompletionContext,
): CompletionList | null {
const triggerCharacter = completionContext?.triggerCharacter;
const triggerKind = completionContext?.triggerKind;
const isCustomTriggerCharater = triggerKind === CompletionTriggerKind.TriggerCharacter;
if (
isCustomTriggerCharater &&
triggerCharacter &&
!this.triggerCharacters.includes(triggerCharacter)
) {
return null;
}
if (!this.featureEnabled('completions')) {
return null;
}
const cssDocument = this.getCSSDoc(document);
if (!cssDocument.isInGenerated(position)) {
return null;
}
if (isSASS(cssDocument)) {
// the css language service does not support sass, still we can use
// the emmet helper directly to at least get emmet completions
return doEmmetComplete(document, position, 'sass', {});
}
const type = extractLanguage(cssDocument);
const lang = getLanguageService(type);
const emmetResults: CompletionList = {
isIncomplete: true,
items: [],
};
lang.setCompletionParticipants([
getEmmetCompletionParticipants(
cssDocument,
cssDocument.getGeneratedPosition(position),
getLanguage(type),
{},
emmetResults,
),
]);
const results = lang.doComplete(
cssDocument,
cssDocument.getGeneratedPosition(position),
cssDocument.stylesheet,
);
return CompletionList.create(
this.appendGlobalVars(
[...(results ? results.items : []), ...emmetResults.items].map((completionItem) =>
mapCompletionItemToOriginal(cssDocument, completionItem),
),
),
// Emmet completions change on every keystroke, so they are never complete
emmetResults.items.length > 0,
);
}
private appendGlobalVars(items: CompletionItem[]): CompletionItem[] {
// Finding one value with that item kind means we are in a value completion scenario
const value = items.find((item) => item.kind === CompletionItemKind.Value);
if (!value) {
return items;
}
const additionalItems: CompletionItem[] = this.globalVars
.getGlobalVars()
.map((globalVar) => ({
label: globalVar.name,
detail: `${globalVar.filename}\n\n${globalVar.name}: ${globalVar.value}`,
textEdit: value.textEdit && {
...value.textEdit,
newText: `var(${globalVar.name})`,
},
kind: CompletionItemKind.Value,
}));
return [...items, ...additionalItems];
}
getDocumentColors(document: Document): ColorInformation[] {
if (!this.featureEnabled('documentColors')) {
return [];
}
const cssDocument = this.getCSSDoc(document);
if (isSASS(cssDocument)) {
return [];
}
return getLanguageService(extractLanguage(cssDocument))
.findDocumentColors(cssDocument, cssDocument.stylesheet)
.map((colorInfo) => mapObjWithRangeToOriginal(cssDocument, colorInfo));
}
getColorPresentations(document: Document, range: Range, color: Color): ColorPresentation[] {
if (!this.featureEnabled('colorPresentations')) {
return [];
}
const cssDocument = this.getCSSDoc(document);
if (
(!cssDocument.isInGenerated(range.start) && !cssDocument.isInGenerated(range.end)) ||
isSASS(cssDocument)
) {
return [];
}
return getLanguageService(extractLanguage(cssDocument))
.getColorPresentations(
cssDocument,
cssDocument.stylesheet,
color,
mapRangeToGenerated(cssDocument, range),
)
.map((colorPres) => mapColorPresentationToOriginal(cssDocument, colorPres));
}
getDocumentSymbols(document: Document): SymbolInformation[] {
if (!this.featureEnabled('documentColors')) {
return [];
}
const cssDocument = this.getCSSDoc(document);
if (isSASS(cssDocument)) {
return [];
}
return getLanguageService(extractLanguage(cssDocument))
.findDocumentSymbols(cssDocument, cssDocument.stylesheet)
.map((symbol) => {
if (!symbol.containerName) {
return {
...symbol,
// TODO: this could contain other things, e.g. style.myclass
containerName: 'style',
};
}
return symbol;
})
.map((symbol) => mapSymbolInformationToOriginal(cssDocument, symbol));
}
private getCSSDoc(document: Document) {
let cssDoc = this.cssDocuments.get(document);
if (!cssDoc || cssDoc.version < document.version) {
cssDoc = new CSSDocument(document);
this.cssDocuments.set(document, cssDoc);
}
return cssDoc;
}
private featureEnabled(feature: keyof LSCSSConfig) {
return (
this.configManager.enabled('css.enable') &&
this.configManager.enabled(`css.${feature}.enable`)
);
}
}
function shouldExcludeValidation(kind?: string) {
switch (kind) {
case 'postcss':
case 'text/postcss':
return true;
default:
return false;
}
}
function isSASS(document: CSSDocument) {
switch (extractLanguage(document)) {
case 'sass':
case 'text/sass':
return true;
default:
return false;
}
}
function extractLanguage(document: CSSDocument): string {
const attrs = document.getAttributes();
return attrs.lang || attrs.type;
}