Skip to content

fix: improve {{var}} detection using cursor-based brace matching#6691

Merged
sid-bruno merged 3 commits intousebruno:mainfrom
shubh-bruno:fix/braces-value-recognition
Jan 19, 2026
Merged

fix: improve {{var}} detection using cursor-based brace matching#6691
sid-bruno merged 3 commits intousebruno:mainfrom
shubh-bruno:fix/braces-value-recognition

Conversation

@shubh-bruno
Copy link
Collaborator

@shubh-bruno shubh-bruno commented Jan 6, 2026

Description

This PR is for fixing hover tooltip {{...}} detection in JSON by matching around cursor and ignoring empty/escaped vars.

PR for fixing {{...}} detection in JSON - #6671
Jira

Contribution Checklist:

  • I've used AI significantly to create this pull request
  • The pull request only addresses one issue or adds one feature.
  • The pull request does not introduce any breaking changes
  • I have added screenshots or gifs to help explain the change if applicable.
  • I have read the contribution guidelines.
  • Create an issue and link to the pull request.

Note: Keeping the PR small and focused helps make it easier to review and merge. If you have multiple changes you want to make, please consider submitting them as separate pull requests.

Publishing to New Package Managers

Please see here for more information.

Summary by CodeRabbit

  • Refactor
    • Improved variable-hover detection so tooltips appear only for well-formed, non-empty variable references by switching to a more robust line-based extraction and validation flow; reduces false/malformed tooltip triggers while preserving existing tooltip rendering behavior.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 6, 2026

Walkthrough

Replaced token-based hover detection with cursor-position scanning in brunoVarInfo.js: the code reads the full line, searches left for {{ and right for }}, validates non-empty inner content and balanced braces, constructs a synthetic variable token, and only then invokes the existing render path to show tooltips.

Changes

Cohort / File(s) Summary
Variable hover/tooltip logic
packages/bruno-app/src/utils/codemirror/brunoVarInfo.js
Replaced reliance on CodeMirror token-at-cursor with full-line scanning: finds {{ leftward and }} rightward from cursor, validates non-empty/non-whitespace inner content and correct brace order, builds a synthetic token object, and passes it to existing renderVarInfo/showPopup. Early-returns on mismatches; no exported signature changes.

Sequence Diagram(s)

(omitted — change is localized and does not introduce multi-component sequential flows requiring a diagram)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • helloanoop
  • lohit-bruno
  • naman-bruno

Poem

Between two braces the cursor peers,
It scans left and right, dispelling fears,
Only well-formed vars are shown,
A tidy token, truth well known,
Tooltips sing where code appears ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: improving variable detection using cursor-based brace matching in the codebase.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI Agents
In @packages/bruno-app/src/utils/codemirror/brunoVarInfo.js:
- Around line 880-882: Update the inaccurate inline comment immediately after
the else-if that uses variableNameRegex (the block that sets variableName = str
and variableValue = interpolate(get(variables, variableName), variables));
replace the existing text that says it only covers "{" "}" or random JSON syntax
with a clear fallback comment such as "Fallback: return undefined for strings
that don't match any supported format" and add a short JSDoc note near
variableNameRegex (the /^[\w-.]*$/ pattern) clarifying that it intentionally
allows word chars, hyphens, and dots and thus will match short strings.
🧹 Nitpick comments (1)
packages/bruno-app/src/utils/codemirror/brunoVarInfo.js (1)

883-885: Update comment to reflect broader scope.

The comment states "If it's just '{', '}', or random JSON syntax" but this else block now catches ALL non-matching strings, not just those specific cases. Consider updating to:

  } else {
-    // If it's just "{", "}", or random JSON syntax, return undefined
+    // No matching pattern - return undefined for non-variable strings
    return { variableName: undefined, variableValue: undefined };
  }
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8300abe and ebf3b06.

📒 Files selected for processing (1)
  • packages/bruno-app/src/utils/codemirror/brunoVarInfo.js
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{js,jsx,ts,tsx}

📄 CodeRabbit inference engine (CODING_STANDARDS.md)

