Skip to content

Commit

Permalink
refactor: remove word patterns for css plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Nov 26, 2022
1 parent 9ad89d5 commit 05c84a0
Showing 1 changed file with 1 addition and 47 deletions.
48 changes: 1 addition & 47 deletions plugins/css/src/index.ts
Expand Up @@ -4,13 +4,6 @@ import * as vscode from 'vscode-languageserver-protocol';
import { TextDocument } from 'vscode-languageserver-textdocument';
import * as path from 'path';

const wordPatterns: { [lang: string]: RegExp; } = {
css: /(#?-?\d*\.\d\w*%?)|(::?[\w-]*(?=[^,{;]*[,{]))|(([@#.!])?[\w-?]+%?|[@#!.])/g,
less: /(#?-?\d*\.\d\w*%?)|(::?[\w-]+(?=[^,{;]*[,{]))|(([@#.!])?[\w-?]+%?|[@#!.])/g,
scss: /(#?-?\d*\.\d\w*%?)|(::?[\w-]*(?=[^,{;]*[,{]))|(([@$#.!])?[\w-?]+%?|[@#!$.])/g,
postcss: /(#?-?\d*\.\d\w*%?)|(::?[\w-]*(?=[^,{;]*[,{]))|(([@$#.!])?[\w-?]+%?|[@#!$.])/g, // scss
};

export default function (): LanguageServicePlugin {

const stylesheets = new WeakMap<TextDocument, [number, css.Stylesheet]>();
Expand Down Expand Up @@ -52,17 +45,9 @@ export default function (): LanguageServicePlugin {
if (!context.env.documentContext)
return;

const wordPattern = wordPatterns[document.languageId] ?? wordPatterns.css;
const wordStart = getWordRange(wordPattern, position, document)?.start; // TODO: use end?
const wordRange = vscode.Range.create(wordStart ?? position, position);
const settings = await context.env.configurationHost?.getConfiguration<css.LanguageSettings>(document.languageId, document.uri);
const cssResult = await cssLs.doComplete2(document, position, stylesheet, context.env.documentContext, settings?.completion);

if (cssResult) {
cssResult.itemDefaults ??= {};
cssResult.itemDefaults.editRange ??= wordRange;
}

return cssResult;
});
},
Expand All @@ -72,11 +57,7 @@ export default function (): LanguageServicePlugin {

prepare(document, position) {
return worker(document, (stylesheet, cssLs) => {

const wordPattern = wordPatterns[document.languageId] ?? wordPatterns.css;
const wordRange = getWordRange(wordPattern, position, document);

return wordRange;
return cssLs.prepareRename(document, position, stylesheet);
});
},

Expand Down Expand Up @@ -296,30 +277,3 @@ export default function (): LanguageServicePlugin {
return callback(stylesheet, cssLs);
}
};

// track https://github.com/microsoft/vscode-css-languageservice/pull/301
function getWordRange(wordPattern: RegExp, position: vscode.Position, document: TextDocument): vscode.Range | undefined {
const lineStart: vscode.Position = {
line: position.line,
character: 0,
};
const lineEnd: vscode.Position = {
line: position.line + 1,
character: 0,
};
const offset = document.offsetAt(position);
const lineStartOffset = document.offsetAt(lineStart);
const lineText = document.getText({ start: lineStart, end: lineEnd });
for (const match of lineText.matchAll(wordPattern)) {
if (match.index === undefined) continue;
const matchStart = match.index + lineStartOffset;
const matchEnd = matchStart + match[0].length;
if (offset >= matchStart && offset <= matchEnd) {
return {
start: document.positionAt(matchStart),
end: document.positionAt(matchEnd),
};
}
}
return undefined;
}

0 comments on commit 05c84a0

Please sign in to comment.