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 b7b9669 commit 408322b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
21 changes: 19 additions & 2 deletions client/src/language/EmbeddedLanguageDocsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,19 @@ type EmbeddedLanguageDocsRecord = Partial<Record<EmbeddedLanguageType, EmbeddedL
export default class EmbeddedLanguageDocsManager {
private readonly embeddedLanguageDocsInfos = new Map<string, EmbeddedLanguageDocsRecord>() // map of original uri to embedded documents infos
private _storagePath: string | undefined
private readonly filesWaitingToUpdate = new Map<string, EmbeddedLanguageDoc>()

get storagePath (): string | undefined {
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 +112,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 All @@ -124,6 +132,10 @@ export default class EmbeddedLanguageDocsManager {

private async updateEmbeddedLanguageDocFile (embeddedLanguageDoc: EmbeddedLanguageDoc, uri: Uri): Promise<void> {
const document = await workspace.openTextDocument(uri)
if (document.isDirty) {
this.filesWaitingToUpdate.set(uri.toString(), embeddedLanguageDoc)
return
}
const fullRange = new Range(
document.positionAt(0),
document.positionAt(document.getText().length)
Expand All @@ -133,6 +145,11 @@ export default class EmbeddedLanguageDocsManager {
await workspace.applyEdit(workspaceEdit)
await document.save()
this.registerEmbeddedLanguageDocInfos(embeddedLanguageDoc, uri)
const fileWaitingToUpdate = this.filesWaitingToUpdate.get(uri.toString())
if (fileWaitingToUpdate !== undefined) {
this.filesWaitingToUpdate.delete(uri.toString())
await this.updateEmbeddedLanguageDocFile(fileWaitingToUpdate, uri)
}
}

private async createEmbeddedLanguageDocFile (embeddedLanguageDoc: EmbeddedLanguageDoc): Promise<void> {
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 @@ -114,6 +115,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 408322b

Please sign in to comment.