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

TextDocument.save() closes untitled documents #29156

Closed
itowlson opened this issue Jun 21, 2017 · 6 comments
Closed

TextDocument.save() closes untitled documents #29156

itowlson opened this issue Jun 21, 2017 · 6 comments
Labels
extensions Issues concerning extensions feature-request Request for new features or functionality

Comments

@itowlson
Copy link

I am developing an extension which runs a shell command on the active document. For this to work I need to save the active document first. I can do this using TextDocument.save(). However, if the user has created a new, untitled document, then save() closes the document. In addition, the document's fileName is not updated to reflect the save path.

This is related to #25729 but the workaround described there does not work for me because I do not control the creation of the untitled file, and there seems to be no way to get the file path after the save. So I cannot use that to reopen the document or pass its file path to my command line.


Steps to Reproduce:

  1. Clone the repository https://github.com/itowlson/vscode-sauve-qui-peut, or create a new repository and copy in the following code from https://github.com/itowlson/vscode-sauve-qui-peut/blob/master/src/extension.ts and hook it up to a command:
async function withActiveDocument(action: (doc : vscode.TextDocument) => Promise<void>) {
    const activeEditor = vscode.window.activeTextEditor;
    if (!activeEditor) {
        return;
    }

    const doc = activeEditor.document;
    if (!doc) {
        return;
    }

    await action(doc);
}


async function trySave(doc : vscode.TextDocument) : Promise<void> {
    if (doc.isUntitled) {
        const saved = await doc.save();
        vscode.window.showInformationMessage(`Document ${saved ? "was" : "was not"} saved. Check document is still open.`);
    } else {
        vscode.window.showErrorMessage('Please test on an unsaved document');
    }
}

(The problem still occurs without the asyncs and awaits, though it fails in a slightly different way if you inline the save call in the command handler instead of calling via withActiveDocument.)

  1. Run the extension in the Extension Development Host.

  2. Hit Ctrl+N to create a new untitled file. Type some garbage into the file.

  3. Run the contributed command (e.g. Ctrl+Shift+P > Sauve Qui Peut).

  4. At the save prompt, enter a file name and hit OK.

Expected behaviour: The document remains open in the editor, and its fileName is updated to reflect the save path.

Actual behaviour: The document disappears out of the editor. If the document object is inspected in the debugger, its fileName is still "Untitled-1".


  • VSCode Version: Code 1.13.1 (379d2ef, 2017-06-14T18:21:47.485Z)
  • OS Version: Windows_NT ia32 10.0.16199
  • Extensions:
Extension Author Version
jslint ajhyndman 1.2.1
vs-kubernetes brendandburns 0.0.8
jshint dbaeumer 0.10.15
vscode-eslint dbaeumer 1.2.11
python donjayamanne 0.6.5
EditorConfig EditorConfig 0.9.3
tslint eg2 0.15.0
Go lukehoban 0.6.62
swagger-doc-viewer mimarec 1.0.4
csharp ms-vscode 1.10.0
debugger-for-chrome msjsdiag 3.1.4
vscode-docker PeterJausovec 0.0.16
Ruby rebornix 0.12.1
stylelint shinnn 0.25.0
@bpasero
Copy link
Member

bpasero commented Jun 21, 2017

I see now way how to support this because our model with untitled:// scheme changes to a model with file:// scheme, so we dispose the old model and create a new model. Today we have no way to change the resource of existing models.

/cc @jrieken

@bpasero bpasero added the extensions Issues concerning extensions label Jun 21, 2017
@bpasero bpasero added this to the Backlog milestone Jun 21, 2017
@jrieken jrieken removed their assignment Jun 21, 2017
@itowlson
Copy link
Author

Is there a workaround? E.g. a way to get the new file path so I can re-open the document? At the moment it seems my options are:

  1. Prompt the user to save manually, and refuse to continue with the command until they do, which makes the extension feel hostile.
  2. Save the document content to a temporary file and use that, which is inconsistent with what I do on named documents.

@bpasero bpasero added the feature-request Request for new features or functionality label Nov 6, 2017
@bpasero bpasero removed their assignment Nov 6, 2017
@paustint
Copy link

paustint commented Mar 9, 2019

I would like the ability to handle this use-case as well.

Here is my use-case:

  1. User opens a new untitled document
  2. User calls my command, which will push the document to a remote resource (requiring the name of the file for the remote resource)
  3. the extension calls window.activeTextEditor.document.save()
  4. User is presented with a dialog to save

From here the file closes, there is no programatic way to know what the filename/path the user has chosen, so I cannot send the file to the remote resource.

For now I will just require that the user save the file prior to calling the command, but it would be ideal to be able to support this case.

@bpasero bpasero modified the milestones: Backlog, Backlog Candidates Nov 17, 2019
@vscodebot
Copy link

vscodebot bot commented Jan 16, 2020

This feature request is now a candidate for our backlog. The community has 60 days to upvote the issue. If it receives 20 upvotes we will move it to our backlog. If not, we will close it. To learn more about how we handle feature requests, please see our documentation.

Happy Coding!

@vscodebot
Copy link

vscodebot bot commented Mar 6, 2020

This feature request has not yet received the 20 community upvotes it takes to make to our backlog. 10 days to go. To learn more about how we handle feature requests, please see our documentation.

Happy Coding

@vscodebot
Copy link

vscodebot bot commented Mar 16, 2020

🙁 In the last 60 days, this feature request has received less than 20 community upvotes and we closed it. Still a big Thank You to you for taking the time to create this issue! To learn more about how we handle feature requests, please see our documentation.

Happy Coding!

@vscodebot vscodebot bot closed this as completed Mar 16, 2020
@github-actions github-actions bot locked and limited conversation to collaborators May 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
extensions Issues concerning extensions feature-request Request for new features or functionality
Projects
None yet
Development

No branches or pull requests

5 participants