From d63ad9ac93659bb4986f6b665307cbe81f9c3810 Mon Sep 17 00:00:00 2001 From: David Date: Fri, 3 Jan 2020 12:16:27 +0100 Subject: [PATCH] Prometheus: Disable suggestions at beginning of value (#21302) * Prometheus: Disable suggestions at beginning of value - fixed comparison operator regex - no longer suggest anything at beginning of label value * Detect cursor in front of label values --- .../datasource/prometheus/language_provider.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/public/app/plugins/datasource/prometheus/language_provider.ts b/public/app/plugins/datasource/prometheus/language_provider.ts index 7e616dd066c..ec280f34cf7 100644 --- a/public/app/plugins/datasource/prometheus/language_provider.ts +++ b/public/app/plugins/datasource/prometheus/language_provider.ts @@ -303,10 +303,17 @@ export default class PromQlLanguageProvider extends LanguageProvider { const suggestions: CompletionItemGroup[] = []; const line = value.anchorBlock.getText(); const cursorOffset = value.selection.anchor.offset; - const nextChar = line[cursorOffset]; - const isValueContext = wrapperClasses.includes('attr-value'); - if (!nextChar.match(/["}]/)) { - // Don't suggest anything inside a value + const suffix = line.substr(cursorOffset); + const prefix = line.substr(0, cursorOffset); + const isValueStart = text.match(/^(=|=~|!=|!~)/); + const isValueEnd = suffix.match(/^"?[,}]/); + // detect cursor in front of value, e.g., {key=|"} + const isPreValue = prefix.match(/(=|=~|!=|!~)$/) && suffix.match(/^"/); + + // Don't suggestq anything at the beginning or inside a value + const isValueEmpty = isValueStart && isValueEnd; + const hasValuePrefix = isValueEnd && !isValueStart; + if ((!isValueEmpty && !hasValuePrefix) || isPreValue) { return { suggestions }; } @@ -335,7 +342,7 @@ export default class PromQlLanguageProvider extends LanguageProvider { } let context: string; - if ((text && text.match(/^!?=~?/)) || isValueContext) { + if ((text && isValueStart) || wrapperClasses.includes('attr-value')) { // Label values if (labelKey && labelValues[labelKey]) { context = 'context-label-values';