Skip to content

Commit e872942

Browse files
author
Colin Grant
committed
Implement revert file command
Signed-off-by: Colin Grant <colin.grant@ericsson.com>
1 parent 54b6ced commit e872942

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

packages/editor/src/browser/editor-command.ts

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ export namespace EditorCommands {
6969
category: EDITOR_CATEGORY,
7070
label: 'Change File Encoding'
7171
};
72+
export const REVERT_EDITOR: Command = {
73+
id: 'workbench.action.files.revert',
74+
category: 'File',
75+
label: 'Revert File',
76+
};
7277
export const REVERT_AND_CLOSE: Command = {
7378
id: 'workbench.action.revertAndCloseActiveEditor',
7479
category: 'View',
@@ -234,6 +239,7 @@ export class EditorCommandContribution implements CommandContribution {
234239
registry.registerCommand(EditorCommands.CONFIG_EOL);
235240
registry.registerCommand(EditorCommands.INDENT_USING_SPACES);
236241
registry.registerCommand(EditorCommands.INDENT_USING_TABS);
242+
registry.registerCommand(EditorCommands.REVERT_EDITOR);
237243
registry.registerCommand(EditorCommands.REVERT_AND_CLOSE);
238244
registry.registerCommand(EditorCommands.CHANGE_LANGUAGE, {
239245
isEnabled: () => this.canConfigureLanguage(),

packages/editor/src/browser/editor-menu.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class EditorMenuContribution implements MenuContribution {
105105
registry.registerMenuAction(CommonMenus.FILE_CLOSE, {
106106
commandId: CommonCommands.CLOSE_MAIN_TAB.id,
107107
label: 'Close Editor',
108-
order: '0'
108+
order: '1'
109109
});
110110
}
111111

packages/monaco/src/browser/monaco-command.ts

+26-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { injectable, inject, optional } from '@theia/core/shared/inversify';
1818
import { Position, Location } from '@theia/core/shared/vscode-languageserver-types';
1919
import { CommandContribution, CommandRegistry, CommandHandler } from '@theia/core/lib/common/command';
2020
import { CommonCommands, QuickInputService, ApplicationShell } from '@theia/core/lib/browser';
21-
import { EditorCommands, EditorManager } from '@theia/editor/lib/browser';
21+
import { EditorCommands, EditorManager, EditorWidget } from '@theia/editor/lib/browser';
2222
import { MonacoEditor } from './monaco-editor';
2323
import { MonacoCommandRegistry, MonacoEditorCommandHandler } from './monaco-command-registry';
2424
import { MonacoEditorService } from './monaco-editor-service';
@@ -191,6 +191,7 @@ export class MonacoEditorCommandHandlers implements CommandContribution {
191191
this.monacoCommandRegistry.registerHandler(EditorCommands.CONFIG_EOL.id, this.newConfigEolHandler());
192192
this.monacoCommandRegistry.registerHandler(EditorCommands.INDENT_USING_SPACES.id, this.newConfigTabSizeHandler(true));
193193
this.monacoCommandRegistry.registerHandler(EditorCommands.INDENT_USING_TABS.id, this.newConfigTabSizeHandler(false));
194+
this.monacoCommandRegistry.registerHandler(EditorCommands.REVERT_EDITOR.id, this.newRevertActiveEditorHandler());
194195
this.monacoCommandRegistry.registerHandler(EditorCommands.REVERT_AND_CLOSE.id, this.newRevertAndCloseActiveEditorHandler());
195196
}
196197

@@ -272,23 +273,36 @@ export class MonacoEditorCommandHandlers implements CommandContribution {
272273
}
273274
}
274275

276+
protected newRevertActiveEditorHandler(): MonacoEditorCommandHandler {
277+
return {
278+
execute: () => this.revertEditor(this.getActiveEditor().editor),
279+
};
280+
}
281+
275282
protected newRevertAndCloseActiveEditorHandler(): MonacoEditorCommandHandler {
276283
return {
277-
execute: async () => this.revertAndCloseActiveEditor()
284+
execute: async () => this.revertAndCloseActiveEditor(this.getActiveEditor())
278285
};
279286
}
280287

281-
protected async revertAndCloseActiveEditor(): Promise<void> {
282-
const editor = this.editorManager.currentEditor;
288+
protected getActiveEditor(): { widget?: EditorWidget, editor?: MonacoEditor } {
289+
const widget = this.editorManager.currentEditor;
290+
return { widget, editor: widget && MonacoEditor.getCurrent(this.editorManager) };
291+
}
292+
293+
protected async revertEditor(editor?: MonacoEditor): Promise<void> {
283294
if (editor) {
284-
const monacoEditor = MonacoEditor.getCurrent(this.editorManager);
285-
if (monacoEditor) {
286-
try {
287-
await monacoEditor.document.revert();
288-
editor.close();
289-
} catch (error) {
290-
await this.shell.closeWidget(editor.id, { save: false });
291-
}
295+
return editor.document.revert();
296+
}
297+
}
298+
299+
protected async revertAndCloseActiveEditor(current: { widget?: EditorWidget, editor?: MonacoEditor }): Promise<void> {
300+
if (current.editor && current.widget) {
301+
try {
302+
await this.revertEditor(current.editor);
303+
current.widget.close();
304+
} catch (error) {
305+
await this.shell.closeWidget(current.widget.id, { save: false });
292306
}
293307
}
294308
}

0 commit comments

Comments
 (0)