From 02bbdca60419179677b01d52246b3a65efcd4eb3 Mon Sep 17 00:00:00 2001 From: kay delaney <45561153+kaydelaney@users.noreply.github.com> Date: Wed, 4 Dec 2019 09:00:35 +0000 Subject: [PATCH] Datasource/Loki: Simplifies autocompletion (#20840) Unifies loki autocomplete so behavior isn't different across explore modes. Closes #20769 --- .../datasource/loki/language_provider.test.ts | 18 ++++++++---- .../datasource/loki/language_provider.ts | 28 ------------------- 2 files changed, 12 insertions(+), 34 deletions(-) diff --git a/public/app/plugins/datasource/loki/language_provider.test.ts b/public/app/plugins/datasource/loki/language_provider.test.ts index 3c6971d5634..7bcce3afca7 100644 --- a/public/app/plugins/datasource/loki/language_provider.test.ts +++ b/public/app/plugins/datasource/loki/language_provider.test.ts @@ -9,6 +9,7 @@ import { beforeEach } from 'test/lib/common'; import { makeMockLokiDatasource } from './mocks'; import LokiDatasource from './datasource'; +import { FUNCTIONS } from './syntax'; jest.mock('app/store/store', () => ({ store: { @@ -31,16 +32,17 @@ describe('Language completion provider', () => { }; describe('empty query suggestions', () => { - it('returns no suggestions on empty context', async () => { + it('returns function suggestions on empty context', async () => { const instance = new LanguageProvider(datasource); const value = Plain.deserialize(''); const result = await instance.provideCompletionItems({ text: '', prefix: '', value, wrapperClasses: [] }); expect(result.context).toBeUndefined(); - expect(result.suggestions.length).toEqual(0); + expect(result.suggestions.length).toEqual(1); + expect(result.suggestions[0].label).toEqual('Functions'); }); - it('returns default suggestions with history on empty context when history was provided', async () => { + it('returns function suggestions with history on empty context when history was provided', async () => { const instance = new LanguageProvider(datasource); const value = Plain.deserialize(''); const history: LokiHistoryItem[] = [ @@ -64,10 +66,14 @@ describe('Language completion provider', () => { }, ], }, + { + label: 'Functions', + items: FUNCTIONS, + }, ]); }); - it('returns no suggestions within regexp', async () => { + it('returns function suggestions within regexp', async () => { const instance = new LanguageProvider(datasource); const input = createTypeaheadInput('{} ()', '', undefined, 4, []); const history: LokiHistoryItem[] = [ @@ -78,8 +84,8 @@ describe('Language completion provider', () => { ]; const result = await instance.provideCompletionItems(input, { history }); expect(result.context).toBeUndefined(); - - expect(result.suggestions.length).toEqual(0); + expect(result.suggestions.length).toEqual(1); + expect(result.suggestions[0].label).toEqual('Functions'); }); }); diff --git a/public/app/plugins/datasource/loki/language_provider.ts b/public/app/plugins/datasource/loki/language_provider.ts index 48ce8e6db95..efd2aaa62ec 100644 --- a/public/app/plugins/datasource/loki/language_provider.ts +++ b/public/app/plugins/datasource/loki/language_provider.ts @@ -3,7 +3,6 @@ import _ from 'lodash'; // Services & Utils import { parseSelector, labelRegexp, selectorRegexp } from 'app/plugins/datasource/prometheus/language_utils'; -import { store } from 'app/store/store'; import syntax, { FUNCTIONS } from './syntax'; // Types @@ -113,16 +112,6 @@ export default class LokiLanguageProvider extends LanguageProvider { * @param context.history Optional used only in getEmptyCompletionItems */ async provideCompletionItems(input: TypeaheadInput, context?: TypeaheadContext): Promise { - const exploreMode = store.getState().explore.left.mode; - - if (exploreMode === ExploreMode.Logs) { - return this.provideLogCompletionItems(input, context); - } - - return this.provideMetricsCompletionItems(input, context); - } - - async provideMetricsCompletionItems(input: TypeaheadInput, context?: TypeaheadContext): Promise { const { wrapperClasses, value, prefix, text } = input; // Local text properties @@ -167,23 +156,6 @@ export default class LokiLanguageProvider extends LanguageProvider { }; } - async provideLogCompletionItems(input: TypeaheadInput, context?: TypeaheadContext): Promise { - const { wrapperClasses, value } = input; - // Local text properties - const empty = value.document.text.length === 0; - // Determine candidates by CSS context - if (wrapperClasses.includes('context-labels')) { - // Suggestions for {|} and {foo=|} - return await this.getLabelCompletionItems(input, context); - } else if (empty) { - return this.getEmptyCompletionItems(context || {}, ExploreMode.Logs); - } - - return { - suggestions: [], - }; - } - getEmptyCompletionItems(context: TypeaheadContext, mode?: ExploreMode): TypeaheadOutput { const { history } = context; const suggestions = [];