Skip to content

Commit

Permalink
fix: Query missing in AUTO_COMPLETE_SHOW event (appsmithorg#24898)
Browse files Browse the repository at this point in the history
## Description
Query missing in AUTO_COMPLETE_SHOW event

#### PR fixes following issue(s)
Fixes appsmithorg#23838

#### Media
> A video or a GIF is preferred. when using Loom, don’t embed because it
looks like it’s a GIF. instead, just link to the video
>
>
#### Type of change
> Please delete options that are not relevant.
- Bug fix (non-breaking change which fixes an issue)
- New feature (non-breaking change which adds functionality)
- Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- Chore (housekeeping or task changes that don't impact user perception)
- This change requires a documentation update
>
>
>
## Testing
>
#### How Has This Been Tested?
> Please describe the tests that you ran to verify your changes. Also
list any relevant details for your test configuration.
> Delete anything that is not relevant
- [ ] Manual
- [ ] Jest
- [ ] Cypress
>
>
#### Test Plan
> Add Testsmith test cases links that relate to this PR
>
>
#### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
>
>
>
## Checklist:
#### Dev activity
- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag


#### QA activity:
- [ ] [Speedbreak
features](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#speedbreakers-)
have been covered
- [ ] Test plan covers all impacted features and [areas of
interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#areas-of-interest-)
- [ ] Test plan has been peer reviewed by project stakeholders and other
QA members
- [ ] Manually tested functionality on DP
- [ ] We had an implementation alignment call with stakeholders post QA
Round 2
- [ ] Cypress test cases have been added and approved by SDET/manual QA
- [ ] Added `Test Plan Approved` label after Cypress tests were reviewed
- [ ] Added `Test Plan Approved` label after JUnit tests were reviewed
  • Loading branch information
Druthi authored Jul 5, 2023
1 parent 87b6090 commit 499de5a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
39 changes: 35 additions & 4 deletions app/client/src/utils/autocomplete/CodemirrorTernService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<string, unknown>) => h.isHeader !== true,
);

const selectedResultIndex = findIndex(
hints.list,
hintsWithoutHeaders,
(item: Record<string, unknown>) =>
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:
Expand Down Expand Up @@ -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);
Expand All @@ -628,6 +635,7 @@ class CodeMirrorTernService {
ch: cursor.ch,
},
extraChars,
isSingleDynamicValue: isDynamicValue(value),
};
}

Expand Down Expand Up @@ -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 => ({
Expand Down
2 changes: 1 addition & 1 deletion app/client/src/utils/autocomplete/TernServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 499de5a

Please sign in to comment.