Skip to content

Fix #288: preserve $1 snippet placeholder when cursor is at end of a … - #1319

Open
jwrhw7tueydwtt7575g wants to merge 2 commits into
redhat-developer:mainfrom
jwrhw7tueydwtt7575g:main
Open

Fix #288: preserve $1 snippet placeholder when cursor is at end of a …#1319
jwrhw7tueydwtt7575g wants to merge 2 commits into
redhat-developer:mainfrom
jwrhw7tueydwtt7575g:main

Conversation

@jwrhw7tueydwtt7575g

@jwrhw7tueydwtt7575g jwrhw7tueydwtt7575g commented Aug 8, 2026

Copy link
Copy Markdown

What does this PR do?

Fixes an issue in the completion collector (yamlCompletion.ts) where a trailing
$1 snippet placeholder was unconditionally stripped from completionItem.insertText.

This stripping was correct when non-newline content after the cursor needed to
coexist with the inserted text, but incorrect when the cursor sat at the end of
a line that ended with a newline — in that case the $1 placeholder is the
editor's signal for caret placement and must be preserved.

For example, with content '- \n' and the caret at the end of '- ', the
service returned 'prop1: ' instead of 'prop1: $1'.

The fix skips the strip when the cursor is at the end of a line that has both
a trailing newline and non-whitespace content before the cursor (e.g. the
sequence marker '- '). The strip is still applied for:

  • content '-' (no newline)
  • whitespace-only lines
  • empty lines

The existing PR #280 workaround
(overwriteRange.start.line === overwriteRange.end.line) is left in place, as
it guards against a separate class of malformed multi-line textEdits unrelated
to this bug.

What issues does this PR fix or reference?

Fixes #288
References #280 (regression introduced by this PR)
References commit 73567b2 (removed the test that should have caught this)

Is it tested? How?

Yes. A regression test, "Check text edit when there is a newline", was
added to test/autoCompletion.test.ts. It asserts that all three completion
items from testArrayMaxProperties.json preserve $1 for content '- \n' at
the end of '- '. This mirrors the assertions from the test that was removed
in commit 73567b2.

… is at end of a non-empty line followed by a newline

The completion collector in yamlCompletion.ts stripped a trailing $1 from
completionItem.insertText unconditionally. This was correct when there was
non-newline content after the cursor that the inserted text needed to
coexist with, but wrong when the cursor sat at the end of a line that
ended with a newline. In the latter case the snippet placeholder is the
editor's signal for caret placement and must be preserved.

For example, with content '- \n' and the caret at the end of '- ', the
service returned 'prop1: ' instead of 'prop1: $1'. This regressed
behavior recorded in PR redhat-developer#280 and the test removed in commit 73567b2.

The strip is now skipped when the cursor is at the end of a line that
has both a trailing newline AND non-whitespace content before the cursor
(e.g. the sequence marker '- '). For content '- ' (no newline) the strip
is still applied; for content '  ' (whitespace-only line) the strip is
still applied; for empty lines the strip is still applied.

The PR redhat-developer#280 workaround (overwriteRange.start.line === overwriteRange.end.line)
is left in place - it guards against a separate class of malformed
multi-line textEdits unrelated to this bug.

A regression test 'Check text edit when there is a newline' is added to
test/autoCompletion.test.ts that asserts all three completion items from
testArrayMaxProperties.json preserve $1 for content '- \n' at the end
of '- '. It mirrors the assertions from the test removed in 73567b2.

Co-Authored-By: Claude <noreply@anthropic.com>
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.

Missing textEdit in CompletionItem when there is a newline

1 participant