Skip to content

Commit

Permalink
editor: add 'revert and close editor' command
Browse files Browse the repository at this point in the history
The pull-request moves the `revert and close editor` command out of the
plugin-system so it can be used by end-users as well as for vscode
plugins.

Signed-off-by: vince-fugnitto <[email protected]>
  • Loading branch information
vince-fugnitto committed Jul 14, 2021
1 parent a38224c commit 8dd8401
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 31 deletions.
6 changes: 6 additions & 0 deletions packages/editor/src/browser/editor-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export namespace EditorCommands {
category: EDITOR_CATEGORY,
label: 'Change File Encoding'
};
export const REVERT_AND_CLOSE: Command = {
id: 'workbench.action.revertAndCloseActiveEditor',
category: 'View',
label: 'Revert and Close Editor'
};

/**
* Command for going back to the last editor navigation location.
Expand Down Expand Up @@ -229,6 +234,7 @@ export class EditorCommandContribution implements CommandContribution {
registry.registerCommand(EditorCommands.CONFIG_EOL);
registry.registerCommand(EditorCommands.INDENT_USING_SPACES);
registry.registerCommand(EditorCommands.INDENT_USING_TABS);
registry.registerCommand(EditorCommands.REVERT_AND_CLOSE);
registry.registerCommand(EditorCommands.CHANGE_LANGUAGE, {
isEnabled: () => this.canConfigureLanguage(),
isVisible: () => this.canConfigureLanguage(),
Expand Down
56 changes: 43 additions & 13 deletions packages/monaco/src/browser/monaco-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import { injectable, inject, optional } from '@theia/core/shared/inversify';
import { Position, Location } from '@theia/core/shared/vscode-languageserver-types';
import { CommandContribution, CommandRegistry, CommandHandler } from '@theia/core/lib/common/command';
import { CommonCommands, QuickInputService } from '@theia/core/lib/browser';
import { EditorCommands } from '@theia/editor/lib/browser';
import { CommonCommands, QuickInputService, ApplicationShell } from '@theia/core/lib/browser';
import { EditorCommands, EditorManager } from '@theia/editor/lib/browser';
import { MonacoEditor } from './monaco-editor';
import { MonacoCommandRegistry, MonacoEditorCommandHandler } from './monaco-command-registry';
import { MonacoEditorService } from './monaco-editor-service';
Expand Down Expand Up @@ -69,6 +69,12 @@ export class MonacoEditorCommandHandlers implements CommandContribution {
@inject(monaco.contextKeyService.ContextKeyService)
protected readonly contextKeyService: monaco.contextKeyService.ContextKeyService;

@inject(ApplicationShell)
protected readonly shell: ApplicationShell;

@inject(EditorManager)
protected editorManager: EditorManager;

registerCommands(): void {
this.registerMonacoCommands();
this.registerEditorCommandHandlers();
Expand Down Expand Up @@ -185,6 +191,7 @@ export class MonacoEditorCommandHandlers implements CommandContribution {
this.monacoCommandRegistry.registerHandler(EditorCommands.CONFIG_EOL.id, this.newConfigEolHandler());
this.monacoCommandRegistry.registerHandler(EditorCommands.INDENT_USING_SPACES.id, this.newConfigTabSizeHandler(true));
this.monacoCommandRegistry.registerHandler(EditorCommands.INDENT_USING_TABS.id, this.newConfigTabSizeHandler(false));
this.monacoCommandRegistry.registerHandler(EditorCommands.REVERT_AND_CLOSE.id, this.newRevertAndCloseActiveEditorHandler());
}

protected newShowReferenceHandler(): MonacoEditorCommandHandler {
Expand Down Expand Up @@ -222,10 +229,11 @@ export class MonacoEditorCommandHandlers implements CommandContribution {

protected configureEol(editor: MonacoEditor): void {
const items = ['LF', 'CRLF'].map(lineEnding =>
({
label: lineEnding,
execute: () => this.setEol(editor, lineEnding)
}));
({
label: lineEnding,
execute: () => this.setEol(editor, lineEnding)
})
);
this.quickInputService?.showQuickPick(items, { placeholder: 'Select End of Line Sequence' });
}

Expand All @@ -251,15 +259,37 @@ export class MonacoEditorCommandHandlers implements CommandContribution {
const { tabSize } = model.getOptions();
const sizes = Array.from(Array(8), (_, x) => x + 1);
const tabSizeOptions = sizes.map(size =>
({
label: size === tabSize ? `${size} Configured Tab Size` : size.toString(),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
execute: (quickPick: any, lookFor: string) => model.updateOptions({
tabSize: size || tabSize,
insertSpaces: useSpaces
({
label: size === tabSize ? `${size} Configured Tab Size` : size.toString(),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
execute: (quickPick: any, lookFor: string) => model.updateOptions({
tabSize: size || tabSize,
insertSpaces: useSpaces
})
})
}));
);
this.quickInputService?.showQuickPick(tabSizeOptions, { placeholder: 'Select Tab Size for Current File' });
}
}

protected newRevertAndCloseActiveEditorHandler(): MonacoEditorCommandHandler {
return {
execute: async () => this.revertAndCloseActiveEditor()
};
}

protected async revertAndCloseActiveEditor(): Promise<void> {
const editor = this.editorManager.currentEditor;
if (editor) {
const monacoEditor = MonacoEditor.getCurrent(this.editorManager);
if (monacoEditor) {
try {
await monacoEditor.document.revert();
editor.close();
} catch (error) {
await this.shell.closeWidget(editor.id, { save: false });
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import { inject, injectable, optional } from '@theia/core/shared/inversify';
import { Position } from '@theia/plugin-ext/lib/common/plugin-api-rpc';
import { URI } from '@theia/core/shared/vscode-uri';
import { PluginServer } from '@theia/plugin-ext/lib/common/plugin-protocol';
import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor';
import { TerminalFrontendContribution } from '@theia/terminal/lib/browser/terminal-frontend-contribution';
import { QuickOpenWorkspace } from '@theia/workspace/lib/browser/quick-open-workspace';
import { TerminalService } from '@theia/terminal/lib/browser/base/terminal-service';
Expand Down Expand Up @@ -399,23 +398,6 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
}
});

commands.registerCommand({ id: 'workbench.action.revertAndCloseActiveEditor' }, {
execute: async () => {
const editor = this.editorManager.currentEditor;
if (editor) {
const monacoEditor = MonacoEditor.getCurrent(this.editorManager);
if (monacoEditor) {
try {
await monacoEditor.document.revert();
editor.close();
} catch (error) {
await this.shell.closeWidget(editor.id, { save: false });
}
}
}
}
});

/**
* TODO:
* Open Next: workbench.action.openNextRecentlyUsedEditorInGroup
Expand Down

0 comments on commit 8dd8401

Please sign in to comment.