Skip to content

Commit 934cd9c

Browse files
committed
fix: determine scoped
1 parent 09a81fe commit 934cd9c

File tree

1 file changed

+23
-5
lines changed
  • packages/language-service/lib/plugins

1 file changed

+23
-5
lines changed

packages/language-service/lib/plugins/css.ts

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type { LanguageServicePlugin, LanguageServicePluginInstance } from '@volar/language-service';
22
import type { TextDocument } from 'vscode-languageserver-textdocument';
3-
import * as css from 'vscode-css-languageservice';
3+
import { VueVirtualCode } from '@vue/language-core';
44
import { create as baseCreate, type Provide } from 'volar-service-css';
5+
import * as css from 'vscode-css-languageservice';
6+
import { URI } from 'vscode-uri';
57

68
const cssClassNameReg = /(?=(\.[a-z_][-\w]*)[\s.,+~>:#[{])/gi;
79
const vBindCssVarReg = /\bv-bind\(\s*(?:'([^']+)'|"([^"]+)"|([a-z_]\w+))\s*\)/gi;
@@ -29,6 +31,25 @@ export function create(): LanguageServicePlugin {
2931
return diagnostics;
3032
},
3133
provideRenameRange(document, position) {
34+
35+
const decoded = context.decodeEmbeddedDocumentUri(URI.parse(document.uri));
36+
const sourceScript = decoded && context.language.scripts.get(decoded[0]);
37+
const virtualCode = decoded && sourceScript?.generated?.embeddedCodes.get(decoded[1]);
38+
39+
const regexps: RegExp[] = [];
40+
41+
if (virtualCode?.id.startsWith('style_')) {
42+
const i = Number(virtualCode.id.slice('style_'.length));
43+
if (sourceScript?.generated?.root instanceof VueVirtualCode) {
44+
const style = sourceScript.generated.root.sfc.styles[i];
45+
const option = sourceScript.generated.root.vueCompilerOptions.experimentalResolveStyleCssClasses;
46+
if (option === 'always' || (option === 'scoped' && style.scoped)) {
47+
regexps.push(cssClassNameReg);
48+
}
49+
}
50+
}
51+
regexps.push(vBindCssVarReg);
52+
3253
return worker(document, (stylesheet, cssLs) => {
3354
const text = document.getText();
3455
const offset = document.offsetAt(position);
@@ -41,10 +62,7 @@ export function create(): LanguageServicePlugin {
4162
return cssLs.prepareRename(document, position, stylesheet);
4263

4364
function* forEachRegExp() {
44-
for (const reg of [
45-
cssClassNameReg,
46-
vBindCssVarReg
47-
]) {
65+
for (const reg of regexps) {
4866
for (const match of text.matchAll(reg)) {
4967
const matchText = match.slice(1).find(t => t);
5068
if (matchText) {

0 commit comments

Comments
 (0)