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

[plugin] Implement SCM Repository selected event #6150

Merged
merged 1 commit into from
Sep 12, 2019
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/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