You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
API: Extra events like onPreSaveTextDocument? #239
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...
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.
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.
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.
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
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:
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
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.
/**
* 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;
}
Activity
lukehoban commentedon Nov 22, 2015
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.
bpasero commentedon Nov 23, 2015
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.
jrieken commentedon Apr 4, 2016
fyi Dirk Bäumer (@dbaeumer)
dbaeumer commentedon Apr 5, 2016
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.
bpasero commentedon Apr 5, 2016
Yeah, stretch for April, otherwise May.
Remove auto fix action and auto fix on save
mysticatea commentedon Jun 1, 2016
What is happening on this?
jaime-olivares commentedon Jul 12, 2016
So, it was planned for April, and we are July. Is this already included in VSCode 1.3?
mysticatea commentedon Jul 20, 2016
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. 😕
37 remaining items
SamVerschueren commentedon Sep 22, 2016
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.
ignore listeners that failed (error, timeout) more than n times, #239
jrieken commentedon Sep 23, 2016
Benjamin Pasero (@bpasero) Thinking of adding
sourceorreasonproperty to the event. Is it possible to expose the different save reasons to the participants? Likeexplicit(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 wellbpasero commentedon Sep 23, 2016
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
isAutoSavedproperty, I think all the info is there:false: it can only be a manual savetrue: go to configuration service and ask for the auto save setting (or use the ITextFileService#getAutoSaveConfiguration() for convenience (https://github.com/Microsoft/vscode/blob/master/src/vs/workbench/parts/files/common/files.ts#L400)jrieken commentedon Sep 23, 2016
Actually not. I see
isAutoSavedasfalsewhen using save on focus out. Which I like btw because it makes a nice format on focus lost experiencebpasero commentedon Sep 23, 2016
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
jrieken commentedon Sep 23, 2016
k
add an overall timeout to onWillSaveTextDocument, #239
save reason for #239
bpasero commentedon Sep 26, 2016
Johannes Rieken (@jrieken) there is now a new
SaveReasonenum 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.
add TextDocumentWillSaveEvent#reason, #239
jrieken commentedon Sep 26, 2016
That is how it looks
SamVerschueren commentedon Sep 26, 2016
Great work guys! 🍻