mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Prometheus: Code editor autocomplete returns bad results when text is highlighted (#70856)
* adjust cursor when text is selected to give better autocomplete results in prometheus code editor
This commit is contained in:
parent
7252c6dd91
commit
927393633c
@ -43,6 +43,7 @@ function getMonacoCompletionItemKind(type: CompletionType, monaco: Monaco): mona
|
||||
throw new NeverCaseError(type);
|
||||
}
|
||||
}
|
||||
|
||||
export function getCompletionProvider(
|
||||
monaco: Monaco,
|
||||
dataProvider: DataProvider
|
||||
@ -63,10 +64,21 @@ export function getCompletionProvider(
|
||||
: monaco.Range.fromPositions(position);
|
||||
// documentation says `position` will be "adjusted" in `getOffsetAt`
|
||||
// i don't know what that means, to be sure i clone it
|
||||
|
||||
const positionClone = {
|
||||
column: position.column,
|
||||
lineNumber: position.lineNumber,
|
||||
};
|
||||
|
||||
// Check to see if the browser supports window.getSelection()
|
||||
if (window.getSelection) {
|
||||
const selectedText = window.getSelection()?.toString();
|
||||
// If the user has selected text, adjust the cursor position to be at the start of the selection, instead of the end
|
||||
if (selectedText && selectedText.length > 0) {
|
||||
positionClone.column = positionClone.column - selectedText.length;
|
||||
}
|
||||
}
|
||||
|
||||
const offset = model.getOffsetAt(positionClone);
|
||||
const situation = getSituation(model.getValue(), offset);
|
||||
const completionsPromise = situation != null ? getCompletions(situation, dataProvider) : Promise.resolve([]);
|
||||
|
Loading…
Reference in New Issue
Block a user