Home

tailwind-ctp-intellisense @5f6b8e3f4e3e5b05772156d73abdab8613b69c21 - refs - log -
-
https://git.jolheiser.com/tailwind-ctp-intellisense.git
Tailwind intellisense + Catppuccin
tailwind-ctp-intellisense / src / lsp / util / find.ts
- raw
  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
import { TextDocument, Range, Position } from 'vscode-languageserver'
import { DocumentClassName, DocumentClassList, State } from './state'
import lineColumn from 'line-column'
import { isCssContext, isCssDoc } from './css'
import { isHtmlContext, isHtmlDoc, isSvelteDoc, isVueDoc } from './html'
import { isWithinRange } from './isWithinRange'
import { isJsContext, isJsDoc } from './js'
import { getClassAttributeLexer } from './lexers'

export function findAll(re: RegExp, str: string): RegExpMatchArray[] {
  let match: RegExpMatchArray
  let matches: RegExpMatchArray[] = []
  while ((match = re.exec(str)) !== null) {
    matches.push({ ...match })
  }
  return matches
}

export function findLast(re: RegExp, str: string): RegExpMatchArray {
  const matches = findAll(re, str)
  if (matches.length === 0) {
    return null
  }
  return matches[matches.length - 1]
}

export function findClassNamesInRange(
  doc: TextDocument,
  range?: Range,
  mode?: 'html' | 'css'
): DocumentClassName[] {
  const classLists = findClassListsInRange(doc, range, mode)
  return [].concat.apply(
    [],
    classLists.map(({ classList, range }) => {
      const parts = classList.split(/(\s+)/)
      const names: DocumentClassName[] = []
      let index = 0
      for (let i = 0; i < parts.length; i++) {
        if (i % 2 === 0) {
          const start = indexToPosition(classList, index)
          const end = indexToPosition(classList, index + parts[i].length)
          names.push({
            className: parts[i],
            range: {
              start: {
                line: range.start.line + start.line,
                character:
                  (end.line === 0 ? range.start.character : 0) +
                  start.character,
              },
              end: {
                line: range.start.line + end.line,
                character:
                  (end.line === 0 ? range.start.character : 0) + end.character,
              },
            },
          })
        }
        index += parts[i].length
      }
      return names
    })
  )
}

export function findClassListsInCssRange(
  doc: TextDocument,
  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)
    const end = indexToPosition(
      text,
      match.index + match[1].length + match.groups.classList.length
    )
    return {
      classList: match.groups.classList,
      range: {
        start: {
          line: globalStart.line + start.line,
          character:
            (end.line === 0 ? globalStart.character : 0) + start.character,
        },
        end: {
          line: globalStart.line + end.line,
          character:
            (end.line === 0 ? globalStart.character : 0) + end.character,
        },
      },
    }
  })
}

export function findClassListsInHtmlRange(
  doc: TextDocument,
  range: Range
): DocumentClassList[] {
  const text = doc.getText(range)
  const matches = findAll(/[\s:]class(?:Name)?=['"`{]/g, text)
  const result: DocumentClassList[] = []

  matches.forEach((match) => {
    const subtext = text.substr(match.index + match[0].length - 1, 200)

    let lexer = getClassAttributeLexer()
    lexer.reset(subtext)

    let classLists: { value: string; offset: number }[] = []
    let token: moo.Token
    let currentClassList: { value: string; offset: number }

    try {
      for (let token of lexer) {
        if (token.type === 'classlist') {
          if (currentClassList) {
            currentClassList.value += token.value
          } else {
            currentClassList = {
              value: token.value,
              offset: token.offset,
            }
          }
        } else {
          if (currentClassList) {
            classLists.push({
              value: currentClassList.value,
              offset: currentClassList.offset,
            })
          }
          currentClassList = undefined
        }
      }
    } catch (_) {}

    if (currentClassList) {
      classLists.push({
        value: currentClassList.value,
        offset: currentClassList.offset,
      })
    }

    result.push(
      ...classLists
        .map(({ value, offset }) => {
          if (value.trim() === '') {
            return null
          }

          const before = value.match(/^\s*/)
          const beforeOffset = before === null ? 0 : before[0].length
          const after = value.match(/\s*$/)
          const afterOffset = after === null ? 0 : -after[0].length

          const start = indexToPosition(
            text,
            match.index + match[0].length - 1 + offset + beforeOffset
          )
          const end = indexToPosition(
            text,
            match.index +
              match[0].length -
              1 +
              offset +
              value.length +
              afterOffset
          )

          return {
            classList: value,
            range: {
              start: {
                line: range.start.line + start.line,
                character:
                  (end.line === 0 ? range.start.character : 0) +
                  start.character,
              },
              end: {
                line: range.start.line + end.line,
                character:
                  (end.line === 0 ? range.start.character : 0) + end.character,
              },
            },
          }
        })
        .filter((x) => x !== null)
    )
  })

  return result
}

export function findClassListsInRange(
  doc: TextDocument,
  range: Range,
  mode: 'html' | 'css'
): DocumentClassList[] {
  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 {
  const { line, col } = lineColumn(str + '\n', index)
  return { line: line - 1, character: col - 1 }
}

export function findClassNameAtPosition(
  state: State,
  doc: TextDocument,
  position: Position
): DocumentClassName {
  let classNames = []
  const searchRange = {
    start: { line: Math.max(position.line - 10, 0), character: 0 },
    end: { line: position.line + 10, character: 0 },
  }

  if (isCssContext(state, doc, position)) {
    classNames = findClassNamesInRange(doc, searchRange, 'css')
  } else if (
    isHtmlContext(state, doc, position) ||
    isJsContext(state, doc, position)
  ) {
    classNames = findClassNamesInRange(doc, searchRange, 'html')
  }

  if (classNames.length === 0) {
    return null
  }

  const className = classNames.find(({ range }) =>
    isWithinRange(position, range)
  )

  if (!className) return null

  return className
}