chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 12:38:42 +08:00
commit 2d485ba600
8326 changed files with 2641263 additions and 0 deletions
@@ -0,0 +1,70 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { CancellationToken, ResponseError, LSPErrorCodes } from 'vscode-languageserver';
import { RuntimeEnvironment } from '../jsonServer';
export function formatError(message: string, err: any): string {
if (err instanceof Error) {
const error = <Error>err;
return `${message}: ${error.message}\n${error.stack}`;
} else if (typeof err === 'string') {
return `${message}: ${err}`;
} else if (err) {
return `${message}: ${err.toString()}`;
}
return message;
}
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) => {
runtime.timer.setImmediate(() => {
if (token.isCancellationRequested) {
resolve(cancelValue());
return;
}
return func().then(result => {
if (token.isCancellationRequested) {
resolve(cancelValue());
return;
} else {
resolve(result);
}
}, e => {
console.error(formatError(errorMessage, e));
resolve(errorVal);
});
});
});
}
export function runSafe<T, E>(runtime: RuntimeEnvironment, func: () => T, errorVal: T, errorMessage: string, token: CancellationToken): Thenable<T | ResponseError<E>> {
return new Promise<T | ResponseError<E>>((resolve) => {
runtime.timer.setImmediate(() => {
if (token.isCancellationRequested) {
resolve(cancelValue());
} else {
try {
const result = func();
if (token.isCancellationRequested) {
resolve(cancelValue());
return;
} else {
resolve(result);
}
} catch (e) {
console.error(formatError(errorMessage, e));
resolve(errorVal);
}
}
});
});
}
function cancelValue<E>() {
console.log('cancelled');
return new ResponseError<E>(LSPErrorCodes.RequestCancelled, 'Request cancelled');
}
@@ -0,0 +1,22 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/**
* Determines if haystack ends with needle.
*/
export function endsWith(haystack: string, needle: string): boolean {
const diff = haystack.length - needle.length;
if (diff > 0) {
return haystack.lastIndexOf(needle) === diff;
} else if (diff === 0) {
return haystack === needle;
} else {
return false;
}
}
export function convertSimple2RegExpPattern(pattern: string): string {
return pattern.replace(/[\-\\\{\}\+\?\|\^\$\.\,\[\]\(\)\#\s]/g, '\\$&').replace(/[\*]/g, '.*');
}
@@ -0,0 +1,108 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { CancellationToken, Connection, Diagnostic, Disposable, DocumentDiagnosticParams, DocumentDiagnosticReport, DocumentDiagnosticReportKind, TextDocuments } from 'vscode-languageserver';
import { TextDocument } from 'vscode-json-languageservice';
import { formatError, runSafeAsync } from './runner';
import { RuntimeEnvironment } from '../jsonServer';
export type Validator = (textDocument: TextDocument) => Promise<Diagnostic[]>;
export type DiagnosticsSupport = {
dispose(): void;
requestRefresh(): void;
};
export function registerDiagnosticsPushSupport(documents: TextDocuments<TextDocument>, connection: Connection, runtime: RuntimeEnvironment, validate: Validator): DiagnosticsSupport {
const pendingValidationRequests: { [uri: string]: Disposable } = {};
const validationDelayMs = 500;
const disposables: Disposable[] = [];
// The content of a text document has changed. This event is emitted
// when the text document first opened or when its content has changed.
documents.onDidChangeContent(change => {
triggerValidation(change.document);
}, undefined, disposables);
// a document has closed: clear all diagnostics
documents.onDidClose(event => {
cleanPendingValidation(event.document);
connection.sendDiagnostics({ uri: event.document.uri, diagnostics: [] });
}, undefined, disposables);
function cleanPendingValidation(textDocument: TextDocument): void {
const request = pendingValidationRequests[textDocument.uri];
if (request) {
request.dispose();
delete pendingValidationRequests[textDocument.uri];
}
}
function triggerValidation(textDocument: TextDocument): void {
cleanPendingValidation(textDocument);
const request = pendingValidationRequests[textDocument.uri] = runtime.timer.setTimeout(async () => {
if (request === pendingValidationRequests[textDocument.uri]) {
try {
const diagnostics = await validate(textDocument);
if (request === pendingValidationRequests[textDocument.uri]) {
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
}
delete pendingValidationRequests[textDocument.uri];
} catch (e) {
connection.console.error(formatError(`Error while validating ${textDocument.uri}`, e));
}
}
}, validationDelayMs);
}
return {
requestRefresh: () => {
documents.all().forEach(triggerValidation);
},
dispose: () => {
disposables.forEach(d => d.dispose());
disposables.length = 0;
const keys = Object.keys(pendingValidationRequests);
for (const key of keys) {
pendingValidationRequests[key].dispose();
delete pendingValidationRequests[key];
}
}
};
}
export function registerDiagnosticsPullSupport(documents: TextDocuments<TextDocument>, connection: Connection, runtime: RuntimeEnvironment, validate: Validator): DiagnosticsSupport {
function newDocumentDiagnosticReport(diagnostics: Diagnostic[]): DocumentDiagnosticReport {
return {
kind: DocumentDiagnosticReportKind.Full,
items: diagnostics
};
}
const registration = connection.languages.diagnostics.on(async (params: DocumentDiagnosticParams, token: CancellationToken) => {
return runSafeAsync(runtime, async () => {
const document = documents.get(params.textDocument.uri);
if (document) {
return newDocumentDiagnosticReport(await validate(document));
}
return newDocumentDiagnosticReport([]);
}, newDocumentDiagnosticReport([]), `Error while computing diagnostics for ${params.textDocument.uri}`, token);
});
function requestRefresh(): void {
connection.languages.diagnostics.refresh();
}
return {
requestRefresh,
dispose: () => {
registration.dispose();
}
};
}