mirror of
https://github.com/prometheus/prometheus.git
synced 2026-06-09 08:32:26 -04:00
fix: Suppress autocomplete for duration units when unit already present
- No duration unit suggestions shown if a valid unit follows the digit (e.g. , ) - Adds related test cases Signed-off-by: ADITYA TIWARI <adityatiwari342005@gmail.com>
This commit is contained in:
parent
fb47037435
commit
04a5a488b8
2 changed files with 60 additions and 1 deletions
|
|
@ -559,6 +559,18 @@ describe('analyzeCompletion test', () => {
|
|||
pos: 28,
|
||||
expectedContext: [{ kind: ContextKind.Duration }],
|
||||
},
|
||||
{
|
||||
title: 'do not autocomplete duration when unit already present in matrixSelector',
|
||||
expr: 'rate(foo[5m])',
|
||||
pos: 10,
|
||||
expectedContext: [],
|
||||
},
|
||||
{
|
||||
title: 'do not autocomplete duration when multi char unit already present in matrixSelector',
|
||||
expr: 'rate(foo[5ms])',
|
||||
pos: 10,
|
||||
expectedContext: [],
|
||||
},
|
||||
{
|
||||
title: 'autocomplete duration for a subQuery',
|
||||
expr: 'go[5d:5]',
|
||||
|
|
@ -1229,6 +1241,28 @@ describe('autocomplete promQL test', () => {
|
|||
validFor: undefined,
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'offline do not autocomplete duration when unit already present in matrixSelector',
|
||||
expr: 'rate(foo[5m])',
|
||||
pos: 10,
|
||||
expectedResult: {
|
||||
options: [],
|
||||
from: 10,
|
||||
to: 10,
|
||||
validFor: /^[a-zA-Z0-9_:]+$/,
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'offline do not autocomplete duration when multi char unit already present in matrixSelector',
|
||||
expr: 'rate(foo[5ms])',
|
||||
pos: 10,
|
||||
expectedResult: {
|
||||
options: [],
|
||||
from: 10,
|
||||
to: 10,
|
||||
validFor: /^[a-zA-Z0-9_:]+$/,
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'offline autocomplete duration for a subQuery',
|
||||
expr: 'go[5d:5]',
|
||||
|
|
|
|||
|
|
@ -166,6 +166,25 @@ function arrayToCompletionResult(data: Completion[], from: number, to: number, i
|
|||
} as CompletionResult;
|
||||
}
|
||||
|
||||
const durationUnitLabels = durationTerms
|
||||
.map((term) => term.label)
|
||||
.filter((label): label is string => typeof label === 'string')
|
||||
.sort((a, b) => b.length - a.length);
|
||||
|
||||
const durationWithUnitRegexp = new RegExp(`^\\d+(?:${durationUnitLabels.map((label) => escapeRegExp(label)).join('|')})$`);
|
||||
|
||||
function escapeRegExp(value: string): string {
|
||||
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
}
|
||||
|
||||
function hasCompleteDurationUnit(state: EditorState, node: SyntaxNode): boolean {
|
||||
if (node.from >= node.to) {
|
||||
return false;
|
||||
}
|
||||
const nodeContent = state.sliceDoc(node.from, node.to);
|
||||
return durationWithUnitRegexp.test(nodeContent);
|
||||
}
|
||||
|
||||
// computeStartCompleteLabelPositionInLabelMatcherOrInGroupingLabel calculates the start position only when the node is a LabelMatchers or a GroupingLabels
|
||||
function computeStartCompleteLabelPositionInLabelMatcherOrInGroupingLabel(node: SyntaxNode, pos: number): number {
|
||||
// Here we can have two different situations:
|
||||
|
|
@ -477,12 +496,18 @@ export function analyzeCompletion(state: EditorState, node: SyntaxNode, pos: num
|
|||
// Duration, Duration, ⚠(NumberLiteral)
|
||||
// )
|
||||
// So we should continue to autocomplete a duration
|
||||
result.push({ kind: ContextKind.Duration });
|
||||
if (!hasCompleteDurationUnit(state, node)) {
|
||||
result.push({ kind: ContextKind.Duration });
|
||||
}
|
||||
} else {
|
||||
result.push({ kind: ContextKind.Number });
|
||||
}
|
||||
break;
|
||||
case NumberDurationLiteralInDurationContext:
|
||||
if (!hasCompleteDurationUnit(state, node)) {
|
||||
result.push({ kind: ContextKind.Duration });
|
||||
}
|
||||
break;
|
||||
case OffsetExpr:
|
||||
result.push({ kind: ContextKind.Duration });
|
||||
break;
|
||||
|
|
|
|||
Loading…
Reference in a new issue