1
1
import type { LanguageServicePlugin , LanguageServicePluginInstance } from '@volar/language-service' ;
2
2
import type { TextDocument } from 'vscode-languageserver-textdocument' ;
3
- import * as css from 'vscode-css-languageservice ' ;
3
+ import { VueVirtualCode } from '@vue/language-core ' ;
4
4
import { create as baseCreate , type Provide } from 'volar-service-css' ;
5
+ import * as css from 'vscode-css-languageservice' ;
6
+ import { URI } from 'vscode-uri' ;
5
7
6
8
const cssClassNameReg = / (? = ( \. [ a - z _ ] [ - \w ] * ) [ \s . , + ~ > : # [ { ] ) / gi;
7
9
const vBindCssVarReg = / \b v - b i n d \( \s * (?: ' ( [ ^ ' ] + ) ' | " ( [ ^ " ] + ) " | ( [ a - z _ ] \w + ) ) \s * \) / gi;
@@ -29,6 +31,25 @@ export function create(): LanguageServicePlugin {
29
31
return diagnostics ;
30
32
} ,
31
33
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
+
32
53
return worker ( document , ( stylesheet , cssLs ) => {
33
54
const text = document . getText ( ) ;
34
55
const offset = document . offsetAt ( position ) ;
@@ -41,10 +62,7 @@ export function create(): LanguageServicePlugin {
41
62
return cssLs . prepareRename ( document , position , stylesheet ) ;
42
63
43
64
function * forEachRegExp ( ) {
44
- for ( const reg of [
45
- cssClassNameReg ,
46
- vBindCssVarReg
47
- ] ) {
65
+ for ( const reg of regexps ) {
48
66
for ( const match of text . matchAll ( reg ) ) {
49
67
const matchText = match . slice ( 1 ) . find ( t => t ) ;
50
68
if ( matchText ) {
0 commit comments