suggest - simplify how to tell from which provider suggestions should comple, towards #2508
This commit is contained in:
parent
3233ef35a0
commit
48ff5e7973
|
@ -7,6 +7,8 @@
|
|||
import * as nls from 'vs/nls';
|
||||
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
|
||||
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
|
||||
import { isFalsyOrEmpty } from 'vs/base/common/arrays';
|
||||
import { forEach } from 'vs/base/common/collections';
|
||||
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
|
||||
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
|
||||
import { ICommonCodeEditor, IEditorContribution, EditorContextKeys, ModeContextKeys } from 'vs/editor/common/editorCommon';
|
||||
|
@ -95,47 +97,30 @@ export class SuggestController implements IEditorContribution {
|
|||
return;
|
||||
}
|
||||
|
||||
let groups = SuggestRegistry.orderedGroups(this.editor.getModel());
|
||||
if (groups.length === 0) {
|
||||
return;
|
||||
const supportsByTriggerCharacter: { [ch: string]: ISuggestSupport[] } = Object.create(null);
|
||||
for (const support of SuggestRegistry.all(this.editor.getModel())) {
|
||||
if (isFalsyOrEmpty(support.triggerCharacters)) {
|
||||
continue;
|
||||
}
|
||||
for (const ch of support.triggerCharacters) {
|
||||
const array = supportsByTriggerCharacter[ch];
|
||||
if (!array) {
|
||||
supportsByTriggerCharacter[ch] = [support];
|
||||
} else {
|
||||
array.push(support);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let triggerCharacters: { [ch: string]: ISuggestSupport[][] } = Object.create(null);
|
||||
|
||||
groups.forEach(group => {
|
||||
|
||||
let groupTriggerCharacters: { [ch: string]: ISuggestSupport[] } = Object.create(null);
|
||||
|
||||
group.forEach(support => {
|
||||
let localTriggerCharacters = support.triggerCharacters;
|
||||
if (localTriggerCharacters) {
|
||||
for (let ch of localTriggerCharacters) {
|
||||
let array = groupTriggerCharacters[ch];
|
||||
if (array) {
|
||||
array.push(support);
|
||||
} else {
|
||||
array = [support];
|
||||
groupTriggerCharacters[ch] = array;
|
||||
if (triggerCharacters[ch]) {
|
||||
triggerCharacters[ch].push(array);
|
||||
} else {
|
||||
triggerCharacters[ch] = [array];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Object.keys(triggerCharacters).forEach(ch => {
|
||||
this.triggerCharacterListeners.push(this.editor.addTypingListener(ch, () => {
|
||||
this.model.trigger(false, ch, false, triggerCharacters[ch]);
|
||||
forEach(supportsByTriggerCharacter, entry => {
|
||||
this.triggerCharacterListeners.push(this.editor.addTypingListener(entry.key, () => {
|
||||
this.model.trigger(true, false, entry.value);
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
triggerSuggest(): void {
|
||||
this.model.trigger(false, undefined, false);
|
||||
this.model.trigger(false, false);
|
||||
this.editor.focus();
|
||||
}
|
||||
|
||||
|
|
|
@ -33,11 +33,6 @@ export interface ISuggestionItem {
|
|||
|
||||
export type SnippetConfig = 'top' | 'bottom' | 'inline' | 'none';
|
||||
|
||||
export interface ISuggestOptions {
|
||||
groups?: ISuggestSupport[][];
|
||||
snippetConfig?: SnippetConfig;
|
||||
}
|
||||
|
||||
|
||||
// add suggestions from snippet registry.
|
||||
const snippetSuggestSupport: ISuggestSupport = {
|
||||
|
@ -52,13 +47,13 @@ const snippetSuggestSupport: ISuggestSupport = {
|
|||
}
|
||||
};
|
||||
|
||||
export function provideSuggestionItems(model: IReadOnlyModel, position: Position, options: ISuggestOptions = {}): TPromise<ISuggestionItem[]> {
|
||||
export function provideSuggestionItems(model: IReadOnlyModel, position: Position, snippetConfig: SnippetConfig = 'bottom', onlyFrom?: ISuggestSupport[]): TPromise<ISuggestionItem[]> {
|
||||
|
||||
const result: ISuggestionItem[] = [];
|
||||
const acceptSuggestion = createSuggesionFilter(options);
|
||||
const acceptSuggestion = createSuggesionFilter(snippetConfig);
|
||||
|
||||
// get provider groups, always add snippet suggestion provider
|
||||
const supports = (options.groups || SuggestRegistry.orderedGroups(model)).slice(0);
|
||||
const supports = SuggestRegistry.orderedGroups(model);
|
||||
supports.unshift([snippetSuggestSupport]);
|
||||
|
||||
// add suggestions from contributed providers - providers are ordered in groups of
|
||||
|
@ -71,35 +66,42 @@ export function provideSuggestionItems(model: IReadOnlyModel, position: Position
|
|||
return;
|
||||
}
|
||||
// for each support in the group ask for suggestions
|
||||
return TPromise.join(supports.map(support => asWinJsPromise(token => support.provideCompletionItems(model, position, token)).then(container => {
|
||||
return TPromise.join(supports.map(support => {
|
||||
|
||||
const len = result.length;
|
||||
if (!isFalsyOrEmpty(onlyFrom) && onlyFrom.indexOf(support) < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (container && !isFalsyOrEmpty(container.suggestions)) {
|
||||
for (let suggestion of container.suggestions) {
|
||||
if (acceptSuggestion(suggestion)) {
|
||||
return asWinJsPromise(token => support.provideCompletionItems(model, position, token)).then(container => {
|
||||
|
||||
fixOverwriteBeforeAfter(suggestion, container);
|
||||
const len = result.length;
|
||||
|
||||
result.push({
|
||||
container,
|
||||
suggestion,
|
||||
support,
|
||||
resolve: createSuggestionResolver(support, suggestion, model, position)
|
||||
});
|
||||
if (container && !isFalsyOrEmpty(container.suggestions)) {
|
||||
for (let suggestion of container.suggestions) {
|
||||
if (acceptSuggestion(suggestion)) {
|
||||
|
||||
fixOverwriteBeforeAfter(suggestion, container);
|
||||
|
||||
result.push({
|
||||
container,
|
||||
suggestion,
|
||||
support,
|
||||
resolve: createSuggestionResolver(support, suggestion, model, position)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (len !== result.length && support !== snippetSuggestSupport) {
|
||||
hasResult = true;
|
||||
}
|
||||
if (len !== result.length && support !== snippetSuggestSupport) {
|
||||
hasResult = true;
|
||||
}
|
||||
|
||||
}, onUnexpectedError)));
|
||||
}, onUnexpectedError);
|
||||
}));
|
||||
};
|
||||
});
|
||||
|
||||
return sequence(factory).then(() => result.sort(createSuggesionComparator(options)));
|
||||
return sequence(factory).then(() => result.sort(createSuggesionComparator(snippetConfig)));
|
||||
}
|
||||
|
||||
function fixOverwriteBeforeAfter(suggestion: ISuggestion, container: ISuggestResult): void {
|
||||
|
@ -121,15 +123,15 @@ function createSuggestionResolver(provider: ISuggestSupport, suggestion: ISugges
|
|||
};
|
||||
}
|
||||
|
||||
function createSuggesionFilter(options: ISuggestOptions): (candidate: ISuggestion) => boolean {
|
||||
if (options.snippetConfig === 'none') {
|
||||
function createSuggesionFilter(snippetConfig: SnippetConfig): (candidate: ISuggestion) => boolean {
|
||||
if (snippetConfig === 'none') {
|
||||
return suggestion => suggestion.type !== 'snippet';
|
||||
} else {
|
||||
return () => true;
|
||||
}
|
||||
}
|
||||
|
||||
function createSuggesionComparator(options: ISuggestOptions): (a: ISuggestionItem, b: ISuggestionItem) => number {
|
||||
function createSuggesionComparator(snippetConfig: SnippetConfig): (a: ISuggestionItem, b: ISuggestionItem) => number {
|
||||
|
||||
function defaultComparator(a: ISuggestionItem, b: ISuggestionItem): number {
|
||||
|
||||
|
@ -179,9 +181,9 @@ function createSuggesionComparator(options: ISuggestOptions): (a: ISuggestionIte
|
|||
return defaultComparator(a, b);
|
||||
}
|
||||
|
||||
if (options.snippetConfig === 'top') {
|
||||
if (snippetConfig === 'top') {
|
||||
return snippetUpComparator;
|
||||
} else if (options.snippetConfig === 'bottom') {
|
||||
} else if (snippetConfig === 'bottom') {
|
||||
return snippetDownComparator;
|
||||
} else {
|
||||
return defaultComparator;
|
||||
|
|
|
@ -21,8 +21,6 @@ export interface ICancelEvent {
|
|||
|
||||
export interface ITriggerEvent {
|
||||
auto: boolean;
|
||||
characterTriggered: boolean;
|
||||
retrigger: boolean;
|
||||
}
|
||||
|
||||
export interface ISuggestEvent {
|
||||
|
@ -263,7 +261,7 @@ export class SuggestModel implements IDisposable {
|
|||
}
|
||||
|
||||
} else if (this.raw && this.incomplete) {
|
||||
this.trigger(this.state === State.Auto, undefined, true);
|
||||
this.trigger(this.state === State.Auto, true);
|
||||
} else {
|
||||
this.onNewContext(ctx);
|
||||
}
|
||||
|
@ -279,23 +277,17 @@ export class SuggestModel implements IDisposable {
|
|||
return;
|
||||
}
|
||||
|
||||
this.trigger(this.state === State.Auto, undefined, true);
|
||||
this.trigger(this.state === State.Auto, true);
|
||||
}
|
||||
|
||||
trigger(auto: boolean, triggerCharacter?: string, retrigger: boolean = false, groups?: ISuggestSupport[][]): void {
|
||||
public trigger(auto: boolean, retrigger: boolean = false, onlyFrom?: ISuggestSupport[]): void {
|
||||
|
||||
const model = this.editor.getModel();
|
||||
|
||||
if (!model) {
|
||||
return;
|
||||
}
|
||||
|
||||
const characterTriggered = !!triggerCharacter;
|
||||
groups = groups || SuggestRegistry.orderedGroups(model);
|
||||
|
||||
if (groups.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const ctx = new Context(model, this.editor.getPosition(), auto);
|
||||
|
||||
if (!ctx.isInEditableRange) {
|
||||
|
@ -304,15 +296,15 @@ export class SuggestModel implements IDisposable {
|
|||
|
||||
// Cancel previous requests, change state & update UI
|
||||
this.cancel(false, retrigger);
|
||||
this.state = (auto || characterTriggered) ? State.Auto : State.Manual;
|
||||
this._onDidTrigger.fire({ auto: this.isAutoSuggest(), characterTriggered, retrigger });
|
||||
this.state = auto ? State.Auto : State.Manual;
|
||||
this._onDidTrigger.fire({ auto: this.isAutoSuggest() });
|
||||
|
||||
// Capture context when request was sent
|
||||
this.context = ctx;
|
||||
|
||||
const position = this.editor.getPosition();
|
||||
this.requestPromise = provideSuggestionItems(model, this.editor.getPosition(),
|
||||
this.editor.getConfiguration().contribInfo.snippetSuggestions, onlyFrom).then(all => {
|
||||
|
||||
this.requestPromise = provideSuggestionItems(model, position, { groups, snippetConfig: this.editor.getConfiguration().contribInfo.snippetSuggestions }).then(all => {
|
||||
this.requestPromise = null;
|
||||
|
||||
if (this.state === State.Idle) {
|
||||
|
@ -335,7 +327,7 @@ export class SuggestModel implements IDisposable {
|
|||
private onNewContext(ctx: Context): void {
|
||||
if (this.context && this.context.isDifferentContext(ctx)) {
|
||||
if (this.context.shouldRetrigger(ctx)) {
|
||||
this.trigger(this.state === State.Auto, undefined, true);
|
||||
this.trigger(this.state === State.Auto, true);
|
||||
} else {
|
||||
this.cancel();
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@ suite('Suggest', function () {
|
|||
});
|
||||
|
||||
test('sort - snippet inline', function () {
|
||||
return provideSuggestionItems(model, new Position(1, 1), { snippetConfig: 'inline' }).then(items => {
|
||||
return provideSuggestionItems(model, new Position(1, 1), 'inline').then(items => {
|
||||
assert.equal(items.length, 3);
|
||||
assert.equal(items[0].suggestion.label, 'aaa');
|
||||
assert.equal(items[1].suggestion.label, 'fff');
|
||||
|
@ -60,7 +60,7 @@ suite('Suggest', function () {
|
|||
});
|
||||
|
||||
test('sort - snippet top', function () {
|
||||
return provideSuggestionItems(model, new Position(1, 1), { snippetConfig: 'top' }).then(items => {
|
||||
return provideSuggestionItems(model, new Position(1, 1), 'top').then(items => {
|
||||
assert.equal(items.length, 3);
|
||||
assert.equal(items[0].suggestion.label, 'aaa');
|
||||
assert.equal(items[1].suggestion.label, 'zzz');
|
||||
|
@ -69,7 +69,7 @@ suite('Suggest', function () {
|
|||
});
|
||||
|
||||
test('sort - snippet bottom', function () {
|
||||
return provideSuggestionItems(model, new Position(1, 1), { snippetConfig: 'bottom' }).then(items => {
|
||||
return provideSuggestionItems(model, new Position(1, 1), 'bottom').then(items => {
|
||||
assert.equal(items.length, 3);
|
||||
assert.equal(items[0].suggestion.label, 'fff');
|
||||
assert.equal(items[1].suggestion.label, 'aaa');
|
||||
|
@ -78,9 +78,35 @@ suite('Suggest', function () {
|
|||
});
|
||||
|
||||
test('sort - snippet none', function () {
|
||||
return provideSuggestionItems(model, new Position(1, 1), { snippetConfig: 'none' }).then(items => {
|
||||
return provideSuggestionItems(model, new Position(1, 1), 'none').then(items => {
|
||||
assert.equal(items.length, 1);
|
||||
assert.equal(items[0].suggestion.label, 'fff');
|
||||
});
|
||||
});
|
||||
|
||||
test('only from', function () {
|
||||
|
||||
const foo: any = {
|
||||
triggerCharacters: [],
|
||||
provideCompletionItems() {
|
||||
return {
|
||||
currentWord: '',
|
||||
incomplete: false,
|
||||
suggestions: [{
|
||||
label: 'jjj',
|
||||
type: 'property',
|
||||
insertText: 'jjj'
|
||||
}]
|
||||
};
|
||||
}
|
||||
};
|
||||
const registration = SuggestRegistry.register({ pattern: 'bar/path' }, foo);
|
||||
|
||||
provideSuggestionItems(model, new Position(1, 1), undefined, [foo]).then(items => {
|
||||
registration.dispose();
|
||||
|
||||
assert.equal(items.length, 1);
|
||||
assert.ok(items[0].support === foo);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -765,7 +765,7 @@ suite('ExtHostLanguageFeatures', function() {
|
|||
}, []));
|
||||
|
||||
return threadService.sync().then(() => {
|
||||
return provideSuggestionItems(model, new EditorPosition(1, 1), { snippetConfig: 'none' }).then(value => {
|
||||
return provideSuggestionItems(model, new EditorPosition(1, 1), 'none').then(value => {
|
||||
assert.equal(value.length, 1);
|
||||
assert.equal(value[0].suggestion.insertText, 'testing2');
|
||||
});
|
||||
|
@ -787,7 +787,7 @@ suite('ExtHostLanguageFeatures', function() {
|
|||
}, []));
|
||||
|
||||
return threadService.sync().then(() => {
|
||||
return provideSuggestionItems(model, new EditorPosition(1, 1), { snippetConfig: 'none' }).then(value => {
|
||||
return provideSuggestionItems(model, new EditorPosition(1, 1), 'none').then(value => {
|
||||
assert.equal(value.length, 1);
|
||||
assert.equal(value[0].suggestion.insertText, 'weak-selector');
|
||||
});
|
||||
|
@ -809,7 +809,7 @@ suite('ExtHostLanguageFeatures', function() {
|
|||
}, []));
|
||||
|
||||
return threadService.sync().then(() => {
|
||||
return provideSuggestionItems(model, new EditorPosition(1, 1), { snippetConfig: 'none' }).then(value => {
|
||||
return provideSuggestionItems(model, new EditorPosition(1, 1), 'none').then(value => {
|
||||
assert.equal(value.length, 2);
|
||||
assert.equal(value[0].suggestion.insertText, 'strong-1'); // sort by label
|
||||
assert.equal(value[1].suggestion.insertText, 'strong-2');
|
||||
|
@ -834,7 +834,7 @@ suite('ExtHostLanguageFeatures', function() {
|
|||
|
||||
return threadService.sync().then(() => {
|
||||
|
||||
return provideSuggestionItems(model, new EditorPosition(1, 1), { snippetConfig: 'none' }).then(value => {
|
||||
return provideSuggestionItems(model, new EditorPosition(1, 1), 'none').then(value => {
|
||||
assert.equal(value[0].container.incomplete, undefined);
|
||||
});
|
||||
});
|
||||
|
@ -850,7 +850,7 @@ suite('ExtHostLanguageFeatures', function() {
|
|||
|
||||
return threadService.sync().then(() => {
|
||||
|
||||
provideSuggestionItems(model, new EditorPosition(1, 1), { snippetConfig: 'none' }).then(value => {
|
||||
provideSuggestionItems(model, new EditorPosition(1, 1), 'none').then(value => {
|
||||
assert.equal(value[0].container.incomplete, true);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue