-
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) completion for ids/classes in the template (#844)
Scan the Stylesheet for class and id names and provide autocompletion. Works for Less/SCSS/CSS.
- Loading branch information
1 parent
cbcbea3
commit 214d105
Showing
4 changed files
with
223 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/language-server/src/plugins/css/features/getIdClassCompletion.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { CompletionItem, CompletionItemKind, CompletionList } from 'vscode-languageserver'; | ||
import { AttributeContext } from '../../../lib/documents/parseHtml'; | ||
import { CSSDocument } from '../CSSDocument'; | ||
|
||
export function getIdClassCompletion( | ||
cssDoc: CSSDocument, | ||
attributeContext: AttributeContext | ||
): CompletionList | undefined { | ||
const collectingType = getCollectingType(attributeContext); | ||
|
||
if (!collectingType) { | ||
return; | ||
} | ||
const items = collectSelectors(cssDoc.stylesheet as CSSNode, collectingType); | ||
|
||
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; | ||
} | ||
} | ||
|
||
/** | ||
* incomplete see | ||
* https://github.com/microsoft/vscode-css-languageservice/blob/master/src/parser/cssNodes.ts#L14 | ||
* The enum is not exported. we have to update this whenever it changes | ||
*/ | ||
export enum NodeType { | ||
ClassSelector = 14, | ||
IdentifierSelector = 15 | ||
} | ||
|
||
export type CSSNode = { | ||
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); | ||
} | ||
}); | ||
|
||
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)); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
packages/language-server/test/plugins/css/features/getIdClassCompletion.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import assert from 'assert'; | ||
import { CompletionItem, CompletionItemKind, CompletionList } from 'vscode-languageserver'; | ||
import { Document, DocumentManager } from '../../../../src/lib/documents'; | ||
import { LSConfigManager } from '../../../../src/ls-config'; | ||
import { CSSPlugin } from '../../../../src/plugins'; | ||
import { CSSDocument } from '../../../../src/plugins/css/CSSDocument'; | ||
import { | ||
collectSelectors, | ||
NodeType, | ||
CSSNode | ||
} from '../../../../src/plugins/css/features/getIdClassCompletion'; | ||
|
||
describe('getIdClassCompletion', () => { | ||
function createDocument(content: string) { | ||
return new Document('file:///hello.svelte', content); | ||
} | ||
|
||
function createCSSDocument(content: string) { | ||
return new CSSDocument(createDocument(content)); | ||
} | ||
|
||
function testSelectors(items: CompletionItem[], expectedSelectors: string[]) { | ||
assert.deepStrictEqual( | ||
items.map((item) => item.label), | ||
expectedSelectors, | ||
'vscode-language-services might have changed the NodeType enum. Check if we need to update it' | ||
); | ||
} | ||
|
||
it('collect css classes', () => { | ||
const actual = collectSelectors( | ||
createCSSDocument('<style>.abc {}</style>').stylesheet as CSSNode, | ||
NodeType.ClassSelector | ||
); | ||
testSelectors(actual, ['abc']); | ||
}); | ||
|
||
it('collect css ids', () => { | ||
const actual = collectSelectors( | ||
createCSSDocument('<style>#abc {}</style>').stylesheet as CSSNode, | ||
NodeType.IdentifierSelector | ||
); | ||
testSelectors(actual, ['abc']); | ||
}); | ||
|
||
function setup(content: string) { | ||
const document = createDocument(content); | ||
const docManager = new DocumentManager(() => document); | ||
const pluginManager = new LSConfigManager(); | ||
const plugin = new CSSPlugin(docManager, pluginManager); | ||
docManager.openDocument(<any>'some doc'); | ||
return { plugin, document }; | ||
} | ||
|
||
it('provides css classes completion for class attribute', () => { | ||
const { plugin, document } = setup('<div class=></div><style>.abc{}</style>'); | ||
assert.deepStrictEqual(plugin.getCompletions(document, { line: 0, character: 11 }), { | ||
isIncomplete: false, | ||
items: [{ label: 'abc', kind: CompletionItemKind.Keyword }] | ||
} as CompletionList); | ||
}); | ||
|
||
it('provides css classes completion for class directive', () => { | ||
const { plugin, document } = setup('<div class:></div><style>.abc{}</style>'); | ||
assert.deepStrictEqual(plugin.getCompletions(document, { line: 0, character: 11 }), { | ||
isIncomplete: false, | ||
items: [{ label: 'abc', kind: CompletionItemKind.Keyword }] | ||
} as CompletionList); | ||
}); | ||
|
||
it('provides css id completion for id attribute', () => { | ||
const { plugin, document } = setup('<div id=></div><style>#abc{}</style>'); | ||
assert.deepStrictEqual(plugin.getCompletions(document, { line: 0, character: 8 }), { | ||
isIncomplete: false, | ||
items: [{ label: 'abc', kind: CompletionItemKind.Keyword }] | ||
} as CompletionList); | ||
}); | ||
}); |