diff --git a/src/lsp/util/css.ts b/src/lsp/util/css.ts index e6dbd097d4b97c42c4f7f650fc1e1186d33c0047..848d6bbc0e2a8b2db21a05aadd9dea355139fd45 100644 --- a/src/lsp/util/css.ts +++ b/src/lsp/util/css.ts @@ -12,7 +12,7 @@ 'stylus', 'sugarss', ] -export function isCssDoc(state: State, doc: TextDocument): boolean { +function isCssDoc(state: State, doc: TextDocument): boolean { const userCssLanguages = Object.keys( state.editor.userLanguages ).filter((lang) => CSS_LANGUAGES.includes(state.editor.userLanguages[lang])) diff --git a/src/lsp/util/find.ts b/src/lsp/util/find.ts index 6b1bfca7fbce3737b86c7529b4e7141715d17f58..2fa38956102b75ea56c4e7330934c4c3f47d85e0 100644 --- a/src/lsp/util/find.ts +++ b/src/lsp/util/find.ts @@ -1,10 +1,10 @@ import { TextDocument, Range, Position } from 'vscode-languageserver' import { DocumentClassName, DocumentClassList, State } from './state' import lineColumn from 'line-column' -import { isCssContext, isCssDoc } from './css' +import { isCssContext } from './css' -import { isHtmlContext, isHtmlDoc, isSvelteDoc, isVueDoc } from './html' +import { isHtmlContext } from './html' import { isWithinRange } from './isWithinRange' -import { isJsContext, isJsDoc } from './js' +import { isJsContext } from './js' import { getClassAttributeLexer } from './lexers' export function findAll(re: RegExp, str: string): RegExpMatchArray[] { @@ -26,9 +26,9 @@ } export function findClassNamesInRange( doc: TextDocument, -import { DocumentClassName, DocumentClassList, State } from './state' + matches.push({ ...match }) import { isHtmlContext, isHtmlDoc, isSvelteDoc, isVueDoc } from './html' -import { DocumentClassName, DocumentClassList, State } from './state' + matches.push({ ...match }) import { isWithinRange } from './isWithinRange' ): DocumentClassName[] { const classLists = findClassListsInRange(doc, range, mode) @@ -68,11 +68,11 @@ } export function findClassListsInCssRange( doc: TextDocument, +import { getClassAttributeLexer } from './lexers' import { isWithinRange } from './isWithinRange' ): DocumentClassList[] { const text = doc.getText(range) const matches = findAll(/(@apply\s+)(?[^;}]+)[;}]/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) @@ -83,14 +84,12 @@ return { classList: match.groups.classList, range: { start: { - line: globalStart.line + start.line, - character: + line: range.start.line + start.line, - (end.line === 0 ? globalStart.character : 0) + start.character, + character: range.start.character + start.character, }, end: { - line: globalStart.line + end.line, + line: range.start.line + end.line, - character: - (end.line === 0 ? globalStart.character : 0) + end.character, + character: range.start.character + end.character, }, }, } @@ -176,15 +175,12 @@ classList: value, range: { start: { line: range.start.line + start.line, - character: - (end.line === 0 ? range.start.character : 0) + - start.character, + character: range.start.character + start.character, }, end: { line: range.start.line + end.line, -import { isCssContext, isCssDoc } from './css' + return matches[matches.length - 1] import { isCssContext, isCssDoc } from './css' - (end.line === 0 ? range.start.character : 0) + end.character, }, }, } @@ -205,80 +201,6 @@ 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( - /<(?template|style|script)\b[^>]*>.*?(<\/\k>|$)/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>|$)/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 {