Skip to content
This repository was archived by the owner on Nov 5, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 25 additions & 92 deletions scripts/importTypescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,110 +101,43 @@ export = ts;
})();

function importLibs() {
function getFileName(name) {
return (name === '' ? 'lib.d.ts' : `lib.${name}.d.ts`);
}
function getVariableName(name) {
return (name === '' ? 'lib_dts' : `lib_${name.replace(/\./g, '_')}_dts`);
}
function readLibFile(name) {
var srcPath = path.join(TYPESCRIPT_LIB_SOURCE, getFileName(name));
var srcPath = path.join(TYPESCRIPT_LIB_SOURCE, name);
return fs.readFileSync(srcPath).toString();
}

var queue = [];
var in_queue = {};

var enqueue = function (name) {
if (in_queue[name]) {
return;
}
in_queue[name] = true;
queue.push(name);
};

enqueue('');
enqueue('es2015');

var result = [];
while (queue.length > 0) {
var name = queue.shift();
var contents = readLibFile(name);
var lines = contents.split(/\r\n|\r|\n/);

var output = '';
var writeOutput = function (text) {
if (output.length === 0) {
output = text;
} else {
output += ` + ${text}`;
}
};
var outputLines = [];
var flushOutputLines = function () {
writeOutput(`"${escapeText(outputLines.join('\n'))}"`);
outputLines = [];
};
var deps = [];
for (let i = 0; i < lines.length; i++) {
let m = lines[i].match(/\/\/\/\s*<reference\s*lib="([^"]+)"/);
if (m) {
flushOutputLines();
writeOutput(getVariableName(m[1]));
deps.push(getVariableName(m[1]));
enqueue(m[1]);
continue;
}
outputLines.push(lines[i]);
}
flushOutputLines();

result.push({
name: getVariableName(name),
deps: deps,
output: output
});
}

var strResult = `/*---------------------------------------------------------------------------------------------
var strLibResult = `/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
${generatedNote}`;
// Do a topological sort
while (result.length > 0) {
for (let i = result.length - 1; i >= 0; i--) {
if (result[i].deps.length === 0) {
// emit this node
strResult += `\nexport const ${result[i].name}: string = ${result[i].output};\n`;

// mark dep as resolved
for (let j = 0; j < result.length; j++) {
for (let k = 0; k < result[j].deps.length; k++) {
if (result[j].deps[k] === result[i].name) {
result[j].deps.splice(k, 1);
break;
}
}
}
${generatedNote}

// remove from result
result.splice(i, 1);
break;
}
}
}
/** Contains all the lib files */
export const libFileMap: Record<string, string> = {}
`
;

strResult += `
/** This is the DTS which is used when the target is ES6 or below */
export const lib_es5_bundled_dts = lib_dts;
var strIndexResult = `/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
${generatedNote}

