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

Distinguish a user removing a row from 'clear' in search #96803

Merged
merged 2 commits into from May 3, 2020
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
2 changes: 1 addition & 1 deletion src/vs/workbench/contrib/search/browser/searchActions.ts
Expand Up @@ -615,7 +615,7 @@ export class RemoveAction extends AbstractSearchAndReplaceAction {
this.viewer.setFocus([nextFocusElement], getSelectionKeyboardEvent());
}

this.element.parent().remove(<any>this.element);
this.element.parent().remove(<any>this.element, true);
this.viewer.domFocus();

return Promise.resolve();
Expand Down
37 changes: 19 additions & 18 deletions src/vs/workbench/contrib/search/common/searchModel.ts
Expand Up @@ -192,8 +192,8 @@ export class FileMatch extends Disposable implements IFileMatch {
return (selected ? FileMatch._CURRENT_FIND_MATCH : FileMatch._FIND_MATCH);
}

private _onChange = this._register(new Emitter<{ didRemove?: boolean; forceUpdateModel?: boolean }>());
readonly onChange: Event<{ didRemove?: boolean; forceUpdateModel?: boolean }> = this._onChange.event;
private _onChange = this._register(new Emitter<{ didRemove?: boolean; forceUpdateModel?: boolean; userRemoved?: boolean }>());
readonly onChange: Event<{ didRemove?: boolean; forceUpdateModel?: boolean; userRemoved?: boolean }> = this._onChange.event;

private _onDispose = this._register(new Emitter<void>());
readonly onDispose: Event<void> = this._onDispose.event;
Expand Down Expand Up @@ -354,10 +354,10 @@ export class FileMatch extends Disposable implements IFileMatch {
return values(this._matches);
}

remove(match: Match): void {
remove(match: Match, userRemoved?: boolean): void {
this.removeMatch(match);
this._removedMatches.add(match.id());
this._onChange.fire({ didRemove: true });
this._onChange.fire({ didRemove: true, userRemoved });
}

replace(toReplace: Match): Promise<void> {
Expand Down Expand Up @@ -447,6 +447,7 @@ export interface IChangeEvent {
elements: FileMatch[];
added?: boolean;
removed?: boolean;
userRemoved?: boolean;
}

export class FolderMatch extends Disposable {
Expand Down Expand Up @@ -529,7 +530,7 @@ export class FolderMatch extends Disposable {
const fileMatch = this.instantiationService.createInstance(FileMatch, this._query.contentPattern, this._query.previewOptions, this._query.maxResults, this, rawFileMatch);
this.doAdd(fileMatch);
added.push(fileMatch);
const disposable = fileMatch.onChange(({ didRemove }) => this.onFileChange(fileMatch, didRemove));
const disposable = fileMatch.onChange(({ didRemove, userRemoved }) => this.onFileChange(fileMatch, didRemove, userRemoved));
fileMatch.onDispose(() => disposable.dispose());
}
});
Expand All @@ -540,14 +541,14 @@ export class FolderMatch extends Disposable {
}
}

