@@ -10,8 +10,8 @@ export interface VirtualDocumentProvider {
10
10
createVirtualDocument ( context : TemplateContext ) : vscode . TextDocument ;
11
11
toVirtualDocPosition ( position : ts . LineAndCharacter ) : ts . LineAndCharacter ;
12
12
fromVirtualDocPosition ( position : ts . LineAndCharacter ) : ts . LineAndCharacter ;
13
- toVirtualDocOffset ( offset : number ) : number ;
14
- fromVirtualDocOffset ( offset : number ) : number ;
13
+ toVirtualDocOffset ( offset : number , context : TemplateContext ) : number ;
14
+ fromVirtualDocOffset ( offset : number , context : TemplateContext ) : number ;
15
15
}
16
16
17
17
/**
@@ -21,24 +21,25 @@ export interface VirtualDocumentProvider {
21
21
* since styled allows properties to be top level elements.
22
22
*/
23
23
export class StyledVirtualDocumentFactory implements VirtualDocumentProvider {
24
- private static readonly wrapperPre = ':root{\n' ;
24
+ private static readonly wrapperPreRoot = ':root{\n' ;
25
+ private static readonly wrapperPreKeyframes = '@keyframes custom {\n' ;
25
26
26
27
public createVirtualDocument (
27
28
context : TemplateContext
28
29
) : vscode . TextDocument {
29
- const contents = `${ StyledVirtualDocumentFactory . wrapperPre } ${ context . text } \n }` ;
30
+ const contents = `${ this . getVirtualDocumentRoot ( context ) } ${ context . text } }` ;
30
31
return {
31
32
uri : 'untitled://embedded.scss' ,
32
33
languageId : 'scss' ,
33
34
version : 1 ,
34
35
getText : ( ) => contents ,
35
36
positionAt : ( offset : number ) => {
36
- const pos = context . toPosition ( this . fromVirtualDocOffset ( offset ) ) ;
37
+ const pos = context . toPosition ( this . fromVirtualDocOffset ( offset , context ) ) ;
37
38
return this . toVirtualDocPosition ( pos ) ;
38
39
} ,
39
40
offsetAt : ( p : vscode . Position ) => {
40
41
const offset = context . toOffset ( this . fromVirtualDocPosition ( p ) ) ;
41
- return this . toVirtualDocOffset ( offset ) ;
42
+ return this . toVirtualDocOffset ( offset , context ) ;
42
43
} ,
43
44
lineCount : contents . split ( / \n / g) . length + 1 ,
44
45
} ;
@@ -58,11 +59,16 @@ export class StyledVirtualDocumentFactory implements VirtualDocumentProvider {
58
59
} ;
59
60
}
60
61
61
- public toVirtualDocOffset ( offset : number ) : number {
62
- return offset + StyledVirtualDocumentFactory . wrapperPre . length ;
62
+ public toVirtualDocOffset ( offset : number , context : TemplateContext ) : number {
63
+ return offset + this . getVirtualDocumentRoot ( context ) . length ;
63
64
}
64
65
65
- public fromVirtualDocOffset ( offset : number ) : number {
66
- return offset - StyledVirtualDocumentFactory . wrapperPre . length ;
66
+ public fromVirtualDocOffset ( offset : number , context : TemplateContext ) : number {
67
+ return offset - this . getVirtualDocumentRoot ( context ) . length ;
67
68
}
68
- }
69
+
70
+ private getVirtualDocumentRoot ( context : TemplateContext ) : string {
71
+ const tag = ( context . node . parent as ts . Node & { tag : any } ) ?. tag ?. escapedText ;
72
+ return tag === 'keyframes' ? StyledVirtualDocumentFactory . wrapperPreKeyframes : StyledVirtualDocumentFactory . wrapperPreRoot ;
73
+ }
74
+ }
0 commit comments