/** This is the DTS which is used by default in monaco-typescript, and when the target is 2015 or above */
export const lib_es2015_bundled_dts = lib_es2015_dts + "" + lib_dom_dts + "" + lib_webworker_importscripts_dts + "" + lib_scripthost_dts + "";
/** Contains all the lib files */
export const libFileSet: Record<string, boolean> = {}
`
;

var dtsFiles = fs.readdirSync(TYPESCRIPT_LIB_SOURCE).filter(f => f.includes("lib."));
while (dtsFiles.length > 0) {
var name = dtsFiles.shift();
var output = readLibFile(name).replace(/\r\n/g, '\n');
strLibResult += `libFileMap['${name}'] = "${escapeText(output)}";\n`;
strIndexResult += `libFileSet['${name}'] = true;\n`;
}

var dstPath = path.join(TYPESCRIPT_LIB_DESTINATION, 'lib.ts');
fs.writeFileSync(dstPath, strResult);
fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'lib.ts'), strLibResult);
fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'lib.index.ts'), strIndexResult);
}

/**
Expand Down
139 changes: 126 additions & 13 deletions src/languageFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { LanguageServiceDefaultsImpl } from './monaco.contribution';
import * as ts from './lib/typescriptServices';
import { TypeScriptWorker } from './tsWorker';
import { libFileSet } from "./lib/lib.index"

import Uri = monaco.Uri;
import Position = monaco.Position;
Expand Down Expand Up @@ -58,7 +59,7 @@ function displayPartsToString(displayParts: ts.SymbolDisplayPart[] | undefined):

export abstract class Adapter {

constructor(protected _worker: (first: Uri, ...more: Uri[]) => Promise<TypeScriptWorker>) {
constructor(protected _worker: (...uris: Uri[]) => Promise<TypeScriptWorker>) {
}

// protected _positionToOffset(model: monaco.editor.ITextModel, position: monaco.IPosition): number {
Expand All @@ -78,6 +79,75 @@ export abstract class Adapter {
}
}

// --- lib files

export class LibFiles {

private _libFiles: Record<string, string>;
private _hasFetchedLibFiles: boolean;
private _fetchLibFilesPromise: Promise<void> | null;

constructor(
private readonly _worker: (...uris: Uri[]) => Promise<TypeScriptWorker>
) {
this._libFiles = {};
this._hasFetchedLibFiles = false;
this._fetchLibFilesPromise = null;
}

public isLibFile(uri: Uri | null): boolean {
if (!uri) {
return false;
}
if (uri.path.indexOf("/lib.") === 0) {
return !!libFileSet[uri.path.slice(1)];
}
return false;
}

public getOrCreateModel(uri: Uri): monaco.editor.ITextModel | null {
const model = monaco.editor.getModel(uri);
if (model) {
return model;
}
if (this.isLibFile(uri) && this._hasFetchedLibFiles) {
return monaco.editor.createModel(this._libFiles[uri.path.slice(1)], "javascript", uri);
}
return null;
}

private _containsLibFile(uris: (Uri | null)[]): boolean {
for (let uri of uris) {
if (this.isLibFile(uri)) {
return true;
}
}
return false;
}

public async fetchLibFilesIfNecessary(uris: (Uri | null)[]): Promise<void> {
if (!this._containsLibFile(uris)) {
// no lib files necessary
return;
}
await this._fetchLibFiles();
}

private _fetchLibFiles(): Promise<void> {
if (!this._fetchLibFilesPromise) {
this._fetchLibFilesPromise = (
this._worker()
.then(w => w.getLibFiles())
.then((libFiles) => {
this._hasFetchedLibFiles = true;
this._libFiles = libFiles;
})
);
}
return this._fetchLibFilesPromise;
}
}

// --- diagnostics --- ---

enum DiagnosticCategory {
Expand All @@ -92,8 +162,11 @@ export class DiagnosticsAdapter extends Adapter {
private _disposables: IDisposable[] = [];
private _listener: { [uri: string]: IDisposable } = Object.create(null);

constructor(private _defaults: LanguageServiceDefaultsImpl, private _selector: string,
worker: (first: Uri, ...more: Uri[]) => Promise<TypeScriptWorker>
constructor(
private readonly _libFiles: LibFiles,
private _defaults: LanguageServiceDefaultsImpl,
private _selector: string,
worker: (...uris: Uri[]) => Promise<TypeScriptWorker>
) {
super(worker);

Expand Down Expand Up @@ -180,19 +253,31 @@ export class DiagnosticsAdapter extends Adapter {
promises.push(worker.getSuggestionDiagnostics(model.uri.toString()));
}

const diagnostics = await Promise.all(promises);
const allDiagnostics = await Promise.all(promises);

if (!diagnostics || model.isDisposed()) {
if (!allDiagnostics || model.isDisposed()) {
// model was disposed in the meantime
return;
}

const markers = diagnostics
const diagnostics = allDiagnostics
.reduce((p, c) => c.concat(p), [])
.filter(d => (this._defaults.getDiagnosticsOptions().diagnosticCodesToIgnore || []).indexOf(d.code) === -1)
.map(d => this._convertDiagnostics(model, d));
.filter(d => (this._defaults.getDiagnosticsOptions().diagnosticCodesToIgnore || []).indexOf(d.code) === -1);

monaco.editor.setModelMarkers(model, this._selector, markers);
// Fetch lib files if necessary
const relatedUris = diagnostics
.map(d => d.relatedInformation || [])
.reduce((p, c) => c.concat(p), [])
.map(relatedInformation => relatedInformation.file ? monaco.Uri.parse(relatedInformation.file.fileName) : null);

await this._libFiles.fetchLibFilesIfNecessary(relatedUris);

if (model.isDisposed()) {
// model was disposed in the meantime
return;
}

monaco.editor.setModelMarkers(model, this._selector, diagnostics.map(d => this._convertDiagnostics(model, d)));
}

private _convertDiagnostics(model: monaco.editor.ITextModel, diag: ts.Diagnostic): monaco.editor.IMarkerData {
Expand Down Expand Up @@ -224,7 +309,7 @@ export class DiagnosticsAdapter extends Adapter {
let relatedResource: monaco.editor.ITextModel | null = model;
if (info.file) {
const relatedResourceUri = monaco.Uri.parse(info.file.fileName);
relatedResource = monaco.editor.getModel(relatedResourceUri);
relatedResource = this._libFiles.getOrCreateModel(relatedResourceUri);
}

if (!relatedResource) {
Expand Down Expand Up @@ -479,6 +564,13 @@ export class OccurrencesAdapter extends Adapter implements monaco.languages.Docu

export class DefinitionAdapter extends Adapter {

constructor(
private readonly _libFiles: LibFiles,
worker: (...uris: Uri[]) => Promise<TypeScriptWorker>
) {
super(worker);
}

public async provideDefinition(model: monaco.editor.ITextModel, position: Position, token: CancellationToken): Promise<monaco.languages.Definition | undefined> {
const resource = model.uri;
const offset = model.getOffsetAt(position);
Expand All @@ -489,10 +581,17 @@ export class DefinitionAdapter extends Adapter {
return;
}

// Fetch lib files if necessary
await this._libFiles.fetchLibFilesIfNecessary(entries.map(entry => Uri.parse(entry.fileName)));

if (model.isDisposed()) {
return;
}

const result: monaco.languages.Location[] = [];
for (let entry of entries) {
const uri = Uri.parse(entry.fileName);
const refModel = monaco.editor.getModel(uri);
const refModel = this._libFiles.getOrCreateModel(uri);
if (refModel) {
result.push({
uri: uri,
Expand All @@ -508,6 +607,13 @@ export class DefinitionAdapter extends Adapter {

export class ReferenceAdapter extends Adapter implements monaco.languages.ReferenceProvider {

constructor(
private readonly _libFiles: LibFiles,
worker: (...uris: Uri[]) => Promise<TypeScriptWorker>
) {
super(worker);
}

public async provideReferences(model: monaco.editor.ITextModel, position: Position, context: monaco.languages.ReferenceContext, token: CancellationToken): Promise<monaco.languages.Location[] | undefined> {
const resource = model.uri;
const offset = model.getOffsetAt(position);
Expand All @@ -518,10 +624,17 @@ export class ReferenceAdapter extends Adapter implements monaco.languages.Refere
return;
}

// Fetch lib files if necessary
await this._libFiles.fetchLibFilesIfNecessary(entries.map(entry => Uri.parse(entry.fileName)));

if (model.isDisposed()) {
return;
}

const result: monaco.languages.Location[] = [];
for (let entry of entries) {
const uri = Uri.parse(entry.fileName);
const refModel = monaco.editor.getModel(uri);
const refModel = this._libFiles.getOrCreateModel(uri);
if (refModel) {
result.push({
uri: uri,
Expand Down Expand Up @@ -701,7 +814,7 @@ export class CodeActionAdaptor extends FormatHelper implements monaco.languages.
const codeFixes = await worker.getCodeFixesAtPosition(resource.toString(), start, end, errorCodes, formatOptions);

if (!codeFixes || model.isDisposed()) {
return { actions: [], dispose:() => {} };
return { actions: [], dispose: () => { } };
}

const actions = codeFixes.filter(fix => {
Expand Down
Loading