From 3a6eb4346d7a1e458783744750728f5e597553bf Mon Sep 17 00:00:00 2001 From: Igor Vinokur Date: Sat, 25 May 2019 13:09:22 +0300 Subject: [PATCH] fixup! Add Scm component and bind Git to it Signed-off-by: Igor Vinokur --- packages/core/src/common/command.ts | 3 - packages/git/src/browser/git-contribution.ts | 68 +++++++++++++++---- .../menus/menus-contribution-handler.ts | 9 +-- .../src/browser/scm-group-command-registry.ts | 35 ++++++---- .../browser/scm-resource-command-registry.ts | 35 ++++++---- .../src/browser/scm-title-command-registry.ts | 5 +- packages/scm/src/browser/scm-widget.tsx | 45 ++++++------ 7 files changed, 121 insertions(+), 79 deletions(-) diff --git a/packages/core/src/common/command.ts b/packages/core/src/common/command.ts index 067b8750ff965..6a45b67bf194d 100644 --- a/packages/core/src/common/command.ts +++ b/packages/core/src/common/command.ts @@ -40,9 +40,6 @@ export interface Command { * A category of this command. */ category?: string; - - // tslint:disable-next-line:no-any - props?: { [key: string]: any } } export namespace Command { diff --git a/packages/git/src/browser/git-contribution.ts b/packages/git/src/browser/git-contribution.ts index 0a74010ae402f..21bf40080d1e1 100644 --- a/packages/git/src/browser/git-contribution.ts +++ b/packages/git/src/browser/git-contribution.ts @@ -167,8 +167,6 @@ export namespace GIT_COMMANDS { id: 'git.stage.all', label: 'Stage All Changes', iconClass: 'fa fa-plus', - category: 'inline', - props: { ['group']: 'inline' } }; export const UNSTAGE = { id: 'git.unstage', @@ -179,8 +177,6 @@ export namespace GIT_COMMANDS { id: 'git.unstage.all', iconClass: 'fa fa-minus', label: 'Unstage All', - category: 'inline', - props: { ['group']: 'inline' } }; export const DISCARD = { id: 'git.discard', @@ -191,8 +187,6 @@ export namespace GIT_COMMANDS { id: 'git.discard.all', iconClass: 'fa fa-undo', label: 'Discard All Changes', - category: 'inline', - props: { ['group']: 'inline' } }; export const STASH = { id: 'git.stash', @@ -824,7 +818,7 @@ export class GitContribution implements const { upstreamBranch, aheadBehind } = status; if (upstreamBranch) { return { - text: '$(refresh)' + (aheadBehind ? ` ${aheadBehind.behind} $(arrow-down) ${aheadBehind.ahead} $(arrow-up)` : ''), + text: '$(refresh)' + (aheadBehind && (aheadBehind.ahead + aheadBehind.behind) > 0 ? ` ${aheadBehind.behind}↓ ${aheadBehind.ahead}↑` : ''), command: GIT_COMMANDS.SYNC.id, tooltip: 'Synchronize Changes' }; @@ -837,20 +831,64 @@ export class GitContribution implements } registerScmTitleCommands(registry: ScmTitleCommandRegistry): void { - registry.registerCommand({ command: GIT_COMMANDS.REFRESH.id }); - registry.registerCommand({ command: GIT_COMMANDS.COMMIT_ADD_SIGN_OFF.id }); + registry.registerItem({ command: GIT_COMMANDS.REFRESH.id, group: 'navigation' }); + registry.registerItem({ command: GIT_COMMANDS.COMMIT_ADD_SIGN_OFF.id, group: 'navigation'}); } registerScmResourceCommands(registry: ScmResourceCommandRegistry): void { - registry.registerCommands('Changes', [GIT_COMMANDS.OPEN_CHANGED_FILE.id, GIT_COMMANDS.DISCARD.id, GIT_COMMANDS.STAGE.id]); - registry.registerCommands('Staged changes', [GIT_COMMANDS.OPEN_CHANGED_FILE.id, GIT_COMMANDS.UNSTAGE.id]); - registry.registerCommands('Merged Changes', [GIT_COMMANDS.OPEN_CHANGED_FILE.id, GIT_COMMANDS.DISCARD.id, GIT_COMMANDS.STAGE.id]); + registry.registerItems('Changes', [ + { + command: GIT_COMMANDS.OPEN_CHANGED_FILE.id, + group: 'navigation' + }, + { + command: GIT_COMMANDS.DISCARD.id, + group: 'navigation' + }, + { + command: GIT_COMMANDS.STAGE.id, + group: 'navigation' + } + ]); + registry.registerItems('Staged changes', [ + { + command: GIT_COMMANDS.OPEN_CHANGED_FILE.id, + group: 'navigation' + }, + { + command: GIT_COMMANDS.UNSTAGE.id, + group: 'navigation' + } + ]); + registry.registerItems('Merged Changes', [ + { + command: GIT_COMMANDS.OPEN_CHANGED_FILE.id, + group: 'navigation' + }, + { + command: GIT_COMMANDS.DISCARD.id, + group: 'navigation' + }, + { + command: GIT_COMMANDS.STAGE.id, + group: 'navigation' + } + ]); } registerScmGroupCommands(registry: ScmGroupCommandRegistry): void { - registry.registerCommands('Changes', [GIT_COMMANDS.DISCARD_ALL.id, GIT_COMMANDS.STAGE_ALL.id]); - registry.registerCommands('Staged changes', [GIT_COMMANDS.UNSTAGE_ALL.id]); - registry.registerCommands('Merged Changes', [GIT_COMMANDS.STAGE_ALL.id]); + registry.registerItems('Changes', [ + { + command: GIT_COMMANDS.DISCARD_ALL.id, + group: 'inline' + }, + { + command: GIT_COMMANDS.STAGE_ALL.id, + group: 'inline' + } + ]); + registry.registerItems('Staged changes', [{ command: GIT_COMMANDS.UNSTAGE_ALL.id, group: 'inline' }]); + registry.registerItems('Merged Changes', [{ command: GIT_COMMANDS.STAGE_ALL.id, group: 'inline' }]); } } export interface GitOpenFileOptions { diff --git a/packages/plugin-ext/src/main/browser/menus/menus-contribution-handler.ts b/packages/plugin-ext/src/main/browser/menus/menus-contribution-handler.ts index e1c857c86d45c..e1951dbb1e512 100644 --- a/packages/plugin-ext/src/main/browser/menus/menus-contribution-handler.ts +++ b/packages/plugin-ext/src/main/browser/menus/menus-contribution-handler.ts @@ -126,7 +126,7 @@ export class MenusContributionPointHandler { execute: () => this.commands.executeCommand(menuAction.command) }); - this.scmTitleCommandRegistry.registerCommand({ command: id, when: menuAction.when }); + this.scmTitleCommandRegistry.registerItem({ command: id, when: menuAction.when, group: menuAction.group }); if (menuAction.group && menuAction.group !== 'navigation') { const action: MenuAction = { commandId: id }; @@ -137,7 +137,6 @@ export class MenusContributionPointHandler { command.iconClass = pluginCommand.iconClass; command.category = pluginCommand.category; command.label = pluginCommand.label; - command.props = { ['group']: menuAction.group }; }); } @@ -157,7 +156,7 @@ export class MenusContributionPointHandler { if (action.group !== 'inline') { this.menuRegistry.registerMenuAction(['scm-group-context-menu_' + group], { commandId: id }); } else { - this.scmGroupCommandRegistry.registerCommand(group, id); + this.scmGroupCommandRegistry.registerItem(group, { command: id, group: action.group }); } } @@ -165,7 +164,6 @@ export class MenusContributionPointHandler { command.iconClass = pluginCommand.iconClass; command.category = pluginCommand.category; command.label = pluginCommand.label; - command.props = { ['group']: action.group }; }); } @@ -183,7 +181,7 @@ export class MenusContributionPointHandler { group = group.substring(0, group.indexOf(' &&')); } if (action.group && action.group.startsWith('inline')) { - this.scmResourceCommandRegistry.registerCommand(group, id); + this.scmResourceCommandRegistry.registerItem(group, {command: id, group: 'inline'}); } else { this.menuRegistry.registerMenuAction(['scm-resource-context-menu_' + group], { commandId: id }); } @@ -193,7 +191,6 @@ export class MenusContributionPointHandler { command.iconClass = pluginCommand.iconClass; command.category = pluginCommand.category; command.label = pluginCommand.label; - command.props = { ['group']: action.group }; }); } diff --git a/packages/scm/src/browser/scm-group-command-registry.ts b/packages/scm/src/browser/scm-group-command-registry.ts index 5a8073f4a56bf..13a221f3e3a34 100644 --- a/packages/scm/src/browser/scm-group-command-registry.ts +++ b/packages/scm/src/browser/scm-group-command-registry.ts @@ -24,9 +24,14 @@ export interface ScmGroupCommandContribution { registerScmGroupCommands(registry: ScmGroupCommandRegistry): void; } +export interface ScmGroupItem { + command: string; + group?: string; +} + @injectable() export class ScmGroupCommandRegistry implements FrontendApplicationContribution { - private commands: Map = new Map(); + private items: Map = new Map(); @inject(ContributionProvider) @named(ScmGroupCommandContribution) @@ -39,27 +44,27 @@ export class ScmGroupCommandRegistry implements FrontendApplicationContribution } } - registerCommands(groupId: string, commands: string[]): void { - const savedCommands = this.commands.get(groupId); - if (savedCommands) { - commands.forEach(command => savedCommands.push(command)); - this.commands.set(groupId, savedCommands); + registerItems(groupId: string, items: ScmGroupItem[]): void { + const savedItems = this.items.get(groupId); + if (savedItems) { + items.forEach(item => savedItems.push(item)); + this.items.set(groupId, savedItems); } else { - this.commands.set(groupId, commands); + this.items.set(groupId, items); } } - registerCommand(groupId: string, command: string): void { - const commands = this.commands.get(groupId); - if (commands) { - commands.push(command); - this.commands.set(groupId, commands); + registerItem(groupId: string, item: ScmGroupItem): void { + const items = this.items.get(groupId); + if (items) { + items.push(item); + this.items.set(groupId, items); } else { - this.commands.set(groupId, [command]); + this.items.set(groupId, [item]); } } - getCommands(groupId: string): string[] | undefined { - return this.commands.get(groupId); + getItems(groupId: string): ScmGroupItem[] | undefined { + return this.items.get(groupId); } } diff --git a/packages/scm/src/browser/scm-resource-command-registry.ts b/packages/scm/src/browser/scm-resource-command-registry.ts index fad84e9c0c589..f5eb436ab722d 100644 --- a/packages/scm/src/browser/scm-resource-command-registry.ts +++ b/packages/scm/src/browser/scm-resource-command-registry.ts @@ -23,9 +23,14 @@ export interface ScmResourceCommandContribution { registerScmResourceCommands(registry: ScmResourceCommandRegistry): void; } +export interface ScmResourceItem { + command: string; + group?: string; +} + @injectable() export class ScmResourceCommandRegistry implements FrontendApplicationContribution { - private commands: Map = new Map(); + private items: Map = new Map(); @inject(ContributionProvider) @named(ScmResourceCommandContribution) @@ -38,27 +43,27 @@ export class ScmResourceCommandRegistry implements FrontendApplicationContributi } } - registerCommands(groupId: string, commands: string[]): void { - const savedCommands = this.commands.get(groupId); - if (savedCommands) { - commands.forEach(command => savedCommands.push(command)); - this.commands.set(groupId, savedCommands); + registerItems(groupId: string, items: ScmResourceItem[]): void { + const savedItems = this.items.get(groupId); + if (savedItems) { + items.forEach(item => savedItems.push(item)); + this.items.set(groupId, savedItems); } else { - this.commands.set(groupId, commands); + this.items.set(groupId, items); } } - registerCommand(groupId: string, command: string): void { - const commands = this.commands.get(groupId); - if (commands) { - commands.push(command); - this.commands.set(groupId, commands); + registerItem(groupId: string, item: ScmResourceItem): void { + const items = this.items.get(groupId); + if (items) { + items.push(item); + this.items.set(groupId, items); } else { - this.commands.set(groupId, [command]); + this.items.set(groupId, [item]); } } - getCommands(groupId: string): string[] | undefined { - return this.commands.get(groupId); + getItems(groupId: string): ScmResourceItem[] | undefined { + return this.items.get(groupId); } } diff --git a/packages/scm/src/browser/scm-title-command-registry.ts b/packages/scm/src/browser/scm-title-command-registry.ts index f0698ac4a5c23..a416fd4f02eef 100644 --- a/packages/scm/src/browser/scm-title-command-registry.ts +++ b/packages/scm/src/browser/scm-title-command-registry.ts @@ -25,6 +25,7 @@ export interface ScmTitleCommandsContribution { export interface ScmTitleItem { command: string; when?: string; + group?: string; } @injectable() @@ -42,11 +43,11 @@ export class ScmTitleCommandRegistry implements FrontendApplicationContribution } } - registerCommand(item: ScmTitleItem): void { + registerItem(item: ScmTitleItem): void { this.items.push(item); } - getCommands(): ScmTitleItem[] { + getItems(): ScmTitleItem[] { return this.items; } } diff --git a/packages/scm/src/browser/scm-widget.tsx b/packages/scm/src/browser/scm-widget.tsx index 58a527e263f5f..4d6d03deb169c 100644 --- a/packages/scm/src/browser/scm-widget.tsx +++ b/packages/scm/src/browser/scm-widget.tsx @@ -40,7 +40,8 @@ import { EditorManager } from '@theia/editor/lib/browser'; import { ScmAvatarService } from './scm-avatar-service'; import { ScmTitleCommandRegistry, ScmTitleItem } from './scm-title-command-registry'; import { ScmResourceCommandRegistry } from './scm-resource-command-registry'; -import { ScmGroupCommandRegistry } from './scm-group-command-registry'; +import { ScmResourceItem as ResourceItem} from './scm-resource-command-registry'; +import { ScmGroupCommandRegistry, ScmGroupItem } from './scm-group-command-registry'; import { ScmNavigableListWidget } from './scm-navigable-list-widget'; import { ScmAmendComponent } from './scm-amend-component'; import { KeyboardEvent } from 'react'; @@ -143,9 +144,9 @@ export class ScmWidget extends ScmNavigableListWidget implements St protected handleListEnter() { const selected = this.getSelected(); if (selected) { - const commands = this.scmResourceCommandRegistry.getCommands(selected.group.label); - if (commands && commands.length > 0) { - this.commandRegistry.executeCommand(commands[0], selected.sourceUri.toString()); + const items = this.scmResourceCommandRegistry.getItems(selected.group.label); + if (items && items.length > 0) { + this.commandRegistry.executeCommand(items[0].command, selected.sourceUri.toString()); } } } @@ -290,7 +291,7 @@ export class ScmWidget extends ScmNavigableListWidget implements St }; return
- {this.scmTitleRegistry.getCommands().map(command => this.renderButton(command))} + {this.scmTitleRegistry.getItems().map(command => this.renderButton(command))} @@ -323,9 +324,8 @@ export class ScmWidget extends ScmNavigableListWidget implements St } } } - if (command && command.props) { - const props = command.props; - if (props && props['group'] === 'navigation') { + if (command) { + if (item.group && item.group === 'navigation') { const execute = () => { this.commandRegistry.executeCommand(item.command); }; @@ -496,16 +496,16 @@ class ScmResourceItem extends React.Component { } protected renderScmItemButtons(): React.ReactNode { - const commands = this.props.scmResourceCommandRegistry.getCommands(this.props.groupId); - if (commands) { + const items = this.props.scmResourceCommandRegistry.getItems(this.props.groupId); + if (items) { return
- {commands.map(command => this.renderScmItemButton(command))} + {items.map(item => this.renderScmItemButton(item))}
; } } - protected renderScmItemButton(commandId: string): React.ReactNode { - const command = this.props.commandRegistry.getCommand(commandId); + protected renderScmItemButton(item: ResourceItem): React.ReactNode { + const command = this.props.commandRegistry.getCommand(item.command); if (command) { const execute = () => { const resource = this.props.resource; @@ -516,7 +516,7 @@ class ScmResourceItem extends React.Component { sourceControlHandle: resource.sourceControlHandle, uri: this.props.resource.sourceUri.toString() }; - this.props.commandRegistry.executeCommand(commandId, arg); + this.props.commandRegistry.executeCommand(item.command, arg); }; return ; } } - protected renderGroupButton(commandId: string): React.ReactNode { - const command = this.props.commandRegistry.getCommand(commandId); - if (command && command.props) { - const props = command.props; - if (props && props['group'] === 'inline') { + protected renderGroupButton(item: ScmGroupItem): React.ReactNode { + const command = this.props.commandRegistry.getCommand(item.command); + if (command) { + if (item.group && item.group === 'inline') { const execute = () => { const group = this.props.group; const arg = { @@ -625,7 +624,7 @@ class ScmResourceGroupContainer extends React.Component