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
|
diff --git a/src/lsp/util/find.ts b/src/lsp/util/find.ts
index 2fa38956102b75ea56c4e7330934c4c3f47d85e0..6b1bfca7fbce3737b86c7529b4e7141715d17f58 100644
--- a/src/lsp/util/find.ts
+++ b/src/lsp/util/find.ts
@@ -1,10 +1,10 @@
import { TextDocument, Range, Position } from 'vscode-languageserver'
import { DocumentClassName, DocumentClassList, State } from './state'
import lineColumn from 'line-column'
-import { isCssContext } from './css'
-import { isHtmlContext } from './html'
+import { isCssContext, isCssDoc } from './css'
+import { isHtmlContext, isHtmlDoc, isSvelteDoc, isVueDoc } from './html'
import { isWithinRange } from './isWithinRange'
-import { isJsContext } from './js'
+import { isJsContext, isJsDoc } from './js'
import { getClassAttributeLexer } from './lexers'
export function findAll(re: RegExp, str: string): RegExpMatchArray[] {
@@ -26,8 +26,8 @@ }
export function findClassNamesInRange(
doc: TextDocument,
- range: Range,
- mode: 'html' | 'css'
+ range?: Range,
+ mode?: 'html' | 'css'
): DocumentClassName[] {
const classLists = findClassListsInRange(doc, range, mode)
return [].concat.apply(
@@ -66,10 +66,11 @@ }
export function findClassListsInCssRange(
doc: TextDocument,
- range: Range
+ range?: Range
): DocumentClassList[] {
const text = doc.getText(range)
const matches = findAll(/(@apply\s+)(?<classList>[^;}]+)[;}]/g, text)
+ const globalStart: Position = range ? range.start : { line: 0, character: 0 }
return matches.map((match) => {
const start = indexToPosition(text, match.index + match[1].length)
@@ -81,12 +82,14 @@ return {
classList: match.groups.classList,
range: {
start: {
- line: range.start.line + start.line,
- character: range.start.character + start.character,
+ line: globalStart.line + start.line,
+ character:
+ (end.line === 0 ? globalStart.character : 0) + start.character,
},
end: {
- line: range.start.line + end.line,
- character: range.start.character + end.character,
+ line: globalStart.line + end.line,
+ character:
+ (end.line === 0 ? globalStart.character : 0) + end.character,
},
},
}
@@ -172,11 +175,14 @@ classList: value,
range: {
start: {
line: range.start.line + start.line,
- character: range.start.character + start.character,
+ character:
+ (end.line === 0 ? range.start.character : 0) +
+ start.character,
},
end: {
line: range.start.line + end.line,
- character: range.start.character + end.character,
+ character:
+ (end.line === 0 ? range.start.character : 0) + end.character,
},
},
}
@@ -197,6 +203,80 @@ if (mode === 'css') {
return findClassListsInCssRange(doc, range)
}
return findClassListsInHtmlRange(doc, range)
+}
+
+export function findClassListsInDocument(
+ state: State,
+ doc: TextDocument
+): DocumentClassList[] {
+ if (isCssDoc(state, doc)) {
+ return findClassListsInCssRange(doc)
+ }
+
+ if (isVueDoc(doc)) {
+ let text = doc.getText()
+ let blocks = findAll(
+ /<(?<type>template|style|script)\b[^>]*>.*?(<\/\k<type>>|$)/gis,
+ text
+ )
+ let htmlRanges: Range[] = []
+ let cssRanges: Range[] = []
+ for (let i = 0; i < blocks.length; i++) {
+ let range = {
+ start: indexToPosition(text, blocks[i].index),
+ end: indexToPosition(text, blocks[i].index + blocks[i][0].length),
+ }
+ if (blocks[i].groups.type === 'style') {
+ cssRanges.push(range)
+ } else {
+ htmlRanges.push(range)
+ }
+ }
+ return [].concat.apply(
+ [],
+ [
+ ...htmlRanges.map((range) => findClassListsInHtmlRange(doc, range)),
+ ...cssRanges.map((range) => findClassListsInCssRange(doc, range)),
+ ]
+ )
+ }
+
+ if (isHtmlDoc(state, doc) || isJsDoc(state, doc) || isSvelteDoc(doc)) {
+ let text = doc.getText()
+ let styleBlocks = findAll(/<style(?:\s[^>]*>|>).*?(<\/style>|$)/gis, text)
+ let htmlRanges: Range[] = []
+ let cssRanges: Range[] = []
+ let currentIndex = 0
+
+ for (let i = 0; i < styleBlocks.length; i++) {
+ htmlRanges.push({
+ start: indexToPosition(text, currentIndex),
+ end: indexToPosition(text, styleBlocks[i].index),
+ })
+ cssRanges.push({
+ start: indexToPosition(text, styleBlocks[i].index),
+ end: indexToPosition(
+ text,
+ styleBlocks[i].index + styleBlocks[i][0].length
+ ),
+ })
+ currentIndex = styleBlocks[i].index + styleBlocks[i][0].length
+ }
+ htmlRanges.push({
+ start: indexToPosition(text, currentIndex),
+ end: indexToPosition(text, text.length),
+ })
+
+ return [].concat.apply(
+ [],
+ [
+ ...htmlRanges.map((range) => findClassListsInHtmlRange(doc, range)),
+ ...cssRanges.map((range) => findClassListsInCssRange(doc, range)),
+ ]
+ )
+ }
+
+ return []
}
function indexToPosition(str: string, index: number): Position {
|