Skip to content

Commit

Permalink
Chore: Close embedded document tabs as they apear
Browse files Browse the repository at this point in the history
  • Loading branch information
idillon-sfl committed Jan 4, 2024
1 parent 2b290a5 commit 67e71fb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
11 changes: 9 additions & 2 deletions client/src/language/EmbeddedLanguageDocsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
logger.debug(`Set embedded language documents storage path. New: ${newStoragePath}. Old: ${this._storagePath}`)
if (this._storagePath === newStoragePath) {
Expand Down Expand Up @@ -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}`)
}

Expand Down
25 changes: 24 additions & 1 deletion client/src/language/languageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
type ExtensionContext,
window,
commands,
languages
languages,
TabInputText
} from 'vscode'

import {
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 67e71fb

Please sign in to comment.