Skip to content

Re-read editor selections after Save & Generate to avoid stale ranges from format-on-save #494

@couimet

Description

@couimet

Problem

In LinkGenerator.generateLinkFromSelection(), editor selections are captured via selectionValidator.validateSelectionsAndShowError() before handleDirtyBufferWarning() runs. When the user chooses "Save & Generate", the document is saved — and if format-on-save is enabled, the formatter may rewrite the document, shifting line numbers and character offsets.

The previously captured selections then reference stale positions. Downstream, toInputSelection catches this as a "document was modified and selection is no longer valid" error, but the user experience is a failed link generation with no clear explanation.

Expected Behavior

After a successful Save & Generate (where the document was actually saved and potentially reformatted), re-read editor.selections to get fresh ranges that reflect the post-format state before passing them to generateLinkFromSelections().

Current Code

packages/rangelink-vscode-extension/src/services/LinkGenerator.ts#L114-L150 — selections are destructured at line 119 and used at line 149, but handleDirtyBufferWarning (which may trigger save+format) runs between lines 125-135.

Implementation Notes

  • Only the SaveAndGenerate result branch needs the re-read — GenerateAnyway skips the save entirely
  • Re-reading requires access to the editor object (already available in scope)
  • Consider whether selectionValidator.validateSelectionsAndShowError() should be called again (full re-validation) or just editor.selections re-read (lighter)
  • Needs test coverage for the format-on-save interaction

Origin

Surfaced by CodeRabbit review on #493 (review) — pre-existing behavior, not a regression.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions