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

SCM - extracted code into MainThreadSCMHistoryProvider #193520

Merged
merged 1 commit into from
Sep 19, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
104 changes: 63 additions & 41 deletions src/vs/workbench/api/browser/mainThreadSCM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,55 @@ class MainThreadSCMResource implements ISCMResource {
}
}

class MainThreadSCMHistoryProvider implements ISCMHistoryProvider {

private _onDidChangeActionButton = new Emitter<void>();
readonly onDidChangeActionButton = this._onDidChangeActionButton.event;

private _onDidChangeCurrentHistoryItemGroup = new Emitter<void>();
readonly onDidChangeCurrentHistoryItemGroup = this._onDidChangeCurrentHistoryItemGroup.event;

private _actionButton: ISCMActionButtonDescriptor | undefined;
get actionButton(): ISCMActionButtonDescriptor | undefined { return this._actionButton; }
set actionButton(actionButton: ISCMActionButtonDescriptor | undefined) {
this._actionButton = actionButton;
this._onDidChangeActionButton.fire();
}

private _currentHistoryItemGroup: ISCMHistoryItemGroup | undefined;
get currentHistoryItemGroup(): ISCMHistoryItemGroup | undefined { return this._currentHistoryItemGroup; }
set currentHistoryItemGroup(historyItemGroup: ISCMHistoryItemGroup | undefined) {
this._currentHistoryItemGroup = historyItemGroup;
this._onDidChangeCurrentHistoryItemGroup.fire();
}

constructor(private readonly proxy: ExtHostSCMShape, private readonly handle: number) { }

async resolveHistoryItemGroupBase(historyItemGroupId: string): Promise<ISCMHistoryItemGroup | undefined> {
return this.proxy.$resolveHistoryItemGroupBase(this.handle, historyItemGroupId, CancellationToken.None);
}

async resolveHistoryItemGroupCommonAncestor(historyItemGroupId1: string, historyItemGroupId2: string): Promise<{ id: string; ahead: number; behind: number } | undefined> {
return this.proxy.$resolveHistoryItemGroupCommonAncestor(this.handle, historyItemGroupId1, historyItemGroupId2, CancellationToken.None);
}

async provideHistoryItems(historyItemGroupId: string, options: ISCMHistoryOptions): Promise<ISCMHistoryItem[] | undefined> {
const historyItems = await this.proxy.$provideHistoryItems(this.handle, historyItemGroupId, options, CancellationToken.None);
return historyItems?.map(historyItem => ({ ...historyItem, icon: getSCMHistoryItemIcon(historyItem), }));
}

async provideHistoryItemChanges(historyItemId: string): Promise<ISCMHistoryItemChange[] | undefined> {
const changes = await this.proxy.$provideHistoryItemChanges(this.handle, historyItemId, CancellationToken.None);
return changes?.map(change => ({
uri: URI.revive(change.uri),
originalUri: change.originalUri && URI.revive(change.originalUri),
modifiedUri: change.modifiedUri && URI.revive(change.modifiedUri),
renameUri: change.renameUri && URI.revive(change.renameUri)
}));
}

}

