Skip to content

Commit

Permalink
when the 'query' string is getting longer it's save to score afore sc…
Browse files Browse the repository at this point in the history
…ored/filtered items only, #15419
  • Loading branch information
jrieken committed Nov 24, 2017
1 parent 0b0f7ab commit 3d2d63d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
33 changes: 23 additions & 10 deletions src/vs/editor/contrib/suggest/completionModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,28 @@ export class LineContext {
characterCountDelta: number;
}

const enum Refilter {
Nothing = 0,
All = 1,
Incr = 2
}

export class CompletionModel {

private readonly _column: number;
private readonly _items: ISuggestionItem[];
private readonly _items: ICompletionItem[];
private readonly _snippetCompareFn = CompletionModel._compareCompletionItems;

private _lineContext: LineContext;
private _refilterKind: Refilter;
private _filteredItems: ICompletionItem[];
private _isIncomplete: boolean;
private _stats: ICompletionStats;

constructor(items: ISuggestionItem[], column: number, lineContext: LineContext, snippetConfig?: SnippetConfig) {
this._items = items;
this._column = column;
this._refilterKind = Refilter.All;
this._lineContext = lineContext;

if (snippetConfig === 'top') {
Expand Down Expand Up @@ -78,10 +86,10 @@ export class CompletionModel {

set lineContext(value: LineContext) {
if (this._lineContext.leadingLineContent !== value.leadingLineContent
|| this._lineContext.characterCountDelta !== value.characterCountDelta) {

|| this._lineContext.characterCountDelta !== value.characterCountDelta
) {
this._refilterKind = this._lineContext.characterCountDelta < value.characterCountDelta ? Refilter.Incr : Refilter.All;
this._lineContext = value;
this._filteredItems = undefined;
}
}

Expand Down Expand Up @@ -116,22 +124,26 @@ export class CompletionModel {
}

private _ensureCachedState(): void {
if (!this._filteredItems) {
if (this._refilterKind !== Refilter.Nothing) {
this._createCachedState();
}
}

private _createCachedState(): void {
this._filteredItems = [];

this._isIncomplete = false;
this._stats = { suggestionCount: 0, snippetCount: 0, textCount: 0 };

const { leadingLineContent, characterCountDelta } = this._lineContext;
let word = '';

for (let i = 0; i < this._items.length; i++) {
// incrementally filter less
const source = this._refilterKind === Refilter.All ? this._items : this._filteredItems;
const target: typeof source = [];

for (let i = 0; i < source.length; i++) {

const item = <ICompletionItem>this._items[i];
const item = source[i];
const { suggestion, container } = item;

// collect those supports that signaled having
Expand Down Expand Up @@ -182,7 +194,7 @@ export class CompletionModel {

item.idx = i;

this._filteredItems.push(item);
target.push(item);

// update stats
this._stats.suggestionCount++;
Expand All @@ -192,7 +204,8 @@ export class CompletionModel {
}
}

this._filteredItems.sort(this._snippetCompareFn);
this._filteredItems = target.sort(this._snippetCompareFn);
this._refilterKind = Refilter.Nothing;
}

private static _compareCompletionItems(a: ICompletionItem, b: ICompletionItem): number {
Expand Down
27 changes: 26 additions & 1 deletion src/vs/editor/contrib/suggest/test/completionModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,32 @@ suite('CompletionModel', function () {
const [first, second] = model.items;
assert.equal(first.suggestion.label, 'source');
assert.equal(second.suggestion.label, '<- groups');

});

test('Score only filtered items when typing more, score all when typing less', function () {
model = new CompletionModel([
createSuggestItem('console', 0, 'property'),
createSuggestItem('co_new', 0, 'property'),
createSuggestItem('bar', 0, 'property'),
createSuggestItem('car', 0, 'property'),
createSuggestItem('foo', 0, 'property'),
], 1, {
leadingLineContent: '',
characterCountDelta: 0
}, 'inline');

assert.equal(model.items.length, 5);

// narrow down once
model.lineContext = { leadingLineContent: 'c', characterCountDelta: 1 };
assert.equal(model.items.length, 3);

// query gets longer, narrow down the narrow-down'ed-set from before
model.lineContext = { leadingLineContent: 'cn', characterCountDelta: 2 };
assert.equal(model.items.length, 2);

// query gets shorter, refilter everything
model.lineContext = { leadingLineContent: '', characterCountDelta: 0 };
assert.equal(model.items.length, 5);
});
});

0 comments on commit 3d2d63d

Please sign in to comment.