Skip to content

Commit

Permalink
Command Save All only formats the file if it is dirty
Browse files Browse the repository at this point in the history
+ Refactor `SaveOptions`
+ Command `Save All` only formats the file if it is dirty

Signed-off-by: Duc Nguyen <duc.a.nguyen@ericsson.com>
  • Loading branch information
DucNgn authored and vince-fugnitto committed Oct 7, 2020
1 parent 56c158d commit eb1136b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
7 changes: 4 additions & 3 deletions packages/core/src/browser/common-frontend-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { EncodingRegistry } from './encoding-registry';
import { UTF8 } from '../common/encodings';
import { EnvVariablesServer } from '../common/env-variables';
import { AuthenticationService } from './authentication-service';
import { FormatType } from './saveable';

export namespace CommonMenus {

Expand Down Expand Up @@ -754,13 +755,13 @@ export class CommonFrontendContribution implements FrontendApplicationContributi
});

commandRegistry.registerCommand(CommonCommands.SAVE, {
execute: () => this.shell.save()
execute: () => this.shell.save({ formatType: FormatType.ON })
});
commandRegistry.registerCommand(CommonCommands.SAVE_WITHOUT_FORMATTING, {
execute: () => this.shell.save({ skipFormatting: true })
execute: () => this.shell.save({ formatType: FormatType.OFF })
});
commandRegistry.registerCommand(CommonCommands.SAVE_ALL, {
execute: () => this.shell.saveAll()
execute: () => this.shell.saveAll({ formatType: FormatType.DIRTY })
});
commandRegistry.registerCommand(CommonCommands.ABOUT_COMMAND, {
execute: () => this.openAbout()
Expand Down
22 changes: 20 additions & 2 deletions packages/core/src/browser/saveable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,29 @@ export namespace SaveableWidget {
}
}

/**
* Possible formatting types when saving.
*/
export const enum FormatType {
/**
* Formatting should occur (default).
*/
ON = 1,
/**
* Formatting should not occur.
*/
OFF,
/**
* Formatting should only occur if the resource is dirty.
*/
DIRTY
};

export interface SaveOptions {
/**
* Controls whether formatting should be applied upon saving
* Formatting type to apply when saving.
*/
readonly skipFormatting?: boolean;
readonly formatType?: FormatType;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/browser/shell/application-shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1745,8 +1745,8 @@ export class ApplicationShell extends Widget {
/**
* Save all dirty widgets.
*/
async saveAll(): Promise<void> {
await Promise.all(this.tracker.widgets.map(widget => Saveable.save(widget)));
async saveAll(options?: SaveOptions): Promise<void> {
await Promise.all(this.tracker.widgets.map(widget => Saveable.save(widget, options)));
}

/**
Expand Down
19 changes: 15 additions & 4 deletions packages/monaco/src/browser/monaco-editor-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { MonacoBulkEditService } from './monaco-bulk-edit-service';
import IEditorOverrideServices = monaco.editor.IEditorOverrideServices;
import { ApplicationServer } from '@theia/core/lib/common/application-protocol';
import { OS, ContributionProvider } from '@theia/core';
import { KeybindingRegistry, OpenerService, open, WidgetOpenerOptions } from '@theia/core/lib/browser';
import { KeybindingRegistry, OpenerService, open, WidgetOpenerOptions, FormatType } from '@theia/core/lib/browser';
import { MonacoResolvedKeybinding } from './monaco-resolved-keybinding';
import { HttpOpenHandlerOptions } from '@theia/core/lib/browser/http-open-handler';
import { MonacoToProtocolConverter } from './monaco-to-protocol-converter';
Expand Down Expand Up @@ -289,11 +289,22 @@ export class MonacoEditorProvider {
}
}

protected async formatOnSave(editor: MonacoEditor, event: WillSaveMonacoModelEvent): Promise<monaco.editor.IIdentifiedSingleEditOperation[]> {
protected shouldFormat(editor: MonacoEditor, event: WillSaveMonacoModelEvent): boolean {
if (event.reason !== TextDocumentSaveReason.Manual) {
return [];
return false;
}
if (event.options?.formatType) {
switch (event.options.formatType) {
case FormatType.ON: return true;
case FormatType.OFF: return false;
case FormatType.DIRTY: return editor.document.dirty;
}
}
if (event.options?.skipFormatting) {
return true;
}

protected async formatOnSave(editor: MonacoEditor, event: WillSaveMonacoModelEvent): Promise<monaco.editor.IIdentifiedSingleEditOperation[]> {
if (!this.shouldFormat(editor, event)) {
return [];
}
const overrideIdentifier = editor.document.languageId;
Expand Down

0 comments on commit eb1136b

Please sign in to comment.