Skip to content

Commit

Permalink
[editor] add command 'toggle word wrap'
Browse files Browse the repository at this point in the history
Fixes #2433

- added command `toggle word wrap` which toggles the word wrap
value from all available values present in the preference.

Signed-off-by: Vincent Fugnitto <vincent.fugnitto@ericsson.com>
  • Loading branch information
vince-fugnitto committed Jan 9, 2020
1 parent b0a9da8 commit 2ef80c0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/editor/src/browser/editor-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ export namespace EditorCommands {
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 @@ -178,6 +186,7 @@ export class EditorCommandContribution implements CommandContribution {
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
5 changes: 5 additions & 0 deletions packages/editor/src/browser/editor-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ export class EditorMenuContribution implements MenuContribution {
});

// 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',
Expand Down
22 changes: 22 additions & 0 deletions packages/editor/src/browser/editor-navigation-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export class EditorNavigationContribution implements Disposable, FrontendApplica
isEnabled: () => true,
isToggled: () => this.isRenderWhitespaceEnabled()
});
this.commandRegistry.registerHandler(EditorCommands.TOGGLE_WORD_WRAP.id, {
execute: () => this.toggleWordWrap(),
isEnabled: () => true,
});
}

async onStart(): Promise<void> {
Expand All @@ -102,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 Down

0 comments on commit 2ef80c0

Please sign in to comment.