@@ -17,7 +17,7 @@ export function create(): LanguageServicePlugin {
17
17
const decoded = context . decodeEmbeddedDocumentUri ( uri ) ;
18
18
const sourceScript = decoded && context . language . scripts . get ( decoded [ 0 ] ) ;
19
19
const virtualCode = decoded && sourceScript ?. generated ?. embeddedCodes . get ( decoded [ 1 ] ) ;
20
- if ( ! sourceScript ?. generated || virtualCode ?. id !== 'template' ) {
20
+ if ( ! sourceScript ?. generated || ( virtualCode ?. id !== 'template' && virtualCode ?. id !== "scriptsetup_raw" ) ) {
21
21
return ;
22
22
}
23
23
@@ -26,52 +26,90 @@ export function create(): LanguageServicePlugin {
26
26
return ;
27
27
}
28
28
29
- const result : vscode . DocumentLink [ ] = [ ] ;
30
-
31
29
const { sfc } = root ;
32
30
const codegen = tsCodegen . get ( sfc ) ;
33
- const scopedClasses = codegen ?. getGeneratedTemplate ( ) ?. scopedClasses ?? [ ] ;
34
- const styleClasses = new Map < string , {
35
- index : number ;
36
- style : Sfc [ 'styles' ] [ number ] ;
37
- classOffset : number ;
38
- } [ ] > ( ) ;
39
- const option = root . vueCompilerOptions . experimentalResolveStyleCssClasses ;
31
+ const result : vscode . DocumentLink [ ] = [ ] ;
40
32
41
- for ( let i = 0 ; i < sfc . styles . length ; i ++ ) {
42
- const style = sfc . styles [ i ] ;
43
- if ( option === 'always' || ( option === 'scoped' && style . scoped ) ) {
44
- for ( const className of style . classNames ) {
45
- if ( ! styleClasses . has ( className . text . slice ( 1 ) ) ) {
46
- styleClasses . set ( className . text . slice ( 1 ) , [ ] ) ;
33
+ if ( virtualCode . id === 'template' ) {
34
+ const scopedClasses = codegen ?. getGeneratedTemplate ( ) ?. scopedClasses ?? [ ] ;
35
+ const styleClasses = new Map < string , {
36
+ index : number ;
37
+ style : Sfc [ 'styles' ] [ number ] ;
38
+ classOffset : number ;
39
+ } [ ] > ( ) ;
40
+ const option = root . vueCompilerOptions . experimentalResolveStyleCssClasses ;
41
+
42
+ for ( let i = 0 ; i < sfc . styles . length ; i ++ ) {
43
+ const style = sfc . styles [ i ] ;
44
+ if ( option === 'always' || ( option === 'scoped' && style . scoped ) ) {
45
+ for ( const className of style . classNames ) {
46
+ if ( ! styleClasses . has ( className . text . slice ( 1 ) ) ) {
47
+ styleClasses . set ( className . text . slice ( 1 ) , [ ] ) ;
48
+ }
49
+ styleClasses . get ( className . text . slice ( 1 ) ) ! . push ( {
50
+ index : i ,
51
+ style,
52
+ classOffset : className . offset ,
53
+ } ) ;
47
54
}
48
- styleClasses . get ( className . text . slice ( 1 ) ) ! . push ( {
49
- index : i ,
50
- style,
51
- classOffset : className . offset ,
52
- } ) ;
53
55
}
54
56
}
55
- }
56
57
57
- for ( const { className, offset } of scopedClasses ) {
58
- const styles = styleClasses . get ( className ) ;
59
- if ( styles ) {
60
- for ( const style of styles ) {
61
- const styleDocumentUri = context . encodeEmbeddedDocumentUri ( decoded ! [ 0 ] , 'style_' + style . index ) ;
62
- const styleVirtualCode = sourceScript . generated . embeddedCodes . get ( 'style_' + style . index ) ;
63
- if ( ! styleVirtualCode ) {
64
- continue ;
58
+ for ( const { className, offset } of scopedClasses ) {
59
+ const styles = styleClasses . get ( className ) ;
60
+ if ( styles ) {
61
+ for ( const style of styles ) {
62
+ const styleDocumentUri = context . encodeEmbeddedDocumentUri ( decoded ! [ 0 ] , 'style_' + style . index ) ;
63
+ const styleVirtualCode = sourceScript . generated . embeddedCodes . get ( 'style_' + style . index ) ;
64
+ if ( ! styleVirtualCode ) {
65
+ continue ;
66
+ }
67
+ const styleDocument = context . documents . get ( styleDocumentUri , styleVirtualCode . languageId , styleVirtualCode . snapshot ) ;
68
+ const start = styleDocument . positionAt ( style . classOffset ) ;
69
+ const end = styleDocument . positionAt ( style . classOffset + className . length + 1 ) ;
70
+ result . push ( {
71
+ range : {
72
+ start : document . positionAt ( offset ) ,
73
+ end : document . positionAt ( offset + className . length ) ,
74
+ } ,
75
+ target : context . encodeEmbeddedDocumentUri ( decoded ! [ 0 ] , 'style_' + style . index ) + `#L${ start . line + 1 } ,${ start . character + 1 } -L${ end . line + 1 } ,${ end . character + 1 } ` ,
76
+ } ) ;
65
77
}
66
- const styleDocument = context . documents . get ( styleDocumentUri , styleVirtualCode . languageId , styleVirtualCode . snapshot ) ;
67
- const start = styleDocument . positionAt ( style . classOffset ) ;
68
- const end = styleDocument . positionAt ( style . classOffset + className . length + 1 ) ;
78
+ }
79
+ }
80
+ }
81
+ else if ( virtualCode . id === 'scriptsetup_raw' ) {
82
+ if ( ! sfc . scriptSetup ) {
83
+ return ;
84
+ }
85
+
86
+ const templateVirtualCode = sourceScript . generated . embeddedCodes . get ( 'template' ) ;
87
+ if ( ! templateVirtualCode ) {
88
+ return ;
89
+ }
90
+ const templateDocumentUri = context . encodeEmbeddedDocumentUri ( decoded ! [ 0 ] , 'template' ) ;
91
+ const templateDocument = context . documents . get ( templateDocumentUri , templateVirtualCode . languageId , templateVirtualCode . snapshot ) ;
92
+
93
+ const templateRefs = codegen ?. getGeneratedTemplate ( ) ?. templateRefs ;
94
+ const useTemplateRefs = codegen ?. getScriptSetupRanges ( ) ?. useTemplateRef ?? [ ] ;
95
+
96
+ for ( const { arg } of useTemplateRefs ) {
97
+ if ( ! arg ) {
98
+ continue ;
99
+ }
100
+
101
+ const name = sfc . scriptSetup . content . slice ( arg . start + 1 , arg . end - 1 ) ;
102
+
103
+ for ( const { offset } of templateRefs ?. get ( name ) ?? [ ] ) {
104
+ const start = templateDocument . positionAt ( offset ) ;
105
+ const end = templateDocument . positionAt ( offset + name . length ) ;
106
+
69
107
result . push ( {
70
108
range : {
71
- start : document . positionAt ( offset ) ,
72
- end : document . positionAt ( offset + className . length ) ,
109
+ start : document . positionAt ( arg . start + 1 ) ,
110
+ end : document . positionAt ( arg . end - 1 ) ,
73
111
} ,
74
- target : context . encodeEmbeddedDocumentUri ( decoded ! [ 0 ] , 'style_' + style . index ) + `#L${ start . line + 1 } ,${ start . character + 1 } -L${ end . line + 1 } ,${ end . character + 1 } ` ,
112
+ target : templateDocumentUri + `#L${ start . line + 1 } ,${ start . character + 1 } -L${ end . line + 1 } ,${ end . character + 1 } ` ,
75
113
} ) ;
76
114
}
77
115
}
0 commit comments