Skip to content

Fix abbreviation expansion inserting a double trailing space#1111

Merged
fdncred merged 1 commit into
nushell:mainfrom
MerlijnW70:fix/abbr-double-space
Jun 26, 2026
Merged

Fix abbreviation expansion inserting a double trailing space#1111
fdncred merged 1 commit into
nushell:mainfrom
MerlijnW70:fix/abbr-double-space

Conversation

@MerlijnW70

Copy link
Copy Markdown
Contributor

Summary

Expanding an abbreviation by pressing <space> inserted two trailing
spaces instead of one — one before the cursor and one after. This breaks
history-based completion: e.g. git switch (two spaces) is not a prefix of
git switch main, so the ghost-text completion disappears until the extra
space is deleted manually. Fixes #1108.

Fix

On a <space> expansion the triggering space has already been inserted into
the buffer before try_expand_abbreviation_at_cursor runs. The replacement
selected only the abbreviation word (word_start..word_end), leaving that
space in place, and then inserted the expansion followed by its own suffix
space — yielding two trailing spaces.

The selection now extends through cursor_position_in_buffer instead of the
end of the word, so the already-inserted triggering space is replaced by the
expansion's suffix. The submit (<enter>) path is unaffected: there the cursor
sits at the end of the word, so the selected range is unchanged.

Test

Adds abbreviation_expands_on_space_without_double_space, which exercises the
<space> expansion path (submitted = false) — previously untested; all
existing abbreviation tests only covered the submit path. It asserts that
expanding gcgit commit after the triggering space yields exactly
git commit (one trailing space), and fails against the old behaviour
(git commit ).

Reference

Reported in #1108. The issue pinpointed src/engine.rs abbreviation handling
as the cause.

When an abbreviation was expanded by pressing <space>, the triggering
space had already been inserted into the buffer before the expansion ran.
The replacement selected only the abbreviation word (word_start..word_end),
leaving that space in place, and then inserted the expansion followed by
its own suffix space -- producing two trailing spaces (one before the
cursor and one after). This broke history-based completion, since e.g.
"git switch  " is not a prefix of "git switch main".

Select through the cursor position instead of the end of the word so the
already-inserted triggering space is replaced by the expansion's suffix.
The submit (<enter>) path is unaffected: there the cursor sits at the end
of the word, so the selection is unchanged.

Adds a regression test covering the <space> expansion path, which was
previously untested.
@fdncred

fdncred commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

lgtm. what do you think @kronberger-droid ?

@kronberger-droid

Copy link
Copy Markdown
Collaborator

Seems fine to me to.
I am just testing it and check how/if it interacts with my #1109.
I will keep you all updated.

@kronberger-droid

Copy link
Copy Markdown
Collaborator

Found some things which are orthogonal to this change. Fine to land.

@fdncred fdncred merged commit 4d20caf into nushell:main Jun 26, 2026
7 checks passed
@fdncred

fdncred commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Thanks!

@MerlijnW70

Copy link
Copy Markdown
Contributor Author

@fdncred your welcome,

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.

Abbreviation expansion double-inserts a trailing space

3 participants