0

I have a function with the name waitForCompletion(arg1,value,waitTime);

I want to find out a way to know that I am on the second argument of the function i.e. waitForCompletion(arg1,

Use case:- When a user starts typing the function name and is on the second argument, I have to trigger some intellisense for showing the intellisense on what values the second argument can accept.

I currently have the following, but it only matches the function name and triggers my intellisense for all the arguments but it has to only do that for the 2nd argument.

let completionMatch = textUntilPosition.trim().match(/(^waitForCompletion)|(\swaitForCompletion)/);

if(completionMatch){  //trigger intellisense }

I believe we would have to match the ( after the function name but somehow ignore the first argument and match then match the , after the first argument , that is when I can make sure that my pattern matches.

Note:- textUntilPosition gets me the text until the position where my cursor is at i.e. if my cursor is in the second argument waitForCompletion(arg1, , if my cursor is on the first argument it is waitForCompletion(arg1

1 Answer 1

1

When a user starts typing the function name and is on the second argument, I have to trigger some intellisense for showing the intellisense on what values the second argument can accept.

You can't do this with a simple regular expression, you need a JavaScript parser. There are several open source ones available, such as Espree (the parser used by ESLint) and Acorn.

The reason for that is that JavaScript syntax is not regular in the sense of "regular" in "regular expression." For instance, consider:

waitForCompletion(x ? example(1, 2) : y, 

Your regex would have to understand the conditional operator (? :) in order to know that you were in the second argument to waitForCompletion at that point rather than earlier when providing the second argument to example. That's just one of the many, many ways that an argument can be more complex than just a single identifier or literal.


I don't want to seem to be stonewalling this or seem to be unhelpful about it. Here's an example that will work in many cases with the simple kind of argument you've described, but as I've said above (and in a comment below), it will fail with lots of other inputs:

"use strict";
const rex = /(^|\s)waitForCompletion\s*\((?:[^,]+|(["'])[^\1]*?\1),\s*$/;
// true:
console.log(rex.test("waitForCompletion(arg1,"));
// false, wrong function:
console.log(rex.test("blahwaitForCompletion(arg1,"));
// false, second arg present
console.log(rex.test("waitForCompletion(arg1, arg2"));
// true:
console.log(rex.test("\twaitForCompletion(\"hi\","));
// true, there's a comma, but it's in a string (1):
console.log(rex.test("waitForCompletion(\"h,i\","));
// true, there's a comma, but it's in a string (2):
console.log(rex.test("waitForCompletion('h,i',"));

Sign up to request clarification or add additional context in comments.

6 Comments

the arguments are very specific and don't involve conditionals, they are plain string fields. Ex:- waitForCompletion("hello", ,)
@Veryon890 - Two responses to that: :-) First, how can you know? Why can't people create the strings based on a condition and a function call? But second: It doesn't matter, even just literal strings are too complex for this: waitForCompletion("Well, he said \"But then there's nested quotes with commas,\" and I began to wonder..." And that's not even getting into template literals. I've been down this road too many times (twice probably should have been enough, but I'm dense): Any naive regex solution you apply here will fail in real-world use, I'm afraid.
sorry for not making this clear,so the function has a signature and we are strict with the values of the first argument, user can only input a set of values ( say some custom database columns ) which are just plain string values with no special characters.
@Veryon890 - But again, why can't I choose what columns to pass it based on a condition and use a function to return the string? You can be strict about values, but (in the vast majority of cases) you can't be strict about how that value is determined. In any case, I added an example of a regex that will work provided no commas exist in the first argument and provided there are no line breaks.
@Veryon890 - waitForCompletion(,,) is a syntax error in JavaScript. :-) (So is waitForCompletion( ,, but the regex is naive and...) You can elide values in array literals, but not function argument lists. (But if you want it to match, change the first + to * in the regex.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.