|
1 | 1 | import type { State } from '../util/state'
|
2 |
| -import { type Range } from 'vscode-languageserver' |
3 |
| -import type { TextDocument } from 'vscode-languageserver-textdocument' |
| 2 | +import type { TextDocument, Range } from 'vscode-languageserver-textdocument' |
4 | 3 | import { getLanguageBoundaries } from '../util/getLanguageBoundaries'
|
5 | 4 | import { isCssDoc } from '../util/css'
|
6 | 5 | import { getTextWithoutComments } from './doc'
|
| 6 | +import { isHtmlDoc } from './html' |
| 7 | +import { isJsDoc } from './js' |
7 | 8 |
|
8 | 9 | export interface LanguageBlock {
|
9 |
| - document: TextDocument |
| 10 | + context: 'html' | 'js' | 'css' | 'other' |
10 | 11 | range: Range | undefined
|
11 | 12 | lang: string
|
12 |
| - readonly text: string |
| 13 | + text: string |
13 | 14 | }
|
14 | 15 |
|
15 |
| -export function* getCssBlocks( |
16 |
| - state: State, |
17 |
| - document: TextDocument, |
18 |
| -): Iterable<LanguageBlock | undefined> { |
19 |
| - if (isCssDoc(state, document)) { |
20 |
| - yield { |
21 |
| - document, |
22 |
| - range: undefined, |
23 |
| - lang: document.languageId, |
24 |
| - get text() { |
25 |
| - return getTextWithoutComments(document, 'css') |
26 |
| - }, |
27 |
| - } |
28 |
| - } else { |
29 |
| - let boundaries = getLanguageBoundaries(state, document) |
30 |
| - if (!boundaries) return [] |
| 16 | +export function getDocumentBlocks(state: State, doc: TextDocument): LanguageBlock[] { |
| 17 | + let text = doc.getText() |
31 | 18 |
|
32 |
| - for (let boundary of boundaries) { |
33 |
| - if (boundary.type !== 'css') continue |
| 19 | + let boundaries = getLanguageBoundaries(state, doc, text) |
| 20 | + if (boundaries && boundaries.length > 0) { |
| 21 | + return boundaries.map((boundary) => { |
| 22 | + let context: 'html' | 'js' | 'css' | 'other' |
| 23 | + |
| 24 | + if (boundary.type === 'html') { |
| 25 | + context = 'html' |
| 26 | + } else if (boundary.type === 'css') { |
| 27 | + context = 'css' |
| 28 | + } else if (boundary.type === 'js' || boundary.type === 'jsx') { |
| 29 | + context = 'js' |
| 30 | + } else { |
| 31 | + context = 'other' |
| 32 | + } |
34 | 33 |
|
35 |
| - yield { |
36 |
| - document, |
| 34 | + let text = doc.getText(boundary.range) |
| 35 | + |
| 36 | + return { |
| 37 | + context, |
37 | 38 | range: boundary.range,
|
38 |
| - lang: boundary.lang ?? document.languageId, |
39 |
| - get text() { |
40 |
| - return getTextWithoutComments(document, 'css', boundary.range) |
41 |
| - }, |
| 39 | + lang: boundary.lang ?? doc.languageId, |
| 40 | + text: context === 'other' ? text : getTextWithoutComments(text, context), |
42 | 41 | }
|
43 |
| - } |
| 42 | + }) |
44 | 43 | }
|
| 44 | + |
| 45 | + // If we get here we most likely have non-HTML document in a single language |
| 46 | + let context: 'html' | 'js' | 'css' | 'other' |
| 47 | + |
| 48 | + if (isHtmlDoc(state, doc)) { |
| 49 | + context = 'html' |
| 50 | + } else if (isCssDoc(state, doc)) { |
| 51 | + context = 'css' |
| 52 | + } else if (isJsDoc(state, doc)) { |
| 53 | + context = 'js' |
| 54 | + } else { |
| 55 | + context = 'other' |
| 56 | + } |
| 57 | + |
| 58 | + return [ |
| 59 | + { |
| 60 | + context, |
| 61 | + range: { |
| 62 | + start: doc.positionAt(0), |
| 63 | + end: doc.positionAt(text.length), |
| 64 | + }, |
| 65 | + lang: doc.languageId, |
| 66 | + text: context === 'other' ? text : getTextWithoutComments(text, context), |
| 67 | + }, |
| 68 | + ] |
| 69 | +} |
| 70 | + |
| 71 | +export function getCssBlocks(state: State, document: TextDocument): LanguageBlock[] { |
| 72 | + return getDocumentBlocks(state, document).filter((block) => block.context === 'css') |
45 | 73 | }
|
0 commit comments