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.
Problem
In
LinkGenerator.generateLinkFromSelection(), editor selections are captured viaselectionValidator.validateSelectionsAndShowError()beforehandleDirtyBufferWarning()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
selectionsthen reference stale positions. Downstream,toInputSelectioncatches 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.selectionsto get fresh ranges that reflect the post-format state before passing them togenerateLinkFromSelections().Current Code
packages/rangelink-vscode-extension/src/services/LinkGenerator.ts#L114-L150— selections are destructured at line 119 and used at line 149, buthandleDirtyBufferWarning(which may trigger save+format) runs between lines 125-135.Implementation Notes
SaveAndGenerateresult branch needs the re-read —GenerateAnywayskips the save entirelyeditorobject (already available in scope)selectionValidator.validateSelectionsAndShowError()should be called again (full re-validation) or justeditor.selectionsre-read (lighter)Origin
Surfaced by CodeRabbit review on #493 (review) — pre-existing behavior, not a regression.