mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-24 12:19:20 +00:00
extract loadLibrary
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { join, basename, dirname } from 'path';
|
||||
import { readFileSync } from 'fs';
|
||||
|
||||
const contents: { [name: string]: string } = {};
|
||||
|
||||
const serverFolder = basename(__dirname) === 'dist' ? dirname(__dirname) : dirname(dirname(__dirname));
|
||||
const TYPESCRIPT_LIB_SOURCE = join(serverFolder, '../../node_modules/typescript/lib');
|
||||
const JQUERY_PATH = join(serverFolder, 'lib/jquery.d.ts');
|
||||
|
||||
export function loadLibrary(name: string) {
|
||||
let content = contents[name];
|
||||
if (typeof content !== 'string') {
|
||||
let libPath;
|
||||
if (name === 'jquery') {
|
||||
libPath = JQUERY_PATH;
|
||||
} else {
|
||||
libPath = join(TYPESCRIPT_LIB_SOURCE, name); // from source
|
||||
}
|
||||
try {
|
||||
content = readFileSync(libPath).toString();
|
||||
} catch (e) {
|
||||
console.log(`Unable to load library ${name} at ${libPath}: ${e.message}`);
|
||||
content = '';
|
||||
}
|
||||
contents[name] = content;
|
||||
}
|
||||
return content;
|
||||
}
|
||||
@@ -10,25 +10,20 @@ import {
|
||||
DocumentHighlight, DocumentHighlightKind, CompletionList, Position, FormattingOptions, FoldingRange, FoldingRangeKind, SelectionRange,
|
||||
LanguageMode, Settings, SemanticTokenData, Workspace, DocumentContext
|
||||
} from './languageModes';
|
||||
import { getWordAtText, startsWith, isWhitespaceOnly, repeat } from '../utils/strings';
|
||||
import { getWordAtText, isWhitespaceOnly, repeat } from '../utils/strings';
|
||||
import { HTMLDocumentRegions } from './embeddedSupport';
|
||||
|
||||
import * as ts from 'typescript';
|
||||
import { getSemanticTokens, getSemanticTokenLegend } from './javascriptSemanticTokens';
|
||||
import { joinPath } from '../requests';
|
||||
|
||||
import { loadLibrary } from './javascriptLibs';
|
||||
|
||||
const JS_WORD_REGEX = /(-?\d*\.\d\w*)|([^\`\~\!\@\#\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s]+)/g;
|
||||
|
||||
let jquery_d_ts = joinPath(__dirname, '../lib/jquery.d.ts'); // when packaged
|
||||
if (!ts.sys.fileExists(jquery_d_ts)) {
|
||||
jquery_d_ts = joinPath(__dirname, '../../lib/jquery.d.ts'); // from source
|
||||
}
|
||||
|
||||
export function getJavaScriptMode(documentRegions: LanguageModelCache<HTMLDocumentRegions>, languageId: 'javascript' | 'typescript', workspace: Workspace): LanguageMode {
|
||||
let jsDocuments = getLanguageModelCache<TextDocument>(10, 60, document => documentRegions.get(document).getEmbeddedDocument(languageId));
|
||||
|
||||
const workingFile = languageId === 'javascript' ? 'vscode://javascript/1.js' : 'vscode://javascript/2.ts'; // the same 'file' is used for all contents
|
||||
const jQueryFile = 'jquery';
|
||||
|
||||
let compilerOptions: ts.CompilerOptions = { allowNonTsExtensions: true, allowJs: true, lib: ['lib.es6.d.ts'], target: ts.ScriptTarget.Latest, moduleResolution: ts.ModuleResolutionKind.Classic, experimentalDecorators: false };
|
||||
let currentTextDocument: TextDocument;
|
||||
@@ -41,7 +36,7 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache<HTMLDocume
|
||||
}
|
||||
const host: ts.LanguageServiceHost = {
|
||||
getCompilationSettings: () => compilerOptions,
|
||||
getScriptFileNames: () => [workingFile, jquery_d_ts],
|
||||
getScriptFileNames: () => [workingFile, jQueryFile],
|
||||
getScriptKind: (fileName) => fileName.substr(fileName.length - 2) === 'ts' ? ts.ScriptKind.TS : ts.ScriptKind.JS,
|
||||
getScriptVersion: (fileName: string) => {
|
||||
if (fileName === workingFile) {
|
||||
@@ -51,12 +46,10 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache<HTMLDocume
|
||||
},
|
||||
getScriptSnapshot: (fileName: string) => {
|
||||
let text = '';
|
||||
if (startsWith(fileName, 'vscode:')) {
|
||||
if (fileName === workingFile) {
|
||||
text = currentTextDocument.getText();
|
||||
}
|
||||
if (fileName === workingFile) {
|
||||
text = currentTextDocument.getText();
|
||||
} else {
|
||||
text = ts.sys.readFile(fileName) || '';
|
||||
text = loadLibrary(fileName);
|
||||
}
|
||||
return {
|
||||
getText: (start, end) => text.substring(start, end),
|
||||
@@ -65,8 +58,7 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache<HTMLDocume
|
||||
};
|
||||
},
|
||||
getCurrentDirectory: () => '',
|
||||
getDefaultLibFileName: (options) => ts.getDefaultLibFilePath(options),
|
||||
|
||||
getDefaultLibFileName: (_options: ts.CompilerOptions) => 'es6'
|
||||
};
|
||||
let jsLanguageService = ts.createLanguageService(host);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user