Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[editor] add misc toggle commands to the view menu #6843

Merged
merged 3 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/src/browser/common-frontend-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export namespace CommonMenus {
export const VIEW_PRIMARY = [...VIEW, '0_primary'];
export const VIEW_VIEWS = [...VIEW, '1_views'];
export const VIEW_LAYOUT = [...VIEW, '2_layout'];
export const VIEW_TOGGLE = [...VIEW, '3_toggle'];

// last menu item
export const HELP = [...MAIN_MENU_BAR, '9_help'];
Expand Down
18 changes: 18 additions & 0 deletions packages/editor/src/browser/editor-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ export namespace EditorCommands {
category: 'View',
label: 'Toggle Minimap'
};
/**
* Command that toggles the rendering of whitespace characters in the editor.
*/
export const TOGGLE_RENDER_WHITESPACE: Command = {
id: 'editor.action.toggleRenderWhitespace',
category: 'View',
label: 'Toggle Render Whitespace'
};
/**
* Command that toggles the word wrap.
*/
export const TOGGLE_WORD_WRAP: Command = {
id: 'editor.action.toggleWordWrap',
category: 'View',
label: 'Toggle Word Wrap'
};
}

@injectable()
Expand Down Expand Up @@ -169,6 +185,8 @@ export class EditorCommandContribution implements CommandContribution {
registry.registerCommand(EditorCommands.GO_LAST_EDIT);
registry.registerCommand(EditorCommands.CLEAR_EDITOR_HISTORY);
registry.registerCommand(EditorCommands.TOGGLE_MINIMAP);
registry.registerCommand(EditorCommands.TOGGLE_RENDER_WHITESPACE);
registry.registerCommand(EditorCommands.TOGGLE_WORD_WRAP);

registry.registerCommand(CommonCommands.AUTO_SAVE, {
isToggled: () => this.isAutoSaveOn(),
Expand Down
4 changes: 4 additions & 0 deletions packages/editor/src/browser/editor-keybinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export class EditorKeybindingContribution implements KeybindingContribution {
{
command: EditorCommands.GO_LAST_EDIT.id,
keybinding: 'ctrl+alt+q'
},
{
command: EditorCommands.TOGGLE_WORD_WRAP.id,
keybinding: 'alt+z'
}
);
}
Expand Down
19 changes: 18 additions & 1 deletion packages/editor/src/browser/editor-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { injectable } from 'inversify';
import { MenuContribution, MenuModelRegistry, MenuPath, MAIN_MENU_BAR } from '@theia/core';
import { CommonCommands } from '@theia/core/lib/browser';
import { CommonCommands, CommonMenus } from '@theia/core/lib/browser';
import { EditorCommands } from './editor-command';

export const EDITOR_CONTEXT_MENU: MenuPath = ['editor_context_menu'];
Expand Down Expand Up @@ -85,6 +85,23 @@ export class EditorMenuContribution implements MenuContribution {
commandId: EditorCommands.GO_LAST_EDIT.id,
label: 'Last Edit Location'
});

// Toggle Commands.
registry.registerMenuAction(CommonMenus.VIEW_TOGGLE, {
commandId: EditorCommands.TOGGLE_WORD_WRAP.id,
label: EditorCommands.TOGGLE_WORD_WRAP.label,
order: '0'
});
registry.registerMenuAction(CommonMenus.VIEW_TOGGLE, {
commandId: EditorCommands.TOGGLE_MINIMAP.id,
label: 'Show Minimap',
order: '1',
});
registry.registerMenuAction(CommonMenus.VIEW_TOGGLE, {
commandId: EditorCommands.TOGGLE_RENDER_WHITESPACE.id,
label: 'Render Whitespace',
order: '2'
});
}

}
53 changes: 52 additions & 1 deletion packages/editor/src/browser/editor-navigation-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,17 @@ export class EditorNavigationContribution implements Disposable, FrontendApplica
});
this.commandRegistry.registerHandler(EditorCommands.TOGGLE_MINIMAP.id, {
execute: () => this.toggleMinimap(),
isEnabled: () => true
isEnabled: () => true,
isToggled: () => this.isMinimapEnabled()
});
this.commandRegistry.registerHandler(EditorCommands.TOGGLE_RENDER_WHITESPACE.id, {
execute: () => this.toggleRenderWhitespace(),
isEnabled: () => true,
isToggled: () => this.isRenderWhitespaceEnabled()
});
this.commandRegistry.registerHandler(EditorCommands.TOGGLE_WORD_WRAP.id, {
execute: () => this.toggleWordWrap(),
isEnabled: () => true,
});
}

Expand All @@ -96,6 +106,24 @@ export class EditorNavigationContribution implements Disposable, FrontendApplica
this.toDispose.dispose();
}

/**
* Toggle the editor word wrap behavior.
*/
protected async toggleWordWrap(): Promise<void> {
// Get the current word wrap.
const wordWrap: string | undefined = this.preferenceService.get('editor.wordWrap');
if (wordWrap === undefined) {
return;
}
// The list of allowed word wrap values.
const values: string[] = ['off', 'on', 'wordWrapColumn', 'bounded'];
// Get the index of the current value, and toggle to the next available value.
const index = values.indexOf(wordWrap) + 1;
if (index > -1) {
this.preferenceService.set('editor.wordWrap', values[index % values.length], PreferenceScope.User);
}
}

/**
* Toggle the display of minimap in the editor.
*/
Expand All @@ -104,6 +132,20 @@ export class EditorNavigationContribution implements Disposable, FrontendApplica
this.preferenceService.set('editor.minimap.enabled', !value, PreferenceScope.User);
}

/**
* Toggle the rendering of whitespace in the editor.
*/
protected async toggleRenderWhitespace(): Promise<void> {
const renderWhitespace: string | undefined = this.preferenceService.get('editor.renderWhitespace');
let updatedRenderWhitespace: string;
if (renderWhitespace === 'none') {
updatedRenderWhitespace = 'all';
} else {
updatedRenderWhitespace = 'none';
}
this.preferenceService.set('editor.renderWhitespace', updatedRenderWhitespace, PreferenceScope.User);
}

protected onCurrentEditorChanged(editorWidget: EditorWidget | undefined): void {
this.toDisposePerCurrentEditor.dispose();
if (editorWidget) {
Expand Down Expand Up @@ -167,4 +209,13 @@ export class EditorNavigationContribution implements Disposable, FrontendApplica
}
}

private isMinimapEnabled(): boolean {
return !!this.preferenceService.get('editor.minimap.enabled');
}

private isRenderWhitespaceEnabled(): boolean {
const renderWhitespace = this.preferenceService.get('editor.renderWhitespace');
return renderWhitespace === 'none' ? false : true;
}

}