Skip to content

Commit

Permalink
chore: Enable entity tern server to lookup fnParams in entity definit…
Browse files Browse the repository at this point in the history
…ion to autocomplete function params (#33726)

## Description
This PR enables a functionality to autofill params any entity function
like a query's run of js object's function. This autofill only has one
pre-condition to work and i.e there has to be a property called
`!fnParams` for the entity that should autofill.

The current use-case is for query module instances to be autofilled with
the inputs defined in the modules when they are selected from the
autocomplete in any code editor.


PR for appsmithorg/appsmith-ee#4281

## Automation

/ok-to-test tags="@tag.All"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/9361159573>
> Commit: 4364bd8
> Cypress dashboard url: <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=9361159573&attempt=2"
target="_blank">Click here!</a>

<!-- end of auto-generated comment: Cypress test results  -->












## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Improved autocomplete functionality with enhanced completion text
generation for functions.
- Added a new feature to extract the final object path from a given
input string.
  
- **Tests**
- Added new test cases to validate Tern server completion functionality,
ensuring accurate parameter identification and application.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
ashit-rath authored Jun 4, 2024
1 parent f173066 commit d967cfe
Show file tree
Hide file tree
Showing 2 changed files with 343 additions and 2 deletions.
41 changes: 39 additions & 2 deletions app/client/src/utils/autocomplete/CodemirrorTernService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,25 @@ export function isCustomKeywordType(
);
}

// Define the regex for extracting the final object path
const FINAL_OBJECT_PATH_REGEX = /(?:\w+\.)*\w+$/;

/**
* Extracts the final object path from a given input string.
* The final object path is the rightmost dot-separated path in the string.
*
* @param {string} input - The input string from which to extract the object path.
* @returns {string|null} - The extracted object path or null if no match is found.
*
* Example:
* Input: '\tconst k = PageQuery.run'
* Output: 'PageQuery.run'
*/
export function extractFinalObjectPath(input: string) {
const match = (input || "")?.trim().match(FINAL_OBJECT_PATH_REGEX);
return match ? match[0] : null;
}

export function getDataType(type: string): AutocompleteDataType {
if (type === "?") return AutocompleteDataType.UNKNOWN;
else if (type === "number") return AutocompleteDataType.NUMBER;
Expand Down Expand Up @@ -177,12 +196,14 @@ class CodeMirrorTernService {
string,
DataTreeDefEntityInformation
>();
entityDef: Def;
options: { async: boolean };
recentEntities: string[] = [];

constructor(options: { async: boolean }) {
this.options = options;
this.server = new TernWorkerServer(this);
this.entityDef = {};
}

resetServer = () => {
Expand Down Expand Up @@ -397,6 +418,7 @@ class CodeMirrorTernService {
this.server.deleteDefs(name);
}

this.entityDef = def || {};
if (entityInfo) this.defEntityInformation = entityInfo;
}

Expand Down Expand Up @@ -455,9 +477,21 @@ class CodeMirrorTernService {
completion.origin === "DATA_TREE" &&
this.defEntityInformation.has(completion.name);
let completionText = completion.name + after;
const completedLine = lineValue.substring(0, from.ch) + completion.name;
const entityPath = extractFinalObjectPath(completedLine);

if (dataType === "FUNCTION" && !completion.origin?.startsWith("LIB/")) {
if (token.type !== "string" && token.string !== "[") {
completionText = completionText + "()";
const entityDef = entityPath && this.entityDef[entityPath];
if (
entityDef &&
typeof entityDef === "object" &&
"!fnParams" in entityDef
) {
completionText = completionText + `(${entityDef["!fnParams"]})`;
} else {
completionText = completionText + "()";
}
}
}
const codeMirrorCompletion: Completion<TernCompletionResult> = {
Expand Down Expand Up @@ -1204,7 +1238,10 @@ export default new CodeMirrorTernService({
function dotToBracketNotationAtToken(token: CodeMirror.Token) {
return (cm: CodeMirror.Editor, hints: Hints, curr: Hint) => {
let completion = curr.text;
if (token.type === "string") {
if (
token.type === "string" ||
("type" in curr && curr.type === AutocompleteDataType.FUNCTION)
) {
// | represents the cursor
// Cases like JSObject1["myV|"]
cm.replaceRange(completion, hints.from, hints.to);
Expand Down
Loading

0 comments on commit d967cfe

Please sign in to comment.