提交 f4dd1d13 编写于 作者: R Rob Lourens

Search tree - fix tests and match actions

上级 edd6891f
......@@ -4,10 +4,9 @@
*--------------------------------------------------------------------------------------------*/
import * as sinon from 'sinon';
import * as types from 'vs/base/common/types';
import { ServiceIdentifier } from 'vs/platform/instantiation/common/instantiation';
import { InstantiationService } from 'vs/platform/instantiation/common/instantiationService';
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { ServiceIdentifier } from 'vs/platform/instantiation/common/instantiation';
interface IServiceMock<T> {
id: ServiceIdentifier<T>;
......@@ -122,13 +121,6 @@ export class TestInstantiationService extends InstantiationService {
}
}
export function stubFunction<T>(ctor: any, fnProperty: string, value: any): T | sinon.SinonStub {
let stub = sinon.createStubInstance(ctor);
stub[fnProperty].restore();
sinon.stub(stub, fnProperty, types.isFunction(value) ? value : () => { return value; });
return stub;
}
interface SinonOptions {
mock?: boolean;
stub?: boolean;
......
......@@ -266,9 +266,6 @@ export class CollapseDeepestExpandedLevelAction extends Action {
const searchView = getSearchView(this.viewletService, this.panelService);
if (searchView) {
const viewer = searchView.getControl();
// if (viewer.getHighlight()) {
// return Promise.resolve(null); // Global action disabled if user is in edit mode from another action
// }
/**
* one level to collapse so collapse everything. If FolderMatch, check if there are visible grandchildren,
......
......@@ -27,7 +27,7 @@ import { attachBadgeStyler } from 'vs/platform/theme/common/styler';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { FileLabel } from 'vs/workbench/browser/labels';
import { RemoveAction, ReplaceAllAction, ReplaceAllInFolderAction } from 'vs/workbench/parts/search/browser/searchActions';
import { RemoveAction, ReplaceAllAction, ReplaceAllInFolderAction, ReplaceAction } from 'vs/workbench/parts/search/browser/searchActions';
import { SearchView } from 'vs/workbench/parts/search/browser/searchView';
import { FileMatch, FileMatchOrMatch, FolderMatch, Match, RenderableMatch, searchMatchComparer, SearchModel, SearchResult } from 'vs/workbench/parts/search/common/searchModel';
......@@ -85,8 +85,8 @@ export class FolderMatchRenderer extends Disposable implements ITreeRenderer<Fol
readonly templateId = FolderMatchRenderer.TEMPLATE_ID;
constructor(
private searchView: SearchView,
private searchModel: SearchModel,
private searchView: SearchView,
@IInstantiationService private instantiationService: IInstantiationService,
@IThemeService private themeService: IThemeService,
@IWorkspaceContextService protected contextService: IWorkspaceContextService
......@@ -200,6 +200,8 @@ export class MatchRenderer extends Disposable implements ITreeRenderer<Match, vo
constructor(
private searchModel: SearchModel,
private searchView: SearchView,
@IInstantiationService private instantiationService: IInstantiationService,
@IWorkspaceContextService protected contextService: IWorkspaceContextService,
@IConfigurationService private configurationService: IConfigurationService,
) {
......@@ -252,11 +254,11 @@ export class MatchRenderer extends Disposable implements ITreeRenderer<Match, vo
templateData.lineNumber.setAttribute('title', this.getMatchTitle(match, showLineNumbers));
templateData.actions.clear();
// if (searchModel.isReplaceActive()) {
// templateData.actions.push([this.instantiationService.createInstance(ReplaceAction, tree, match, this.searchView), new RemoveAction(tree, match)], { icon: true, label: false });
// } else {
// templateData.actions.push([new RemoveAction(tree, match)], { icon: true, label: false });
// }
if (this.searchModel.isReplaceActive()) {
templateData.actions.push([this.instantiationService.createInstance(ReplaceAction, this.searchView.getControl(), match, this.searchView), new RemoveAction(this.searchView, this.searchView.getControl(), match)], { icon: true, label: false });
} else {
templateData.actions.push([new RemoveAction(this.searchView, this.searchView.getControl(), match)], { icon: true, label: false });
}
}
disposeElement(element: ITreeNode<Match, any>, index: number, templateData: IMatchTemplate): void {
......
......@@ -92,7 +92,7 @@ function createFileIterator(fileMatch: FileMatch): Iterator<ITreeElement<Rendera
return Iterator.map(matchesIt, r => (<ITreeElement<RenderableMatch>>{ element: r }));
}
function createIterator(match: FolderMatch | FileMatch | SearchResult, collapseResults: ISearchConfigurationProperties['collapseResults']): Iterator<ITreeElement<RenderableMatch>> {
export function createIterator(match: FolderMatch | FileMatch | SearchResult, collapseResults: ISearchConfigurationProperties['collapseResults']): Iterator<ITreeElement<RenderableMatch>> {
return match instanceof SearchResult ? createResultIterator(match, collapseResults) :
match instanceof FolderMatch ? createFolderIterator(match, collapseResults) :
createFileIterator(match);
......@@ -574,9 +574,9 @@ export class SearchView extends Viewlet implements IViewlet, IPanel {
this.resultsElement,
delegate,
[
this._register(this.instantiationService.createInstance(FolderMatchRenderer, this, this.viewModel)),
this._register(this.instantiationService.createInstance(FolderMatchRenderer, this.viewModel, this)),
this._register(this.instantiationService.createInstance(FileMatchRenderer, this.viewModel, this)),
this._register(this.instantiationService.createInstance(MatchRenderer, this.viewModel)),
this._register(this.instantiationService.createInstance(MatchRenderer, this.viewModel, this)),
],
{});
......
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ITreeNavigator } from 'vs/base/browser/ui/tree/tree';
import { Emitter } from 'vs/base/common/event';
import { IDisposable } from 'vs/base/common/lifecycle';
const someEvent = new Emitter().event;
/**
* Add stub methods as needed
*/
export class MockObjectTree<T, TRef> implements IDisposable {
get onDidChangeFocus() { return someEvent; }
get onDidChangeSelection() { return someEvent; }
get onDidOpen() { return someEvent; }
get onMouseClick() { return someEvent; }
get onMouseDblClick() { return someEvent; }
get onContextMenu() { return someEvent; }
get onKeyDown() { return someEvent; }
get onKeyUp() { return someEvent; }
get onKeyPress() { return someEvent; }
get onDidFocus() { return someEvent; }
get onDidBlur() { return someEvent; }
get onDidChangeCollapseState() { return someEvent; }
get onDidChangeRenderNodeCount() { return someEvent; }
get onDidDispose() { return someEvent; }
constructor(private elements: any[]) { }
domFocus(): void { }
collapse(location: TRef, recursive: boolean = false): boolean {
return true;
}
expand(location: TRef, recursive: boolean = false): boolean {
return true;
}
navigate(start?: TRef): ITreeNavigator<T> {
const startIdx = start ? this.elements.indexOf(start) :
undefined;
return new ArrayNavigator(this.elements, startIdx);
}
dispose(): void {
}
}
class ArrayNavigator<T> implements ITreeNavigator<T> {
constructor(private elements: T[], private index = 0) { }
current(): T | null {
return this.elements[this.index];
}
previous(): T | null {
return this.elements[--this.index];
}
parent(): T | null {
throw new Error('not implemented');
}
first(): T | null {
this.index = 0;
return this.elements[this.index];
}
last(): T | null {
this.index = this.elements.length - 1;
return this.elements[this.index];
}
next(): T | null {
return this.elements[++this.index];
}
}
......@@ -2,22 +2,22 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { Keybinding } from 'vs/base/common/keyCodes';
import { OS } from 'vs/base/common/platform';
import { URI } from 'vs/base/common/uri';
import { TestInstantiationService, stubFunction } from 'vs/platform/instantiation/test/common/instantiationServiceMock';
import { Match, FileMatch, FileMatchOrMatch } from 'vs/workbench/parts/search/common/searchModel';
import { ReplaceAction } from 'vs/workbench/parts/search/browser/searchActions';
import { ArrayNavigator } from 'vs/base/common/iterator';
import { IFileMatch } from 'vs/platform/search/common/search';
import { IModelService } from 'vs/editor/common/services/modelService';
import { ModelServiceImpl } from 'vs/editor/common/services/modelServiceImpl';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService';
import { ModelServiceImpl } from 'vs/editor/common/services/modelServiceImpl';
import { IModelService } from 'vs/editor/common/services/modelService';
import { Tree } from 'vs/base/parts/tree/browser/treeImpl';
import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { USLayoutResolvedKeybinding } from 'vs/platform/keybinding/common/usLayoutResolvedKeybinding';
import { OS } from 'vs/base/common/platform';
import { Keybinding } from 'vs/base/common/keyCodes';
import { IFileMatch } from 'vs/platform/search/common/search';
import { ReplaceAction } from 'vs/workbench/parts/search/browser/searchActions';
import { FileMatch, FileMatchOrMatch, Match } from 'vs/workbench/parts/search/common/searchModel';
import { MockObjectTree } from 'vs/workbench/parts/search/test/browser/mockSearchTree';
suite('Search Actions', () => {
......@@ -148,7 +148,7 @@ suite('Search Actions', () => {
}
function aTree(elements: FileMatchOrMatch[]): any {
return stubFunction(Tree, 'getNavigator', () => { return new ArrayNavigator(elements); });
return new MockObjectTree(elements);
}
function stubModelService(instantiationService: TestInstantiationService): IModelService {
......
......@@ -13,8 +13,11 @@ import { IFileMatch, ITextSearchMatch, OneLineRange, QueryType } from 'vs/platfo
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { TestWorkspace } from 'vs/platform/workspace/test/common/testWorkspace';
import { SearchSorter } from 'vs/workbench/parts/search/browser/searchResultsView';
import { FileMatch, Match, SearchResult } from 'vs/workbench/parts/search/common/searchModel';
import { FileMatch, Match, SearchResult, RenderableMatch } from 'vs/workbench/parts/search/common/searchModel';
import { TestContextService } from 'vs/workbench/test/workbenchTestServices';
import { createIterator } from 'vs/workbench/parts/search/browser/searchView';
import { ITreeElement } from 'vs/base/browser/ui/tree/tree';
import { Iterator } from 'vs/base/common/iterator';
suite('Search - Viewlet', () => {
let instantiation: TestInstantiationService;
......@@ -25,50 +28,49 @@ suite('Search - Viewlet', () => {
instantiation.set(IWorkspaceContextService, new TestContextService(TestWorkspace));
});
// test('Data Source', function () {
// let ds = instantiation.createInstance(SearchDataSource);
// let result: SearchResult = instantiation.createInstance(SearchResult, null);
// result.query = {
// type: QueryType.Text,
// contentPattern: { pattern: 'foo' },
// folderQueries: [{
// folder: uri.parse('file://c:/')
// }]
// };
test('Data Source', function () {
let result: SearchResult = instantiation.createInstance(SearchResult, null);
result.query = {
type: QueryType.Text,
contentPattern: { pattern: 'foo' },
folderQueries: [{
folder: uri.parse('file://c:/')
}]
};
result.add([{
resource: uri.parse('file:///c:/foo'),
results: [{
preview: {
text: 'bar',
matches: {
startLineNumber: 0,
startColumn: 0,
endLineNumber: 0,
endColumn: 1
}
},
ranges: {
startLineNumber: 1,
startColumn: 0,
endLineNumber: 1,
endColumn: 1
}
}]
}]);
// result.add([{
// resource: uri.parse('file:///c:/foo'),
// results: [{
// preview: {
// text: 'bar',
// matches: {
// startLineNumber: 0,
// startColumn: 0,
// endLineNumber: 0,
// endColumn: 1
// }
// },
// ranges: {
// startLineNumber: 1,
// startColumn: 0,
// endLineNumber: 1,
// endColumn: 1
// }
// }]
// }]);
let fileMatch = result.matches()[0];
let lineMatch = fileMatch.matches()[0];
// let fileMatch = result.matches()[0];
// let lineMatch = fileMatch.matches()[0];
assert.equal(fileMatch.id(), 'file:///c%3A/foo');
assert.equal(lineMatch.id(), 'file:///c%3A/foo>[2,1 -> 2,2]b');
// assert.equal(ds.getId(null, result), 'root');
// assert.equal(ds.getId(null, fileMatch), 'file:///c%3A/foo');
// assert.equal(ds.getId(null, lineMatch), 'file:///c%3A/foo>[2,1 -> 2,2]b');
const resultIterator = createIterator(result, 'auto');
const first = resultIterator.next();
// assert(!ds.hasChildren(null, 'foo'));
// assert(ds.hasChildren(null, result));
// assert(ds.hasChildren(null, fileMatch));
// assert(!ds.hasChildren(null, lineMatch));
// });
assert(!!first.value.children);
assert.equal((<Iterator<ITreeElement<RenderableMatch>>>first.value.children).next().value.element.id(), 'file:///c%3A/foo>[2,1 -> 2,2]b');
});
test('Sorter', () => {
let fileMatch1 = aFileMatch('C:\\foo');
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册