From 67e71fbe4a18d95fe427efcf39c43a2046d97a05 Mon Sep 17 00:00:00 2001 From: idillon Date: Wed, 3 Jan 2024 19:21:49 -0500 Subject: [PATCH] Chore: Close embedded document tabs as they apear --- .../language/EmbeddedLanguageDocsManager.ts | 11 ++++++-- client/src/language/languageClient.ts | 25 ++++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/client/src/language/EmbeddedLanguageDocsManager.ts b/client/src/language/EmbeddedLanguageDocsManager.ts index 841b8f49e..9211c9bd2 100644 --- a/client/src/language/EmbeddedLanguageDocsManager.ts +++ b/client/src/language/EmbeddedLanguageDocsManager.ts @@ -34,6 +34,13 @@ export default class EmbeddedLanguageDocsManager { return this._storagePath } + get embeddedLanguageDocsFolder (): string | undefined { + if (this._storagePath === undefined) { + return + } + return path.join(this._storagePath, EMBEDDED_DOCUMENTS_FOLDER) + } + async setStoragePath (newStoragePath: string | undefined): Promise { logger.debug(`Set embedded language documents storage path. New: ${newStoragePath}. Old: ${this._storagePath}`) if (this._storagePath === newStoragePath) { @@ -104,13 +111,13 @@ export default class EmbeddedLanguageDocsManager { } private createEmbeddedLanguageDocUri (embeddedLanguageDoc: EmbeddedLanguageDoc): Uri | undefined { - if (this.storagePath === undefined) { + if (this.embeddedLanguageDocsFolder === undefined) { return undefined } const randomName = randomUUID() const fileExtension = fileExtensionsMap[embeddedLanguageDoc.language] const embeddedLanguageDocFilename = randomName + fileExtension - const pathToEmbeddedLanguageDocsFolder = path.join(this.storagePath, EMBEDDED_DOCUMENTS_FOLDER) + const pathToEmbeddedLanguageDocsFolder = this.embeddedLanguageDocsFolder return Uri.parse(`file://${pathToEmbeddedLanguageDocsFolder}/${embeddedLanguageDocFilename}`) } diff --git a/client/src/language/languageClient.ts b/client/src/language/languageClient.ts index 06362066b..64b603629 100644 --- a/client/src/language/languageClient.ts +++ b/client/src/language/languageClient.ts @@ -10,7 +10,8 @@ import { type ExtensionContext, window, commands, - languages + languages, + TabInputText } from 'vscode' import { @@ -112,6 +113,28 @@ export async function activateLanguageServer (context: ExtensionContext): Promis void embeddedLanguageDocsManager.saveEmbeddedLanguageDocs(embeddedLanguageDocs) }) + window.tabGroups.onDidChangeTabs((event) => { + [...event.opened, ...event.changed].forEach((tab) => { + if (tab.input instanceof TabInputText) { + const uri = tab.input.uri + if (embeddedLanguageDocsManager.embeddedLanguageDocsFolder === undefined) { + return + } + // Close embedded document tabs when they open automatically + if (uri.fsPath.includes(embeddedLanguageDocsManager.embeddedLanguageDocsFolder)) { + if ( + // Prevent prompt to appear on unsaved files + !tab.isDirty && + // Make possible to open embedded documents in a tab + !tab.isPreview && !tab.isActive && !tab.isPinned + ) { + void window.tabGroups.close(tab, false) + } + } + } + }) + }) + // Start the client and launch the server await client.start()