Skip to content

Commit

Permalink
fix #3338 Format Document on save
Browse files Browse the repository at this point in the history
Format on manual Save when the preference 'editor.formatOnSave' is "true"

Signed-off-by: Jacques Bouthillier <jacques.bouthillier@ericsson.com>
  • Loading branch information
lmcbout committed Jan 11, 2019
1 parent 9b6cec8 commit 3ce7f1d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/browser/saveable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
if (saveable) {
await saveable.save();
}
Expand Down
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 manual save.'
},
'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
2 changes: 1 addition & 1 deletion packages/monaco/src/browser/monaco-editor-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
return;
}

Expand Down
16 changes: 15 additions & 1 deletion packages/monaco/src/browser/monaco-editor-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

// 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';
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';
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,16 @@ 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 => {
if (event.reason === TextDocumentSaveReason.Manual && 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 3ce7f1d

Please sign in to comment.