From ce59acd141dc744dcaa3a23ec88187c8a252753f Mon Sep 17 00:00:00 2001 From: David Kaltschmidt Date: Thu, 15 Nov 2018 11:20:27 +0000 Subject: [PATCH] Extracted language provider variables for readibility --- .../prometheus/language_provider.ts | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/public/app/plugins/datasource/prometheus/language_provider.ts b/public/app/plugins/datasource/prometheus/language_provider.ts index 326ab93f2ef..6e6f461d341 100644 --- a/public/app/plugins/datasource/prometheus/language_provider.ts +++ b/public/app/plugins/datasource/prometheus/language_provider.ts @@ -79,15 +79,23 @@ export default class PromQlLanguageProvider extends LanguageProvider { // Keep this DOM-free for testing provideCompletionItems({ prefix, wrapperClasses, text, value }: TypeaheadInput, context?: any): TypeaheadOutput { - // Syntax spans have 3 classes by default. More indicate a recognized token - const tokenRecognized = wrapperClasses.length > 3; - // Local text properties const empty = value.document.text.length === 0; const selectedLines = value.document.getTextsAtRangeAsArray(value.selection); const currentLine = selectedLines.length === 1 ? selectedLines[0] : null; const nextCharacter = currentLine ? currentLine.text[value.selection.anchorOffset] : null; + // Syntax spans have 3 classes by default. More indicate a recognized token + const tokenRecognized = wrapperClasses.length > 3; + // Non-empty prefix, but not inside known token + const prefixUnrecognized = prefix && !tokenRecognized; + // Prevent suggestions in `function(|suffix)` + const noSuffix = !nextCharacter || nextCharacter === ')'; + // Empty prefix is safe if it does not immediately folllow a complete expression and has no text after it + const safeEmptyPrefix = prefix === '' && !text.match(/^[\]})\s]+$/) && noSuffix; + // About to type next operand if preceded by binary operator + const isNextOperand = text.match(/[+\-*/^%]/); + // Determine candidates by CSS context if (_.includes(wrapperClasses, 'context-range')) { // Suggestions for metric[|] @@ -96,16 +104,13 @@ export default class PromQlLanguageProvider extends LanguageProvider { // Suggestions for metric{|} and metric{foo=|}, as well as metric-independent label queries like {|} return this.getLabelCompletionItems.apply(this, arguments); } else if (_.includes(wrapperClasses, 'context-aggregation')) { + // Suggestions for sum(metric) by (|) return this.getAggregationCompletionItems.apply(this, arguments); } else if (empty) { + // Suggestions for empty query field return this.getEmptyCompletionItems(context || {}); - } else if ( - // Show default suggestions in a couple of scenarios - (prefix && !tokenRecognized) || // Non-empty prefix, but not inside known token - // Empty prefix, but not directly following a closing brace (e.g., `]|`), or not succeeded by anything except a closing parens, e.g., `sum(|)` - (prefix === '' && !text.match(/^[\]})\s]+$/) && (!nextCharacter || nextCharacter === ')')) || - text.match(/[+\-*/^%]/) // Anything after binary operator - ) { + } else if (prefixUnrecognized || safeEmptyPrefix || isNextOperand) { + // Show term suggestions in a couple of scenarios return this.getTermCompletionItems(); }