mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-26 11:38:51 +01:00
[css] Generalize languageModelCache
This commit is contained in:
@@ -9,8 +9,8 @@ import {
|
||||
TextDocuments, TextDocument, InitializeParams, InitializeResult, RequestType
|
||||
} from 'vscode-languageserver';
|
||||
|
||||
import {getCSSLanguageService, getSCSSLanguageService, getLESSLanguageService, LanguageSettings, LanguageService} from 'vscode-css-languageservice';
|
||||
import {getStylesheetCache} from './stylesheetCache';
|
||||
import {getCSSLanguageService, getSCSSLanguageService, getLESSLanguageService, LanguageSettings, LanguageService, Stylesheet} from 'vscode-css-languageservice';
|
||||
import {getLanguageModelCache} from './languageModelCache';
|
||||
|
||||
namespace ColorSymbolRequest {
|
||||
export const type: RequestType<string, Range[], any> = { get method() { return 'css/colorSymbols'; } };
|
||||
@@ -36,7 +36,7 @@ let documents: TextDocuments = new TextDocuments();
|
||||
// for open, change and close text document events
|
||||
documents.listen(connection);
|
||||
|
||||
let stylesheets = getStylesheetCache(10, 60, document => getLanguageService(document).parseStylesheet(document));
|
||||
let stylesheets = getLanguageModelCache<Stylesheet>(10, 60, document => getLanguageService(document).parseStylesheet(document));
|
||||
documents.onDidClose(e => {
|
||||
stylesheets.onDocumentRemoved(e.document);
|
||||
});
|
||||
@@ -119,7 +119,7 @@ function triggerValidation(textDocument: TextDocument): void {
|
||||
}
|
||||
|
||||
function validateTextDocument(textDocument: TextDocument): void {
|
||||
let stylesheet = stylesheets.getStylesheet(textDocument);
|
||||
let stylesheet = stylesheets.get(textDocument);
|
||||
let diagnostics = getLanguageService(textDocument).doValidation(textDocument, stylesheet);
|
||||
// Send the computed diagnostics to VSCode.
|
||||
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
|
||||
@@ -127,55 +127,55 @@ function validateTextDocument(textDocument: TextDocument): void {
|
||||
|
||||
connection.onCompletion(textDocumentPosition => {
|
||||
let document = documents.get(textDocumentPosition.textDocument.uri);
|
||||
let stylesheet = stylesheets.getStylesheet(document);
|
||||
let stylesheet = stylesheets.get(document);
|
||||
return getLanguageService(document).doComplete(document, textDocumentPosition.position, stylesheet);
|
||||
});
|
||||
|
||||
connection.onHover(textDocumentPosition => {
|
||||
let document = documents.get(textDocumentPosition.textDocument.uri);
|
||||
let styleSheet = stylesheets.getStylesheet(document);
|
||||
let styleSheet = stylesheets.get(document);
|
||||
return getLanguageService(document).doHover(document, textDocumentPosition.position, styleSheet);
|
||||
});
|
||||
|
||||
connection.onDocumentSymbol(documentSymbolParams => {
|
||||
let document = documents.get(documentSymbolParams.textDocument.uri);
|
||||
let stylesheet = stylesheets.getStylesheet(document);
|
||||
let stylesheet = stylesheets.get(document);
|
||||
return getLanguageService(document).findDocumentSymbols(document, stylesheet);
|
||||
});
|
||||
|
||||
connection.onDefinition(documentSymbolParams => {
|
||||
let document = documents.get(documentSymbolParams.textDocument.uri);
|
||||
let stylesheet = stylesheets.getStylesheet(document);
|
||||
let stylesheet = stylesheets.get(document);
|
||||
return getLanguageService(document).findDefinition(document, documentSymbolParams.position, stylesheet);
|
||||
});
|
||||
|
||||
connection.onDocumentHighlight(documentSymbolParams => {
|
||||
let document = documents.get(documentSymbolParams.textDocument.uri);
|
||||
let stylesheet = stylesheets.getStylesheet(document);
|
||||
let stylesheet = stylesheets.get(document);
|
||||
return getLanguageService(document).findDocumentHighlights(document, documentSymbolParams.position, stylesheet);
|
||||
});
|
||||
|
||||
connection.onReferences(referenceParams => {
|
||||
let document = documents.get(referenceParams.textDocument.uri);
|
||||
let stylesheet = stylesheets.getStylesheet(document);
|
||||
let stylesheet = stylesheets.get(document);
|
||||
return getLanguageService(document).findReferences(document, referenceParams.position, stylesheet);
|
||||
});
|
||||
|
||||
connection.onCodeAction(codeActionParams => {
|
||||
let document = documents.get(codeActionParams.textDocument.uri);
|
||||
let stylesheet = stylesheets.getStylesheet(document);
|
||||
let stylesheet = stylesheets.get(document);
|
||||
return getLanguageService(document).doCodeActions(document, codeActionParams.range, codeActionParams.context, stylesheet);
|
||||
});
|
||||
|
||||
connection.onRequest(ColorSymbolRequest.type, uri => {
|
||||
let document = documents.get(uri);
|
||||
let stylesheet = stylesheets.getStylesheet(document);
|
||||
let stylesheet = stylesheets.get(document);
|
||||
return getLanguageService(document).findColorSymbols(document, stylesheet);
|
||||
});
|
||||
|
||||
connection.onRenameRequest(renameParameters => {
|
||||
let document = documents.get(renameParameters.textDocument.uri);
|
||||
let stylesheet = stylesheets.getStylesheet(document);
|
||||
let stylesheet = stylesheets.get(document);
|
||||
return getLanguageService(document).doRename(document, renameParameters.position, renameParameters.newName, stylesheet);
|
||||
});
|
||||
|
||||
|
||||
83
extensions/css/server/src/languageModelCache.ts
Normal file
83
extensions/css/server/src/languageModelCache.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
|
||||
import {TextDocument} from 'vscode-languageserver';
|
||||
|
||||
export interface LanguageModelCache<T> {
|
||||
get(document: TextDocument): T;
|
||||
onDocumentRemoved(document: TextDocument): void;
|
||||
dispose(): void;
|
||||
}
|
||||
|
||||
export function getLanguageModelCache<T>(maxEntries: number, cleanupIntervalTimeInSec: number, parse: (document: TextDocument) => T) : LanguageModelCache<T> {
|
||||
let languageModels: { [uri:string]: {version:number, languageId: string, cTime: number, languageModel: T}} = {};
|
||||
let nModels = 0;
|
||||
|
||||
let cleanupInterval = void 0;
|
||||
if (cleanupIntervalTimeInSec > 0) {
|
||||
cleanupInterval = setInterval(() => {
|
||||
let cutoffTime = Date.now() - cleanupIntervalTimeInSec * 1000;
|
||||
let uris = Object.keys(languageModels);
|
||||
for (let uri of uris) {
|
||||
let languageModelInfo = languageModels[uri];
|
||||
if (languageModelInfo.cTime < cutoffTime) {
|
||||
delete languageModels[uri];
|
||||
nModels--;
|
||||
}
|
||||
}
|
||||
}, cleanupIntervalTimeInSec * 1000);
|
||||
}
|
||||
|
||||
return {
|
||||
get(document: TextDocument) : T {
|
||||
let version = document.version;
|
||||
let languageId = document.languageId;
|
||||
let languageModelInfo = languageModels[document.uri];
|
||||
if (languageModelInfo && languageModelInfo.version === version && languageModelInfo.languageId === languageId) {
|
||||
languageModelInfo.cTime = Date.now();
|
||||
return languageModelInfo.languageModel;
|
||||
}
|
||||
let languageModel = parse(document);
|
||||
languageModels[document.uri] = { languageModel, version, languageId, cTime: Date.now()};
|
||||
if (!languageModelInfo) {
|
||||
nModels++;
|
||||
}
|
||||
|
||||
if (nModels === maxEntries) {
|
||||
let oldestTime = Number.MAX_VALUE;
|
||||
let oldestUri = null;
|
||||
for (let uri in languageModels) {
|
||||
let languageModelInfo = languageModels[uri];
|
||||
if (languageModelInfo.cTime < oldestTime) {
|
||||
oldestUri = uri;
|
||||
oldestTime = languageModelInfo.cTime;
|
||||
}
|
||||
}
|
||||
if (oldestUri) {
|
||||
delete languageModels[oldestUri];
|
||||
nModels--;
|
||||
}
|
||||
}
|
||||
return languageModel;
|
||||
|
||||
},
|
||||
onDocumentRemoved(document: TextDocument) {
|
||||
let uri = document.uri;
|
||||
if (languageModels[uri]) {
|
||||
delete languageModels[uri];
|
||||
nModels--;
|
||||
}
|
||||
},
|
||||
dispose() {
|
||||
if (typeof cleanupInterval !== 'undefined') {
|
||||
clearInterval(cleanupInterval);
|
||||
cleanupInterval = void 0;
|
||||
languageModels = {};
|
||||
nModels = 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
|
||||
import {TextDocument} from 'vscode-languageserver';
|
||||
import {Stylesheet} from 'vscode-css-languageservice';
|
||||
|
||||
export interface StylesheetCache {
|
||||
getStylesheet(document: TextDocument): Stylesheet;
|
||||
onDocumentRemoved(document: TextDocument): void;
|
||||
dispose(): void;
|
||||
}
|
||||
|
||||
export function getStylesheetCache(maxEntries: number, cleanupIntervalTimeInSec: number, parseStylesheet: (document: TextDocument) => Stylesheet) : StylesheetCache {
|
||||
let styleSheets: { [uri:string]: {version:number, languageId: string, cTime: number, stylesheet: Stylesheet}} = {};
|
||||
let nStyleSheets = 0;
|
||||
|
||||
let cleanupInterval = void 0;
|
||||
if (cleanupIntervalTimeInSec > 0) {
|
||||
cleanupInterval = setInterval(() => {
|
||||
let cutoffTime = Date.now() - cleanupIntervalTimeInSec * 1000;
|
||||
let uris = Object.keys(styleSheets);
|
||||
for (let uri of uris) {
|
||||
let stylesheetInfo = styleSheets[uri];
|
||||
if (stylesheetInfo.cTime < cutoffTime) {
|
||||
delete styleSheets[uri];
|
||||
nStyleSheets--;
|
||||
}
|
||||
}
|
||||
}, cleanupIntervalTimeInSec * 1000);
|
||||
}
|
||||
|
||||
return {
|
||||
getStylesheet(document: TextDocument) {
|
||||
let version = document.version;
|
||||
let languageId = document.languageId;
|
||||
let stylesheetInfo = styleSheets[document.uri];
|
||||
if (stylesheetInfo && stylesheetInfo.version === version && stylesheetInfo.languageId === languageId) {
|
||||
stylesheetInfo.cTime = Date.now();
|
||||
return stylesheetInfo.stylesheet;
|
||||
}
|
||||
let stylesheet = parseStylesheet(document);
|
||||
styleSheets[document.uri] = { stylesheet, version, languageId, cTime: Date.now()};
|
||||
if (!stylesheetInfo) {
|
||||
nStyleSheets++;
|
||||
}
|
||||
|
||||
if (nStyleSheets === maxEntries) {
|
||||
let oldestTime = Number.MAX_VALUE;
|
||||
let oldestUri = null;
|
||||
for (let uri in styleSheets) {
|
||||
let stylesheetInfo = styleSheets[uri];
|
||||
if (stylesheetInfo.cTime < oldestTime) {
|
||||
oldestUri = uri;
|
||||
oldestTime = stylesheetInfo.cTime;
|
||||
}
|
||||
}
|
||||
if (oldestUri) {
|
||||
delete styleSheets[oldestUri];
|
||||
nStyleSheets--;
|
||||
}
|
||||
}
|
||||
return stylesheet;
|
||||
|
||||
},
|
||||
onDocumentRemoved(document: TextDocument) {
|
||||
let uri = document.uri;
|
||||
if (styleSheets[uri]) {
|
||||
delete styleSheets[uri];
|
||||
nStyleSheets--;
|
||||
}
|
||||
},
|
||||
dispose() {
|
||||
if (typeof cleanupInterval !== 'undefined') {
|
||||
clearInterval(cleanupInterval);
|
||||
cleanupInterval = void 0;
|
||||
styleSheets = {};
|
||||
nStyleSheets = 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user