Home

tailwind-ctp-intellisense @master - refs - log -
-
https://git.jolheiser.com/tailwind-ctp-intellisense.git
Tailwind intellisense + Catppuccin
tailwind-ctp-intellisense / packages / tailwindcss-language-service / src / util / getLanguageBoundaries.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
import type { Range } from 'vscode-languageserver'
import type { TextDocument } from 'vscode-languageserver-textdocument'
import { isVueDoc, isHtmlDoc, isSvelteDoc } from './html'
import { State } from './state'
import { indexToPosition } from './find'
import { isJsDoc } from './js'
import moo from 'moo'
import Cache from 'tmp-cache'
import { getTextWithoutComments } from './doc'

export type LanguageBoundary = { type: 'html' | 'js' | 'css' | (string & {}); range: Range }

let htmlScriptTypes = [
  // https://v3-migration.vuejs.org/breaking-changes/inline-template-attribute.html#option-1-use-script-tag
  'text/html',
  // https://vuejs.org/guide/essentials/component-basics.html#dom-template-parsing-caveats
  'text/x-template',
  // https://github.com/tailwindlabs/tailwindcss-intellisense/issues/722
  'text/x-handlebars-template',
]

let text = { text: { match: /[^]/, lineBreaks: true } }

let states = {
  main: {
    cssBlockStart: { match: /<style(?=[>\s])/, push: 'cssBlock' },
    jsBlockStart: { match: '<script', push: 'jsBlock' },
    ...text,
  },
  cssBlock: {
    styleStart: { match: '>', next: 'style' },
    cssBlockEnd: { match: '/>', pop: 1 },
    attrStartDouble: { match: '"', push: 'attrDouble' },
    attrStartSingle: { match: "'", push: 'attrSingle' },
    interp: { match: '{', push: 'interp' },
    ...text,
  },
  jsBlock: {
    scriptStart: { match: '>', next: 'script' },
    jsBlockEnd: { match: '/>', pop: 1 },
    langAttrStartDouble: { match: 'lang="', push: 'langAttrDouble' },
    langAttrStartSingle: { match: "lang='", push: 'langAttrSingle' },
    typeAttrStartDouble: { match: 'type="', push: 'typeAttrDouble' },
    typeAttrStartSingle: { match: "type='", push: 'typeAttrSingle' },
    attrStartDouble: { match: '"', push: 'attrDouble' },
    attrStartSingle: { match: "'", push: 'attrSingle' },
    interp: { match: '{', push: 'interp' },
    ...text,
  },
  interp: {
    interp: { match: '{', push: 'interp' },
    end: { match: '}', pop: 1 },
    ...text,
  },
  langAttrDouble: {
    langAttrEnd: { match: '"', pop: 1 },
    lang: { match: /[^"]+/, lineBreaks: true },
  },
  langAttrSingle: {
    langAttrEnd: { match: "'", pop: 1 },
    lang: { match: /[^']+/, lineBreaks: true },
  },
  typeAttrDouble: {
    langAttrEnd: { match: '"', pop: 1 },
    type: { match: /[^"]+/, lineBreaks: true },
  },
  typeAttrSingle: {
    langAttrEnd: { match: "'", pop: 1 },
    type: { match: /[^']+/, lineBreaks: true },
  },
  attrDouble: {
    attrEnd: { match: '"', pop: 1 },
    ...text,
  },
  attrSingle: {
    attrEnd: { match: "'", pop: 1 },
    ...text,
  },
  style: {
    cssBlockEnd: { match: /<\/style\s*>/, pop: 1 },
    ...text,
  },
  script: {
    jsBlockEnd: { match: /<\/script\s*>/, pop: 1 },
    ...text,
  },
}

let vueStates = {
  ...states,
  main: {
    htmlBlockStart: { match: '<template', push: 'htmlBlock' },
    ...states.main,
  },
  htmlBlock: {
    htmlStart: { match: '>', next: 'html' },
    htmlBlockEnd: { match: '/>', pop: 1 },
    attrStartDouble: { match: '"', push: 'attrDouble' },
    attrStartSingle: { match: "'", push: 'attrSingle' },
    interp: { match: '{', push: 'interp' },
    ...text,
  },
  html: {
    htmlBlockEnd: { match: '</template>', pop: 1 },
    nestedBlockStart: { match: '<template', push: 'nestedBlock' },
    ...text,
  },
  nestedBlock: {
    nestedStart: { match: '>', next: 'nested' },
    nestedBlockEnd: { match: '/>', pop: 1 },
    ...text,
  },
  nested: {
    nestedBlockEnd: { match: '</template>', pop: 1 },
    nestedBlockStart: { match: '<template', push: 'nestedBlock' },
    ...text,
  },
}

let defaultLexer = moo.states(states)
let vueLexer = moo.states(vueStates)

let cache = new Cache<string, LanguageBoundary[] | null>({ max: 25, maxAge: 1000 })

export function getLanguageBoundaries(
  state: State,
  doc: TextDocument,
  text: string = doc.getText()
): LanguageBoundary[] | null {
  let cacheKey = `${doc.languageId}:${text}`

  let cachedBoundaries = cache.get(cacheKey)
  if (cachedBoundaries !== undefined) {
    return cachedBoundaries
  }

  let isJs = isJsDoc(state, doc)

  let defaultType = isVueDoc(doc)
    ? 'none'
    : isHtmlDoc(state, doc) || isSvelteDoc(doc)
    ? 'html'
    : isJs
    ? 'jsx'
    : null

  if (defaultType === null) {
    cache.set(cacheKey, null)
    return null
  }

  text = getTextWithoutComments(text, isJs ? 'js' : 'html')

  let lexer = defaultType === 'none' ? vueLexer : defaultLexer
  lexer.reset(text)

  let type = defaultType
  let boundaries: LanguageBoundary[] = [
    { type: defaultType, range: { start: { line: 0, character: 0 }, end: undefined } },
  ]
  let offset = 0

  try {
    for (let token of lexer) {
      if (!token.type.startsWith('nested')) {
        if (token.type.endsWith('BlockStart')) {
          let position = indexToPosition(text, offset)
          if (!boundaries[boundaries.length - 1].range.end) {
            boundaries[boundaries.length - 1].range.end = position
          }
          type = token.type.replace(/BlockStart$/, '')
          boundaries.push({ type, range: { start: position, end: undefined } })
        } else if (token.type.endsWith('BlockEnd')) {
          let position = indexToPosition(text, offset)
          boundaries[boundaries.length - 1].range.end = position
          boundaries.push({ type: defaultType, range: { start: position, end: undefined } })
        } else if (token.type === 'lang') {
          boundaries[boundaries.length - 1].type = token.text
        } else if (token.type === 'type' && htmlScriptTypes.includes(token.text)) {
          boundaries[boundaries.length - 1].type = 'html'
        }
      }
      offset += token.text.length
    }
  } catch {
    cache.set(cacheKey, null)
    return null
  }

  if (!boundaries[boundaries.length - 1].range.end) {
    boundaries[boundaries.length - 1].range.end = indexToPosition(text, offset)
  }

  cache.set(cacheKey, boundaries)

  return boundaries
}