Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection is lost after save #292

Open
fregante opened this issue Aug 21, 2023 · 4 comments
Open

Connection is lost after save #292

fregante opened this issue Aug 21, 2023 · 4 comments

Comments

@fregante
Copy link
Owner

fregante commented Aug 21, 2023

I don't remember if this is a consequence of VSCode's document management, but it's not a desired behavior. We're used to saving often so it should not trigger connection closure when one mistakenly saves a GhostText document.

Extracted from:

@kevintraver
Copy link

kevintraver commented Aug 21, 2023

Ok, I think I figured out why this is happening:

When the file is created it is using an untitled scheme

https://github.com/fregante/GhostText-for-VSCode/blob/d414e46f25df9e78f8a8c126e9fc913dadff2f55/source/extension.ts#L37-L40

const file = vscode.Uri.from({
  scheme: 'untitled',
  path: `${tmpdir()}/${avoidsOverlappingFiles}/${filename}`,
});

from the documentation for onDidCloseTextDocument :

An event that is emitted when a text document is disposed or when the language id of a text document has been changed.

The onDidCloseTextDocument event is being triggered on save because the untitled document is "closed" when it gets saved to disk and becomes a file document.

@kevintraver
Copy link

kevintraver commented Aug 21, 2023

A possible solution:

async function onDocumentClose(closedDocument: vscode.TextDocument) {
  if (closedDocument.uri.scheme === 'untitled') {
    // This is an untitled document. Skip further logic.
    return;
  }
  if (closedDocument.isClosed) {
    documents.delete(closedDocument.uri.toString());
  }
}

the other issue is that the closedDocument.uri is different between when it's unsaved vs saved to disk

untitled:/var/folders/br/l8jb39_j2sl_8mrr859y01_00000gn/

vs

file:///var/folders/br/l8jb39_j2sl_8mrr859y01_00000gn/

@fregante
Copy link
Owner Author

VSCode's document management is a joke. I think I just need to avoid using untitled://

This was also mentioned in another issue: autosave to avoid closure prompts

@Moulick
Copy link

Moulick commented Feb 27, 2024

Can this be something like how git rebase --interactive does it? I have exported EDITOR="code --wait --add" on my terminal. When I run git rebase --interactive, it creates a file and then tells vscode to open that file. Saving the file works fine, and on closing the editor it somehow triggers continue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants