Skip to content

Commit 6e50c3c

Browse files
committed
Add intellisense support for keyframes
- Dynamically set the wrapp to be keyframes when the keyframes tag is used - Pass context to offset functions so they can calculate the correct wrapper length
1 parent 560fb78 commit 6e50c3c

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/_virtual-document-provider.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export interface VirtualDocumentProvider {
1010
createVirtualDocument(context: TemplateContext): vscode.TextDocument;
1111
toVirtualDocPosition(position: ts.LineAndCharacter): ts.LineAndCharacter;
1212
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;
1515
}
1616

1717
/**
@@ -21,24 +21,25 @@ export interface VirtualDocumentProvider {
2121
* since styled allows properties to be top level elements.
2222
*/
2323
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';
2526

2627
public createVirtualDocument(
2728
context: TemplateContext
2829
): vscode.TextDocument {
29-
const contents = `${StyledVirtualDocumentFactory.wrapperPre}${context.text}\n}`;
30+
const contents = `${this.getVirtualDocumentRoot(context)}${context.text}}`;
3031
return {
3132
uri: 'untitled://embedded.scss',
3233
languageId: 'scss',
3334
version: 1,
3435
getText: () => contents,
3536
positionAt: (offset: number) => {
36-
const pos = context.toPosition(this.fromVirtualDocOffset(offset));
37+
const pos = context.toPosition(this.fromVirtualDocOffset(offset, context));
3738
return this.toVirtualDocPosition(pos);
3839
},
3940
offsetAt: (p: vscode.Position) => {
4041
const offset = context.toOffset(this.fromVirtualDocPosition(p));
41-
return this.toVirtualDocOffset(offset);
42+
return this.toVirtualDocOffset(offset, context);
4243
},
4344
lineCount: contents.split(/\n/g).length + 1,
4445
};
@@ -58,11 +59,16 @@ export class StyledVirtualDocumentFactory implements VirtualDocumentProvider {
5859
};
5960
}
6061

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;
6364
}
6465

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;
6768
}
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

Comments
 (0)