Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow loading of custom data #142

Merged
merged 7 commits into from
Jan 11, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions src/cssLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ import { LESSParser } from './parser/lessParser';
import { LESSCompletion } from './services/lessCompletion';
import { getFoldingRanges } from './services/cssFolding';
import { LanguageSettings, ICompletionParticipant, DocumentContext } from './cssLanguageTypes';
import { addProperties, addAtDirectives, addPseudoClasses, addPseudoElements, IEntryData } from './services/languageFacts/index';

export type Stylesheet = {};
export * from './cssLanguageTypes';
export * from 'vscode-languageserver-types';

export interface LanguageServiceOptions {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to 'types'...

customProperties?: IEntryData[];
customAtDirectives?: IEntryData[];
customPseudoClasses?: IEntryData[];
customPseudoElements?: IEntryData[];
}

export interface LanguageService {
configure(raw: LanguageSettings): void;
doValidation(document: TextDocument, stylesheet: Stylesheet, documentSettings?: LanguageSettings): Diagnostic[];
Expand Down Expand Up @@ -72,15 +80,34 @@ function createFacade(parser: Parser, completion: CSSCompletion, hover: CSSHover
};
}

function handleCustomData(options?: LanguageServiceOptions) {
if (options) {
if (options.customProperties) {
addProperties(options.customProperties);
}
if (options.customAtDirectives) {
addAtDirectives(options.customAtDirectives);
}
if (options.customPseudoClasses) {
addPseudoClasses(options.customPseudoClasses);
}
if (options.customPseudoElements) {
addPseudoElements(options.customPseudoElements);
}
}
}

export function getCSSLanguageService(): LanguageService {
export function getCSSLanguageService(options?: LanguageServiceOptions): LanguageService {
handleCustomData(options);
Copy link
Contributor

@aeschli aeschli Jan 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we choose this API, we must store the properies/directives per LanguageService, not in global arrays. Users can create any number of language services, each with a different set of data.

I would suggest to make a new type CSSPropertySet (name to be debated) containing properties/directives/..., that is registered globaly with the vscode-css-languageservice (exported const propertySets : CSSPropertySet[]) Our built-in properties (generated from MDN) should also be represented in one (or multiple) CSSPropertySets.

In a first step, all language services just use that global list of propertySets. In a later step we allow to create a language service with a custom list of Sets. That language service would then only use that set. It would be good to already prepare our code to avoid accessing CSSPropertySet globaly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was about to check with you. The same problem with HTML now. Do we make the language services stateful? I'll go with stateful then.

return createFacade(new Parser(), new CSSCompletion(), new CSSHover(), new CSSNavigation(), new CSSCodeActions(), new CSSValidation());
}

export function getSCSSLanguageService(): LanguageService {
export function getSCSSLanguageService(options?: LanguageServiceOptions): LanguageService {
handleCustomData(options);
return createFacade(new SCSSParser(), new SCSSCompletion(), new CSSHover(), new CSSNavigation(), new CSSCodeActions(), new CSSValidation());
}

export function getLESSLanguageService(): LanguageService {
export function getLESSLanguageService(options?: LanguageServiceOptions): LanguageService {
handleCustomData(options);
return createFacade(new LESSParser(), new LESSCompletion(), new CSSHover(), new CSSNavigation(), new CSSCodeActions(), new CSSValidation());
}
13 changes: 12 additions & 1 deletion src/data/browsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@
*--------------------------------------------------------------------------------------------*/
// file generated from css-schema.xml and https://github.com/mdn/data using css-exclude_generate_browserjs.js

export const data : any = {
import { IEntryData } from "../services/languageFacts";

interface BrowserData {
css: {
properties: IEntryData[];
atdirectives: IEntryData[];
pseudoclasses: IEntryData[];
pseudoelements: IEntryData[];
};
}

export const data : BrowserData = {
"css": {
"atdirectives": [
{
Expand Down
2 changes: 1 addition & 1 deletion src/parser/cssParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ export class Parser {
}
let node = <nodes.PageBoxMarginBox>this.create(nodes.PageBoxMarginBox);

if (!this.acceptOneKeyword(languageFacts.getPageBoxDirectives())) {
if (!this.acceptOneKeyword(languageFacts.pageBoxDirectives)) {
this.markError(node, ParseError.UnknownAtRule, [], [TokenType.CurlyL]);
}

Expand Down
15 changes: 12 additions & 3 deletions src/services/cssCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,10 @@ export class CSSCompletion {
}

public getCompletionForTopLevel(result: CompletionList): CompletionList {
for (let entry of languageFacts.getAtDirectives()) {
const atDirectives = languageFacts.getAtDirectives();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the original code more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need atDirectives[entryName] for accessing the each entry. It previously returns an array, so let...of works, now it's an object.


for (let entryName in atDirectives) {
const entry = atDirectives[entryName];
if (entry.browsers.count > 0) {
result.items.push({
label: entry.name,
Expand All @@ -591,6 +594,7 @@ export class CSSCompletion {
});
}
}

this.getCompletionsForSelector(null, false, result);
return result;
}
Expand Down Expand Up @@ -618,7 +622,9 @@ export class CSSCompletion {
this.defaultReplaceRange = Range.create(Position.create(this.position.line, this.position.character - this.currentWord.length), this.position);
}

for (let entry of languageFacts.getPseudoClasses()) {
const pseudoClasses = languageFacts.getPseudoClasses();
for (let entryName in pseudoClasses) {
const entry = pseudoClasses[entryName];
if (entry.browsers.onCodeComplete) {
let insertText = moveCursorInsideParenthesis(entry.name);
let item: CompletionItem = {
Expand All @@ -634,7 +640,10 @@ export class CSSCompletion {
result.items.push(item);
}
}
for (let entry of languageFacts.getPseudoElements()) {

const pseudoElements = languageFacts.getPseudoElements();
for (let entryName in pseudoElements) {
const entry = pseudoElements[entryName];
if (entry.browsers.onCodeComplete) {
let insertText = moveCursorInsideParenthesis(entry.name);
let item: CompletionItem = {
Expand Down
2 changes: 1 addition & 1 deletion src/services/cssNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as nls from 'vscode-nls';
import { DocumentContext } from '../cssLanguageTypes';
import * as nodes from '../parser/cssNodes';
import { Symbols } from '../parser/cssSymbolScope';
import { getColorValue, hslFromColor } from '../services/languageFacts';
import { getColorValue, hslFromColor } from './languageFacts';
import { endsWith, startsWith } from '../utils/strings';

const localize = nls.loadMessageBundle();
Expand Down
Loading