Skip to content

Commit

Permalink
Merge pull request #66228 from Microsoft/octref/customCSSData
Browse files Browse the repository at this point in the history
css.experimental.customData
  • Loading branch information
octref authored Jan 11, 2019
2 parents 1dbdc3c + 6797372 commit 3e8028e
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 13 deletions.
6 changes: 5 additions & 1 deletion extensions/css-language-features/client/src/cssMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ 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';
import { getCustomDataPathsInAllWorkspaces } from './customData';

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

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

let dataPaths = getCustomDataPathsInAllWorkspaces(workspace.workspaceFolders);

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

Expand Down
39 changes: 39 additions & 0 deletions extensions/css-language-features/client/src/customData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as path from 'path';
import { workspace, WorkspaceFolder } from 'vscode';

interface ExperimentalConfig {
experimental?: {
customData?: string[]
};
}

export function getCustomDataPathsInAllWorkspaces(workspaceFolders: WorkspaceFolder[] | undefined): string[] {
const dataPaths: string[] = [];

if (!workspaceFolders) {
return dataPaths;
}

workspaceFolders.forEach(wf => {
const allCssConfig = workspace.getConfiguration(undefined, wf.uri);
const wfCSSConfig = allCssConfig.inspect<ExperimentalConfig>('css');
if (
wfCSSConfig &&
wfCSSConfig.workspaceFolderValue &&
wfCSSConfig.workspaceFolderValue.experimental &&
wfCSSConfig.workspaceFolderValue.experimental.customData
) {

wfCSSConfig.workspaceFolderValue.experimental.customData.forEach(p => [
dataPaths.push(path.resolve(wf.uri.fsPath, p))
]);
}
});

return dataPaths;
}
6 changes: 6 additions & 0 deletions extensions/css-language-features/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
"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.",
"default": [],
"scope": "resource"
},
"css.validate": {
"type": "boolean",
"scope": "resource",
Expand Down
2 changes: 1 addition & 1 deletion extensions/css-language-features/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"main": "./out/cssServerMain",
"dependencies": {
"vscode-css-languageservice": "^3.0.13-next.3",
"vscode-css-languageservice": "^3.0.13-next.4",
"vscode-languageserver": "^5.1.0"
},
"devDependencies": {
Expand Down
29 changes: 22 additions & 7 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 { getCSSLanguageService, getSCSSLanguageService, getLESSLanguageService, LanguageSettings, LanguageService, Stylesheet, CSSData } 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 './utils/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 } = {};

// 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,19 @@ connection.onInitialize((params: InitializeParams): InitializeResult => {
}
}

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

const customDataCollections: CSSData[] = [];

dataPaths.forEach(p => {
if (fs.existsSync(p)) {
const data = parseCSSData(fs.readFileSync(p, 'utf-8'));
customDataCollections.push(data);
} else {
return;
}
});

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

languageServices.css = getCSSLanguageService({ customDataCollections });
languageServices.scss = getSCSSLanguageService({ customDataCollections });
languageServices.less = getLESSLanguageService({ customDataCollections });

const capabilities: ServerCapabilities = {
// Tell the client that the server works in FULL text document sync mode
textDocumentSync: documents.syncKind,
Expand All @@ -96,12 +117,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
23 changes: 23 additions & 0 deletions extensions/css-language-features/server/src/utils/languageFacts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { CSSData } from 'vscode-css-languageservice';

export function parseCSSData(source: string): CSSData {
let rawData: any;

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

return {
properties: rawData.properties || [],
atDirectives: rawData.atdirectives || [],
pseudoClasses: rawData.pseudoclasses || [],
pseudoElements: rawData.pseudoelements || []
};
}
8 changes: 4 additions & 4 deletions extensions/css-language-features/server/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,10 @@ supports-color@5.4.0:
dependencies:
has-flag "^3.0.0"

vscode-css-languageservice@^3.0.13-next.3:
version "3.0.13-next.3"
resolved "https://registry.yarnpkg.com/vscode-css-languageservice/-/vscode-css-languageservice-3.0.13-next.3.tgz#b9e6f253cace52fbb749d2fe194ae4b196f3543e"
integrity sha512-7+7JddZRt8zFRLbqygxJw+GHtbQ5o2YvgEFvi4ixvbUAX6KlY3Yw6CgUUbg9dmBla7h+GbtoDjwiLFV6Oy+SBQ==
vscode-css-languageservice@^3.0.13-next.4:
version "3.0.13-next.4"
resolved "https://registry.yarnpkg.com/vscode-css-languageservice/-/vscode-css-languageservice-3.0.13-next.4.tgz#3ff1a0fefe7b47c698fd48baf05dcea2b709b99d"
integrity sha512-Ji7YpGyGkUZF3KSz1da3dVuKkdGujeDrHE73gRfGxDxb2yE00ifPFueRf1DMXVBSQ4rCeVWzC4gxEhnnLJEc2w==
dependencies:
vscode-languageserver-types "^3.13.0"
vscode-nls "^4.0.0"
Expand Down

0 comments on commit 3e8028e

Please sign in to comment.