From ff09e5998557f44ebb23dab4e8bae5540e0af449 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Tue, 2 Oct 2018 07:14:10 +0100 Subject: [PATCH 1/3] support bulk libs setup --- src/monaco.contribution.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/monaco.contribution.ts b/src/monaco.contribution.ts index edde6228..e5373f72 100644 --- a/src/monaco.contribution.ts +++ b/src/monaco.contribution.ts @@ -61,6 +61,34 @@ export class LanguageServiceDefaultsImpl implements monaco.languages.typescript. }; } + setExtraLibs(libs: Array<{ content: string; filePath?: string }> = []): IDisposable { + const paths = []; + + if (libs && libs.length > 0) { + libs.forEach(lib => { + const filePath = lib.filePath || `ts:extralib-${Date.now()}`; + const content = lib.content; + + this._extraLibs[filePath] = content; + paths.push(filePath); + }); + + this._onDidChange.fire(this); + } + + return { + dispose: () => { + if (paths.length > 0) { + paths.forEach(filePath => { + delete this._extraLibs[filePath]; + }); + + this._onDidChange.fire(this); + } + } + } + } + getCompilerOptions(): monaco.languages.typescript.CompilerOptions { return this._compilerOptions; } From c6c45135c2229d89e9545f83ced3695762e44a59 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Tue, 2 Oct 2018 07:19:43 +0100 Subject: [PATCH 2/3] definition and docs --- src/monaco.contribution.ts | 2 +- src/monaco.d.ts | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/monaco.contribution.ts b/src/monaco.contribution.ts index e5373f72..9700c5a7 100644 --- a/src/monaco.contribution.ts +++ b/src/monaco.contribution.ts @@ -61,7 +61,7 @@ export class LanguageServiceDefaultsImpl implements monaco.languages.typescript. }; } - setExtraLibs(libs: Array<{ content: string; filePath?: string }> = []): IDisposable { + setExtraLibs(libs: Array<{ content: string; filePath?: string }>): IDisposable { const paths = []; if (libs && libs.length > 0) { diff --git a/src/monaco.d.ts b/src/monaco.d.ts index 35806f17..145efe61 100644 --- a/src/monaco.d.ts +++ b/src/monaco.d.ts @@ -135,11 +135,23 @@ declare module monaco.languages.typescript { * * @param content The file content * @param filePath An optional file path - * @returns A disposabled which will remove the file from the - * language service upon disposal. + * @returns A disposable which will remove the file from the + * language service upon cleanup. */ addExtraLib(content: string, filePath?: string): IDisposable; + /** + * Add multiple source files to the language service. + * Use this for multiple typescript (definition) files that won't be loaded + * as editor document, like `jquery.d.ts`. + * This method is optimised for performance and raises change events only once + * for the whole list. + * @param libs An array of entries to register. + * @returns A disposable which will remove the file from the + * language service upon cleanup. + */ + setExtraLibs(libs: Array<{ content: string; filePath?: string }>): IDisposable; + /** * Set TypeScript compiler options. */ From e556e53389ab97e4aad5f21b6b002779c418926e Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Tue, 2 Oct 2018 08:53:32 +0100 Subject: [PATCH 3/3] raise onDidChange only when needed --- src/monaco.contribution.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/monaco.contribution.ts b/src/monaco.contribution.ts index 9700c5a7..b58c296f 100644 --- a/src/monaco.contribution.ts +++ b/src/monaco.contribution.ts @@ -79,11 +79,17 @@ export class LanguageServiceDefaultsImpl implements monaco.languages.typescript. return { dispose: () => { if (paths.length > 0) { + let changed = false; + paths.forEach(filePath => { - delete this._extraLibs[filePath]; + if (delete this._extraLibs[filePath]) { + changed = true; + } }); - this._onDidChange.fire(this); + if (changed) { + this._onDidChange.fire(this); + } } } }