Skip to content

Comments

Fix autocomplete double insertion by using live cursor position for replacement range#3563

Open
NamanGoyalK wants to merge 1 commit intodart-lang:mainfrom
NamanGoyalK:fix-issue-3562
Open

Fix autocomplete double insertion by using live cursor position for replacement range#3563
NamanGoyalK wants to merge 1 commit intodart-lang:mainfrom
NamanGoyalK:fix-issue-3562

Conversation

@NamanGoyalK
Copy link

@NamanGoyalK NamanGoyalK commented Feb 5, 2026

This PR fixes a race condition in the editor where accepting an autocomplete suggestion would sometimes result in duplicated characters (e.g., typing Text.ri and selecting rich resulted in Text.richri).

fixes #3562

The Issue

The issue was caused by a "stale state" race condition:

  1. When the completion request is sent (e.g., at Text.), the Analysis Server calculates a replacement range based on that snapshot (length 0).
  2. If the user continues typing (e.g., ri) while the request is in flight, the cursor advances.
  3. When the user accepts the suggestion, the editor applied the original (stale) replacement range, failing to consume the newly typed characters.

The Fix

This change updates _applyCompletion in editor_service.dart to calculate the replacement range dynamically at the moment of selection.

Instead of relying solely on the static replacementLength returned by the server, we now calculate the effectiveEnd of the replacement using the live cursor position:

final effectiveEnd = math.max(cursorIndex, serverEnd);
image
  • I’ve reviewed the contributor guide and applied the relevant portions to this PR.

@github-actions
Copy link

github-actions bot commented Feb 8, 2026

PR Health

License Headers ✔️
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
Files
no missing headers

All source files should start with a license header.

Unrelated files missing license headers
Files
pkgs/dart_services/lib/src/context.dart
pkgs/samples/lib/brick_breaker.dart
pkgs/samples/lib/fibonacci.dart
pkgs/samples/lib/hello_world.dart
pkgs/samples/lib/main.dart
pkgs/samples/lib/sunflower.dart

This check can be disabled by tagging the PR with skip-license-check.

API leaks ✔️

The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.

Package Leaked API symbol Leaking sources

This check can be disabled by tagging the PR with skip-leaking-check.

Breaking changes ✔️
Package Change Current Version New Version Needed Version Looking good?

This check can be disabled by tagging the PR with skip-breaking-check.

Unused Dependencies ✔️
Package Status
None No packages found to check.

For details on how to fix these, see dependency_validator.

This check can be disabled by tagging the PR with skip-unused-dependencies-check.

Coverage ✔️
File Coverage

This check for test coverage is informational (issues shown here will not fail the PR).

This check can be disabled by tagging the PR with skip-coverage-check.

Changelog Entry ✔️
Package Changed Files

Changes to files need to be accounted for in their respective changelogs.

This check can be disabled by tagging the PR with skip-changelog-check.

…ally

Previously, the editor relied on the static replacement length returned by the Analysis Server. If the user continued typing while the completion request was in flight, the server's response would contain a stale (shorter) replacement range. This caused the newly typed characters to be preserved, resulting in duplication (e.g., typing 'ri' resulted in 'Text.richri' instead of 'Text.rich').

This change introduces a custom hint handler that calculates the effective replacement end position at the moment of selection. By taking the maximum of the server's intended end and the current live cursor position, we ensure that all characters typed by the user are correctly replaced.

Fixes: dart-lang#3562
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.

Autocomplete duplicates suggestions (double insertion) instead of replacing prefix

1 participant