kunwp1 commented on code in PR #9314:
URL: https://github.com/apache/texera/pull/9314#discussion_r4484966337


##########
frontend/src/app/workspace/service/code-editor/r-ui-udf-parameters-parser.ts:
##########
@@ -198,12 +249,31 @@ export function computeRParameterInsertion(
   const tokens = tokenize(code);
   const names = new Set<string>();
   let depth = 0;
+  let argumentDepth = 0;
   for (let index = 0; index < tokens.length; index++) {
     const token = tokens[index];
+    if (
+      ((token.kind === "keyword" && token.text === "function") || (token.kind 
=== "symbol" && token.text === "\\")) &&
+      tokens[index + 1]?.text === "("
+    ) {
+      index = endOfExpression(tokens, index);
+      continue;
+    }
     if (token.kind === "symbol") {
       if (["(", "[", "{"].includes(token.text)) depth++;
       else if ([")", "]", "}"].includes(token.text)) depth--;
+      if (["(", "["].includes(token.text)) argumentDepth++;
+      else if ([")", "]"].includes(token.text)) argumentDepth--;
     }
+    if (
+      argumentDepth === 0 &&
+      token.kind === "keyword" &&
+      token.text === "for" &&
+      tokens[index + 1]?.text === "(" &&
+      tokens[index + 2]?.kind === "name" &&
+      tokens[index + 3]?.text === "in"
+    )
+      names.add(tokens[index + 2].text);
     if (
       depth === 0 &&

Review Comment:
   This covers loops, but other script-level bindings still go through the 
`depth === 0` check which treats braces as a scope. R doesn't: `if (TRUE) { 
count <- 3 }` or a top-level `for (i in 1:3) { count <- i }` still replaces the 
parameter with 3. The loop check also skips anything inside parentheses, so 
`tryCatch({ for (count in 1:3) NULL }, ...)` and `suppressWarnings(for (count 
in 1:3) NULL)` are missed even though both bind `count` at script level. 
   
   Since function bodies are now skipped on their own, the scan could count 
`<-`, `<<-`, `->`, `->>` and `for` iterators at any depth and keep the 
argument-depth check only for `=`, which inside a call is a named argument. 
That also drops the separate `depth` counter. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to