Skip to content

Commit

Permalink
fix #3338 Format Document on save
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
lmcbout committed Jan 10, 2019
1 parent 0a6abc4 commit 9c837f3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/editor/src/browser/editor-preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 save when the autoSave preference is off.'
},
'editor.formatOnType': {
'type': 'boolean',
'default': false,
Expand Down Expand Up @@ -504,6 +509,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
Expand Down
15 changes: 15 additions & 0 deletions packages/monaco/src/browser/monaco-editor-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

// tslint:disable:no-any
import URI from '@theia/core/lib/common/uri';
import { CommandService } from '@theia/core';
import { EditorPreferenceChange, EditorPreferences, TextEditor, DiffNavigator } from '@theia/editor/lib/browser';
import { DiffUris } from '@theia/core/lib/browser/diff-uris';
import { inject, injectable } from 'inversify';
Expand All @@ -41,6 +42,9 @@ export class MonacoEditorProvider {
@inject(MonacoBulkEditService)
protected readonly bulkEditService: MonacoBulkEditService;

@inject(CommandService)
protected readonly commandService: CommandService;

constructor(
@inject(MonacoEditorService) protected readonly codeEditorService: MonacoEditorService,
@inject(MonacoTextModelService) protected readonly textModelService: MonacoTextModelService,
Expand Down Expand Up @@ -101,6 +105,17 @@ 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<monaco.editor.IIdentifiedSingleEditOperation[]>(async resolve => {
// event.reason = 2: TextDocumentSaveReason.AfterDelay means: autoSave
if (event.reason !== 2 && this.editorPreferences['editor.formatOnSave']) {
await this.commandService.executeCommand('monaco.editor.action.formatDocument');
}
resolve([]);
}));
});

return editor;
}
protected createMonacoEditorOptions(model: MonacoEditorModel): MonacoEditor.IOptions {
Expand Down

0 comments on commit 9c837f3

Please sign in to comment.