Debian Codegraph @

codemirror-legacy-modes/mode/sass.js

raw
1 import {keywords as cssKeywords} from "./css.js"
2 const propertyKeywords = new Set(cssKeywords.properties)
3 const colorKeywords = new Set(cssKeywords.colors)
4 const valueKeywords = new Set(cssKeywords.values)
5 const fontProperties = new Set(cssKeywords.fonts)
6
7 function tokenRegexp(words) {
8 return new RegExp("^" + words.join("|"))
9 }
10
11 let keywords = ["true", "false", "null", "auto"]
12 let keywordsRegexp = new RegExp("^" + keywords.join("|"))
13
14 let operators = ["\\(", "\\)", "=", ">", "<", "==", ">=", "<=", "\\+", "-",
15 "\\!=", "/", "\\*", "%", "and", "or", "not", ";","\\{","\\}",":"]
16 let opRegexp = tokenRegexp(operators)
17
18 let pseudoElementsRegexp = /^::?[a-zA-Z_][\w\-]*/
19
20 let word
21
22 function isEndLine(stream) {
23 return !stream.peek() || stream.match(/\s+$/, false)
24 }
25
26 function urlTokens(stream, state) {
27 let ch = stream.peek()
28
29 if (ch === ")") {
30 stream.next()
31 state.tokenizer = tokenBase
32 return "operator"
33 } else if (ch === "(") {
34 stream.next()
35 stream.eatSpace()
36
37 return "operator"
38 } else if (ch === "'" || ch === '"') {
39 state.tokenizer = buildStringTokenizer(stream.next())
40 return "string"
41 } else {
42 state.tokenizer = buildStringTokenizer(")", false)
43 return "string"
44 }
45 }
46 function comment(indentation, multiLine) {
47 return function(stream, state) {
48 if (stream.sol() && stream.indentation() <= indentation) {
49 state.tokenizer = tokenBase
50 return tokenBase(stream, state)
51 }
52
53 if (multiLine && stream.skipTo("*/")) {
54 stream.next()
55 stream.next()
56 state.tokenizer = tokenBase
57 } else {
58 stream.skipToEnd()
59 }
60
61 return "comment"
62 }
63 }
64
65 function buildStringTokenizer(quote, greedy) {
66 if (greedy == null) { greedy = true }
67
68 function stringTokenizer(stream, state) {
69 let nextChar = stream.next()
70 let peekChar = stream.peek()
71 let previousChar = stream.string.charAt(stream.pos-2)
72
73 let endingString = ((nextChar !== "\\" && peekChar === quote) || (nextChar === quote && previousChar !== "\\"))
74
75 if (endingString) {
76 if (nextChar !== quote && greedy) { stream.next() }
77 if (isEndLine(stream)) {
78 state.cursorHalf = 0
79 }
80 state.tokenizer = tokenBase
81 return "string"
82 } else if (nextChar === "#" && peekChar === "{") {
83 state.tokenizer = buildInterpolationTokenizer(stringTokenizer)
84 stream.next()
85 return "operator"
86 } else {
87 return "string"
88 }
89 }
90
91 return stringTokenizer
92 }
93
94 function buildInterpolationTokenizer(currentTokenizer) {
95 return function(stream, state) {
96 if (stream.peek() === "}") {
97 stream.next()
98 state.tokenizer = currentTokenizer
99 return "operator"
100 } else {
101 return tokenBase(stream, state)
102 }
103 }
104 }
105
106 function indent(state, stream) {
107 if (state.indentCount == 0) {
108 state.indentCount++
109 let lastScopeOffset = state.scopes[0].offset
110 let currentOffset = lastScopeOffset + stream.indentUnit
111 state.scopes.unshift({ offset:currentOffset })
112 }
113 }
114
115 function dedent(state) {
116 if (state.scopes.length == 1) return
117
118 state.scopes.shift()
119 }
120
121 function tokenBase(stream, state) {
122 let ch = stream.peek()
123
124 // Comment
125 if (stream.match("/*")) {
126 state.tokenizer = comment(stream.indentation(), true)
127 return state.tokenizer(stream, state)
128 }
129 if (stream.match("//")) {
130 state.tokenizer = comment(stream.indentation(), false)
131 return state.tokenizer(stream, state)
132 }
133
134 // Interpolation
135 if (stream.match("#{")) {
136 state.tokenizer = buildInterpolationTokenizer(tokenBase)
137 return "operator"
138 }
139
140 // Strings
141 if (ch === '"' || ch === "'") {
142 stream.next()
143 state.tokenizer = buildStringTokenizer(ch)
144 return "string"
145 }
146
147 if (!state.cursorHalf) {
148 // first half i.e. before : for key-value pairs
149 // including selectors
150 if (ch === "-") {
151 if (stream.match(/^-\w+-/)) {
152 return "meta"
153 }
154 }
155
156 if (ch === ".") {
157 stream.next()
158 if (stream.match(/^[\w-]+/)) {
159 indent(state, stream)
160 return "qualifier"
161 } else if (stream.peek() === "#") {
162 indent(state, stream)
163 return "tag"
164 }
165 }
166
167 if (ch === "#") {
168 stream.next()
169 // ID selectors
170 if (stream.match(/^[\w-]+/)) {
171 indent(state, stream)
172 return "builtin"
173 }
174 if (stream.peek() === "#") {
175 indent(state, stream)
176 return "tag"
177 }
178 }
179
180 // Variables
181 if (ch === "$") {
182 stream.next()
183 stream.eatWhile(/[\w-]/)
184 return "variable-2"
185 }
186
187 // Numbers
188 if (stream.match(/^-?[0-9\.]+/))
189 return "number"
190
191 // Units
192 if (stream.match(/^(px|em|in)\b/))
193 return "unit"
194
195 if (stream.match(keywordsRegexp))
196 return "keyword"
197
198 if (stream.match(/^url/) && stream.peek() === "(") {
199 state.tokenizer = urlTokens
200 return "atom"
201 }
202
203 if (ch === "=") {
204 // Match shortcut mixin definition
205 if (stream.match(/^=[\w-]+/)) {
206 indent(state, stream)
207 return "meta"
208 }
209 }
210
211 if (ch === "+") {
212 // Match shortcut mixin definition
213 if (stream.match(/^\+[\w-]+/)) {
214 return "meta"
215 }
216 }
217
218 if (ch === "@") {
219 if (stream.match('@extend')) {
220 if (!stream.match(/\s*[\w]/))
221 dedent(state)
222 }
223 }
224
225
226 // Indent Directives
227 if (stream.match(/^@(else if|if|media|else|for|each|while|mixin|function)/)) {
228 indent(state, stream)
229 return "def"
230 }
231
232 // Other Directives
233 if (ch === "@") {
234 stream.next()
235 stream.eatWhile(/[\w-]/)
236 return "def"
237 }
238
239 if (stream.eatWhile(/[\w-]/)) {
240 if (stream.match(/ *: *[\w-\+\$#!\("']/,false)) {
241 word = stream.current().toLowerCase()
242 let prop = state.prevProp + "-" + word
243 if (propertyKeywords.has(prop)) {
244 return "property"
245 } else if (propertyKeywords.has(word)) {
246 state.prevProp = word
247 return "property"
248 } else if (fontProperties.has(word)) {
249 return "property"
250 }
251 return "tag"
252 } else if (stream.match(/ *:/,false)) {
253 indent(state, stream)
254 state.cursorHalf = 1
255 state.prevProp = stream.current().toLowerCase()
256 return "property"
257 } else if (stream.match(/ *,/,false)) {
258 return "tag"
259 } else {
260 indent(state, stream)
261 return "tag"
262 }
263 }
264
265 if (ch === ":") {
266 if (stream.match(pseudoElementsRegexp)) { // could be a pseudo-element
267 return "type"
268 }
269 stream.next()
270 state.cursorHalf=1
271 return "operator"
272 }
273 } else {
274 if (ch === "#") {
275 stream.next()
276 // Hex numbers
277 if (stream.match(/[0-9a-fA-F]{6}|[0-9a-fA-F]{3}/)) {
278 if (isEndLine(stream)) {
279 state.cursorHalf = 0
280 }
281 return "number"
282 }
283 }
284
285 // Numbers
286 if (stream.match(/^-?[0-9\.]+/)) {
287 if (isEndLine(stream)) {
288 state.cursorHalf = 0
289 }
290 return "number"
291 }
292
293 // Units
294 if (stream.match(/^(px|em|in)\b/)) {
295 if (isEndLine(stream)) {
296 state.cursorHalf = 0
297 }
298 return "unit"
299 }
300
301 if (stream.match(keywordsRegexp)) {
302 if (isEndLine(stream)) {
303 state.cursorHalf = 0
304 }
305 return "keyword"
306 }
307
308 if (stream.match(/^url/) && stream.peek() === "(") {
309 state.tokenizer = urlTokens
310 if (isEndLine(stream)) {
311 state.cursorHalf = 0
312 }
313 return "atom"
314 }
315
316 // Variables
317 if (ch === "$") {
318 stream.next()
319 stream.eatWhile(/[\w-]/)
320 if (isEndLine(stream)) {
321 state.cursorHalf = 0
322 }
323 return "variable-2"
324 }
325
326 // bang character for !important, !default, etc.
327 if (ch === "!") {
328 stream.next()
329 state.cursorHalf = 0
330 return stream.match(/^[\w]+/) ? "keyword": "operator"
331 }
332
333 if (stream.match(opRegexp)) {
334 if (isEndLine(stream)) {
335 state.cursorHalf = 0
336 }
337 return "operator"
338 }
339
340 // attributes
341 if (stream.eatWhile(/[\w-]/)) {
342 if (isEndLine(stream)) {
343 state.cursorHalf = 0
344 }
345 word = stream.current().toLowerCase()
346 if (valueKeywords.has(word)) {
347 return "atom"
348 } else if (colorKeywords.has(word)) {
349 return "keyword"
350 } else if (propertyKeywords.has(word)) {
351 state.prevProp = stream.current().toLowerCase()
352 return "property"
353 } else {
354 return "tag"
355 }
356 }
357
358 if (isEndLine(stream)) {
359 state.cursorHalf = 0
360 return null
361 }
362 }
363
364 if (stream.match(opRegexp))
365 return "operator"
366
367 stream.next()
368 return null
369 }
370
371 function tokenLexer(stream, state) {
372 if (stream.sol()) state.indentCount = 0
373 let style = state.tokenizer(stream, state)
374 let current = stream.current()
375
376 if (current === "@return" || current === "}") {
377 dedent(state)
378 }
379
380 if (style !== null) {
381 let startOfToken = stream.pos - current.length
382
383 let withCurrentIndent = startOfToken + (stream.indentUnit * state.indentCount)
384
385 let newScopes = []
386
387 for (let i = 0; i < state.scopes.length; i++) {
388 let scope = state.scopes[i]
389
390 if (scope.offset <= withCurrentIndent)
391 newScopes.push(scope)
392 }
393
394 state.scopes = newScopes
395 }
396
397 return style
398 }
399
400 export const sass = {
401 name: "sass",
402 startState: function() {
403 return {
404 tokenizer: tokenBase,
405 scopes: [{offset: 0, type: "sass"}],
406 indentCount: 0,
407 cursorHalf: 0, // cursor half tells us if cursor lies after (1)
408 // or before (0) colon (well... more or less)
409 definedVars: [],
410 definedMixins: []
411 }
412 },
413 token: function(stream, state) {
414 let style = tokenLexer(stream, state)
415 state.lastToken = { style: style, content: stream.current() }
416 return style
417 },
418
419 indent: function(state) {
420 return state.scopes[0].offset
421 },
422
423 languageData: {
424 commentTokens: {line: "//", block: {open: "/*", close: "*/"}},
425 autocomplete: cssKeywords.all
426 }
427 }