Move function to javascriptLib

This commit is contained in:
nknguyenhc
2025-03-21 19:02:12 +08:00
parent b9d464f54a
commit f2ea517495
2 changed files with 13 additions and 14 deletions

View File

@@ -5,6 +5,7 @@
import { join, basename, dirname } from 'path';
import { readFileSync } from 'fs';
import { TextDocument } from './languageModes';
const contents: { [name: string]: string } = {};
@@ -31,3 +32,10 @@ export function loadLibrary(name: string) {
}
return content;
}
export function getLibUriAndDocument(libName: string): [string, TextDocument] {
const filepath = join(TYPESCRIPT_LIB_SOURCE, libName);
const fileUri = `file:///${filepath}`;
const content = loadLibrary(libName);
return [fileUri, TextDocument.create(fileUri, 'typescript', 1, content)];
}

View File

@@ -15,8 +15,7 @@ import { HTMLDocumentRegions } from './embeddedSupport';
import * as ts from 'typescript';
import { getSemanticTokens, getSemanticTokenLegend } from './javascriptSemanticTokens';
import path from 'path';
import fs from 'fs';
import { getLibUriAndDocument } from './javascriptLibs';
const JS_WORD_REGEX = /(-?\d*\.\d\w*)|([^\`\~\!\@\#\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s]+)/g;
@@ -304,17 +303,17 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache<HTMLDocume
const jsLanguageService = await host.getLanguageService(jsDocument);
const definition = jsLanguageService.getDefinitionAtPosition(jsDocument.uri, jsDocument.offsetAt(position));
if (definition) {
return definition.filter(d => d.fileName === jsDocument.uri || d.fileName === 'lib.dom.d.ts').map(d => {
return definition.map(d => {
if (d.fileName === jsDocument.uri) {
return {
uri: document.uri,
range: convertRange(jsDocument, d.textSpan)
};
} else {
const [libDomUri, libDomDocument] = getLibDomUriAndDocument();
const [libUri, libDocument] = getLibUriAndDocument(d.fileName);
return {
uri: libDomUri,
range: convertRange(libDomDocument, d.textSpan)
uri: libUri,
range: convertRange(libDocument, d.textSpan)
};
}
});
@@ -611,11 +610,3 @@ function generateIndent(level: number, options: FormattingOptions) {
return repeat('\t', level);
}
}
function getLibDomUriAndDocument(): [string, TextDocument] {
const folderName = path.dirname(path.dirname(path.dirname(path.dirname(__dirname))));
const filepath = path.resolve(folderName, 'node_modules', 'typescript', 'lib', 'lib.dom.d.ts');
const fileUri = `file:///${filepath}`;
const content = fs.readFileSync(filepath, 'utf8');
return [fileUri, TextDocument.create(fileUri, 'typescript', 1, content)];
}