Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flitering for comments #158264

Merged
merged 6 commits into from Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/vs/workbench/contrib/comments/browser/comments.ts
@@ -0,0 +1,25 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { Event } from 'vs/base/common/event';
import { RawContextKey } from 'vs/platform/contextkey/common/contextkey';
import { IView } from 'vs/workbench/common/views';
import { CommentsFilters } from 'vs/workbench/contrib/comments/browser/commentsViewActions';

export const CommentsViewFilterFocusContextKey = new RawContextKey<boolean>('commentsFilterFocus', false);
export const CommentsViewSmallLayoutContextKey = new RawContextKey<boolean>(`commentsView.smallLayout`, false);

export interface ICommentsView extends IView {

readonly onDidFocusFilter: Event<void>;
readonly onDidClearFilterText: Event<void>;
readonly filters: CommentsFilters;
readonly onDidChangeFilterStats: Event<{ total: number; filtered: number }>;
focusFilter(): void;
clearFilterText(): void;
getFilterStats(): { total: number; filtered: number };

collapseAll(): void;
}
30 changes: 30 additions & 0 deletions src/vs/workbench/contrib/comments/browser/commentsFilterOptions.ts
@@ -0,0 +1,30 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { IFilter, matchesFuzzy, matchesFuzzy2 } from 'vs/base/common/filters';
import * as strings from 'vs/base/common/strings';

export class FilterOptions {

static readonly _filter: IFilter = matchesFuzzy2;
static readonly _messageFilter: IFilter = matchesFuzzy;

readonly showResolved: boolean = true;
readonly showUnresolved: boolean = true;
readonly textFilter: { readonly text: string; readonly negate: boolean };

constructor(
readonly filter: string,
showResolved: boolean,
showUnresolved: boolean,
) {
filter = filter.trim();
this.showResolved = showResolved;
this.showUnresolved = showUnresolved;

const negate = filter.startsWith('!');
this.textFilter = { text: (negate ? strings.ltrim(filter, '!') : filter).trim(), negate };
}
}
106 changes: 102 additions & 4 deletions src/vs/workbench/contrib/comments/browser/commentsTreeViewer.ts
Expand Up @@ -11,7 +11,7 @@ import { IDisposable, DisposableStore } from 'vs/base/common/lifecycle';
import { IOpenerService } from 'vs/platform/opener/common/opener';
import { IResourceLabel, ResourceLabels } from 'vs/workbench/browser/labels';
import { CommentNode, CommentsModel, ResourceWithCommentThreads } from 'vs/workbench/contrib/comments/common/commentModel';
import { IAsyncDataSource, ITreeNode } from 'vs/base/browser/ui/tree/tree';
import { IAsyncDataSource, ITreeFilter, ITreeNode, TreeFilterResult, TreeVisibility } from 'vs/base/browser/ui/tree/tree';
import { IListVirtualDelegate, IListRenderer } from 'vs/base/browser/ui/list/list';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
Expand All @@ -25,6 +25,9 @@ import { IMarkdownString } from 'vs/base/common/htmlContent';
import { commentViewThreadStateColorVar, getCommentThreadStateColor } from 'vs/workbench/contrib/comments/browser/commentColors';
import { CommentThreadState } from 'vs/editor/common/languages';
import { Color } from 'vs/base/common/color';
import { IMatch } from 'vs/base/common/filters';
import { FilterOptions } from 'vs/workbench/contrib/comments/browser/commentsFilterOptions';
import { basename } from 'vs/base/common/resources';

export const COMMENTS_VIEW_ID = 'workbench.panel.comments';
export const COMMENTS_VIEW_STORAGE_ID = 'Comments';
Expand Down Expand Up @@ -70,7 +73,7 @@ interface ICommentThreadTemplateData {
disposables: IDisposable[];
}

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

Expand Down Expand Up @@ -253,7 +256,81 @@ export interface ICommentsListOptions extends IWorkbenchAsyncDataTreeOptions<any
overrideStyles?: IColorMapping;
}

export class CommentsList extends WorkbenchAsyncDataTree<any, any> {
const enum FilterDataType {
Resource,
Comment
}

interface ResourceFilterData {
type: FilterDataType.Resource;
uriMatches: IMatch[];
}

interface CommentFilterData {
type: FilterDataType.Comment;
textMatches: IMatch[];
}

export type FilterData = ResourceFilterData | CommentFilterData;

export class Filter implements ITreeFilter<ResourceWithCommentThreads | CommentNode, FilterData> {

constructor(public options: FilterOptions) { }

filter(element: ResourceWithCommentThreads | CommentNode, parentVisibility: TreeVisibility): TreeFilterResult<FilterData> {
if (element instanceof ResourceWithCommentThreads) {
return this.filterResourceMarkers(element);
} else {
return this.filterCommentNode(element, parentVisibility);
}
}

private filterResourceMarkers(resourceMarkers: ResourceWithCommentThreads): TreeFilterResult<FilterData> {
// Filter by text. Do not apply negated filters on resources instead use exclude patterns
if (this.options.textFilter.text && !this.options.textFilter.negate) {
const uriMatches = FilterOptions._filter(this.options.textFilter.text, basename(resourceMarkers.resource));
if (uriMatches) {
return { visibility: true, data: { type: FilterDataType.Resource, uriMatches: uriMatches || [] } };
}
}

return TreeVisibility.Recurse;
}

private filterCommentNode(comment: CommentNode, parentVisibility: TreeVisibility): TreeFilterResult<FilterData> {
const matchesResolvedState = (comment.threadState === undefined) || (this.options.showResolved && CommentThreadState.Resolved === comment.threadState) ||
(this.options.showUnresolved && CommentThreadState.Unresolved === comment.threadState);

if (!matchesResolvedState) {
return false;
}

if (!this.options.textFilter.text) {
return true;
}

const textMatches = FilterOptions._messageFilter(this.options.textFilter.text, typeof comment.comment.body === 'string' ? comment.comment.body : comment.comment.body.value);

// Matched and not negated
if (textMatches && !this.options.textFilter.negate) {
return { visibility: true, data: { type: FilterDataType.Comment, textMatches } };
}

// Matched and negated - exclude it only if parent visibility is not set
if (textMatches && this.options.textFilter.negate && parentVisibility === TreeVisibility.Recurse) {
return false;
}

// Not matched and negated - include it only if parent visibility is not set
if (!textMatches && this.options.textFilter.negate && parentVisibility === TreeVisibility.Recurse) {
return true;
}

return parentVisibility;
}
}

export class CommentsList extends WorkbenchAsyncDataTree<CommentsModel | ResourceWithCommentThreads | CommentNode, any> {
constructor(
labels: ResourceLabels,
container: HTMLElement,
Expand Down Expand Up @@ -304,7 +381,9 @@ export class CommentsList extends WorkbenchAsyncDataTree<any, any> {
collapseByDefault: () => {
return false;
},
overrideStyles: options.overrideStyles
overrideStyles: options.overrideStyles,
filter: options.filter,
findWidgetEnabled: false
},
instantiationService,
contextKeyService,
Expand All @@ -313,4 +392,23 @@ export class CommentsList extends WorkbenchAsyncDataTree<any, any> {
configurationService
);
}

filterComments(): void {
this.refilter();
}

getVisibleItemCount(): number {
let filtered = 0;
const root = this.getNode();

for (const resourceNode of root.children) {
for (const commentNode of resourceNode.children) {
if (commentNode.visible && resourceNode.visible) {
filtered++;
}
}
}

return filtered;
}
}