summaryrefslogtreecommitdiff
path: root/tools/astro-languageserver/src/plugins/css/features/getIdClassCompletion.ts
diff options
context:
space:
mode:
Diffstat (limited to 'tools/astro-languageserver/src/plugins/css/features/getIdClassCompletion.ts')
-rw-r--r--tools/astro-languageserver/src/plugins/css/features/getIdClassCompletion.ts81
1 files changed, 39 insertions, 42 deletions
diff --git a/tools/astro-languageserver/src/plugins/css/features/getIdClassCompletion.ts b/tools/astro-languageserver/src/plugins/css/features/getIdClassCompletion.ts
index 368359ac9..45acb5ad6 100644
--- a/tools/astro-languageserver/src/plugins/css/features/getIdClassCompletion.ts
+++ b/tools/astro-languageserver/src/plugins/css/features/getIdClassCompletion.ts
@@ -2,32 +2,29 @@ import { CompletionItem, CompletionItemKind, CompletionList } from 'vscode-langu
import { AttributeContext } from '../../../core/documents/parseHtml';
import { CSSDocument } from '../CSSDocument';
-export function getIdClassCompletion(
- cssDoc: CSSDocument,
- attributeContext: AttributeContext
-): CompletionList | null {
- const collectingType = getCollectingType(attributeContext);
+export function getIdClassCompletion(cssDoc: CSSDocument, attributeContext: AttributeContext): CompletionList | null {
+ const collectingType = getCollectingType(attributeContext);
- if (!collectingType) {
- return null;
- }
- const items = collectSelectors(cssDoc.stylesheet as CSSNode, collectingType);
+ if (!collectingType) {
+ return null;
+ }
+ const items = collectSelectors(cssDoc.stylesheet as CSSNode, collectingType);
- console.log("getIdClassCompletion items", items.length);
- return CompletionList.create(items);
+ console.log('getIdClassCompletion items', items.length);
+ return CompletionList.create(items);
}
function getCollectingType(attributeContext: AttributeContext): number | undefined {
- if (attributeContext.inValue) {
- if (attributeContext.name === 'class') {
- return NodeType.ClassSelector;
- }
- if (attributeContext.name === 'id') {
- return NodeType.IdentifierSelector;
- }
- } else if (attributeContext.name.startsWith('class:')) {
- return NodeType.ClassSelector;
+ if (attributeContext.inValue) {
+ if (attributeContext.name === 'class') {
+ return NodeType.ClassSelector;
+ }
+ if (attributeContext.name === 'id') {
+ return NodeType.IdentifierSelector;
}
+ } else if (attributeContext.name.startsWith('class:')) {
+ return NodeType.ClassSelector;
+ }
}
/**
@@ -36,35 +33,35 @@ function getCollectingType(attributeContext: AttributeContext): number | undefin
* The enum is not exported. we have to update this whenever it changes
*/
export enum NodeType {
- ClassSelector = 14,
- IdentifierSelector = 15
+ ClassSelector = 14,
+ IdentifierSelector = 15,
}
export type CSSNode = {
- type: number;
- children: CSSNode[] | undefined;
- getText(): string;
+ type: number;
+ children: CSSNode[] | undefined;
+ getText(): string;
};
export function collectSelectors(stylesheet: CSSNode, type: number) {
- const result: CSSNode[] = [];
- walk(stylesheet, (node) => {
- if (node.type === type) {
- result.push(node);
- }
- });
+ const result: CSSNode[] = [];
+ walk(stylesheet, (node) => {
+ if (node.type === type) {
+ result.push(node);
+ }
+ });
- return result.map(
- (node): CompletionItem => ({
- label: node.getText().substring(1),
- kind: CompletionItemKind.Keyword
- })
- );
+ return result.map(
+ (node): CompletionItem => ({
+ label: node.getText().substring(1),
+ kind: CompletionItemKind.Keyword,
+ })
+ );
}
function walk(node: CSSNode, callback: (node: CSSNode) => void) {
- callback(node);
- if (node.children) {
- node.children.forEach((node) => walk(node, callback));
- }
-} \ No newline at end of file
+ callback(node);
+ if (node.children) {
+ node.children.forEach((node) => walk(node, callback));
+ }
+}