Home

tailwind-ctp-intellisense @master - refs - log -
-
https://git.jolheiser.com/tailwind-ctp-intellisense.git
Tailwind intellisense + Catppuccin
tree log patch
enable diagnostics in multi-language documents
Brad Cornes <bradlc41@gmail.com>
5 years ago
4 changed files, 252 additions(+), 157 deletions(-)
src/lsp/providers/diagnosticsProvider.tssrc/lsp/util/absoluteRange.tssrc/lsp/util/find.tssrc/lsp/util/getLanguageBoundaries.ts
M src/lsp/providers/diagnosticsProvider.tssrc/lsp/providers/diagnosticsProvider.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
diff --git a/src/lsp/providers/diagnosticsProvider.ts b/src/lsp/providers/diagnosticsProvider.ts
index 866c07d56dabc00b1759e3d154f43154c872931c..bb59ff571ca903793d4eb34a45d4775e9e9beaca 100644
--- a/src/lsp/providers/diagnosticsProvider.ts
+++ b/src/lsp/providers/diagnosticsProvider.ts
@@ -2,6 +2,7 @@ import {
   TextDocument,
   Diagnostic,
   DiagnosticSeverity,
+  Range,
 } from 'vscode-languageserver'
 import { State, Settings } from '../util/state'
 import { isCssDoc } from '../util/css'
@@ -18,6 +19,8 @@ import { equal, flatten } from '../../util/array'
 import { getDocumentSettings } from '../util/getDocumentSettings'
 const dlv = require('dlv')
 import semver from 'semver'
