What is vscode-css-languageservice?
The vscode-css-languageservice package provides a set of tools and services for working with CSS in a way that integrates seamlessly with Visual Studio Code. It offers features like CSS validation, completion, hover information, and more, making it easier for developers to write and maintain CSS code.
What are vscode-css-languageservice's main functionalities?
CSS Validation
This feature allows you to validate CSS code and get diagnostics information. The code sample demonstrates how to create a CSS language service, parse a stylesheet, and perform validation to get diagnostics.
const cssService = require('vscode-css-languageservice').getCSSLanguageService();
const document = { uri: 'test.css', languageId: 'css', version: 1, getText: () => 'body { color: red; }' };
const stylesheet = cssService.parseStylesheet(document);
const diagnostics = cssService.doValidation(document, stylesheet);
console.log(diagnostics);
CSS Completion
This feature provides code completion suggestions for CSS. The code sample shows how to get completion items for a given position in a CSS document.
const cssService = require('vscode-css-languageservice').getCSSLanguageService();
const document = { uri: 'test.css', languageId: 'css', version: 1, getText: () => 'body { col' } };
const position = { line: 0, character: 10 };
const stylesheet = cssService.parseStylesheet(document);
const completions = cssService.doComplete(document, position, stylesheet);
console.log(completions);
CSS Hover Information
This feature provides hover information for CSS code. The code sample demonstrates how to get hover information for a specific position in a CSS document.
const cssService = require('vscode-css-languageservice').getCSSLanguageService();
const document = { uri: 'test.css', languageId: 'css', version: 1, getText: () => 'body { color: red; }' };
const position = { line: 0, character: 7 };
const stylesheet = cssService.parseStylesheet(document);
const hover = cssService.doHover(document, position, stylesheet);
console.log(hover);
Other packages similar to vscode-css-languageservice
postcss
PostCSS is a tool for transforming CSS with JavaScript plugins. It offers a wide range of functionalities including linting, autoprefixing, and more. Unlike vscode-css-languageservice, which is focused on providing language services for CSS in an editor environment, PostCSS is more about transforming and processing CSS code.
stylelint
Stylelint is a modern linter that helps you avoid errors and enforce conventions in your styles. It is highly configurable and can be integrated into various build systems. While vscode-css-languageservice provides validation and other language services within an editor, Stylelint is more focused on linting and enforcing coding standards.
css-tree
CSS-tree is a tool for working with CSS, including parsing, generating, and analyzing CSS code. It provides a low-level API for manipulating CSS. Compared to vscode-css-languageservice, which offers high-level language services, CSS-tree provides more granular control over CSS parsing and manipulation.
vscode-css-languageservice
Language services for CSS, LESS and SCSS
Why?
The vscode-css-languageservice contains the language smarts behind the CSS, LESS and SCSS editing experience of Visual Studio Code
and the Moanco editor.
- doValidation analyses an input string and returns syntax and lint errros.
- doComplete provides completion proposals for a given location.
- doHover provides a hover text for a given location.
- findDefinition finds the definition of the symbol at the given location.
- findReferences finds all references to the symbol at the given location.
- findDocumentHighlights finds all symbols connected to the given location.
- findDocumentSymbols provides all symbols in the given document
- doCodeActions evaluats code actions for the given location, typically to fix a problem.
- findColorSymbols evaluates all color symbols in the given document
- doRename renames all symbols connected connected to the given location.
Installation
npm install --save vscode-css-languageservice
API
export interface LanguageService {
configure(raw: LanguageSettings): void;
doValidation(document: TextDocument, stylesheet: Stylesheet): Diagnostic[];
parseStylesheet(document: TextDocument): Stylesheet;
doComplete(document: TextDocument, position: Position, stylesheet: Stylesheet): CompletionList;
doHover(document: TextDocument, position: Position, stylesheet: Stylesheet): Hover;
findDefinition(document: TextDocument, position: Position, stylesheet: Stylesheet): Location;
findReferences(document: TextDocument, position: Position, stylesheet: Stylesheet): Location[];
findDocumentHighlights(document: TextDocument, position: Position, stylesheet: Stylesheet): DocumentHighlight[];
findDocumentSymbols(document: TextDocument, stylesheet: Stylesheet): SymbolInformation[];
doCodeActions(document: TextDocument, range: Range, context: CodeActionContext, stylesheet: Stylesheet): Command[];
findColorSymbols(document: TextDocument, stylesheet: Stylesheet): Range[];
doRename(document: TextDocument, position: Position, newName: string, stylesheet: Stylesheet): WorkspaceEdit;
}
export interface LanguageSettings {
validate?: boolean;
lint?: any;
}
License
(MIT License)
Copyright 2016, Microsoft