diff --git a/app/client/src/utils/autocomplete/CodemirrorTernService.ts b/app/client/src/utils/autocomplete/CodemirrorTernService.ts index 84d0d07cc061..d244ea18d729 100644 --- a/app/client/src/utils/autocomplete/CodemirrorTernService.ts +++ b/app/client/src/utils/autocomplete/CodemirrorTernService.ts @@ -4,7 +4,6 @@ import type { Server, Def } from "tern"; import type { Hint, Hints } from "codemirror"; import type CodeMirror from "codemirror"; import { - getDynamicBindings, getDynamicStringSegments, isDynamicValue, } from "utils/DynamicBindingUtils"; @@ -204,6 +203,8 @@ class CodeMirrorTernService { const cursor = cm.getCursor(); const { extraChars } = this.getFocusedDocValueAndPos(doc); + const query = this.getQueryForAutocomplete(cm); + let completions: Completion[] = []; let after = ""; const { end, start } = data; @@ -309,13 +310,14 @@ class CodeMirrorTernService { list: completions, selectedHint: indexToBeSelected, lineValue, + query: this.getQueryForAutocomplete(cm), }; let tooltip: HTMLElement | undefined = undefined; const CodeMirror = getCodeMirrorNamespaceFromEditor(cm); CodeMirror.on(obj, "shown", () => { AnalyticsUtil.logEvent("AUTO_COMPLETE_SHOW", { - query: getDynamicBindings(lineValue)?.jsSnippets[0], + query, numberOfResults: completions.filter( (completion) => !completion.isHeader, ).length, @@ -380,15 +382,19 @@ class CodeMirrorTernService { // When a function is picked, move the cursor between the parenthesis const CodeMirror = getCodeMirrorNamespaceFromEditor(cm); CodeMirror.on(hints, "pick", (selected: CommandsCompletion) => { + const hintsWithoutHeaders = hints.list.filter( + (h: Record) => h.isHeader !== true, + ); + const selectedResultIndex = findIndex( - hints.list, + hintsWithoutHeaders, (item: Record) => item.displayText === selected.displayText, ); AnalyticsUtil.logEvent("AUTO_COMPLETE_SELECT", { selectedResult: selected.text, - query: getDynamicBindings(hints.lineValue)?.jsSnippets[0], + query: hints.query, selectedResultIndex, selectedResultType: selected.type, isBestMatch: @@ -613,6 +619,7 @@ class CodeMirrorTernService { value: string; end: { line: number; ch: number }; extraChars: number; + isSingleDynamicValue?: boolean; } { const cursor = doc.doc.getCursor("end"); const value = this.docValue(doc); @@ -628,6 +635,7 @@ class CodeMirrorTernService { ch: cursor.ch, }, extraChars, + isSingleDynamicValue: isDynamicValue(value), }; } @@ -886,6 +894,29 @@ class CodeMirrorTernService { setEntityInformation(entityInformation: FieldEntityInformation) { this.fieldEntityInformation = entityInformation; } + + getQueryForAutocomplete(cm: CodeMirror.Editor) { + const doc = this.findDoc(cm.getDoc()); + const lineValue = this.lineValue(doc); + const { end, extraChars, isSingleDynamicValue } = + this.getFocusedDocValueAndPos(doc); + let extraCharsInString = extraChars; + const endOfString = end.ch + extraChars; + + if (isSingleDynamicValue) { + extraCharsInString += 2; + } + + const stringFromEndCh = lineValue.substring( + extraCharsInString, + endOfString, + ); + + const splitBySpace = stringFromEndCh.split(" "); + const query = splitBySpace[splitBySpace.length - 1]; + + return query; + } } export const createCompletionHeader = (name: string): Completion => ({ diff --git a/app/client/src/utils/autocomplete/TernServer.test.ts b/app/client/src/utils/autocomplete/TernServer.test.ts index 7ddd15f4d9d1..3a3c53eabf92 100644 --- a/app/client/src/utils/autocomplete/TernServer.test.ts +++ b/app/client/src/utils/autocomplete/TernServer.test.ts @@ -191,7 +191,7 @@ describe("Tern server", () => { MockCodemirrorEditor.getCursor.mockReturnValueOnce( testCase.input.codeEditor.cursor, ); - MockCodemirrorEditor.getDoc.mockReturnValueOnce( + MockCodemirrorEditor.getDoc.mockReturnValue( testCase.input.codeEditor.doc, ); MockCodemirrorEditor.getTokenAt.mockReturnValueOnce({