-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
css.experimental.customData #66228
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -30,13 +30,30 @@ export function activate(context: ExtensionContext) { | |
|
||
let documentSelector = ['css', 'scss', 'less']; | ||
|
||
let dataPaths: string[] = workspace.getConfiguration('css').get('experimental.customData', []); | ||
if (dataPaths && dataPaths.length > 0) { | ||
if (!workspace.workspaceFolders) { | ||
dataPaths = []; | ||
} else { | ||
try { | ||
const workspaceRoot = workspace.workspaceFolders[0].uri.fsPath; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -50,6 +52,8 @@ let scopedSettingsSupport = false; | |
let foldingRangeLimit = Number.MAX_VALUE; | ||
let workspaceFolders: WorkspaceFolder[]; | ||
|
||
const languageServices: { [id: string]: LanguageService } = {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess that's not really a necessary change. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 => { | ||
|
@@ -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; | ||
|
@@ -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, | ||
|
@@ -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) { | ||
|
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest to move that to |
||
let rawData: any; | ||
|
||
try { | ||
rawData = JSON.parse(source); | ||
} catch (err) { | ||
return {}; | ||
} | ||
|
||
return { | ||
properties: rawData.properties || [], | ||
atDirectives: rawData.atdirectives || [], | ||
pseudoClasses: rawData.pseudoclasses || [], | ||
pseudoElements: rawData.pseudoelements || [] | ||
}; | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.