From 30a309dc0eb81e12b5f2201d4b8acb5108339f6c Mon Sep 17 00:00:00 2001 From: Jacques Bouthillier Date: Thu, 17 Jan 2019 09:55:44 -0500 Subject: [PATCH] fix #3338 Format Document on save Format on manual Save when the preference 'editor.formatOnSave' is "true" Signed-off-by: Jacques Bouthillier --- packages/core/src/browser/saveable.ts | 2 +- packages/editor/src/browser/editor-preferences.ts | 6 ++++++ packages/monaco/src/browser/monaco-editor-model.ts | 2 +- packages/monaco/src/browser/monaco-editor-provider.ts | 11 ++++++++++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/core/src/browser/saveable.ts b/packages/core/src/browser/saveable.ts index 4ce5ff29b69ea..10006a62b779a 100644 --- a/packages/core/src/browser/saveable.ts +++ b/packages/core/src/browser/saveable.ts @@ -64,7 +64,7 @@ export namespace Saveable { } // tslint:disable-next-line:no-any export async function save(arg: any): Promise { - const saveable = getDirty(arg); + const saveable = get(arg); if (saveable) { await saveable.save(); } diff --git a/packages/editor/src/browser/editor-preferences.ts b/packages/editor/src/browser/editor-preferences.ts index 2e9c4060bdd54..60abb4f8e76c4 100644 --- a/packages/editor/src/browser/editor-preferences.ts +++ b/packages/editor/src/browser/editor-preferences.ts @@ -248,6 +248,11 @@ export const editorPreferenceSchema: PreferenceSchema = { 'default': false, 'description': 'Enable auto indentation adjustment.' }, + 'editor.formatOnSave': { + 'type': 'boolean', + 'default': false, + 'description': 'Enable format on manual save.' + }, 'editor.formatOnType': { 'type': 'boolean', 'default': false, @@ -519,6 +524,7 @@ export interface EditorConfiguration { 'editor.autoClosingBrackets'?: boolean 'editor.autoIndent'?: boolean 'editor.formatOnType'?: boolean + 'editor.formatOnSave'?: boolean 'editor.formatOnPaste'?: boolean 'editor.dragAndDrop'?: boolean 'editor.suggestOnTriggerCharacters'?: boolean diff --git a/packages/monaco/src/browser/monaco-editor-model.ts b/packages/monaco/src/browser/monaco-editor-model.ts index f3b4db57661f6..d9fe71152716d 100644 --- a/packages/monaco/src/browser/monaco-editor-model.ts +++ b/packages/monaco/src/browser/monaco-editor-model.ts @@ -308,7 +308,7 @@ export class MonacoEditorModel implements ITextEditorModel, TextEditorDocument { } protected async doSave(reason: TextDocumentSaveReason, token: CancellationToken): Promise { - if (token.isCancellationRequested || !this.resource.saveContents || !this.dirty) { + if (token.isCancellationRequested || !this.resource.saveContents) { return; } diff --git a/packages/monaco/src/browser/monaco-editor-provider.ts b/packages/monaco/src/browser/monaco-editor-provider.ts index f6cbe6458c721..5e90e8946c7ed 100644 --- a/packages/monaco/src/browser/monaco-editor-provider.ts +++ b/packages/monaco/src/browser/monaco-editor-provider.ts @@ -20,7 +20,7 @@ import { EditorPreferenceChange, EditorPreferences, TextEditor, DiffNavigator, E import { DiffUris } from '@theia/core/lib/browser/diff-uris'; import { inject, injectable } from 'inversify'; import { DisposableCollection } from '@theia/core/lib/common'; -import { MonacoToProtocolConverter, ProtocolToMonacoConverter } from 'monaco-languageclient'; +import { MonacoToProtocolConverter, ProtocolToMonacoConverter, TextDocumentSaveReason } from 'monaco-languageclient'; import { MonacoCommandServiceFactory } from './monaco-command-service'; import { MonacoContextMenuService } from './monaco-context-menu'; import { MonacoDiffEditor } from './monaco-diff-editor'; @@ -142,6 +142,15 @@ export class MonacoEditorProvider { const options = this.createMonacoEditorOptions(model); const editor = new MonacoEditor(uri, model, document.createElement('div'), this.m2p, this.p2m, options, override); toDispose.push(this.editorPreferences.onPreferenceChanged(event => this.updateMonacoEditorOptions(editor, event))); + editor.document.onWillSaveModel(event => { + event.waitUntil(new Promise(async resolve => { + if (event.reason === TextDocumentSaveReason.Manual && this.editorPreferences['editor.formatOnSave']) { + await this.commandServiceFactory().executeCommand('monaco.editor.action.formatDocument'); + } + resolve([]); + })); + }); + return editor; } protected createMonacoEditorOptions(model: MonacoEditorModel): MonacoEditor.IOptions {