Repository navigation
Fix #127: Format keeps the cursor, Format Selection changes only the selection - #146
Conversation
…y the selection
- Range formatting is used whenever the context is not the whole document
(previously only when it did not start at offset 0).
- The range sent to the server ends before a line the selection only
touches at column 0; edits are limited to the selected whole lines.
- Server edits are applied as the minimal changed text (common prefix and
suffix are kept), so unchanged code keeps its positions, and the caret
and selection stay on their lines instead of jumping to the end.
- LSP positions past a line or the document are clamped instead of
removing text up to an incorrect limit.
- Fixed the log messages (String.format with "{0}") and restore the
interrupt flag when interrupted.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
albilu
left a comment
There was a problem hiding this comment.
Thanks for the contribution. The ordinary single-caret Black case now keeps the cursor on its original line, and the tested Black/autopep8 selection-boundary and EOF cases behave correctly.
Requesting changes before merging because two reproduced issues remain in the new code:
- P1 — YAPF Format Selection can delete selected code. The per-edit selection filter can accept a deletion while rejecting the corresponding insertion. Please handle related edits together so selection filtering cannot apply only half of a replacement.
- P2 — Caret restoration drops additional carets. Calling
EditorCaret.setDot()to restore the last caret removes the other carets. Please preserve all caret/selection entries through the NetBeans multicaret APIs.
The inline comments include reproductions and expected behavior. Since you mentioned having tests in your branch, please include focused regression tests for these two cases in this PR, with the minimal test setup needed to run them. The tests should assert that selected statements are preserved and correctly formatted, unselected code remains unchanged, and multiple carets/selections survive formatting. Please also retain coverage for the original single-caret cursor-jump fix.
Validation: JDK 21 / RELEASE310 compilation passed. A temporary harness exercised 27 formatter-response cases using actual Black, autopep8 and YAPF output, plus NetBeans BaseDocument/EditorCaret checks under a virtual display on both the UI and background threads. The relevant behavior was compared with base commit b5201d4. This was harness validation; a full interactive IDE session was not performed.
Reviewed head: 0ac67dfd522dd1eff9ff6f1aa98b6f4aefa4fc80.
…lbilu#146) - Formatter edits are applied to a copy of the text as LSP specifies (ranges refer to the original text, inserts at one position keep their order), and the result is compared with the original. Changed lines are split only where the code before the split is the same in both versions, and each part is applied or rejected as a whole. YAPF's separate deletions and insertions can no longer delete selected code, and whole-document YAPF formatting no longer reorders or drops lines. - If a rejected part re-indents code, no part does, so a block is never left half re-indented. - All carets are read with EditorCaret.getCarets() and restored with replaceCarets(), each at the same code as before (setDot() kept only the last caret, and EditorCaret merges carets that meet in replaced text). - Regression tests built from real yapf, autopep8 and Black responses; JUnit test dependency and a headless surefire setting in the pom. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the detailed review. Both points are fixed in cde1b6a, and the PR now includes tests:
Also found while testing: with YAPF, whole-document Format reorders or drops lines, on master too. Other validation: a harness applied 648 real formatter responses (9 sample files × yapf, autopep8 and Black × the whole document and every line range). Whole-document results matched the formatter output, unselected lines never changed, results compiled whenever the input and the formatter output did, and 2,187 caret positions stayed at the same code. Caret restoration was also checked while holding the document lock, on the UI thread and on a background thread. I haven't done an interactive IDE session. One test detail: |
albilu
left a comment
There was a problem hiding this comment.
Thanks for addressing the findings and adding the regression tests. Rechecked cde1b6a: both original reproductions now pass. YAPF selection formatting preserves the selected statement and surrounding code, and the original multicaret case retains both caret positions.
All 13 tests pass with JDK 21 on both RELEASE290 and RELEASE310. Independent harness checks also covered 363 real formatter responses (39 whole-document responses and 324 selection responses).
Keeping changes requested for one newly reproduced P2 issue in FormatterEdits.map(), detailed inline: Black can remove parentheses, and counting non-whitespace characters then maps a cursor in unchanged code to the end of a later line. The previous PR revision keeps the cursor correctly in this case, so this is a regression in the follow-up commit.
Please update the mapping to account for inserted/removed non-whitespace characters and add a regression test for the supplied Black example, including the background-thread path. Validation used actual NetBeans document/caret classes under a virtual display; no full interactive IDE session was performed.
…u#146) Black removes optional parentheses, so counting non-whitespace characters moved a cursor in unchanged code to a later line. Inside a change, the code tokens of both versions are now aligned (Myers diff), and a caret stays before the same token, or after the last kept token before it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the recheck. The P2 is fixed in 6b3978b (details inline), with three regression tests for your Black example, including the background-thread path. The 16 tests pass with JDK 21 on RELEASE290 and RELEASE310, also without a display. Harness: I added three parenthesis-heavy sources to the formatter harness: your example,
The only disagreements with the harness's own difflib alignment are cases with more than one valid alignment: which of two |
Distinguish incomplete token alignment from deleted tokens and fall back to a clamped line and column within the replacement. Cover large Black quote normalization, multiple carets and selections, background formatting, and shortened lines.
albilu
left a comment
There was a problem hiding this comment.
Thanks for addressing the review findings and adding the regression coverage. I verified the YAPF selection fix, preservation of multiple carets/selections, and the Black parenthesis-removal fix.
I added a small follow-up commit, 108378f, for one remaining edge case found during verification: Black's quote normalization in a 600-item list exceeds the bounded token-alignment limit. The incomplete alignment was being treated as deleted code, moving the cursor from offset 4705 to 15 and collapsing multiple carets.
The follow-up distinguishes an incomplete alignment from deleted tokens and falls back to the same line and column within the replacement, clamping to the available text. Exact token mapping remains in use when alignment completes. Three regression tests cover multiple carets/selections on both the UI and background threads, plus clamping on a shortened line.
Validation: all 19 tests pass with JDK 21 on both RELEASE290 and RELEASE310. The independent harness also passes 39 whole-document and 324 selection cases from real formatter responses. NetBeans document/caret checks under a virtual display confirm that the 600-item case now keeps offset 4705 on both execution paths. This was automated editor validation, not a full interactive IDE session.
The requested code changes are resolved, and I found no remaining code blocker in the reviewed head, 108378f. Approving; merge will follow once the required build and CodeQL checks pass.
Fixes #127. First of the per-issue PRs discussed in #145.
What changes (one file,
PythonReformatTask.java):String.formatwith{0}, and the interrupt flag is now restored.How to test
x=1on line 3 andy=2on line 10.x = 1changes, line 10 staysy=2.Built with RELEASE310 and RELEASE290; tested by hand in NetBeans 30/31 on Linux. I also have unit tests for this in my branch, but since the repo has no test setup they aren't included. I can send them as a separate PR if you want a test suite.
Written with AI assistance (Claude Opus), reviewed and tested by me.
🤖 Generated with Claude Code