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

css.experimental.customData #66228

Merged
merged 5 commits into from
Jan 11, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 18 additions & 1 deletion extensions/css-language-features/client/src/cssMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as fs from 'fs';
import * as nls from 'vscode-nls';
const localize = nls.loadMessageBundle();

import { languages, window, commands, ExtensionContext, Range, Position, CompletionItem, CompletionItemKind, TextEdit, SnippetString } from 'vscode';
import { languages, window, commands, ExtensionContext, Range, Position, CompletionItem, CompletionItemKind, TextEdit, SnippetString, workspace } from 'vscode';
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind, Disposable } from 'vscode-languageclient';

// this method is called when vs code is activated
Expand All @@ -30,13 +30,30 @@ export function activate(context: ExtensionContext) {

let documentSelector = ['css', 'scss', 'less'];

let dataPaths: string[] = workspace.getConfiguration('css').get('experimental.customData', []);
Copy link
Contributor

Choose a reason for hiding this comment

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

I suspect this is just for teting purposes, but I assume in the end, these files will not be located in the workspace but part of a extension, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They'll, see an example for HTML: https://github.com/octref/web-components-examples

Don't think we can assume everyone wants to publish extensions for enhancing their CSS.

if (dataPaths && dataPaths.length > 0) {
if (!workspace.workspaceFolders) {
dataPaths = [];
} else {
try {
const workspaceRoot = workspace.workspaceFolders[0].uri.fsPath;
Copy link
Contributor

Choose a reason for hiding this comment

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

Workspace folders can also have other schemes than file:// (in combination with contributed filesystem providers)

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 need to handle that. Have never tried custom filesystem providers though. Tracked in #66307.

dataPaths = dataPaths.map(d => {
return path.resolve(workspaceRoot, d);
});
} catch (err) {
dataPaths = [];
}
}
}

// Options to control the language client
let clientOptions: LanguageClientOptions = {
documentSelector,
synchronize: {
configurationSection: ['css', 'scss', 'less']
},
initializationOptions: {
dataPaths
}
};

Expand Down
4 changes: 4 additions & 0 deletions extensions/css-language-features/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
"id": "css",
"title": "%css.title%",
"properties": {
"css.experimental.customData": {
"type": "array",
"description": "A list of JSON file paths that define custom CSS data that loads custom properties, at directives, pseudo classes / elements."
},
"css.validate": {
"type": "boolean",
"scope": "resource",
Expand Down
41 changes: 35 additions & 6 deletions extensions/css-language-features/server/src/cssServerMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import {
createConnection, IConnection, TextDocuments, InitializeParams, InitializeResult, ServerCapabilities, ConfigurationRequest, WorkspaceFolder
} from 'vscode-languageserver';
import URI from 'vscode-uri';
import * as fs from 'fs';
import { TextDocument, CompletionList } from 'vscode-languageserver-types';

import { getCSSLanguageService, getSCSSLanguageService, getLESSLanguageService, LanguageSettings, LanguageService, Stylesheet } from 'vscode-css-languageservice';
import { getLanguageModelCache } from './languageModelCache';
import { getPathCompletionParticipant } from './pathCompletion';
import { formatError, runSafe } from './utils/runner';
import { getDocumentContext } from './utils/documentContext';
import { parseCSSData } from './languageFacts';

export interface Settings {
css: LanguageSettings;
Expand Down Expand Up @@ -50,6 +52,8 @@ let scopedSettingsSupport = false;
let foldingRangeLimit = Number.MAX_VALUE;
let workspaceFolders: WorkspaceFolder[];

const languageServices: { [id: string]: LanguageService } = {};
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess that's not really a necessary change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doing this so I can locally scope the data variables. I don't want the customData to be on top level.

Choose a reason for hiding this comment

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

So you say vs code can complete css variables by setting this setting


// After the server has started the client sends an initialize request. The server receives
// in the passed params the rootPath of the workspace plus the client capabilities.
connection.onInitialize((params: InitializeParams): InitializeResult => {
Expand All @@ -61,6 +65,33 @@ connection.onInitialize((params: InitializeParams): InitializeResult => {
}
}

const dataPaths: string[] = params.initializationOptions.dataPaths;

let customData = {
customProperties: [],
customAtDirectives: [],
customPseudoElements: [],
customPseudoClasses: []
};

dataPaths.forEach(p => {
if (fs.existsSync(p)) {
const {
properties,
atDirectives,
pseudoClasses,
pseudoElements
} = parseCSSData(fs.readFileSync(p, 'utf-8'));

customData.customProperties = customData.customProperties.concat(properties);
customData.customAtDirectives = customData.customAtDirectives.concat(atDirectives);
customData.customPseudoClasses = customData.customPseudoClasses.concat(pseudoClasses);
customData.customPseudoElements = customData.customPseudoElements.concat(pseudoElements);
} else {
return;
}
});

function getClientCapability<T>(name: string, def: T) {
const keys = name.split('.');
let c: any = params.capabilities;
Expand All @@ -76,6 +107,10 @@ connection.onInitialize((params: InitializeParams): InitializeResult => {
scopedSettingsSupport = !!getClientCapability('workspace.configuration', false);
foldingRangeLimit = getClientCapability('textDocument.foldingRange.rangeLimit', Number.MAX_VALUE);

languageServices.css = getCSSLanguageService(customData);
languageServices.scss = getSCSSLanguageService(customData);
languageServices.less = getLESSLanguageService(customData);

const capabilities: ServerCapabilities = {
// Tell the client that the server works in FULL text document sync mode
textDocumentSync: documents.syncKind,
Expand All @@ -96,12 +131,6 @@ connection.onInitialize((params: InitializeParams): InitializeResult => {
return { capabilities };
});

const languageServices: { [id: string]: LanguageService } = {
css: getCSSLanguageService(),
scss: getSCSSLanguageService(),
less: getLESSLanguageService()
};

function getLanguageService(document: TextDocument) {
let service = languageServices[document.languageId];
if (!service) {
Expand Down
21 changes: 21 additions & 0 deletions extensions/css-language-features/server/src/languageFacts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

export function parseCSSData(source: string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest to move that to util

let rawData: any;

try {
rawData = JSON.parse(source);
} catch (err) {
return {};
}

return {
properties: rawData.properties || [],
atDirectives: rawData.atdirectives || [],
pseudoClasses: rawData.pseudoclasses || [],
pseudoElements: rawData.pseudoelements || []
};
}