clear(): void {
clear(userRemoved?: boolean): void {
const changed: FileMatch[] = this.matches();
this.disposeMatches();
this._onChange.fire({ elements: changed, removed: true });
this._onChange.fire({ elements: changed, removed: true, userRemoved });
}

remove(matches: FileMatch | FileMatch[]): void {
this.doRemove(matches);
remove(matches: FileMatch | FileMatch[], userRemoved?: boolean): void {
this.doRemove(matches, undefined, undefined, userRemoved);
}

replace(match: FileMatch): Promise<any> {
Expand Down Expand Up @@ -577,7 +578,7 @@ export class FolderMatch extends Disposable {
return this.matches().reduce<number>((prev, match) => prev + match.count(), 0);
}

private onFileChange(fileMatch: FileMatch, removed = false): void {
private onFileChange(fileMatch: FileMatch, removed = false, userRemoved = false): void {
let added = false;
if (!this._fileMatches.has(fileMatch.resource)) {
this.doAdd(fileMatch);
Expand All @@ -589,7 +590,7 @@ export class FolderMatch extends Disposable {
removed = true;
}
if (!this._replacingAll) {
this._onChange.fire({ elements: [fileMatch], added: added, removed: removed });
this._onChange.fire({ elements: [fileMatch], added: added, removed: removed, userRemoved });
}
}

Expand All @@ -600,7 +601,7 @@ export class FolderMatch extends Disposable {
}
}

private doRemove(fileMatches: FileMatch | FileMatch[], dispose: boolean = true, trigger: boolean = true): void {
private doRemove(fileMatches: FileMatch | FileMatch[], dispose: boolean = true, trigger: boolean = true, userRemoved: boolean = false): void {
if (!Array.isArray(fileMatches)) {
fileMatches = [fileMatches];
}
Expand All @@ -615,7 +616,7 @@ export class FolderMatch extends Disposable {
}

if (trigger) {
this._onChange.fire({ elements: fileMatches, removed: true });
this._onChange.fire({ elements: fileMatches, removed: true, userRemoved });
}
}

Expand Down Expand Up @@ -717,7 +718,7 @@ export class SearchResult extends Disposable {
this._register(this.modelService.onModelAdded(model => this.onModelAdded(model)));

this._register(this.onChange(e => {
if (e.removed) {
if (e.userRemoved) {
this._hasRemovedResults = true;
}
}));
Expand Down Expand Up @@ -808,14 +809,14 @@ export class SearchResult extends Disposable {
this._otherFilesMatch = null;
}

remove(matches: FileMatch | FolderMatch | (FileMatch | FolderMatch)[]): void {
remove(matches: FileMatch | FolderMatch | (FileMatch | FolderMatch)[], userRemoved?: boolean): void {
if (!Array.isArray(matches)) {
matches = [matches];
}

matches.forEach(m => {
if (m instanceof FolderMatch) {
m.clear();
m.clear(userRemoved);
}
});

Expand All @@ -827,11 +828,11 @@ export class SearchResult extends Disposable {
return;
}

this.getFolderMatch(matches[0].resource).remove(<FileMatch[]>matches);
this.getFolderMatch(matches[0].resource).remove(<FileMatch[]>matches, userRemoved);
});

if (other.length) {
this.getFolderMatch(other[0].resource).remove(<FileMatch[]>other);
this.getFolderMatch(other[0].resource).remove(<FileMatch[]>other, userRemoved);
}
}

Expand Down
Expand Up @@ -236,7 +236,7 @@ suite('SearchResult', () => {
testObject.remove(objectToRemove);

assert.ok(target.calledOnce);
assert.deepEqual([{ elements: [objectToRemove], removed: true }], target.args[0]);
assert.deepEqual([{ elements: [objectToRemove], removed: true, userRemoved: false }], target.args[0]);
});

test('remove array triggers change event', function () {
Expand All @@ -253,7 +253,7 @@ suite('SearchResult', () => {
testObject.remove(arrayToRemove);

assert.ok(target.calledOnce);
assert.deepEqual([{ elements: arrayToRemove, removed: true }], target.args[0]);
assert.deepEqual([{ elements: arrayToRemove, removed: true, userRemoved: false }], target.args[0]);
});

test('remove triggers change event', function () {
Expand All @@ -268,7 +268,7 @@ suite('SearchResult', () => {
testObject.remove(objectToRemove);

assert.ok(target.calledOnce);
assert.deepEqual([{ elements: [objectToRemove], removed: true }], target.args[0]);
assert.deepEqual([{ elements: [objectToRemove], removed: true, userRemoved: false }], target.args[0]);
});

test('Removing all line matches and adding back will add file back to result', function () {
Expand Down Expand Up @@ -315,7 +315,7 @@ suite('SearchResult', () => {

return voidPromise.then(() => {
assert.ok(target.calledOnce);
assert.deepEqual([{ elements: [objectToRemove], removed: true }], target.args[0]);
assert.deepEqual([{ elements: [objectToRemove], removed: true, userRemoved: false }], target.args[0]);
});
});

Expand Down