|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as assert from 'assert'; |
| 7 | +import { EditorSimpleWorker } from 'vs/editor/common/services/editorSimpleWorker'; |
| 8 | +import { mock } from 'vs/editor/contrib/suggest/test/suggestModel.test'; |
| 9 | +import { EditorWorkerHost, EditorWorkerServiceImpl } from 'vs/editor/common/services/editorWorkerServiceImpl'; |
| 10 | +import { IModelService } from 'vs/editor/common/services/modelService'; |
| 11 | +import { TextModel } from 'vs/editor/common/model/textModel'; |
| 12 | +import { URI } from 'vs/base/common/uri'; |
| 13 | +import { ITextResourceConfigurationService } from 'vs/editor/common/services/textResourceConfigurationService'; |
| 14 | +import { NullLogService } from 'vs/platform/log/common/log'; |
| 15 | +import { WordDistance } from 'vs/editor/contrib/suggest/wordDistance'; |
| 16 | +import { createTestCodeEditor } from 'vs/editor/test/browser/testCodeEditor'; |
| 17 | +import { IRange } from 'vs/editor/common/core/range'; |
| 18 | +import { DEFAULT_WORD_REGEXP } from 'vs/editor/common/model/wordHelper'; |
| 19 | +import { Event } from 'vs/base/common/event'; |
| 20 | +import { CompletionItem } from 'vs/editor/contrib/suggest/suggest'; |
| 21 | +import { IPosition } from 'vs/editor/common/core/position'; |
| 22 | +import * as modes from 'vs/editor/common/modes'; |
| 23 | +import { DisposableStore } from 'vs/base/common/lifecycle'; |
| 24 | +import { LanguageConfigurationRegistry } from 'vs/editor/common/modes/languageConfigurationRegistry'; |
| 25 | +import { MockMode } from 'vs/editor/test/common/mocks/mockMode'; |
| 26 | + |
| 27 | +suite('suggest, word distance', function () { |
| 28 | + |
| 29 | + class BracketMode extends MockMode { |
| 30 | + |
| 31 | + private static readonly _id = new modes.LanguageIdentifier('bracketMode', 3); |
| 32 | + |
| 33 | + constructor() { |
| 34 | + super(BracketMode._id); |
| 35 | + this._register(LanguageConfigurationRegistry.register(this.getLanguageIdentifier(), { |
| 36 | + brackets: [ |
| 37 | + ['{', '}'], |
| 38 | + ['[', ']'], |
| 39 | + ['(', ')'], |
| 40 | + ] |
| 41 | + })); |
| 42 | + } |
| 43 | + } |
| 44 | + let distance: WordDistance; |
| 45 | + let disposables = new DisposableStore(); |
| 46 | + |
| 47 | + setup(async function () { |
| 48 | + |
| 49 | + disposables.clear(); |
| 50 | + let mode = new BracketMode(); |
| 51 | + let model = TextModel.createFromString('function abc(aa, ab){\na\n}', undefined, mode.getLanguageIdentifier(), URI.parse('test:///some.path')); |
| 52 | + let editor = createTestCodeEditor({ model: model }); |
| 53 | + editor.updateOptions({ suggest: { localityBonus: true } }); |
| 54 | + editor.setPosition({ lineNumber: 2, column: 2 }); |
| 55 | + |
| 56 | + let modelService = new class extends mock<IModelService>() { |
| 57 | + onModelRemoved = Event.None; |
| 58 | + getModel(uri: URI) { |
| 59 | + return uri.toString() === model.uri.toString() ? model : null; |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + let service = new class extends EditorWorkerServiceImpl { |
| 64 | + |
| 65 | + private _worker = new EditorSimpleWorker(new class extends mock<EditorWorkerHost>() { }, null); |
| 66 | + |
| 67 | + constructor() { |
| 68 | + super(modelService, new class extends mock<ITextResourceConfigurationService>() { }, new NullLogService()); |
| 69 | + this._worker.acceptNewModel({ |
| 70 | + url: model.uri.toString(), |
| 71 | + lines: model.getLinesContent(), |
| 72 | + EOL: model.getEOL(), |
| 73 | + versionId: model.getVersionId() |
| 74 | + }); |
| 75 | + model.onDidChangeContent(e => this._worker.acceptModelChanged(model.uri.toString(), e)); |
| 76 | + } |
| 77 | + computeWordRanges(resource: URI, range: IRange): Promise<{ [word: string]: IRange[] } | null> { |
| 78 | + return this._worker.computeWordRanges(resource.toString(), range, DEFAULT_WORD_REGEXP.source, DEFAULT_WORD_REGEXP.flags); |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + distance = await WordDistance.create(service, editor); |
| 83 | + |
| 84 | + disposables.add(mode); |
| 85 | + disposables.add(model); |
| 86 | + disposables.add(editor); |
| 87 | + }); |
| 88 | + |
| 89 | + function createSuggestItem(label: string, overwriteBefore: number, position: IPosition): CompletionItem { |
| 90 | + const suggestion: modes.CompletionItem = { |
| 91 | + label, |
| 92 | + range: { startLineNumber: position.lineNumber, startColumn: position.column - overwriteBefore, endLineNumber: position.lineNumber, endColumn: position.column }, |
| 93 | + insertText: label, |
| 94 | + kind: 0 |
| 95 | + }; |
| 96 | + const container: modes.CompletionList = { |
| 97 | + suggestions: [suggestion] |
| 98 | + }; |
| 99 | + const provider: modes.CompletionItemProvider = { |
| 100 | + provideCompletionItems(): any { |
| 101 | + return; |
| 102 | + } |
| 103 | + }; |
| 104 | + return new CompletionItem(position, suggestion, container, provider, undefined!); |
| 105 | + } |
| 106 | + |
| 107 | + test('Suggest locality bonus can boost current word #90515', function () { |
| 108 | + this.skip(); |
| 109 | + |
| 110 | + const pos = { lineNumber: 2, column: 2 }; |
| 111 | + const d1 = distance.distance(pos, createSuggestItem('a', 1, pos).completion); |
| 112 | + const d2 = distance.distance(pos, createSuggestItem('aa', 1, pos).completion); |
| 113 | + const d3 = distance.distance(pos, createSuggestItem('ab', 1, pos).completion); |
| 114 | + |
| 115 | + assert.ok(d1 > d2); |
| 116 | + assert.ok(d2 === d3); |
| 117 | + }); |
| 118 | +}); |
0 commit comments