Skip to content

Commit

Permalink
πŸ’„ comment sorting based on feedback (#224296)
Browse files Browse the repository at this point in the history
* πŸ’„ comment sorting based on feedback

* Align with extension view sorting
  • Loading branch information
alexr00 authored Jul 30, 2024
1 parent 117a4c7 commit b8606d6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
3 changes: 0 additions & 3 deletions src/vs/workbench/contrib/comments/browser/commentsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ interface ICommentThreadTemplateData {
disposables: IDisposable[];
}

class CommentsModelVirualDelegate implements IListVirtualDelegate<ResourceWithCommentThreads | CommentNode> {
class CommentsModelVirtualDelegate implements IListVirtualDelegate<ResourceWithCommentThreads | CommentNode> {
private static readonly RESOURCE_ID = 'resource-with-comments';
private static readonly COMMENT_ID = 'comment-node';

Expand All @@ -90,10 +90,10 @@ class CommentsModelVirualDelegate implements IListVirtualDelegate<ResourceWithCo

public getTemplateId(element: any): string {
if (element instanceof ResourceWithCommentThreads) {
return CommentsModelVirualDelegate.RESOURCE_ID;
return CommentsModelVirtualDelegate.RESOURCE_ID;
}
if (element instanceof CommentNode) {
return CommentsModelVirualDelegate.COMMENT_ID;
return CommentsModelVirtualDelegate.COMMENT_ID;
}

return '';
Expand Down Expand Up @@ -450,7 +450,7 @@ export class CommentsList extends WorkbenchObjectTree<CommentsModel | ResourceWi
@IContextMenuService private readonly contextMenuService: IContextMenuService,
@IKeybindingService private readonly keybindingService: IKeybindingService
) {
const delegate = new CommentsModelVirualDelegate();
const delegate = new CommentsModelVirtualDelegate();
const actionViewItemProvider = createActionViewItem.bind(undefined, instantiationService);
const menus = instantiationService.createInstance(CommentsMenus);
menus.setContextKeyService(contextKeyService);
Expand Down
20 changes: 18 additions & 2 deletions src/vs/workbench/contrib/comments/browser/commentsView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { IHoverService } from 'vs/platform/hover/browser/hover';
import { AccessibilityVerbositySettingId } from 'vs/workbench/contrib/accessibility/browser/accessibilityConfiguration';
import { AccessibleViewAction } from 'vs/workbench/contrib/accessibility/browser/accessibleViewActions';
import type { ITreeElement } from 'vs/base/browser/ui/tree/tree';
import { IPathService } from 'vs/workbench/services/path/common/pathService';

export const CONTEXT_KEY_HAS_COMMENTS = new RawContextKey<boolean>('commentsView.hasComments', false);
export const CONTEXT_KEY_SOME_COMMENTS_EXPANDED = new RawContextKey<boolean>('commentsView.someCommentsExpanded', false);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
},
Expand Down
30 changes: 27 additions & 3 deletions src/vs/workbench/contrib/comments/browser/commentsViewActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,32 @@ registerAction2(class extends ViewAction<ICommentsView> {
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<void> {
view.filters.sortBy = CommentsSortOrder.UpdatedAtDescending;
}
});

registerAction2(class extends ViewAction<ICommentsView> {
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',
Expand All @@ -221,8 +247,6 @@ registerAction2(class extends ViewAction<ICommentsView> {
}

async runInView(serviceAccessor: ServicesAccessor, view: ICommentsView): Promise<void> {
view.filters.sortBy = view.filters.sortBy === CommentsSortOrder.ResourceAscending
? CommentsSortOrder.UpdatedAtDescending
: CommentsSortOrder.ResourceAscending;
view.filters.sortBy = CommentsSortOrder.ResourceAscending;
}
});

0 comments on commit b8606d6

Please sign in to comment.