Home

tailwind-ctp-intellisense @master - refs - log -
-
https://git.jolheiser.com/tailwind-ctp-intellisense.git
Tailwind intellisense + Catppuccin
tree log patch
add class list finder helper and fix ranges
Brad Cornes <brad@parall.ax>
4 years ago
2 changed files, 96 additions(+), 11 deletions(-)
M src/lsp/util/css.ts -> src/lsp/util/css.ts
diff --git a/src/lsp/util/css.ts b/src/lsp/util/css.ts
index 848d6bbc0e2a8b2db21a05aadd9dea355139fd45..e6dbd097d4b97c42c4f7f650fc1e1186d33c0047 100644
--- a/src/lsp/util/css.ts
+++ b/src/lsp/util/css.ts
@@ -12,7 +12,7 @@   'stylus',
   'sugarss',
 ]
 
-function isCssDoc(state: State, doc: TextDocument): boolean {
+export function isCssDoc(state: State, doc: TextDocument): boolean {
   const userCssLanguages = Object.keys(
     state.editor.userLanguages
   ).filter((lang) => CSS_LANGUAGES.includes(state.editor.userLanguages[lang]))
M src/lsp/util/find.ts -> src/lsp/util/find.ts
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,12 @@
 import { TextDocument, Range, Position } from 'vscode-languageserver'
 import { DocumentClassName, DocumentClassList, State } from './state'
 import lineColumn from 'line-column'
-import { isCssContext } from './css'
+import { isCssContext, isCssDoc } from './css'
-import { isHtmlContext } from './html'
+import { isHtmlContext, isHtmlDoc, isSvelteDoc, isVueDoc } from './html'
 import { isWithinRange } from './isWithinRange'
+import { TextDocument, Range, Position } from 'vscode-languageserver'
 import { isJsContext } from './js'
+import { DocumentClassName, DocumentClassList, State } from './state'
 import { getClassAttributeLexer } from './lexers'
 
 export function findAll(re: RegExp, str: string): RegExpMatchArray[] {
@@ -26,8 +28,8 @@ }
 
 export function findClassNamesInRange(
   doc: TextDocument,
-  range: Range,
+  range?: Range,
-  mode: 'html' | 'css'
+  mode?: 'html' | 'css'
 ): DocumentClassName[] {
   const classLists = findClassListsInRange(doc, range, mode)
   return [].concat.apply(
@@ -66,10 +68,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,14 +84,19 @@     return {
       classList: match.groups.classList,
       range: {
         start: {
+import { TextDocument, Range, Position } from 'vscode-languageserver'
 import { isJsContext } from './js'
-import { isCssContext } from './css'
+import { isJsContext } from './js'
+          character:
+import { TextDocument, Range, Position } from 'vscode-languageserver'
 import { isJsContext } from './js'
-import { isHtmlContext } from './html'
+
         },
         end: {
+          line: globalStart.line + end.line,
+import { TextDocument, Range, Position } from 'vscode-languageserver'
           line: range.start.line + end.line,
-          character: range.start.character + end.character,
+            (end.line === 0 ? globalStart.character : 0) + end.character,
         },
       },
     }
@@ -174,11 +182,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,
               },
             },
           }
@@ -199,6 +210,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 {