+import { getLanguageBoundaries } from '../util/getLanguageBoundaries'
+import { absoluteRange } from '../util/absoluteRange'
 
 function getUnsupportedApplyDiagnostics(
   state: State,
@@ -140,35 +143,51 @@ ): Diagnostic[] {
   let severity = settings.lint.unknownScreen
   if (severity === 'ignore') return []
 
-  let text = document.getText()
-  let matches = findAll(/(?:\s|^)@screen\s+(?<screen>[^\s{]+)/g, text)
+  let diagnostics: Diagnostic[] = []
+  let ranges: Range[] = []
 
-  let screens = Object.keys(
-    dlv(state.config, 'theme.screens', dlv(state.config, 'screens', {}))
-  )
+  if (isCssDoc(state, document)) {
+    ranges.push(undefined)
+  } else {
+    let boundaries = getLanguageBoundaries(state, document)
+    if (!boundaries) return []
+    ranges.push(...boundaries.css)
+  }
 
-  return matches
-    .map((match) => {
+  ranges.forEach((range) => {
+    let text = document.getText(range)
+    let matches = findAll(/(?:\s|^)@screen\s+(?<screen>[^\s{]+)/g, text)
+
+    let screens = Object.keys(
+      dlv(state.config, 'theme.screens', dlv(state.config, 'screens', {}))
+    )
+
+    matches.forEach((match) => {
       if (screens.includes(match.groups.screen)) {
         return null
       }
 
-      return {
-        range: {
-          start: indexToPosition(
-            text,
-            match.index + match[0].length - match.groups.screen.length
-          ),
-          end: indexToPosition(text, match.index + match[0].length),
-        },
+      diagnostics.push({
+        range: absoluteRange(
+          {
+            start: indexToPosition(
+              text,
+              match.index + match[0].length - match.groups.screen.length
+            ),
+            end: indexToPosition(text, match.index + match[0].length),
+          },
+          range
+        ),
         severity:
           severity === 'error'
             ? DiagnosticSeverity.Error
             : DiagnosticSeverity.Warning,
         message: 'Unknown screen',
-      }
+      })
     })
-    .filter(Boolean)
+  })
+
+  return diagnostics
 }
 
 function getUnknownVariantDiagnostics(
@@ -179,43 +198,54 @@ ): Diagnostic[] {
   let severity = settings.lint.unknownVariant
   if (severity === 'ignore') return []
 
-  let text = document.getText()
-  let matches = findAll(/(?:\s|^)@variants\s+(?<variants>[^{]+)/g, text)
+  let diagnostics: Diagnostic[] = []
+  let ranges: Range[] = []
+
+  if (isCssDoc(state, document)) {
+    ranges.push(undefined)
+  } else {
+    let boundaries = getLanguageBoundaries(state, document)
+    if (!boundaries) return []
+    ranges.push(...boundaries.css)
+  }
 
-  return flatten(
-    matches
-      .map((match) => {
-        let diagnostics: Diagnostic[] = []
-        let variants = match.groups.variants.split(/(\s*,\s*)/)
-        let listStartIndex =
-          match.index + match[0].length - match.groups.variants.length
+  ranges.forEach((range) => {
+    let text = document.getText(range)
+    let matches = findAll(/(?:\s|^)@variants\s+(?<variants>[^{]+)/g, text)
+
+    matches.forEach((match) => {
+      let variants = match.groups.variants.split(/(\s*,\s*)/)
+      let listStartIndex =
+        match.index + match[0].length - match.groups.variants.length
 
-        for (let i = 0; i < variants.length; i += 2) {
-          let variant = variants[i].trim()
-          if (state.variants.includes(variant)) {
-            continue
-          }
+      for (let i = 0; i < variants.length; i += 2) {
+        let variant = variants[i].trim()
+        if (state.variants.includes(variant)) {
+          continue
+        }
 
-          let variantStartIndex =
-            listStartIndex + variants.slice(0, i).join('').length
+        let variantStartIndex =
+          listStartIndex + variants.slice(0, i).join('').length
 
-          diagnostics.push({
-            range: {
+        diagnostics.push({
+          range: absoluteRange(
+            {
               start: indexToPosition(text, variantStartIndex),
               end: indexToPosition(text, variantStartIndex + variant.length),
             },
-            severity:
-              severity === 'error'
-                ? DiagnosticSeverity.Error
-                : DiagnosticSeverity.Warning,
-            message: `Unknown variant: ${variant}`,
-          })
-        }
+            range
+          ),
+          severity:
+            severity === 'error'
+              ? DiagnosticSeverity.Error
+              : DiagnosticSeverity.Warning,
+          message: `Unknown variant: ${variant}`,
+        })
+      }
+    })
+  })
 
-        return diagnostics
-      })
-      .filter(Boolean)
-  )
+  return diagnostics
 }
 
 function getUnknownConfigKeyDiagnostics(
@@ -226,14 +256,25 @@ ): Diagnostic[] {
   let severity = settings.lint.unknownConfigKey
   if (severity === 'ignore') return []
 
-  let text = document.getText()
-  let matches = findAll(
-    /(?<prefix>\s|^)(?<helper>config|theme)\((?<quote>['"])(?<key>[^)]+)\k<quote>\)/g,
-    text
-  )
+  let diagnostics: Diagnostic[] = []
+  let ranges: Range[] = []
+
+  if (isCssDoc(state, document)) {
+    ranges.push(undefined)
+  } else {
+    let boundaries = getLanguageBoundaries(state, document)
+    if (!boundaries) return []
+    ranges.push(...boundaries.css)
+  }
+
+  ranges.forEach((range) => {
+    let text = document.getText(range)
+    let matches = findAll(
+      /(?<prefix>\s|^)(?<helper>config|theme)\((?<quote>['"])(?<key>[^)]+)\k<quote>\)/g,
+      text
+    )
 
-  return matches
-    .map((match) => {
+    matches.forEach((match) => {
       let base = match.groups.helper === 'theme' ? ['theme'] : []
       let keys = match.groups.key.split(/[.\[\]]/).filter(Boolean)
       let value = dlv(state.config, [...base, ...keys])
@@ -251,19 +292,24 @@         match.groups.helper.length +
         1 + // open paren
         match.groups.quote.length
 
-      return {
-        range: {
-          start: indexToPosition(text, startIndex),
-          end: indexToPosition(text, startIndex + match.groups.key.length),
-        },
+      diagnostics.push({
+        range: absoluteRange(
+          {
+            start: indexToPosition(text, startIndex),
+            end: indexToPosition(text, startIndex + match.groups.key.length),
+          },
+          range
+        ),
         severity:
           severity === 'error'
             ? DiagnosticSeverity.Error
             : DiagnosticSeverity.Warning,
         message: `Unknown ${match.groups.helper} key: ${match.groups.key}`,
-      }
+      })
     })
-    .filter(Boolean)
+  })
+
+  return diagnostics
 }
 
 function getUnsupportedTailwindDirectiveDiagnostics(
@@ -274,30 +320,44 @@ ): Diagnostic[] {
   let severity = settings.lint.unsupportedTailwindDirective
   if (severity === 'ignore') return []
 
-  let text = document.getText()
-  let matches = findAll(/(?:\s|^)@tailwind\s+(?<value>[^;]+)/g, text)
+  let diagnostics: Diagnostic[] = []
+  let ranges: Range[] = []
+
+  if (isCssDoc(state, document)) {
+    ranges.push(undefined)
+  } else {
+    let boundaries = getLanguageBoundaries(state, document)
+    if (!boundaries) return []
+    ranges.push(...boundaries.css)
+  }
+
+  ranges.forEach((range) => {
+    let text = document.getText(range)
+    let matches = findAll(/(?:\s|^)@tailwind\s+(?<value>[^;]+)/g, text)
 
-  let allowed = [
-    'utilities',
-    'components',
-    'screens',
-    semver.gte(state.version, '1.0.0-beta.1') ? 'base' : 'preflight',
-  ]
+    let valid = [
+      'utilities',
+      'components',
+      'screens',
+      semver.gte(state.version, '1.0.0-beta.1') ? 'base' : 'preflight',
+    ]
 
-  return matches
-    .map((match) => {
-      if (allowed.includes(match.groups.value)) {
+    matches.forEach((match) => {
+      if (valid.includes(match.groups.value)) {
         return null
       }
 
-      return {
-        range: {
-          start: indexToPosition(
-            text,
-            match.index + match[0].length - match.groups.value.length
-          ),
-          end: indexToPosition(text, match.index + match[0].length),
-        },
+      diagnostics.push({
+        range: absoluteRange(
+          {
+            start: indexToPosition(
+              text,
+              match.index + match[0].length - match.groups.value.length
+            ),
+            end: indexToPosition(text, match.index + match[0].length),
+          },
+          range
+        ),
         severity:
           severity === 'error'
             ? DiagnosticSeverity.Error
@@ -305,9 +365,11 @@             : DiagnosticSeverity.Warning,
         message: `Unsupported value: ${match.groups.value}${
           match.groups.value === 'preflight' ? '. Use base instead.' : ''
         }`,
-      }
+      })
     })
-    .filter(Boolean)
+  })
+
+  return diagnostics
 }
 
 export async function provideDiagnostics(
@@ -319,19 +381,15 @@
   const diagnostics: Diagnostic[] = settings.validate
     ? [
         ...getUtilityConflictDiagnostics(state, document, settings),
-        ...(isCssDoc(state, document)
-          ? [
-              ...getUnsupportedApplyDiagnostics(state, document, settings),
-              ...getUnknownScreenDiagnostics(state, document, settings),
-              ...getUnknownVariantDiagnostics(state, document, settings),
-              ...getUnknownConfigKeyDiagnostics(state, document, settings),
-              ...getUnsupportedTailwindDirectiveDiagnostics(
-                state,
-                document,
-                settings
-              ),
-            ]
-          : []),
+        ...getUnsupportedApplyDiagnostics(state, document, settings),
+        ...getUnknownScreenDiagnostics(state, document, settings),
+        ...getUnknownVariantDiagnostics(state, document, settings),
+        ...getUnknownConfigKeyDiagnostics(state, document, settings),
+        ...getUnsupportedTailwindDirectiveDiagnostics(
+          state,
+          document,
+          settings
+        ),
       ]
     : []
 
I src/lsp/util/absoluteRange.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
diff --git a/src/lsp/util/absoluteRange.ts b/src/lsp/util/absoluteRange.ts
new file mode 100644
index 0000000000000000000000000000000000000000..9250e4fb9ec9b13ff3124072808f2013ef5009a0
--- /dev/null
+++ b/src/lsp/util/absoluteRange.ts
@@ -0,0 +1,18 @@
+import { Range } from 'vscode-languageserver'
+
+export function absoluteRange(range: Range, reference?: Range) {
+  return {
+    start: {
+      line: (reference?.start.line || 0) + range.start.line,
+      character:
+        (range.end.line === 0 ? reference?.start.character || 0 : 0) +
+        range.start.character,
+    },
+    end: {
+      line: (reference?.start.line || 0) + range.end.line,
+      character:
+        (range.end.line === 0 ? reference?.start.character || 0 : 0) +
+        range.end.character,
+    },
+  }
+}
M src/lsp/util/find.tssrc/lsp/util/find.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
diff --git a/src/lsp/util/find.ts b/src/lsp/util/find.ts
index 0f996a43f914e393b546ae08a4410908687126b3..609de6207d066862bb645e4d9ca199c915010b61 100644
--- a/src/lsp/util/find.ts
+++ b/src/lsp/util/find.ts
@@ -10,6 +10,7 @@ import {
   getClassAttributeLexer,
   getComputedClassAttributeLexer,
 } from './lexers'
+import { getLanguageBoundaries } from './getLanguageBoundaries'
 
 export function findAll(re: RegExp, str: string): RegExpMatchArray[] {
   let match: RegExpMatchArray
@@ -230,70 +231,13 @@   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)),
-      ]
-    )
-  }
+  let boundaries = getLanguageBoundaries(state, doc)
+  if (!boundaries) return []
 
-  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 []
+  return flatten([
+    ...boundaries.html.map((range) => findClassListsInHtmlRange(doc, range)),
+    ...boundaries.css.map((range) => findClassListsInCssRange(doc, range)),
+  ])
 }
 
 export function indexToPosition(str: string, index: number): Position {
I src/lsp/util/getLanguageBoundaries.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
diff --git a/src/lsp/util/getLanguageBoundaries.ts b/src/lsp/util/getLanguageBoundaries.ts
new file mode 100644
index 0000000000000000000000000000000000000000..dfef2300d3a9052cb3f60970538858b81d8af39b
--- /dev/null
+++ b/src/lsp/util/getLanguageBoundaries.ts
@@ -0,0 +1,75 @@
+import { TextDocument, Range } from 'vscode-languageserver'
+import { isVueDoc, isHtmlDoc, isSvelteDoc } from './html'
+import { State } from './state'
+import { findAll, indexToPosition } from './find'
+import { isJsDoc } from './js'
+
+export interface LanguageBoundaries {
+  html: Range[]
+  css: Range[]
+}
+
+export function getLanguageBoundaries(
+  state: State,
+  doc: TextDocument
+): LanguageBoundaries | null {
+  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 {
+      html: htmlRanges,
+      css: cssRanges,
+    }
+  }
+
+  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 {
+      html: htmlRanges,
+      css: cssRanges,
+    }
+  }
+
+  return null
+}