Skip to content

Commit 08a200d

Browse files
committed
Refactor
1 parent 8ec45eb commit 08a200d

File tree

1 file changed

+57
-29
lines changed

1 file changed

+57
-29
lines changed
Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,73 @@
11
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'
43
import { getLanguageBoundaries } from '../util/getLanguageBoundaries'
54
import { isCssDoc } from '../util/css'
65
import { getTextWithoutComments } from './doc'
6+
import { isHtmlDoc } from './html'
7+
import { isJsDoc } from './js'
78

89
export interface LanguageBlock {
9-
document: TextDocument
10+
context: 'html' | 'js' | 'css' | 'other'
1011
range: Range | undefined
1112
lang: string
12-
readonly text: string
13+
text: string
1314
}
1415

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()
3118

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+
}
3433

35-
yield {
36-
document,
34+
let text = doc.getText(boundary.range)
35+
36+
return {
37+
context,
3738
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),
4241
}
43-
}
42+
})
4443
}
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')
4573
}

0 commit comments

Comments
 (0)