-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcss.ts
244 lines (228 loc) · 6.56 KB
/
css.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
import { promises as fs_promises } from "fs";
import path = require("path");
import {
Position,
Range as vscodeRange,
Uri,
TextDocument as vscode_TextDocument,
} from "vscode";
import {
getCSSLanguageService,
getSCSSLanguageService,
TextDocument,
Range,
getLESSLanguageService,
ColorInformation,
} from "vscode-css-languageservice";
import { CssModuleExtensions } from "../../constants";
import {
CustomPropertyDeclaration,
Node,
NodeType,
RuleSet,
Stylesheet,
} from "../../css-node.types";
import {
isChild,
isColorString,
isCombination,
isNormal,
isPsuedo,
isSibling,
isSuffix,
} from "../utils";
import Store from "../../store/Store";
export const getLanguageService = (module: string) => {
switch (path.extname(module) as CssModuleExtensions) {
case ".css":
return getCSSLanguageService();
case ".scss":
return getSCSSLanguageService();
case ".less":
return getLESSLanguageService();
default:
return getCSSLanguageService();
}
};
export const getLanguageId = (module: string) => {
switch (path.extname(module) as CssModuleExtensions) {
case ".css":
return "css";
case ".scss":
return "scss";
case ".less":
return "less";
default:
return "css";
}
};
export type CssParserResult = {
selectors: Map<string, Selector>;
eofRange: vscodeRange;
variables: Variable[];
ast: Stylesheet;
colors: ColorInformation[];
};
export const parseCss = async (
module: string,
): Promise<CssParserResult | undefined> => {
try {
const languageService = getLanguageService(module);
const content = (await fs_promises.readFile(module)).toString();
const document = TextDocument.create(
module,
getLanguageId(module),
1,
content,
);
const ast = languageService.parseStylesheet(document);
const selectors = getSelectors(ast as Stylesheet, document);
const variables = getVariables(ast as Stylesheet, document);
const colors = languageService.findDocumentColors(document, ast);
const eofRange = new vscodeRange(
new Position(document.lineCount + 2, 0),
new Position(document.lineCount + 2, 0),
);
return { selectors, eofRange, variables, ast: ast as Stylesheet, colors };
} catch (e) {
Store.outputChannel.error(
`CSSParserError: Parsing css module ${module} failed`,
);
}
};
export type Selector = {
selector: string;
range: Range;
content: string;
selectionRange: Range;
};
export type Variable = {
name?: string;
value?: string;
kind: "color" | "normal";
location: {
value_range: Range;
uri: Uri;
full_range: Range;
property_range: Range;
};
};
export const getSelectors = (ast: Stylesheet, document: TextDocument) => {
const selectors: Map<string, Selector> = new Map();
const visitedRules: Node[] = [];
const insertSelector = (selectorNode: Node, selector: string) => {
const parentRule = visitedRules[visitedRules.length - 1];
if (!parentRule) return;
if (selectors.has(selector)) {
return;
}
const range = Range.create(
document.positionAt(selectorNode.offset),
document.positionAt(selectorNode.end),
);
const selectionRange = Range.create(
document.positionAt(parentRule.offset),
document.positionAt(parentRule.end),
);
selectors.set(selector, {
selector,
range,
content: parentRule.getText(),
selectionRange,
});
};
function resolveSelectors(node: Node, parent: Node | null) {
switch (node.type) {
case NodeType.Ruleset: {
visitedRules.push(node);
break;
}
case NodeType.SimpleSelector: {
let selector = node.getText();
let isInvalid = false;
if (isSuffix(selector)) {
selector = resolveSuffixSelectors(parent, "");
} else if (isSibling(selector)) {
selector = selector.replace(/&./gi, "");
} else if (isChild(selector)) {
selector = selector.replace(/& ./gi, "");
} else if (isNormal(selector)) {
selector = selector.replace(".", "");
} else {
isInvalid = true;
}
// Psuedo attributes is figured out only later. Thus it has to be its own if block
if (isPsuedo(selector)) {
selector = selector.split(":")[0];
}
if (!isInvalid) {
insertSelector(node, selector);
}
break;
}
}
for (const child of node.getChildren()) {
resolveSelectors(child, node);
}
}
for (const child of ast.getChildren()) {
resolveSelectors(child, null);
}
return selectors;
};
function resolveSuffixSelectors(parent: Node | null, suffixes: string): string {
if (!parent) {
return suffixes;
}
if (parent.type !== NodeType.Ruleset) {
return resolveSuffixSelectors(parent.parent, suffixes);
}
const parentSelector = (parent as RuleSet).getSelectors().getText();
if (isSuffix(parentSelector)) {
suffixes = parentSelector.replace("&", "") + suffixes;
return resolveSuffixSelectors(parent.parent, suffixes);
}
const suffixSelector = parentSelector.replace(".", "") + suffixes;
return suffixSelector;
}
export const getVariables = (ast: Stylesheet, document: TextDocument) => {
const variables: CssParserResult["variables"] = [];
ast.accept((node) => {
if (node.type === NodeType.CustomPropertyDeclaration) {
const _node = node as CustomPropertyDeclaration;
variables.push({
name: _node.property?.getText(),
value: _node.value?.getText(),
kind: isColorString(_node.value?.getText() ?? "") ? "color" : "normal",
location: {
value_range: Range.create(
document.positionAt(_node.value?.offset ?? _node.offset),
document.positionAt(_node.value?.end ?? _node.end),
),
full_range: Range.create(
document.positionAt(_node.offset),
document.positionAt(_node.end),
),
property_range: Range.create(
document.positionAt(_node.property?.offset ?? _node.offset),
document.positionAt(_node.property?.end ?? _node.end),
),
uri: Uri.file(document.uri),
},
});
}
return true;
});
return variables;
};
export const createStyleSheet = (document: vscode_TextDocument): Stylesheet => {
const _document = TextDocument.create(
document.uri.path,
getLanguageId(document.uri.path),
1,
document.getText(),
);
const languageService = getLanguageService(document.uri.path);
const ast = languageService.parseStylesheet(_document);
return ast as Stylesheet;
};