From b8606d68755cead4023ea312e501b6b870f7c8c4 Mon Sep 17 00:00:00 2001 From: Alex Ross Date: Tue, 30 Jul 2024 16:10:33 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=84=20comment=20sorting=20based=20on?= =?UTF-8?q?=20feedback=20(#224296)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 💄 comment sorting based on feedback * Align with extension view sorting --- .../contrib/comments/browser/commentsModel.ts | 3 -- .../comments/browser/commentsTreeViewer.ts | 8 ++--- .../contrib/comments/browser/commentsView.ts | 20 +++++++++++-- .../comments/browser/commentsViewActions.ts | 30 +++++++++++++++++-- 4 files changed, 49 insertions(+), 12 deletions(-) diff --git a/src/vs/workbench/contrib/comments/browser/commentsModel.ts b/src/vs/workbench/contrib/comments/browser/commentsModel.ts index d0701d5f344b7..4a1661aa0b99a 100644 --- a/src/vs/workbench/contrib/comments/browser/commentsModel.ts +++ b/src/vs/workbench/contrib/comments/browser/commentsModel.ts @@ -38,9 +38,6 @@ export class CommentsModel extends Disposable implements ICommentsModel { return resource; }).flat(); }).flat(); - this._resourceCommentThreads.sort((a, b) => { - return a.resource.toString() > b.resource.toString() ? 1 : -1; - }); } public setCommentThreads(uniqueOwner: string, owner: string, ownerLabel: string, commentThreads: CommentThread[]): void { diff --git a/src/vs/workbench/contrib/comments/browser/commentsTreeViewer.ts b/src/vs/workbench/contrib/comments/browser/commentsTreeViewer.ts index c6855a6479835..29100b80901ad 100644 --- a/src/vs/workbench/contrib/comments/browser/commentsTreeViewer.ts +++ b/src/vs/workbench/contrib/comments/browser/commentsTreeViewer.ts @@ -76,7 +76,7 @@ interface ICommentThreadTemplateData { disposables: IDisposable[]; } -class CommentsModelVirualDelegate implements IListVirtualDelegate { +class CommentsModelVirtualDelegate implements IListVirtualDelegate { private static readonly RESOURCE_ID = 'resource-with-comments'; private static readonly COMMENT_ID = 'comment-node'; @@ -90,10 +90,10 @@ class CommentsModelVirualDelegate implements IListVirtualDelegate('commentsView.hasComments', false); export const CONTEXT_KEY_SOME_COMMENTS_EXPANDED = new RawContextKey('commentsView.someCommentsExpanded', false); @@ -140,7 +141,8 @@ export class CommentsPanel extends FilterViewPane implements ICommentsView { @ITelemetryService telemetryService: ITelemetryService, @IHoverService hoverService: IHoverService, @IUriIdentityService private readonly uriIdentityService: IUriIdentityService, - @IStorageService storageService: IStorageService + @IStorageService storageService: IStorageService, + @IPathService private readonly pathService: IPathService ) { const stateMemento = new Memento(VIEW_STORAGE_ID, storageService); const viewState = stateMemento.getMemento(StorageScope.WORKSPACE, StorageTarget.MACHINE); @@ -400,8 +402,22 @@ export class CommentsPanel extends FilterViewPane implements ICommentsView { filter: this.filter, sorter: { compare: (a: CommentsTreeNode, b: CommentsTreeNode) => { - if (this.filters.sortBy === CommentsSortOrder.UpdatedAtDescending && !(a instanceof CommentsModel) && !(b instanceof CommentsModel)) { + if (a instanceof CommentsModel || b instanceof CommentsModel) { + return 0; + } + if (this.filters.sortBy === CommentsSortOrder.UpdatedAtDescending) { return a.lastUpdatedAt > b.lastUpdatedAt ? -1 : 1; + } else if (this.filters.sortBy === CommentsSortOrder.ResourceAscending) { + if (a instanceof ResourceWithCommentThreads && b instanceof ResourceWithCommentThreads) { + const workspaceScheme = this.pathService.defaultUriScheme; + if ((a.resource.scheme !== b.resource.scheme) && (a.resource.scheme === workspaceScheme || b.resource.scheme === workspaceScheme)) { + // Workspace scheme should always come first + return b.resource.scheme === workspaceScheme ? 1 : -1; + } + return a.resource.toString() > b.resource.toString() ? 1 : -1; + } else if (a instanceof CommentNode && b instanceof CommentNode && a.thread.range && b.thread.range) { + return a.thread.range?.startLineNumber > b.thread.range?.startLineNumber ? 1 : -1; + } } return 0; }, diff --git a/src/vs/workbench/contrib/comments/browser/commentsViewActions.ts b/src/vs/workbench/contrib/comments/browser/commentsViewActions.ts index 05440049e5df3..8fedca8e845e6 100644 --- a/src/vs/workbench/contrib/comments/browser/commentsViewActions.ts +++ b/src/vs/workbench/contrib/comments/browser/commentsViewActions.ts @@ -211,6 +211,32 @@ registerAction2(class extends ViewAction { condition: ContextKeyExpr.equals('commentsView.sortBy', CommentsSortOrder.UpdatedAtDescending), title: localize('sorting by updated at', "Updated Time"), }, + menu: { + id: commentSortSubmenu, + group: 'navigation', + order: 1, + isHiddenByDefault: false, + }, + }); + } + + async runInView(serviceAccessor: ServicesAccessor, view: ICommentsView): Promise { + view.filters.sortBy = CommentsSortOrder.UpdatedAtDescending; + } +}); + +registerAction2(class extends ViewAction { + constructor() { + super({ + id: `workbench.actions.${COMMENTS_VIEW_ID}.toggleSortByResource`, + title: localize('toggle sorting by resource', "File"), + category: localize('comments', "Comments"), + icon: Codicon.history, + viewId: COMMENTS_VIEW_ID, + toggled: { + condition: ContextKeyExpr.equals('commentsView.sortBy', CommentsSortOrder.ResourceAscending), + title: localize('sorting by file', "File"), + }, menu: { id: commentSortSubmenu, group: 'navigation', @@ -221,8 +247,6 @@ registerAction2(class extends ViewAction { } async runInView(serviceAccessor: ServicesAccessor, view: ICommentsView): Promise { - view.filters.sortBy = view.filters.sortBy === CommentsSortOrder.ResourceAscending - ? CommentsSortOrder.UpdatedAtDescending - : CommentsSortOrder.ResourceAscending; + view.filters.sortBy = CommentsSortOrder.ResourceAscending; } });