SCM - unify code to get a repository using a resource (#214510)

This commit is contained in:
Ladislau Szomoru 2024-06-06 22:38:30 +02:00 committed by GitHub
parent 26ce0989f0
commit 88583e6f15
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 46 additions and 36 deletions

View file

@ -42,8 +42,7 @@ export class SCMStatusController implements IWorkbenchContribution {
@IStatusbarService private readonly statusbarService: IStatusbarService,
@IActivityService private readonly activityService: IActivityService,
@IEditorService private readonly editorService: IEditorService,
@IConfigurationService private readonly configurationService: IConfigurationService,
@IUriIdentityService private readonly uriIdentityService: IUriIdentityService
@IConfigurationService private readonly configurationService: IConfigurationService
) {
this.scmService.onDidAddRepository(this.onDidAddRepository, this, this.disposables);
this.scmService.onDidRemoveRepository(this.onDidRemoveRepository, this, this.disposables);
@ -69,29 +68,12 @@ export class SCMStatusController implements IWorkbenchContribution {
return false;
}
let bestRepository: ISCMRepository | null = null;
let bestMatchLength = Number.POSITIVE_INFINITY;
for (const repository of repositories) {
const root = repository.provider.rootUri;
if (!root) {
continue;
}
const path = this.uriIdentityService.extUri.relativePath(root, resource);
if (path && !/^\.\./.test(path) && path.length < bestMatchLength) {
bestRepository = repository;
bestMatchLength = path.length;
}
}
if (!bestRepository) {
const repository = this.scmService.getRepository(resource);
if (!repository) {
return false;
}
this.focusRepository(bestRepository);
this.focusRepository(repository);
return true;
}
@ -230,9 +212,9 @@ export class SCMActiveRepositoryContextKeyController implements IWorkbenchContri
constructor(
@IContextKeyService contextKeyService: IContextKeyService,
@IEditorService private readonly editorService: IEditorService,
@ISCMViewService private readonly scmViewService: ISCMViewService,
@ITitleService titleService: ITitleService,
@IUriIdentityService private readonly uriIdentityService: IUriIdentityService
@ISCMViewService scmViewService: ISCMViewService,
@ISCMService private readonly scmService: ISCMService,
@ITitleService titleService: ITitleService
) {
this.activeRepositoryNameContextKey = ActiveRepositoryContextKeys.ActiveRepositoryName.bindTo(contextKeyService);
this.activeRepositoryBranchNameContextKey = ActiveRepositoryContextKeys.ActiveRepositoryBranchName.bindTo(contextKeyService);
@ -249,16 +231,11 @@ export class SCMActiveRepositoryContextKeyController implements IWorkbenchContri
private onDidActiveEditorChange(): void {
const activeResource = EditorResourceAccessor.getOriginalUri(this.editorService.activeEditor);
if (activeResource?.scheme !== Schemas.file && activeResource?.scheme !== Schemas.vscodeRemote) {
if (!activeResource) {
return;
}
const repository = Iterable.find(
this.scmViewService.repositories,
r => Boolean(r.provider.rootUri && this.uriIdentityService.extUri.isEqualOrParent(activeResource, r.provider.rootUri))
);
const repository = this.scmService.getRepository(activeResource);
this.onDidFocusRepository(repository);
}

View file

@ -175,7 +175,9 @@ export interface ISCMService {
readonly repositoryCount: number;
registerSCMProvider(provider: ISCMProvider): ISCMRepository;
getRepository(id: string): ISCMRepository | undefined;
getRepository(resource: URI): ISCMRepository | undefined;
}
export interface ISCMTitleMenu {

View file

@ -15,6 +15,8 @@ import { ResourceMap } from 'vs/base/common/map';
import { URI } from 'vs/base/common/uri';
import { Iterable } from 'vs/base/common/iterator';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { Schemas } from 'vs/base/common/network';
import { IUriIdentityService } from 'vs/platform/uriIdentity/common/uriIdentity';
class SCMInput extends Disposable implements ISCMInput {
@ -364,7 +366,8 @@ export class SCMService implements ISCMService {
@ILogService private readonly logService: ILogService,
@IWorkspaceContextService workspaceContextService: IWorkspaceContextService,
@IContextKeyService contextKeyService: IContextKeyService,
@IStorageService storageService: IStorageService
@IStorageService storageService: IStorageService,
@IUriIdentityService private readonly uriIdentityService: IUriIdentityService
) {
this.inputHistory = new SCMInputHistory(storageService, workspaceContextService);
this.providerCount = contextKeyService.createKey('scm.providerCount', 0);
@ -391,8 +394,36 @@ export class SCMService implements ISCMService {
return repository;
}
getRepository(id: string): ISCMRepository | undefined {
return this._repositories.get(id);
}
getRepository(id: string): ISCMRepository | undefined;
getRepository(resource: URI): ISCMRepository | undefined;
getRepository(idOrResource: string | URI): ISCMRepository | undefined {
if (typeof idOrResource === 'string') {
return this._repositories.get(idOrResource);
}
if (idOrResource.scheme !== Schemas.file &&
idOrResource.scheme !== Schemas.vscodeRemote) {
return undefined;
}
let bestRepository: ISCMRepository | undefined = undefined;
let bestMatchLength = Number.POSITIVE_INFINITY;
for (const repository of this.repositories) {
const root = repository.provider.rootUri;
if (!root) {
continue;
}
const path = this.uriIdentityService.extUri.relativePath(root, idOrResource);
if (path && !/^\.\./.test(path) && path.length < bestMatchLength) {
bestRepository = repository;
bestMatchLength = path.length;
}
}
return bestRepository;
}
}