Skip to content

Commit 2c575f9

Browse files
authored
Respond with repo change to file change
Fixes microsoft#1768 Fixes microsoft#1741
1 parent 149f334 commit 2c575f9

File tree

5 files changed

+46
-14
lines changed

5 files changed

+46
-14
lines changed

src/extension.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async function init(context: vscode.ExtensionContext, git: ApiImpl, gitAPI: GitA
7373
// No multi-select support, always show last selected repo
7474
if (repo.ui.selected) {
7575
prManager.repository = repo;
76-
reviewManager.repository = repo;
76+
reviewManager.setRepository(repo, false);
7777
tree.updateQueries();
7878
}
7979
});
@@ -83,12 +83,31 @@ async function init(context: vscode.ExtensionContext, git: ApiImpl, gitAPI: GitA
8383
repo.ui.onDidChange(() => {
8484
if (repo.ui.selected) {
8585
prManager.repository = repo;
86-
reviewManager.repository = repo;
86+
reviewManager.setRepository(repo, false);
8787
tree.updateQueries();
8888
}
8989
});
90+
const disposable = repo.state.onDidChange(() => {
91+
prManager.repository = repo;
92+
reviewManager.setRepository(repo, true);
93+
disposable.dispose();
94+
});
9095
});
9196

97+
context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(editorChange => {
98+
if (!editorChange) {
99+
return;
100+
}
101+
for (const repo of git.repositories) {
102+
if (editorChange.document.uri.fsPath.toLowerCase().startsWith(repo.rootUri.fsPath.toLowerCase()) &&
103+
(repo.rootUri.fsPath.toLowerCase() !== prManager.repository.rootUri.fsPath.toLowerCase())) {
104+
prManager.repository = repo;
105+
reviewManager.setRepository(repo, true);
106+
return;
107+
}
108+
}
109+
}));
110+
92111
await vscode.commands.executeCommand('setContext', 'github:initialized', true);
93112
const issuesFeatures = new IssueFeatureRegistrar(gitAPI, prManager, reviewManager, context, telemetry);
94113
context.subscriptions.push(issuesFeatures);

src/github/pullRequestManager.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ export class PullRequestManager implements vscode.Disposable {
142142
private _onDidChangeState = new vscode.EventEmitter<void>();
143143
readonly onDidChangeState: vscode.Event<void> = this._onDidChangeState.event;
144144

145+
private _onDidChangeRepositories = new vscode.EventEmitter<void>();
146+
readonly onDidChangeRepositories: vscode.Event<void> = this._onDidChangeRepositories.event;
147+
145148
private _onDidChangeAssignableUsers = new vscode.EventEmitter<IAccount[]>();
146149
readonly onDidChangeAssignableUsers: vscode.Event<IAccount[]> = this._onDidChangeAssignableUsers.event;
147150

@@ -160,7 +163,6 @@ export class PullRequestManager implements vscode.Disposable {
160163
this._subs.push(vscode.workspace.onDidChangeConfiguration(async e => {
161164
if (e.affectsConfiguration(`${SETTINGS_NAMESPACE}.${REMOTES_SETTING}`)) {
162165
await this.updateRepositories();
163-
vscode.commands.executeCommand('pr.refreshList');
164166
}
165167
}));
166168

@@ -449,7 +451,7 @@ export class PullRequestManager implements vscode.Disposable {
449451
return activeRemotes;
450452
}
451453

452-
async updateRepositories(): Promise<void> {
454+
async updateRepositories(silent: boolean = false): Promise<void> {
453455
if (this._git.state === 'uninitialized') {
454456
return;
455457
}
@@ -479,6 +481,9 @@ export class PullRequestManager implements vscode.Disposable {
479481
this.getMentionableUsers(repositoriesChanged);
480482
this.getAssignableUsers(repositoriesChanged);
481483
this.state = isAuthenticated || !activeRemotes.length ? PRManagerState.RepositoriesLoaded : PRManagerState.NeedsAuthentication;
484+
if (!silent) {
485+
this._onDidChangeRepositories.fire();
486+
}
482487
return Promise.resolve();
483488
});
484489
}

src/issues/stateManager.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ export class StateManager {
6161
readonly gitAPI: GitAPI,
6262
private manager: PullRequestManager,
6363
private context: vscode.ExtensionContext
64-
) { }
64+
) {
65+
this.context.subscriptions.push(this.manager.onDidChangeRepositories(() => this.refresh()));
66+
}
6567

6668
async tryInitializeAndWait() {
6769
if (!this.initializePromise) {

src/view/prsTreeDataProvider.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ export class PullRequestsTreeDataProvider implements vscode.TreeDataProvider<Tre
9797
this._disposables.push(this._prManager.onDidChangeState(() => {
9898
this._onDidChangeTreeData.fire();
9999
}));
100+
this._disposables.push(this._prManager.onDidChangeRepositories(() => {
101+
this._onDidChangeTreeData.fire();
102+
}));
100103
await this.initializeCategories();
101104
this.refresh();
102105
}

src/view/reviewManager.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,9 @@ export class ReviewManager {
217217
return this._statusBarItem;
218218
}
219219

220-
set repository(repository: Repository) {
220+
setRepository(repository: Repository, silent: boolean) {
221221
this._repository = repository;
222-
this.updateState();
222+
this.updateState(silent);
223223
}
224224

225225
private pollForStatusChange() {
@@ -231,27 +231,30 @@ export class ReviewManager {
231231
}, 1000 * 60 * 5);
232232
}
233233

234-
public async updateState() {
234+
public async updateState(silent: boolean = false) {
235235
if (this.switchingToReviewMode) {
236236
return;
237237
}
238238
if (!this._validateStatusInProgress) {
239239
Logger.appendLine('Review> Validate state in progress');
240-
this._validateStatusInProgress = this.validateState();
240+
this._validateStatusInProgress = this.validateState(silent);
241241
return this._validateStatusInProgress;
242242
} else {
243243
Logger.appendLine('Review> Queuing additional validate state');
244244
this._validateStatusInProgress = this._validateStatusInProgress.then(async _ => {
245-
return await this.validateState();
245+
return await this.validateState(silent);
246246
});
247247

248248
return this._validateStatusInProgress;
249249
}
250250
}
251251

252-
private async validateState() {
252+
private async validateState(silent: boolean) {
253253
Logger.appendLine('Review> Validating state...');
254-
await this._prManager.updateRepositories();
254+
await this._prManager.updateRepositories(silent);
255+
if (silent) {
256+
return;
257+
}
255258

256259
if (!this._repository.state.HEAD) {
257260
this.clear(true);
@@ -644,7 +647,7 @@ export class ReviewManager {
644647
return selected;
645648
}
646649

647-
private async getPullRequestTitleAndDescriptionDefaults(progress: vscode.Progress<{message?: string, increment?: number}>): Promise<{ title: string, description: string } | undefined> {
650+
private async getPullRequestTitleAndDescriptionDefaults(progress: vscode.Progress<{ message?: string, increment?: number }>): Promise<{ title: string, description: string } | undefined> {
648651
const pullRequestTemplates = await this._prManager.getPullRequestTemplates();
649652
let template: vscode.Uri | undefined;
650653

@@ -709,7 +712,7 @@ export class ReviewManager {
709712
return method;
710713
}
711714

712-
public async createPullRequest(draft=false): Promise<void> {
715+
public async createPullRequest(draft = false): Promise<void> {
713716
const pullRequestDefaults = await this._prManager.getPullRequestDefaults();
714717
const githubRemotes = this._prManager.getGitHubRemotes();
715718
const targetRemote = await this.getRemote(githubRemotes, 'Select the remote to send the pull request to',

0 commit comments

Comments
 (0)