|
| 1 | +import type { Position, TextDocument } from 'vscode-languageserver-textdocument' |
| 2 | +import type { |
| 3 | + DocumentClassList, |
| 4 | + DocumentClassName, |
| 5 | + DocumentHelperFunction, |
| 6 | + Settings, |
| 7 | + State, |
| 8 | +} from '../util/state' |
| 9 | +import type { ServiceOptions } from '../service' |
| 10 | +import { isWithinRange } from '../util/isWithinRange' |
| 11 | +import { getDocumentBlocks, type LanguageBlock } from '../util/language-blocks' |
| 12 | +import { |
| 13 | + findClassListsInCssRange, |
| 14 | + findClassListsInHtmlRange, |
| 15 | + findCustomClassLists, |
| 16 | + findHelperFunctionsInDocument, |
| 17 | + findHelperFunctionsInRange, |
| 18 | + getClassNamesInClassList, |
| 19 | +} from '../util/find' |
| 20 | +import { dedupeBySpan } from '../util/array' |
| 21 | + |
| 22 | +export interface Document { |
| 23 | + readonly state: State |
| 24 | + readonly version: number |
| 25 | + readonly uri: string |
| 26 | + readonly settings: Settings |
| 27 | + readonly storage: TextDocument |
| 28 | + |
| 29 | + /** |
| 30 | + * Find the language block that contains the cursor |
| 31 | + */ |
| 32 | + blockAt(cursor: Position): LanguageBlock | null |
| 33 | + |
| 34 | + /** |
| 35 | + * Find all class lists in the document |
| 36 | + */ |
| 37 | + classLists(): Iterable<DocumentClassList> |
| 38 | + |
| 39 | + /** |
| 40 | + * Find all class lists at a given cursor position |
| 41 | + */ |
| 42 | + classListsAt(cursor: Position): Iterable<DocumentClassList> |
| 43 | + |
| 44 | + /** |
| 45 | + * Find all class names in the document |
| 46 | + */ |
| 47 | + classNames(): Iterable<DocumentClassName> |
| 48 | + |
| 49 | + /** |
| 50 | + * Find all class names at a given cursor position |
| 51 | + * |
| 52 | + * Theoretically, this function should only ever contain one entry |
| 53 | + * but the presence of custom regexes may produce multiple entries |
| 54 | + */ |
| 55 | + classNamesAt(cursor: Position): Iterable<DocumentClassName> |
| 56 | + |
| 57 | + /** |
| 58 | + * Find all helper functions in the document |
| 59 | + * |
| 60 | + * This only applies to CSS contexts. Other document types will produce |
| 61 | + * zero entries. |
| 62 | + */ |
| 63 | + helperFns(): Iterable<DocumentHelperFunction> |
| 64 | + |
| 65 | + /** |
| 66 | + * Find all helper functions at a given cursor position |
| 67 | + */ |
| 68 | + helperFnsAt(cursor: Position): Iterable<DocumentHelperFunction> |
| 69 | +} |
| 70 | + |
| 71 | +export async function createVirtualDocument( |
| 72 | + opts: ServiceOptions, |
| 73 | + storage: TextDocument, |
| 74 | +): Promise<Document> { |
| 75 | + /** |
| 76 | + * The state of the server at the time of creation |
| 77 | + */ |
| 78 | + let state = opts.state() |
| 79 | + |
| 80 | + /** |
| 81 | + * The current settings for this document |
| 82 | + */ |
| 83 | + let settings = await state.editor.getConfiguration(storage.uri) |
| 84 | + |
| 85 | + /** |
| 86 | + * Conceptual boundaries of the document where different languages are used |
| 87 | + * |
| 88 | + * This is used to determine how the document is structured and what parts |
| 89 | + * are relevant to the current operation. |
| 90 | + */ |
| 91 | + let blocks = getDocumentBlocks(state, storage) |
| 92 | + |
| 93 | + /** |
| 94 | + * All class lists in the document |
| 95 | + */ |
| 96 | + let classLists: DocumentClassList[] = [] |
| 97 | + |
| 98 | + for (let block of blocks) { |
| 99 | + if (block.context === 'css') { |
| 100 | + classLists.push(...findClassListsInCssRange(state, storage, block.range, block.lang)) |
| 101 | + } else if (block.context === 'html') { |
| 102 | + classLists.push(...(await findClassListsInHtmlRange(state, storage, 'html', block.range))) |
| 103 | + } else if (block.context === 'js') { |
| 104 | + classLists.push(...(await findClassListsInHtmlRange(state, storage, 'jsx', block.range))) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + classLists.push(...(await findCustomClassLists(state, storage))) |
| 109 | + |
| 110 | + classLists.sort((a, b) => a.span[0] - b.span[0] || b.span[1] - a.span[1]) |
| 111 | + classLists = dedupeBySpan(classLists) |
| 112 | + |
| 113 | + /** |
| 114 | + * All class names in the document |
| 115 | + */ |
| 116 | + let classNames: DocumentClassName[] = [] |
| 117 | + |
| 118 | + for (let classList of classLists) { |
| 119 | + classNames.push(...getClassNamesInClassList(classList, state.blocklist ?? [])) |
| 120 | + } |
| 121 | + |
| 122 | + classNames.sort((a, b) => a.span[0] - b.span[0] || b.span[1] - a.span[1]) |
| 123 | + classNames = dedupeBySpan(classNames) |
| 124 | + |
| 125 | + /** |
| 126 | + * Helper functions in CSS |
| 127 | + */ |
| 128 | + let helperFns: DocumentHelperFunction[] = [] |
| 129 | + |
| 130 | + for (let block of blocks) { |
| 131 | + if (block.context === 'css') { |
| 132 | + helperFns.push(...findHelperFunctionsInRange(storage, block.range)) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + function blockAt(cursor: Position): LanguageBlock | null { |
| 137 | + for (let block of blocks) { |
| 138 | + if (isWithinRange(cursor, block.range)) { |
| 139 | + return block |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return null |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Find all class lists at a given cursor position |
| 148 | + */ |
| 149 | + function classListsAt(cursor: Position): DocumentClassList[] { |
| 150 | + return classLists.filter((classList) => isWithinRange(cursor, classList.range)) |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Find all class names at a given cursor position |
| 155 | + */ |
| 156 | + function classNamesAt(cursor: Position): DocumentClassName[] { |
| 157 | + return classNames.filter((className) => isWithinRange(cursor, className.range)) |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Find all class names at a given cursor position |
| 162 | + */ |
| 163 | + function helperFnsAt(cursor: Position): DocumentHelperFunction[] { |
| 164 | + return helperFns.filter((fn) => isWithinRange(cursor, fn.ranges.full)) |
| 165 | + } |
| 166 | + |
| 167 | + return { |
| 168 | + settings, |
| 169 | + storage, |
| 170 | + uri: storage.uri, |
| 171 | + |
| 172 | + get version() { |
| 173 | + return storage.version |
| 174 | + }, |
| 175 | + |
| 176 | + get state() { |
| 177 | + return opts.state() |
| 178 | + }, |
| 179 | + |
| 180 | + blockAt, |
| 181 | + |
| 182 | + classLists: () => classLists.slice(), |
| 183 | + classListsAt, |
| 184 | + classNames: () => classNames.slice(), |
| 185 | + classNamesAt, |
| 186 | + |
| 187 | + helperFns: () => helperFns.slice(), |
| 188 | + helperFnsAt, |
| 189 | + } |
| 190 | +} |
0 commit comments