Skip to content

Conversation

@ViscousPot
Copy link

@ViscousPot ViscousPot commented Nov 9, 2025

Description

If trigger is longer than one character, keepTrigger doesn't work as expected. This fixes that :)

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

Summary by CodeRabbit

  • Bug Fixes
    • Fixed autocomplete replacement and cursor positioning when accepting suggestions. The replacement range now uses the actual trigger text length (instead of a fixed value), ensuring correct deletion and accurate cursor placement for variable-length triggers.

@coderabbitai
Copy link

coderabbitai bot commented Nov 9, 2025

Walkthrough

The change updates acceptAutocompleteOption in multi-trigger autocomplete: when not keeping the trigger, the replacement start index now subtracts the actual trigger text length (instead of a fixed 1), altering the replaced range and final cursor position.

Changes

Cohort / File(s) Summary
Trigger text replacement logic
lib/src/multi_trigger_autocomplete.dart
Adjusted start index calculation in acceptAutocompleteOption to subtract the current trigger's text length rather than a fixed offset when the trigger isn't kept; updates replaced text range and resulting cursor placement.

Sequence Diagram(s)

sequenceDiagram
  participant User
  participant Editor
  participant Autocomplete
  Note right of Autocomplete `#D6EAF8`: acceptAutocompleteOption now uses trigger.text.length
  User->>Editor: Choose suggestion
  Editor->>Autocomplete: acceptAutocompleteOption(suggestion, keepTrigger=false)
  Autocomplete->>Autocomplete: startIndex = triggerStart - trigger.text.length
  Autocomplete->>Editor: replace range(startIndex..end) with suggestion
  Editor->>User: document updated, cursor repositioned
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Check edge cases with varying trigger lengths, multi-byte/surrogate characters.
  • Verify cursor placement with adjacent or repeated triggers.

Poem

A rabbit hops through lines of code,
Counts each trigger on the road,
Replaces text and sets the place,
Cursor finds its proper space,
Commit a hop, then off it strode 🐇✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Adjust start offset calculation for query selection' directly relates to the main change: adjusting the start index calculation in acceptAutocompleteOption to fix the keepTrigger bug for multi-character triggers.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

@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

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6f6dc27 and a1358fe.

📒 Files selected for processing (1)
  • lib/src/multi_trigger_autocomplete.dart (1 hunks)

Comment on lines 228 to 229
var start = querySelection.baseOffset;
if (!keepTrigger) start -= 1;
if (!keepTrigger) start -= _currentTrigger.trigger.length;
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

LGTM! Correctly fixes multi-character trigger handling.

The change properly accounts for triggers longer than one character. The previous hardcoded start -= 1 only worked for single-character triggers. Using _currentTrigger.trigger.length ensures the entire trigger is included in the replacement range when keepTrigger is false.

This affects both:

  • The text replacement at line 242: text.replaceRange(start, end, option)
  • The cursor position calculation at line 237: selectionOffset = start + option.length

Consider verifying that tests cover multi-character triggers (e.g., "@@", "###") to prevent regression. You can check if test files exist and validate coverage:


🏁 Script executed:

#!/bin/bash
# Find test files and search for multi-character trigger test cases
fd -e dart -t f test | xargs rg -l "acceptAutocompleteOption|keepTrigger" | head -5

# Check for test cases with multi-character triggers
rg -A 10 "keepTrigger.*false|multi.*character.*trigger" --type dart -g '*test*.dart'

Length of output: 41



🏁 Script executed:

#!/bin/bash
# Read the test file to examine test coverage
cat test/multi_trigger_autocomplete_test.dart | head -200

# Search for test groups or test functions related to acceptAutocompleteOption
rg -n "test|group|acceptAutocompleteOption" test/multi_trigger_autocomplete_test.dart | head -30

Length of output: 8267



🏁 Script executed:

#!/bin/bash
# Search for keepTrigger in test file
echo "=== Searching for keepTrigger usage ==="
rg -n "keepTrigger" test/multi_trigger_autocomplete_test.dart

# Get file length and look for multi-character trigger patterns
echo "=== File statistics and multi-character trigger patterns ==="
wc -l test/multi_trigger_autocomplete_test.dart
rg -n "trigger.*[:=].*['\"][@#]{2,}" test/multi_trigger_autocomplete_test.dart

# List all test functions/groups
echo "=== All test functions in file ==="
rg -n "testWidgets|test\(" test/multi_trigger_autocomplete_test.dart | wc -l
rg -n "testWidgets\|^.*test\(" test/multi_trigger_autocomplete_test.dart | head -20

Length of output: 184


Add tests for multi-character triggers with keepTrigger=false.

The fix correctly updates the trigger offset calculation, but verification shows zero test coverage for this scenario. The test file contains 10 test functions, none of which use the keepTrigger parameter or multi-character triggers.

Add tests covering:

  • Multi-character triggers (e.g., "@@", "###") with keepTrigger=false
  • Correct text replacement range when entire multi-character trigger is removed
  • Cursor position after autocomplete selection

Copy link

@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: 0

🧹 Nitpick comments (1)
lib/src/multi_trigger_autocomplete.dart (1)

229-229: Bug fix correctly handles multi-character triggers.

The change properly accounts for triggers longer than one character by using the actual trigger length instead of the hardcoded 1. This fixes the issue where keepTrigger=false would only remove one character of multi-character triggers like "@@" or "###".

Optional: Use the local trigger variable for consistency.

Since trigger is already null-checked at line 223, you can simplify line 229 to use the local variable directly:

-    if (!keepTrigger) start -= _currentTrigger?.trigger.length ?? 1;
+    if (!keepTrigger) start -= trigger.trigger.length;

This eliminates the unnecessary null-safety operator and fallback, making the code more consistent with the pattern established by the null check above.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b58982f and 60d5b16.

📒 Files selected for processing (1)
  • lib/src/multi_trigger_autocomplete.dart (1 hunks)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant