adopt web extensions to webpack 5

This commit is contained in:
Martin Aeschlimann
2021-07-08 21:41:03 +02:00
parent a3e07245ee
commit 7ebe6a6054
51 changed files with 552 additions and 277 deletions

View File

@@ -18,6 +18,7 @@ module.exports = withBrowserDefaults({
output: {
filename: 'cssServerMain.js',
path: path.join(__dirname, 'dist', 'browser'),
libraryTarget: 'var'
libraryTarget: 'var',
library: 'serverExportVar'
}
});

View File

@@ -3,8 +3,8 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { createConnection, BrowserMessageReader, BrowserMessageWriter } from 'vscode-languageserver/browser';
import { startServer } from '../cssServer';
import { createConnection, BrowserMessageReader, BrowserMessageWriter, Disposable } from 'vscode-languageserver/browser';
import { RuntimeEnvironment, startServer } from '../cssServer';
declare let self: any;
@@ -13,4 +13,17 @@ const messageWriter = new BrowserMessageWriter(self);
const connection = createConnection(messageReader, messageWriter);
startServer(connection, {});
const runtime: RuntimeEnvironment = {
timer: {
setImmediate(callback: (...args: any[]) => void, ...args: any[]): Disposable {
const handle = setTimeout(callback, 0, ...args);
return { dispose: () => clearTimeout(handle) };
},
setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): Disposable {
const handle = setTimeout(callback, ms, ...args);
return { dispose: () => clearTimeout(handle) };
}
}
};
startServer(connection, runtime);

View File

@@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import {
Connection, TextDocuments, InitializeParams, InitializeResult, ServerCapabilities, ConfigurationRequest, WorkspaceFolder, TextDocumentSyncKind, NotificationType
Connection, TextDocuments, InitializeParams, InitializeResult, ServerCapabilities, ConfigurationRequest, WorkspaceFolder, TextDocumentSyncKind, NotificationType, Disposable
} from 'vscode-languageserver';
import { URI } from 'vscode-uri';
import { getCSSLanguageService, getSCSSLanguageService, getLESSLanguageService, LanguageSettings, LanguageService, Stylesheet, TextDocument, Position } from 'vscode-css-languageservice';
@@ -25,8 +25,12 @@ export interface Settings {
}
export interface RuntimeEnvironment {
file?: RequestService;
http?: RequestService
readonly file?: RequestService;
readonly http?: RequestService;
readonly timer: {
setImmediate(callback: (...args: any[]) => void, ...args: any[]): Disposable;
setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): Disposable;
}
}
export function startServer(connection: Connection, runtime: RuntimeEnvironment) {
@@ -150,7 +154,7 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
documents.all().forEach(triggerValidation);
}
const pendingValidationRequests: { [uri: string]: NodeJS.Timer } = {};
const pendingValidationRequests: { [uri: string]: Disposable } = {};
const validationDelayMs = 500;
// The content of a text document has changed. This event is emitted
@@ -168,14 +172,14 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
function cleanPendingValidation(textDocument: TextDocument): void {
const request = pendingValidationRequests[textDocument.uri];
if (request) {
clearTimeout(request);
request.dispose();
delete pendingValidationRequests[textDocument.uri];
}
}
function triggerValidation(textDocument: TextDocument): void {
cleanPendingValidation(textDocument);
pendingValidationRequests[textDocument.uri] = setTimeout(() => {
pendingValidationRequests[textDocument.uri] = runtime.timer.setTimeout(() => {
delete pendingValidationRequests[textDocument.uri];
validateTextDocument(textDocument);
}, validationDelayMs);
@@ -203,7 +207,7 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
}
connection.onCompletion((textDocumentPosition, token) => {
return runSafeAsync(async () => {
return runSafeAsync(runtime, async () => {
const document = documents.get(textDocumentPosition.textDocument.uri);
if (document) {
const [settings,] = await Promise.all([getDocumentSettings(document), dataProvidersReady]);
@@ -216,7 +220,7 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
});
connection.onHover((textDocumentPosition, token) => {
return runSafeAsync(async () => {
return runSafeAsync(runtime, async () => {
const document = documents.get(textDocumentPosition.textDocument.uri);
if (document) {
const [settings,] = await Promise.all([getDocumentSettings(document), dataProvidersReady]);
@@ -228,7 +232,7 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
});
connection.onDocumentSymbol((documentSymbolParams, token) => {
return runSafeAsync(async () => {
return runSafeAsync(runtime, async () => {
const document = documents.get(documentSymbolParams.textDocument.uri);
if (document) {
await dataProvidersReady;
@@ -240,7 +244,7 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
});
connection.onDefinition((documentDefinitionParams, token) => {
return runSafeAsync(async () => {
return runSafeAsync(runtime, async () => {
const document = documents.get(documentDefinitionParams.textDocument.uri);
if (document) {
await dataProvidersReady;
@@ -252,7 +256,7 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
});
connection.onDocumentHighlight((documentHighlightParams, token) => {
return runSafeAsync(async () => {
return runSafeAsync(runtime, async () => {
const document = documents.get(documentHighlightParams.textDocument.uri);
if (document) {
await dataProvidersReady;
@@ -265,7 +269,7 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
connection.onDocumentLinks(async (documentLinkParams, token) => {
return runSafeAsync(async () => {
return runSafeAsync(runtime, async () => {
const document = documents.get(documentLinkParams.textDocument.uri);
if (document) {
await dataProvidersReady;
@@ -279,7 +283,7 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
connection.onReferences((referenceParams, token) => {
return runSafeAsync(async () => {
return runSafeAsync(runtime, async () => {
const document = documents.get(referenceParams.textDocument.uri);
if (document) {
await dataProvidersReady;
@@ -291,7 +295,7 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
});
connection.onCodeAction((codeActionParams, token) => {
return runSafeAsync(async () => {
return runSafeAsync(runtime, async () => {
const document = documents.get(codeActionParams.textDocument.uri);
if (document) {
await dataProvidersReady;
@@ -303,7 +307,7 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
});
connection.onDocumentColor((params, token) => {
return runSafeAsync(async () => {
return runSafeAsync(runtime, async () => {
const document = documents.get(params.textDocument.uri);
if (document) {
await dataProvidersReady;
@@ -315,7 +319,7 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
});
connection.onColorPresentation((params, token) => {
return runSafeAsync(async () => {
return runSafeAsync(runtime, async () => {
const document = documents.get(params.textDocument.uri);
if (document) {
await dataProvidersReady;
@@ -327,7 +331,7 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
});
connection.onRenameRequest((renameParameters, token) => {
return runSafeAsync(async () => {
return runSafeAsync(runtime, async () => {
const document = documents.get(renameParameters.textDocument.uri);
if (document) {
await dataProvidersReady;
@@ -339,7 +343,7 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
});
connection.onFoldingRanges((params, token) => {
return runSafeAsync(async () => {
return runSafeAsync(runtime, async () => {
const document = documents.get(params.textDocument.uri);
if (document) {
await dataProvidersReady;
@@ -350,7 +354,7 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
});
connection.onSelectionRanges((params, token) => {
return runSafeAsync(async () => {
return runSafeAsync(runtime, async () => {
const document = documents.get(params.textDocument.uri);
const positions: Position[] = params.positions;

View File

@@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { TextDocument } from 'vscode-languageserver';
import { TextDocument } from 'vscode-css-languageservice';
export interface LanguageModelCache<T> {
get(document: TextDocument): T;
@@ -79,4 +79,4 @@ export function getLanguageModelCache<T>(maxEntries: number, cleanupIntervalTime
}
}
};
}
}

View File

@@ -3,9 +3,9 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { createConnection, Connection } from 'vscode-languageserver/node';
import { createConnection, Connection, Disposable } from 'vscode-languageserver/node';
import { formatError } from '../utils/runner';
import { startServer } from '../cssServer';
import { RuntimeEnvironment, startServer } from '../cssServer';
import { getNodeFSRequestService } from './nodeFs';
// Create a connection for the server.
@@ -18,4 +18,18 @@ process.on('unhandledRejection', (e: any) => {
connection.console.error(formatError(`Unhandled exception`, e));
});
startServer(connection, { file: getNodeFSRequestService() });
const runtime: RuntimeEnvironment = {
timer: {
setImmediate(callback: (...args: any[]) => void, ...args: any[]): Disposable {
const handle = setImmediate(callback, ...args);
return { dispose: () => clearImmediate(handle) };
},
setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): Disposable {
const handle = setTimeout(callback, ms, ...args);
return { dispose: () => clearTimeout(handle) };
}
},
file: getNodeFSRequestService()
};
startServer(connection, runtime);

View File

@@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import { ResponseError, CancellationToken, LSPErrorCodes } from 'vscode-languageserver';
import { RuntimeEnvironment } from '../cssServer';
export function formatError(message: string, err: any): string {
if (err instanceof Error) {
@@ -17,9 +18,9 @@ export function formatError(message: string, err: any): string {
return message;
}
export function runSafeAsync<T>(func: () => Thenable<T>, errorVal: T, errorMessage: string, token: CancellationToken): Thenable<T | ResponseError<any>> {
export function runSafeAsync<T>(runtime: RuntimeEnvironment, func: () => Thenable<T>, errorVal: T, errorMessage: string, token: CancellationToken): Thenable<T | ResponseError<any>> {
return new Promise<T | ResponseError<any>>((resolve) => {
setImmediate(() => {
runtime.timer.setImmediate(() => {
if (token.isCancellationRequested) {
resolve(cancelValue());
}