Browse Source

register multiple extra libs at once (#24)

register multiple extra libs at once
Alexandru Dima 5 năm trước cách đây
mục cha
commit
72321822f4
2 tập tin đã thay đổi với 27 bổ sung1 xóa
  1. 18 0
      src/monaco.contribution.ts
  2. 9 1
      src/monaco.d.ts

+ 18 - 0
src/monaco.contribution.ts

@@ -93,6 +93,24 @@ export class LanguageServiceDefaultsImpl implements monaco.languages.typescript.
 		};
 	}
 
+	setExtraLibs(libs: { content: string; filePath?: string }[]): void {
+		// clear out everything
+		this._extraLibs = Object.create(null);
+
+		if (libs && libs.length > 0) {
+			for (const lib of libs) {
+				const filePath = lib.filePath || `ts:extralib-${Math.random().toString(36).substring(2, 15)}`;
+				const content = lib.content;
+				this._extraLibs[filePath] = {
+					content: content,
+					version: 1
+				};
+			}
+		}
+
+		this._fireOnDidExtraLibsChangeSoon();
+	}
+
 	private _fireOnDidExtraLibsChangeSoon(): void {
 		if (this._onDidExtraLibsChangeTimeout !== -1) {
 			// already scheduled

+ 9 - 1
src/monaco.d.ts

@@ -136,7 +136,7 @@ declare module monaco.languages.typescript {
         /**
          * Add an additional source file to the language service. Use this
          * for typescript (definition) files that won't be loaded as editor
-         * document, like `jquery.d.ts`.
+         * documents, like `jquery.d.ts`.
          *
          * @param content The file content
          * @param filePath An optional file path
@@ -145,6 +145,14 @@ declare module monaco.languages.typescript {
          */
         addExtraLib(content: string, filePath?: string): IDisposable;
 
+        /**
+         * Remove all existing extra libs and set the additional source
+         * files to the language service. Use this for typescript definition
+         * files that won't be loaded as editor documents, like `jquery.d.ts`.
+         * @param libs An array of entries to register.
+         */
+        setExtraLibs(libs: { content: string; filePath?: string }[]): void;
+
         /**
          * Set TypeScript compiler options.
          */