-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
fix #3338 Format Document on save #4012
Conversation
editor.document.onWillSaveModel(event => { | ||
event.waitUntil(new Promise<monaco.editor.IIdentifiedSingleEditOperation[]>(async resolve => { | ||
// event.reason = 2: TextDocumentSaveReason.AfterDelay means: autoSave | ||
if (event.reason !== 2 && this.editorPreferences['editor.formatOnSave']) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
event.reason !== TextDocumentSaveReason.AfterDelay
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done for the next version, will use event.reason === TextDocumentSaveReason.Manual
@@ -64,7 +64,7 @@ export namespace Saveable { | |||
} | |||
// tslint:disable-next-line:no-any | |||
export async function save(arg: any): Promise<void> { | |||
const saveable = getDirty(arg); | |||
const saveable = get(arg); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should be reverted right? save should not do anything if a document is not dirty
@@ -308,7 +308,7 @@ export class MonacoEditorModel implements ITextEditorModel, TextEditorDocument { | |||
} | |||
|
|||
protected async doSave(reason: TextDocumentSaveReason, token: CancellationToken): Promise<void> { | |||
if (token.isCancellationRequested || !this.resource.saveContents || !this.dirty) { | |||
if (token.isCancellationRequested || !this.resource.saveContents) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should be reverted? save should be aborted if a document is not dirty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess what @lmcbout wanted to do here is "formatting the file on users hitting CTRL+S even if the file is not dirty" - vsCode does it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is exactly the reason, to allow "CTRL+S" like vscode does
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check whether VS Code actually save changes or only try to apply formatting. In cloud case sending unchanged content is expensive and we should avoid doing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried VSCode with the file (theia/.vscode/launch.json). The file is not dirty. By selecting "CTRL+S", it reformatted the file and saved it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant in our case if formatting does nothing we should not write anything to the disk. It is expensive for large files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After the formatting, the frontend is using the monaco-editor-model.doSave()
In that method, we check if there is some changes (~ line 320) code:
const changes = this.popContentChanges();
if (changes.length === 0) {
return;
}
This is the same as testing isDirty(). In fact we could use:
if (this.dirty) return;
to replace those lines above and it would do the same here . So as a resume, the file remains in the frontend if there is no formatting , they are not sent to the backend when there is no changes.
I noticed the command Say for example I have three |
Question: await this.commandService.executeCommand('monaco.editor.action.formatDocument'); Question: is the editor.runAction() broken ? |
Format on manual Save when the preference 'editor.formatOnSave' is "true" Signed-off-by: Jacques Bouthillier <jacques.bouthillier@ericsson.com>
Tested file: ".java, .cpp, .ts, .json" Note: need to set the preference to format the file: if we use the command: await this.commandServiceFactory().executeCommand('monaco.editor.action.formatDocument'); it works fine except if we select from the menu "Save All", it saves all files but only format the file having the focus. The other files are just saved. if we use the command: await editor.runAction('editor.action.formatDocument'); works fine with one file. With multiple file to save (Save All), it re-formats all files and saves them, but after, if you type new code , it will end-up in a file not necessary having the focus. (Is an issue if we try to use this command) To Test:Preparation : step 1 to 5
Menu: "Save All" Result:
|
I'm fine with I see two ways forward:
Also, just curious, with |
Fix #3338
This is a follow-up of the pr #3640
Since the " [monaco] re-implementation of fireWillSaveModel " on Jan 2nd, it is possible to use the "format on Save" .
Format on Save is only available when the preference autoSave is "OFF"
and the preference 'editor.formatOnSave' is "true"
Signed-off-by: Jacques Bouthillier jacques.bouthillier@ericsson.com