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
|
diff --git a/src/lsp/util/find.ts b/src/lsp/util/find.ts
index db5609e3f06571bde42d9a555268d95f6318fc47..8ff4dedcaaa9d3ab1e20e53208b641fde0bf8a89 100644
--- a/src/lsp/util/find.ts
+++ b/src/lsp/util/find.ts
@@ -1,5 +1,10 @@
import { TextDocument, Range, Position } from 'vscode-languageserver'
-import { DocumentClassName, DocumentClassList, State } from './state'
+import {
+ DocumentClassName,
+ DocumentClassList,
+ State,
+ DocumentHelperFunction,
+} from './state'
import lineColumn from 'line-column'
import { isCssContext, isCssDoc } from './css'
import { isHtmlContext, isHtmlDoc, isSvelteDoc, isVueDoc } from './html'
@@ -11,6 +16,7 @@ getClassAttributeLexer,
getComputedClassAttributeLexer,
} from './lexers'
import { getLanguageBoundaries } from './getLanguageBoundaries'
+import { resolveRange } from './resolveRange'
export function findAll(re: RegExp, str: string): RegExpMatchArray[] {
let match: RegExpMatchArray
@@ -252,6 +258,64 @@ return flatten([
...boundaries.html.map((range) => findClassListsInHtmlRange(doc, range)),
...boundaries.css.map((range) => findClassListsInCssRange(doc, range)),
])
+}
+
+export function findHelperFunctionsInDocument(
+ state: State,
+ doc: TextDocument
+): DocumentHelperFunction[] {
+ if (isCssDoc(state, doc)) {
+ return findHelperFunctionsInRange(doc)
+ }
+
+ let boundaries = getLanguageBoundaries(state, doc)
+ if (!boundaries) return []
+
+ return flatten(
+ boundaries.css.map((range) => findHelperFunctionsInRange(doc, range))
+ )
+}
+
+export function findHelperFunctionsInRange(
+ doc: TextDocument,
+ range?: Range
+): DocumentHelperFunction[] {
+ const text = doc.getText(range)
+ const matches = findAll(
+ /(?<before>^|\s)(?<helper>theme|config)\((?:(?<single>')([^']+)'|(?<double>")([^"]+)")\)/gm,
+ text
+ )
+
+ return matches.map((match) => {
+ let value = match[4] || match[6]
+ let startIndex = match.index + match.groups.before.length
+ return {
+ full: match[0].substr(match.groups.before.length),
+ value,
+ helper: match.groups.helper === 'theme' ? 'theme' : 'config',
+ quotes: match.groups.single ? "'" : '"',
+ range: resolveRange(
+ {
+ start: indexToPosition(text, startIndex),
+ end: indexToPosition(text, match.index + match[0].length),
+ },
+ range
+ ),
+ valueRange: resolveRange(
+ {
+ start: indexToPosition(
+ text,
+ startIndex + match.groups.helper.length + 1
+ ),
+ end: indexToPosition(
+ text,
+ startIndex + match.groups.helper.length + 1 + 1 + value.length + 1
+ ),
+ },
+ range
+ ),
+ }
+ })
}
export function indexToPosition(str: string, index: number): Position {
|