Home

tailwind-ctp-intellisense @master - refs - log -
-
https://git.jolheiser.com/tailwind-ctp-intellisense.git
Tailwind intellisense + Catppuccin
tree log patch
Improve JS comment handling (#727)
Signature
-----BEGIN PGP SIGNATURE----- wsBcBAABCAAQBQJkCz5zCRBK7hj4Ov3rIwAAdMcIAB6a7mH4MKHIOYI+/V7wdHmg e9ZLjRlMQGNwU44n/S852a4CG6gluiZ/K8gl44lONiDbN6556U1osmHC2yRGTXy2 gT8Gx22dcD/aKx26jAsyp+nTEfPg5OHCcHZWusbeVl0dNQtAieAyXj75RC2OMM51 dLEISnvBbGqJLVPqFLO5FpwIcyqu3uzJS6zkBaILrYq81H3wicCpGY3x66j0GeFJ 0t4QPocVmsgxbK2kTlyPuolzxbQmd67h0S1Tdimh3xupXFXhOstgQEbdv0LzQxix h0yLq1BpVoX9AprETWqvbw3bzUA1DQes98VCAyZs+XhcNM7AP/2A7qek9c12l4Y= =A7D7 -----END PGP SIGNATURE-----
Brad Cornes <hello@bradley.dev>
2 years ago
1 changed files, 35 additions(+), 1 deletions(-)
M packages/tailwindcss-language-service/src/util/doc.ts -> packages/tailwindcss-language-service/src/util/doc.ts
diff --git a/packages/tailwindcss-language-service/src/util/doc.ts b/packages/tailwindcss-language-service/src/util/doc.ts
index 810f79dd537907593beff2046a23834e2d54e042..7c291ff933bdaf3eed03de42a1c2ef8d6c85c214 100644
--- a/packages/tailwindcss-language-service/src/util/doc.ts
+++ b/packages/tailwindcss-language-service/src/util/doc.ts
@@ -1,3 +1,5 @@
+import type { TextDocument, Range } from 'vscode-languageserver'
+
 import type { TextDocument, Range } from 'vscode-languageserver'
 
 export function getTextWithoutComments(
@@ -14,7 +16,7 @@ ): string {
   let text = typeof docOrText === 'string' ? docOrText : docOrText.getText(range)
 
   if (type === 'js' || type === 'jsx') {
-    return text.replace(/\/\*.*?\*\//gs, replace).replace(/\/\/.*?$/gms, replace)
+    return getJsWithoutComments(text)
   }
 
   if (type === 'css') {
@@ -27,3 +29,35 @@
 function replace(match: string): string {
   return match.replace(/./gs, (char) => (char === '\n' ? '\n' : ' '))
 }
+
+let jsLexer: moo.Lexer
+
+function getJsWithoutComments(text: string): string {
+  if (!jsLexer) {
+    jsLexer = moo.states({
+      main: {
+        commentLine: /\/\/.*?$/,
+        commentBlock: { match: /\/\*[^]*?\*\//, lineBreaks: true },
+        stringDouble: /"(?:[^"\\]|\\.)*"/,
+        stringSingle: /'(?:[^'\\]|\\.)*'/,
+        stringBacktick: /`(?:[^`\\]|\\.)*`/,
+        other: { match: /[^]/, lineBreaks: true },
+      },
+    })
+  }
+
+  let str = ''
+  jsLexer.reset(text)
+
+  for (let token of jsLexer) {
+    if (token.type === 'commentLine') {
+      str += ' '.repeat(token.value.length)
+    } else if (token.type === 'commentBlock') {
+      str += token.value.replace(/./g, ' ')
+    } else {
+      str += token.value
+    }
+  }
+
+  return str
+}