Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lens] Add automatic = insertion on equal comparison typing when in Formula #143286

Merged
merged 1 commit into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import {
getTokenInfo,
offsetToRowColumn,
monacoPositionToOffset,
createEditOperation,
MARKER,
} from './math_completion';
import { LANGUAGE_ID } from './math_tokenization';
import { MemoizedFormulaHelp } from './formula_help';
Expand Down Expand Up @@ -85,6 +87,8 @@ export const WrappedFormulaEditor = ({

const MemoizedFormulaEditor = React.memo(FormulaEditor);

const namedArgumentsTypes = new Set(['kql', 'lucene', 'shift', 'reducedTimeRange']);

export function FormulaEditor({
layer,
paramEditorUpdater,
Expand Down Expand Up @@ -533,47 +537,44 @@ export function FormulaEditor({
const isSingleQuoteCase = /'LENS_MATH_MARKER/;
// Make sure that we are only adding kql='' or lucene='', and also
// check that the = sign isn't inside the KQL expression like kql='='
if (
!tokenInfo ||
typeof tokenInfo.ast === 'number' ||
tokenInfo.ast.type !== 'namedArgument' ||
(tokenInfo.ast.name !== 'kql' &&
tokenInfo.ast.name !== 'lucene' &&
tokenInfo.ast.name !== 'shift' &&
tokenInfo.ast.name !== 'reducedTimeRange') ||
(tokenInfo.ast.value !== 'LENS_MATH_MARKER' &&
!isSingleQuoteCase.test(tokenInfo.ast.value))
) {
return;
if (tokenInfo) {
if (
typeof tokenInfo.ast === 'number' ||
tokenInfo.ast.type !== 'namedArgument' ||
!namedArgumentsTypes.has(tokenInfo.ast.name) ||
(tokenInfo.ast.value !== MARKER && !isSingleQuoteCase.test(tokenInfo.ast.value))
) {
return;
}
}

let editOperation: monaco.editor.IIdentifiedSingleEditOperation | null = null;

const cursorOffset = 2;
if (char === '=') {
editOperation = {
range: {
...currentPosition,
// Insert after the current char
startColumn: currentPosition.startColumn + 1,
endColumn: currentPosition.startColumn + 1,
},
text: `''`,
};
// check also the previous char whether it was already a =
// to avoid infinite loops
if (!tokenInfo && currentText.charAt(offset - 1) !== '=') {
editOperation = createEditOperation('=', currentPosition, 1);
}
if (tokenInfo) {
editOperation = createEditOperation(`''`, currentPosition, 1);
}
}

if (!tokenInfo && !editOperation) {
return;
}

if (
char === "'" &&
tokenInfo?.ast &&
typeof tokenInfo.ast !== 'number' &&
'name' in tokenInfo.ast &&
tokenInfo.ast.name !== 'shift' &&
tokenInfo.ast.name !== 'reducedTimeRange'
) {
editOperation = {
range: {
...currentPosition,
// Insert after the current char
startColumn: currentPosition.startColumn,
endColumn: currentPosition.startColumn + 1,
},
text: `\\'`,
};
editOperation = createEditOperation(`\\'`, currentPosition);
}

if (editOperation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function inLocation(cursorPosition: number, location: TinymathLocation) {
return cursorPosition >= location.min && cursorPosition < location.max;
}

const MARKER = 'LENS_MATH_MARKER';
export const MARKER = 'LENS_MATH_MARKER';

export function getInfoAtZeroIndexedPosition(
ast: TinymathAST,
Expand Down Expand Up @@ -94,6 +94,23 @@ export function getInfoAtZeroIndexedPosition(
};
}

export function createEditOperation(
textToInject: string,
currentPosition: monaco.IRange,
startOffset: number = 0,
endOffset: number = 1
) {
return {
range: {
...currentPosition,
// Insert after the current char
startColumn: currentPosition.startColumn + startOffset,
endColumn: currentPosition.startColumn + endOffset,
},
text: textToInject,
};
}

export function offsetToRowColumn(expression: string, offset: number): monaco.Position {
const lines = expression.split(/\n/);
let remainingChars = offset;
Expand Down