Skip to content

API: Extra events like onPreSaveTextDocument? #239

Closed

Description

It would be nice to have some extra events I guess. The first one I can think of is onPreSaveTextDocument (or onSaveTextDocument). One that will be invoked right before the document gets saved.

Use Cases

The first one I can think of is my own plugin final-newline that will insert a final newline at the end of the file when the user saves the document. At the moment it is implemented like this: Cmd + s → Save → Insert newline (if necessary) → Save
That last save triggers a new iteration of my plugin which will detect a final newline is already present and will stop the execution. So my loop is triggered twice because the plugin is triggered after the document was saved instead of before.

I think there will be other file manipulation plugins that want to manipulate the file before it gets saved as well.

From Alex:

I agree some sort of onBeforeSave action is indeed needed, we just need more thinking regarding how to expose this without causing any data loss. If we block the save to wait on extensions, I can imagine it possible that the wait could be indefinite, resulting in data loss. Maybe a time ceiling of 10s, after which the file gets saved...

Benjamin Pasero (@bpasero) What do you think?

Activity

  1. added
    feature-request
    on Nov 19, 2015
  2. lukehoban commented on Nov 22, 2015

    @lukehoban
    Member

    The top requested feature for the Go extension is a format-on-save option (microsoft/vscode-go#14).

    This really begs for being invoked on a pre-save event.

    Without it, we have to do some sort of double-save with bookkeeping to make sure the first save doesn't re-trigger the post save event. The best I have managed is this: https://github.com/Microsoft/vscode-go/blob/formatonsave/src/goMain.ts#L99-L118. However, the double-save flashes the (unsaved) marker in the editor and I'm sure there are other issues with this approach.

    It would be great to have a pre-save hook to use instead.

  3. bpasero commented on Nov 23, 2015

    @bpasero
    ContributorAuthor

    We have a FILE_SAVING event but a) it is not exposed to extensions and b) it currently expects modifications to happen sync and not async, which would be a requirement when we talk from editor to extensions.

  4. jrieken commented on Apr 4, 2016

    @jrieken
    Contributor
  5. dbaeumer commented on Apr 5, 2016

    @dbaeumer
    Member

    Benjamin Pasero (@bpasero) is this something we can work on. I would like to hook up auto fix support for ESLint and doing this before save would really make a good story.

  6. bpasero commented on Apr 5, 2016

    @bpasero
    ContributorAuthor

    Yeah, stretch for April, otherwise May.

  7. mysticatea commented on Jun 1, 2016

    @mysticatea

    What is happening on this?

  8. jaime-olivares commented on Jul 12, 2016

    @jaime-olivares

    So, it was planned for April, and we are July. Is this already included in VSCode 1.3?

  9. mysticatea commented on Jul 20, 2016

    @mysticatea

    Benjamin Pasero (@bpasero) Dirk Bäumer (@dbaeumer) Is there plan to implement this? This is blocking the feature of auto-fixing on save for several extensions. 😕

  10. 37 remaining items

  11. SamVerschueren commented on Sep 22, 2016

    @SamVerschueren
    Contributor

    Danny Tuppeny (@DanTup) Good point!

    Johannes Rieken (@jrieken) Yes it was just something that came up in my mind that I wanted to share. You have a valid point there. Your solution would be nice though! Keeping track of extensions that misbehave and disable/uninstall them.

  12. jrieken commented on Sep 23, 2016

    @jrieken
    Contributor

    Benjamin Pasero (@bpasero) Thinking of adding source or reason property to the event. Is it possible to expose the different save reasons to the participants? Like explicit (cmd+s, menu item), focuson, window change, auto? Given we need that information in our participants it's likely consumers of this API need it as well

  13. bpasero commented on Sep 23, 2016

    @bpasero
    ContributorAuthor

    Johannes Rieken (@jrieken) I think that is easy to add because a user can only have 1 auto save method enabled. If the participant checks for the isAutoSaved property, I think all the info is there:

  14. jrieken commented on Sep 23, 2016

    @jrieken
    Contributor

    false: it can only be a manual save

    Actually not. I see isAutoSaved as false when using save on focus out. Which I like btw because it makes a nice format on focus lost experience

  15. bpasero commented on Sep 23, 2016

    @bpasero
    ContributorAuthor

    Johannes Rieken (@jrieken) ah yes, I can look into adding this info and then ping you how to expose it to the participant. it needs to be wired in from some places

  16. jrieken commented on Sep 23, 2016

    @jrieken
    Contributor

    k

  17. bpasero commented on Sep 26, 2016

    @bpasero
    ContributorAuthor

    Johannes Rieken (@jrieken) there is now a new SaveReason enum carried all the way to the participant (https://github.com/Microsoft/vscode/blob/master/src/vs/workbench/parts/files/common/editors/textFileEditorModel.ts#L453) if you wanna pick it up.

    Technically there can still be saves happening via API and currently I treat those as manual saves to distinguish from the auto saves (after delay, after focus change, after window change). One examples of where a save can kick in is when you start a debug session and debug triggers a save of all dirty files first. I think it is fair in that case to run the participants because it is an explicit user interaction to start debug.

  18. jrieken commented on Sep 26, 2016

    @jrieken
    Contributor

    That is how it looks

        /**
         * Represents reasons why a text document is saved.
         */
        export enum TextDocumentSaveReason {
    
            /**
             * Explicitly triggered, e.g. by the user pressing save or by an API call.
             */
            Explicit = 1,
    
            /**
             * Automatic after a delay.
             */
            Auto = 2,
    
            /**
             * When the editor lost focus.
             */
            FocusOut = 3
        }
    
        /**
         * An event that is fired when a [document](#TextDocument) will be saved.
         *
         * To make modifications to the document before it is being saved, call the
         * [`waitUntil`](#TextDocumentWillSaveEvent.waitUntil)-function with a thenable
         * that resolves to an array of [text edits](#TextEdit).
         */
        export interface TextDocumentWillSaveEvent {
    
            /**
             * The document that will be saved.
             */
            document: vscode.TextDocument;
    
            /**
             * The reason why save was triggered.
             */
            reason: TextDocumentSaveReason;
    
            /**
             * Allows to pause the event loop and to apply [pre-save-edits](#TextEdit).
             * Edits of subsequent calls to this function will be applied in order. The
             * edits will be *ignored* if concurrent modifications of the document happened.
             *
             * *Note:* This function can only be called during event dispatch and not
             * in an asynchronous manner:
             *
             * ```ts
             * workspace.onWillSaveTextDocument(event => {
                // async, will *throw* an error
                setTimeout(() => event.waitUntil(promise));
    
                // sync, OK
             *  event.waitUntil(promise);
             * })
             * ```
             *
             * @param thenable A thenable that resolves to [pre-save-edits](#TextEdit).
             */
            waitUntil(thenable: Thenable<vscode.TextEdit[]>): void;
    
            /**
             * Allows to pause the event loop until the provided thenable resolved.
             *
             * *Note:* This function can only be called during event dispatch.
             *
             * @param thenable A thenable that delays saving.
             */
            waitUntil(thenable: Thenable<any>): void;
        }
    
  19. SamVerschueren commented on Sep 26, 2016

    @SamVerschueren
    Contributor

    Great work guys! 🍻

  20. locked and limited conversation to collaborators on Nov 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

apifeature-request

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions