Skip to content

Commit

Permalink
[plugin] Implement SCM Repository selected event
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Vinokur <ivinokur@redhat.com>
  • Loading branch information
vinokurig committed Sep 12, 2019
1 parent d0690be commit c7b5a19
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
1 change: 0 additions & 1 deletion examples/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"@theia/file-search": "^0.10.0",
"@theia/filesystem": "^0.10.0",
"@theia/getting-started": "^0.10.0",
"@theia/git": "^0.10.0",
"@theia/java": "^0.10.0",
"@theia/java-debug": "^0.10.0",
"@theia/json": "^0.10.0",
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ export interface ScmExt {
$updateInputBox(sourceControlHandle: number, message: string): Promise<void>;
$executeResourceCommand(sourceControlHandle: number, groupHandle: number, resourceHandle: number): Promise<void>;
$provideOriginalResource(sourceControlHandle: number, uri: string, token: CancellationToken): Promise<UriComponents | undefined>;
$setSourceControlSelection(sourceControlHandle: number, selected: boolean): Promise<void>;
}

export interface DecorationsExt {
Expand Down
23 changes: 23 additions & 0 deletions packages/plugin-ext/src/main/browser/scm-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,32 @@ export class ScmMainImpl implements ScmMain {
private readonly scmService: ScmService;
private readonly scmRepositoryMap: Map<number, ScmRepository>;
private readonly labelProvider: LabelProvider;
private lastSelectedSourceControlHandle: number | undefined;

constructor(rpc: RPCProtocol, container: interfaces.Container) {
this.proxy = rpc.getProxy(MAIN_RPC_CONTEXT.SCM_EXT);
this.scmService = container.get(ScmService);
this.scmRepositoryMap = new Map();
this.labelProvider = container.get(LabelProvider);
this.scmService.onDidChangeSelectedRepository(repository => this.updateSelectedRepository(repository));
}

protected updateSelectedRepository(repository: ScmRepository | undefined): void {
const sourceControlHandle = repository ? this.getSourceControlHandle(repository) : undefined;
if (sourceControlHandle !== undefined) {
this.proxy.$setSourceControlSelection(sourceControlHandle, true);
}
if (this.lastSelectedSourceControlHandle !== undefined && this.lastSelectedSourceControlHandle !== sourceControlHandle) {
this.proxy.$setSourceControlSelection(this.lastSelectedSourceControlHandle, false);
}
this.lastSelectedSourceControlHandle = sourceControlHandle;
}

protected getSourceControlHandle(repository: ScmRepository): number | undefined {
return Array.from(this.scmRepositoryMap.keys()).find(key => {
const scmRepository = this.scmRepositoryMap.get(key);
return scmRepository !== undefined && scmRepository.provider.rootUri === repository.provider.rootUri;
});
}

async $registerSourceControl(sourceControlHandle: number, id: string, label: string, rootUri: string): Promise<void> {
Expand All @@ -52,6 +72,9 @@ export class ScmMainImpl implements ScmMain {
this.proxy.$updateInputBox(sourceControlHandle, repository.input.value)
);
this.scmRepositoryMap.set(sourceControlHandle, repository);
if (this.scmService.repositories.length === 1) {
this.updateSelectedRepository(repository);
}
}

async $updateSourceControl(sourceControlHandle: number, features: SourceControlProviderFeatures): Promise<void> {
Expand Down
21 changes: 21 additions & 0 deletions packages/plugin-ext/src/plugin/scm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { UriComponents } from '../common/uri-components';
import URI from '@theia/core/lib/common/uri';
import { CommandRegistryImpl } from './command-registry';
import { ScmCommand } from '@theia/scm/lib/browser/scm-provider';
import { Emitter } from '@theia/core/lib/common/event';

export class ScmExtImpl implements ScmExt {
private handle: number = 0;
Expand Down Expand Up @@ -97,6 +98,13 @@ export class ScmExtImpl implements ScmExt {
sourceControl.inputBox.$updateValue(value);
}
}

async $setSourceControlSelection(sourceControlHandle: number, selected: boolean): Promise<void> {
const sourceControl = this.sourceControlMap.get(sourceControlHandle);
if (sourceControl) {
sourceControl.selected = selected;
}
}
}

class InputBoxImpl implements theia.SourceControlInputBox {
Expand Down Expand Up @@ -142,9 +150,13 @@ class SourceControlImpl implements theia.SourceControl {
private _commitTemplate: string | undefined;
private _acceptInputCommand: theia.Command | undefined;
private _statusBarCommands: theia.Command[] | undefined;
private _selected: boolean = false;

private readonly toDispose = new DisposableCollection();

private readonly onDidChangeSelectionEmitter = new Emitter<boolean>();
readonly onDidChangeSelection: theia.Event<boolean> = this.onDidChangeSelectionEmitter.event;

constructor(
private proxy: ScmMain,
private commands: CommandRegistryImpl,
Expand Down Expand Up @@ -254,6 +266,15 @@ class SourceControlImpl implements theia.SourceControl {
getResourceGroup(handle: number): SourceControlResourceGroupImpl | undefined {
return this.resourceGroupsMap.get(handle);
}

get selected(): boolean {
return this._selected;
}

set selected(selected: boolean) {
this._selected = selected;
this.onDidChangeSelectionEmitter.fire(selected);
}
}

class SourceControlResourceGroupImpl implements theia.SourceControlResourceGroup {
Expand Down
19 changes: 13 additions & 6 deletions packages/plugin/src/theia-proposed.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,6 @@ declare module '@theia/plugin' {
value: string;
}

export interface SourceControlResourceDecorations {
source?: string;
letter?: string;
color?: ThemeColor;
}

/**
* Enumeration of the supported operating systems.
*/
Expand Down Expand Up @@ -172,6 +166,19 @@ declare module '@theia/plugin' {
source?: string;
}

export interface SourceControl {

/**
* Whether the source control is selected.
*/
readonly selected: boolean;

/**
* An event signaling when the selection state changes.
*/
readonly onDidChangeSelection: Event<boolean>;
}

export interface SourceControlResourceDecorations {
source?: string;
letter?: string;
Expand Down

0 comments on commit c7b5a19

Please sign in to comment.