class MainThreadSCMProvider implements ISCMProvider, QuickDiffProvider {

private static ID_HANDLE = 0;
Expand Down Expand Up @@ -147,11 +196,8 @@ class MainThreadSCMProvider implements ISCMProvider, QuickDiffProvider {
private readonly _onDidChangeStatusBarCommands = new Emitter<readonly Command[]>();
get onDidChangeStatusBarCommands(): Event<readonly Command[]> { return this._onDidChangeStatusBarCommands.event; }

private readonly _onDidChangeHistoryProviderActionButton = new Emitter<void>();
readonly onDidChangeHistoryProviderActionButton: Event<void> = this._onDidChangeHistoryProviderActionButton.event;

private readonly _onDidChangeHistoryProviderCurrentHistoryItemGroup = new Emitter<void>();
readonly onDidChangeHistoryProviderCurrentHistoryItemGroup: Event<void> = this._onDidChangeHistoryProviderCurrentHistoryItemGroup.event;
private readonly _onDidChangeHistoryProvider = new Emitter<void>();
readonly onDidChangeHistoryProvider: Event<void> = this._onDidChangeHistoryProvider.event;

private readonly _onDidChange = new Emitter<void>();
readonly onDidChange: Event<void> = this._onDidChange.event;
Expand All @@ -160,8 +206,6 @@ class MainThreadSCMProvider implements ISCMProvider, QuickDiffProvider {
public readonly isSCM: boolean = true;

private _historyProvider: ISCMHistoryProvider | undefined;
private _historyProviderActionButton: SCMActionButtonDto | undefined | null;
private _historyProviderCurrentHistoryItemGroup: SCMHistoryItemGroupDto | undefined;

constructor(
private readonly proxy: ExtHostSCMShape,
Expand Down Expand Up @@ -198,16 +242,11 @@ class MainThreadSCMProvider implements ISCMProvider, QuickDiffProvider {
}

if (features.hasHistoryProvider && !this._historyProvider) {
this._historyProvider = {
actionButton: () => this._historyProviderActionButton ?? undefined,
currentHistoryItemGroup: () => this._historyProviderCurrentHistoryItemGroup ?? undefined,
provideHistoryItems: (historyItemGroupId: string, options: ISCMHistoryOptions) => this.provideHistoryItems(historyItemGroupId, options),
provideHistoryItemChanges: (historyItemId: string) => this.provideHistoryItemChanges(historyItemId),
resolveHistoryItemGroupBase: (historyItemGroupId: string) => this.resolveHistoryItemGroupBase(historyItemGroupId),
resolveHistoryItemGroupCommonAncestor: (historyItemGroupId1: string, historyItemGroupId2: string) => this.resolveHistoryItemGroupCommonAncestor(historyItemGroupId1, historyItemGroupId2),
};
this._historyProvider = new MainThreadSCMHistoryProvider(this.proxy, this.handle);
this._onDidChangeHistoryProvider.fire();
} else if (features.hasHistoryProvider === false && this._historyProvider) {
this._historyProvider = undefined;
this._onDidChangeHistoryProvider.fire();
}
}

Expand Down Expand Up @@ -319,36 +358,19 @@ class MainThreadSCMProvider implements ISCMProvider, QuickDiffProvider {
}

$onDidChangeHistoryProviderActionButton(actionButton?: SCMActionButtonDto | null): void {
this._historyProviderActionButton = actionButton;
this._onDidChangeHistoryProviderActionButton.fire();
}

$onDidChangeHistoryProviderCurrentHistoryItemGroup(currentHistoryItemGroup?: SCMHistoryItemGroupDto): void {
this._historyProviderCurrentHistoryItemGroup = currentHistoryItemGroup;
this._onDidChangeHistoryProviderCurrentHistoryItemGroup.fire();
}

async resolveHistoryItemGroupBase(historyItemGroupId: string): Promise<ISCMHistoryItemGroup | undefined> {
return this.proxy.$resolveHistoryItemGroupBase(this.handle, historyItemGroupId, CancellationToken.None);
}
if (!this._historyProvider) {
return;
}

async resolveHistoryItemGroupCommonAncestor(historyItemGroupId1: string, historyItemGroupId2: string): Promise<{ id: string; ahead: number; behind: number } | undefined> {
return this.proxy.$resolveHistoryItemGroupCommonAncestor(this.handle, historyItemGroupId1, historyItemGroupId2, CancellationToken.None);
this._historyProvider.actionButton = actionButton ?? undefined;
}

async provideHistoryItems(historyItemGroupId: string, options: ISCMHistoryOptions): Promise<ISCMHistoryItem[] | undefined> {
const historyItems = await this.proxy.$provideHistoryItems(this.handle, historyItemGroupId, options, CancellationToken.None);
return historyItems?.map(historyItem => ({ ...historyItem, icon: getSCMHistoryItemIcon(historyItem), }));
}
$onDidChangeHistoryProviderCurrentHistoryItemGroup(currentHistoryItemGroup?: SCMHistoryItemGroupDto): void {
if (!this._historyProvider) {
return;
}

async provideHistoryItemChanges(historyItemId: string): Promise<ISCMHistoryItemChange[] | undefined> {
const changes = await this.proxy.$provideHistoryItemChanges(this.handle, historyItemId, CancellationToken.None);
return changes?.map(change => ({
uri: URI.revive(change.uri),
originalUri: change.originalUri && URI.revive(change.originalUri),
modifiedUri: change.modifiedUri && URI.revive(change.modifiedUri),
renameUri: change.renameUri && URI.revive(change.renameUri)
}));
this._historyProvider.currentHistoryItemGroup = currentHistoryItemGroup ?? undefined;
}

toJSON(): any {
Expand Down
25 changes: 19 additions & 6 deletions src/vs/workbench/contrib/scm/browser/scmSyncViewPane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ export class SCMSyncViewPane extends ViewPane {
class SCMSyncPaneViewModel {

private repositories = new Map<ISCMRepository, IDisposable>();
private historyProviders = new Map<ISCMRepository, IDisposable>();

private alwaysShowRepositories = false;

private readonly disposables = new DisposableStore();
Expand All @@ -434,10 +436,8 @@ class SCMSyncPaneViewModel {

private _onDidChangeVisibleRepositories({ added, removed }: ISCMViewVisibleRepositoryChangeEvent): void {
for (const repository of added) {
const repositoryDisposable: IDisposable = combinedDisposable(
repository.provider.onDidChangeHistoryProviderActionButton(() => this.refresh(repository)),
repository.provider.onDidChangeHistoryProviderCurrentHistoryItemGroup(() => this.refresh(repository))
);
const repositoryDisposable = repository.provider.onDidChangeHistoryProvider(() => this._onDidChangeHistoryProvider(repository));
this._onDidChangeHistoryProvider(repository);

this.repositories.set(repository, { dispose() { repositoryDisposable.dispose(); } });
}
Expand All @@ -450,6 +450,19 @@ class SCMSyncPaneViewModel {
this.refresh();
}

private _onDidChangeHistoryProvider(repository: ISCMRepository): void {
if (repository.provider.historyProvider) {
const historyProviderDisposable = combinedDisposable(
repository.provider.historyProvider.onDidChangeActionButton(() => this.refresh(repository)),
repository.provider.historyProvider.onDidChangeCurrentHistoryItemGroup(() => this.refresh(repository)));

this.historyProviders.set(repository, historyProviderDisposable);
} else {
this.historyProviders.get(repository)?.dispose();
this.historyProviders.delete(repository);
}
}

private async refresh(repository?: ISCMRepository): Promise<void> {
if (this.repositories.size === 0) {
return;
Expand Down Expand Up @@ -500,14 +513,14 @@ class SCMSyncDataSource implements IAsyncDataSource<TreeElement, TreeElement> {
} else if (isSCMRepository(element)) {
const scmProvider = element.provider;
const historyProvider = scmProvider.historyProvider;
const historyItemGroup = historyProvider?.currentHistoryItemGroup();
const historyItemGroup = historyProvider?.currentHistoryItemGroup;

if (!historyProvider || !historyItemGroup) {
return children;
}

// Action Button
const actionButton = historyProvider.actionButton();
const actionButton = historyProvider.actionButton;
if (actionButton) {
children.push({
type: 'actionButton',
Expand Down
13 changes: 11 additions & 2 deletions src/vs/workbench/contrib/scm/common/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { Event } from 'vs/base/common/event';
import { ThemeIcon } from 'vs/base/common/themables';
import { URI } from 'vs/base/common/uri';
import { ISCMActionButtonDescriptor } from 'vs/workbench/contrib/scm/common/scm';

export interface ISCMHistoryProvider {
actionButton: () => ISCMActionButtonDescriptor | undefined;
currentHistoryItemGroup: () => ISCMHistoryItemGroup | undefined;

readonly onDidChangeActionButton: Event<void>;
readonly onDidChangeCurrentHistoryItemGroup: Event<void>;

get actionButton(): ISCMActionButtonDescriptor | undefined;
set actionButton(button: ISCMActionButtonDescriptor | undefined);

get currentHistoryItemGroup(): ISCMHistoryItemGroup | undefined;
set currentHistoryItemGroup(historyItemGroup: ISCMHistoryItemGroup | undefined);

provideHistoryItems(historyItemGroupId: string, options: ISCMHistoryOptions): Promise<ISCMHistoryItem[] | undefined>;
provideHistoryItemChanges(historyItemId: string): Promise<ISCMHistoryItemChange[] | undefined>;
resolveHistoryItemGroupBase(historyItemGroupId: string): Promise<ISCMHistoryItemGroup | undefined>;
Expand Down
3 changes: 1 addition & 2 deletions src/vs/workbench/contrib/scm/common/scm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ export interface ISCMProvider extends IDisposable {
readonly commitTemplate: string;
readonly historyProvider?: ISCMHistoryProvider;
readonly onDidChangeCommitTemplate: Event<string>;
readonly onDidChangeHistoryProviderActionButton: Event<void>;
readonly onDidChangeHistoryProviderCurrentHistoryItemGroup: Event<void>;
readonly onDidChangeHistoryProvider: Event<void>;
readonly onDidChangeStatusBarCommands?: Event<readonly Command[]>;
readonly acceptInputCommand?: Command;
readonly actionButton?: ISCMActionButtonDescriptor;
Expand Down