**/*.{js,jsx,ts,tsx}: Use 2 spaces for indentation. No tabs, just spaces
Stick to single quotes for strings. For JSX/TSX attributes, use double quotes (e.g., )
Always add semicolons at the end of statements
No trailing commas
Always use parentheses around parameters in arrow functions, even for single params
For multiline constructs, put opening braces on the same line, and ensure consistency. Minimum 2 elements for multiline
No newlines inside function parentheses
Space before and after the arrow in arrow functions. () => {} is good
No space between function name and parentheses. func() not func ()
Semicolons go at the end of the line, not on a new line
Names for functions need to be concise and descriptive
Add in JSDoc comments to add more details to the abstractions if needed
Add in meaningful comments instead of obvious ones where complex code flow is explained properly

Files:

  • packages/bruno-app/src/utils/codemirror/brunoVarInfo.js
🧠 Learnings (1)
📚 Learning: 2025-12-17T21:41:24.730Z
Learnt from: naman-bruno
Repo: usebruno/bruno PR: 6407
File: packages/bruno-app/src/components/Environments/ConfirmCloseEnvironment/index.js:5-41
Timestamp: 2025-12-17T21:41:24.730Z
Learning: Do not suggest PropTypes validation for React components in the Bruno codebase. The project does not use PropTypes, so reviews should avoid proposing PropTypes and rely on the existing typing/validation approach (e.g., TypeScript or alternative runtime checks) if applicable. This guideline applies broadly to all JavaScript/JSX components in the repo.

Applied to files:

  • packages/bruno-app/src/utils/codemirror/brunoVarInfo.js

Comment on lines 880 to 882
} else if (variableNameRegex.test(str)) {
variableName = str;
variableValue = interpolate(get(variables, variableName), variables);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

# Find the variableNameRegex pattern definition
rg -n "variableNameRegex\s*=" packages/bruno-app/src/utils -A 2 --type=js --type=ts

Repository: usebruno/bruno

Length of output: 285


🏁 Script executed:

# Check imports and usage of variableNameRegex in brunoVarInfo.js
rg -n "variableNameRegex" packages/bruno-app/src/utils/codemirror/brunoVarInfo.js -B 2 -A 2

Repository: usebruno/bruno

Length of output: 776


🏁 Script executed:

# Find all calls to extractVariableInfo in the codebase
rg -n "extractVariableInfo\(" packages/bruno-app/src --type=js --type=ts -C 2

Repository: usebruno/bruno

Length of output: 13754


🏁 Script executed:

# Read the full extractVariableInfo function to understand the context
sed -n '850,900p' packages/bruno-app/src/utils/codemirror/brunoVarInfo.js | cat -n

Repository: usebruno/bruno

Length of output: 1727


Update the comment at line 883 to reflect the actual fallback behavior.

The new code at lines 880-882 is intentional—comprehensive test cases confirm it's a feature to support direct variable name references (e.g., baseUrl, userId). However, the comment at line 883 is inaccurate. It states "If it's just "{", "}", or random JSON syntax" but this block actually catches all other strings that don't match the three preceding patterns (double braces, path params, or valid variable names).

Suggest updating the comment to something like:

// Fallback: return undefined for strings that don't match any supported format

Also note: the regex /^[\w-.]*$/ is permissive and will match any string containing only word characters, hyphens, or dots—including very short strings. This is intentional per the test suite, but consider if this behavior should be documented in JSDoc.

🤖 Prompt for AI Agents
In @packages/bruno-app/src/utils/codemirror/brunoVarInfo.js around lines 880 -
882, Update the inaccurate inline comment immediately after the else-if that
uses variableNameRegex (the block that sets variableName = str and variableValue
= interpolate(get(variables, variableName), variables)); replace the existing
text that says it only covers "{" "}" or random JSON syntax with a clear
fallback comment such as "Fallback: return undefined for strings that don't
match any supported format" and add a short JSDoc note near variableNameRegex
(the /^[\w-.]*$/ pattern) clarifying that it intentionally allows word chars,
hyphens, and dots and thus will match short strings.

@pull-request-size pull-request-size bot added size/L and removed size/S labels Jan 6, 2026
@shubh-bruno
Copy link
Collaborator Author

shubh-bruno commented Jan 6, 2026

image

Tested for below json body

{
  "basic_unquoted": {{ val1 }},
  "basic_number": {{123}},
  "basic_quoted": "{{val1}}",
  "basic_quoted": "{{val1}}",
  "multiple_same_line": {
    "a": {{a}},
    "b": {{b}},
    "c": {{c}}
  },
  "array_unquoted": [
        {{val1}},
        {{val2}},
        {{123}}
  ],
  "nested_object": {
    "id": {{userId}},
    "name": "{{userName}}",
    "deep": {
      "token": {{user.token}},
      "email": "{{user.profile.email}}"
    }
  },
  "string_with_multiple_vars": "Hello {{name}}, your id is {{id}} and org is {{org}}",
  "string_var_at_start": "{{val1}} is at the beginning",
  "string_var_at_end": "This ends with {{val1}}",
  "string_adjacent_vars": "{{a}}{{b}}{{c}}",
  "path_dot_notation_unquoted": {{user.profile.name}},
  "path_dot_notation_quoted": "{{user.profile.name}}",
  "variable_as_key": {
    "{{dynamicKey}}": "value",
    "{{anotherKey}}": "{{anotherVal}}"
  },
  "whitespace_inside_braces": {{      spacedVar      }},
  "whitespace_in_string": "Value is {{      spacedVar      }} here",
  "incomplete_should_not_popup_1": "{{",
    "incomplete_should_not_popup_2": "}}",
  "incomplete_should_not_popup_3": "{{val1",
    "incomplete_should_not_popup_4": "val1}}",
  "empty_should_not_popup_1": "{{}}",
  "empty_should_not_popup_2": "{{   }}",
  "invalid_extra_braces_should_not_popup": "{{{val1}}}",
  "curly_braces_not_var": "{ \"a\": 1 }",
  "combo_real_world": {
    "Authorization": "Bearer {{token}}",
    "X-User": "{{user.id}}",
    "payload": {
      "currency": "INR",
      "notes": "Deliver to {{address.city}}, {{address.state}}"
    }
  }
}

@shubh-bruno shubh-bruno changed the title fix: vars value recognition inside braces fix: improve {{var}} detection using cursor-based brace matching Jan 7, 2026
@sid-bruno sid-bruno merged commit 51e3651 into usebruno:main Jan 19, 2026
8 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Jan 28, 2026
6 tasks
@coderabbitai coderabbitai bot mentioned this pull request Feb 19, 2026
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants