Skip to content

Commit b2fc320

Browse files
Copilotalexr00
andauthored
Add collapsePreexisting setting to keep newly added comments expanded (#8270)
* Initial plan * Initial exploration: understand commentExpandState behavior Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com> * Changes before error encountered Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com> * Add collapsePreexisting setting to keep newly created comments expanded Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com> * Fix TypeScript errors by removing unused isJustCreated parameter Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com> * Pass currentUser to updateThread for newly created comments Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com> * Add comments explaining why currentUser is not passed for changed threads Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com> * Change collapsePreexisting to detect any newly added comment, not just from current user Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com> * Remove unrelated file change from commit Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com> * Remove comments --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent bc7d7ad commit b2fc320

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,13 @@
360360
"type": "string",
361361
"enum": [
362362
"expandUnresolved",
363-
"collapseAll"
363+
"collapseAll",
364+
"collapsePreexisting"
364365
],
365366
"enumDescriptions": [
366367
"%githubPullRequests.commentExpandState.expandUnresolved%",
367-
"%githubPullRequests.commentExpandState.collapseAll%"
368+
"%githubPullRequests.commentExpandState.collapseAll%",
369+
"%githubPullRequests.commentExpandState.collapsePreexisting%"
368370
],
369371
"default": "expandUnresolved",
370372
"description": "%githubPullRequests.commentExpandState.description%"

package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"githubPullRequests.commentExpandState.description": "Controls whether comments are expanded when a document with comments is opened. Requires a reload to take effect for comments that have already been added.",
5252
"githubPullRequests.commentExpandState.expandUnresolved": "All unresolved comments will be expanded.",
5353
"githubPullRequests.commentExpandState.collapseAll": "All comments will be collapsed.",
54+
"githubPullRequests.commentExpandState.collapsePreexisting": "Only pre-existing comments will be collapsed. Newly added comments will remain expanded.",
5455
"githubPullRequests.useReviewMode.description": "Choose which pull request states will use review mode. \"Open\" pull requests will always use review mode. Setting to \"auto\" will use review mode for open, closed, and merged pull requests in web, but only open pull requests on desktop.",
5556
"githubPullRequests.useReviewMode.merged": "Use review mode for merged pull requests.",
5657
"githubPullRequests.useReviewMode.closed": "Use review mode for closed pull requests. Merged pull requests are not considered \"closed\".",

src/github/utils.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,23 +153,30 @@ function isResolvedToResolvedState(isResolved: boolean) {
153153

154154
export const COMMENT_EXPAND_STATE_SETTING = 'commentExpandState';
155155
export const COMMENT_EXPAND_STATE_COLLAPSE_VALUE = 'collapseAll';
156+
export const COMMENT_EXPAND_STATE_COLLAPSE_PREEXISTING_VALUE = 'collapsePreexisting';
156157
export const COMMENT_EXPAND_STATE_EXPAND_VALUE = 'expandUnresolved';
157-
export function getCommentCollapsibleState(thread: IReviewThread, expand?: boolean, currentUser?: string) {
158+
export function getCommentCollapsibleState(thread: IReviewThread, expand?: boolean, currentUser?: string, isNewlyAdded?: boolean) {
159+
const config = vscode.workspace.getConfiguration(PR_SETTINGS_NAMESPACE)?.get(COMMENT_EXPAND_STATE_SETTING);
158160
const isFromCurrent = (currentUser && (thread.comments[thread.comments.length - 1].user?.login === currentUser));
159161
const isJustSuggestion = thread.comments.length === 1 && thread.comments[0].body.startsWith('```suggestion') && thread.comments[0].body.endsWith('```');
162+
163+
// When collapsePreexisting is set, keep newly added comments expanded
164+
if (config === COMMENT_EXPAND_STATE_COLLAPSE_PREEXISTING_VALUE && isNewlyAdded && !isJustSuggestion) {
165+
return vscode.CommentThreadCollapsibleState.Expanded;
166+
}
167+
160168
if (thread.isResolved || (!thread.isOutdated && isFromCurrent && !isJustSuggestion)) {
161169
return vscode.CommentThreadCollapsibleState.Collapsed;
162170
}
163171
if (expand === undefined) {
164-
const config = vscode.workspace.getConfiguration(PR_SETTINGS_NAMESPACE)?.get(COMMENT_EXPAND_STATE_SETTING);
165172
expand = config === COMMENT_EXPAND_STATE_EXPAND_VALUE;
166173
}
167174
return expand
168175
? vscode.CommentThreadCollapsibleState.Expanded : vscode.CommentThreadCollapsibleState.Collapsed;
169176
}
170177

171178

172-
export function updateThreadWithRange(context: vscode.ExtensionContext, vscodeThread: GHPRCommentThread, reviewThread: IReviewThread, githubRepositories?: GitHubRepository[], expand?: boolean) {
179+
export function updateThreadWithRange(context: vscode.ExtensionContext, vscodeThread: GHPRCommentThread, reviewThread: IReviewThread, githubRepositories?: GitHubRepository[], expand?: boolean, isNewlyAdded?: boolean) {
173180
if (!vscodeThread.range) {
174181
return;
175182
}
@@ -178,13 +185,13 @@ export function updateThreadWithRange(context: vscode.ExtensionContext, vscodeTh
178185
if (editor.document.uri.toString() === vscodeThread.uri.toString()) {
179186
const endLine = editor.document.lineAt(vscodeThread.range.end.line);
180187
const range = new vscode.Range(vscodeThread.range.start.line, 0, vscodeThread.range.end.line, endLine.text.length);
181-
updateThread(context, vscodeThread, reviewThread, githubRepositories, expand, range);
188+
updateThread(context, vscodeThread, reviewThread, githubRepositories, expand, range, undefined, isNewlyAdded);
182189
break;
183190
}
184191
}
185192
}
186193

187-
export function updateThread(context: vscode.ExtensionContext, vscodeThread: GHPRCommentThread, reviewThread: IReviewThread, githubRepositories?: GitHubRepository[], expand?: boolean, range?: vscode.Range) {
194+
export function updateThread(context: vscode.ExtensionContext, vscodeThread: GHPRCommentThread, reviewThread: IReviewThread, githubRepositories?: GitHubRepository[], expand?: boolean, range?: vscode.Range, currentUser?: string, isNewlyAdded?: boolean) {
188195
if (reviewThread.viewerCanResolve && !reviewThread.isResolved) {
189196
vscodeThread.contextValue = 'canResolve';
190197
} else if (reviewThread.viewerCanUnresolve && reviewThread.isResolved) {
@@ -203,7 +210,7 @@ export function updateThread(context: vscode.ExtensionContext, vscodeThread: GHP
203210
applicability: newApplicabilityState
204211
};
205212
}
206-
vscodeThread.collapsibleState = getCommentCollapsibleState(reviewThread, expand);
213+
vscodeThread.collapsibleState = getCommentCollapsibleState(reviewThread, expand, currentUser, isNewlyAdded);
207214
if (range) {
208215
vscodeThread.range = range;
209216
}

src/view/pullRequestCommentController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ export class PullRequestCommentController extends CommentControllerBase implemen
241241
newThread = this._pendingCommentThreadAdds[index];
242242
newThread.gitHubThreadId = thread.id;
243243
newThread.comments = thread.comments.map(c => new GHPRComment(this._context, c, newThread!, this._githubRepositories));
244-
updateThreadWithRange(this._context, newThread, thread, this._githubRepositories);
244+
updateThreadWithRange(this._context, newThread, thread, this._githubRepositories, undefined, true);
245245
this._pendingCommentThreadAdds.splice(index, 1);
246246
} else {
247247
const openPREditors = await this.getPREditors(vscode.window.visibleTextEditors);

src/view/reviewCommentController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ export class ReviewCommentController extends CommentControllerBase implements Co
297297
newThread = this._pendingCommentThreadAdds[index];
298298
newThread.gitHubThreadId = thread.id;
299299
newThread.comments = thread.comments.map(c => new GHPRComment(this._context, c, newThread, githubRepositories));
300-
updateThreadWithRange(this._context, newThread, thread, githubRepositories);
300+
updateThreadWithRange(this._context, newThread, thread, githubRepositories, undefined, true);
301301
this._pendingCommentThreadAdds.splice(index, 1);
302302
} else {
303303
const fullPath = nodePath.join(this._repository.rootUri.path, path).replace(/\\/g, '/');

0 commit comments

Comments
 (0)