Skip to content

add option to close editor window on save - #15

Closed
kevintraver wants to merge 2 commits into
fregante:mainfrom
kevintraver:fix-close-on-save
Closed

kevintraver wants to merge 2 commits into
fregante:mainfrom
kevintraver:fix-close-on-save

Conversation

@kevintraver

Copy link
Copy Markdown

Ensure text editor closes programmatically on document save, eliminating the need to manually close lingering windows.

Optimized the `onDocumentClose` function in the VSCode extension to manage the programmatic closure of text editors more effectively.

Changes:
- Moved the `isAutoClosingEditor` check to the top, ensuring early exit if an editor closure is programmatic.
- The `isAutoClosingEditor` flag is now set immediately before the programmatic closure of the editor, ensuring precise tracking of such events.

This refactor enhances the reliability of the editor closure mechanism and simplifies the logic flow for better maintainability.
@kevintraver

Copy link
Copy Markdown
Author

Ok, looking this over more, it seems like there is no easy way to determine which of these scenarios actually trigger the onDidCloseTextDocument event:

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

@fregante

fregante commented Aug 21, 2023 •

Copy link
Copy Markdown
Owner

As mentioned:

  • the disconnection on save is a bug and will be fixed
  • autoclosing the editor will not be the default, but it can be an option

What you want to listen to is the disconnection event specifically (see the socket.on('close' line) and then add one more option to the extension.

If you are already in the editor, you can disconnect by closing the tab, so I'm not sure this will get you what you want in the end.

@kevintraver

Copy link
Copy Markdown
Author

Ok, what about something like this:

set a new configuration option

function getCloseOnsave(): boolean {
  return vscode.workspace.getConfiguration('ghostText').get('closeOnSave') || false;
}

set up a new event listener for onDidSaveTextDocument

vscode.workspace.onDidSaveTextDocument(onDocumentSave, ...setup);

and then close on save

async function onDocumentSave(savedDocument: vscode.TextDocument) {
  const field = documents.get(savedDocument.uri.toString());
  if (!field) {
    return;
  }
  if (getCloseOnsave()) {
    await vscode.commands.executeCommand('workbench.action.closeActiveEditor')
  }
}

@kevintraver

Copy link
Copy Markdown
Author

The only problem I'm having is documents does not seem to be available in onDocumentSave, so I can't match the field

@fregante

Copy link
Copy Markdown
Owner

set up a new event listener for onDidSaveTextDocument

No. It's a bug and it will be fixed.

@fregante fregante changed the title Update close editor logic on save Close editor on disconnection Aug 21, 2023
@kevintraver kevintraver changed the title Close editor on disconnection add option to close editor window on save Aug 21, 2023
@kevintraver

Copy link
Copy Markdown
Author

I'll go ahead and close this out.

@kevintraver kevintraver closed this Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants