Skip to content

fix(editor): reindent pasted code without range formatting - #341

Merged
fcoury-oai merged 1 commit into
mainfrom
fcoury/paste-indentation-fallback
Aug 24, 2026
Merged

fcoury-oai merged 1 commit into
mainfrom
fcoury/paste-indentation-fallback

Conversation

@fcoury-oai

Copy link
Copy Markdown
Collaborator

Why

#334 enabled format-on-paste by default, but its implementation relied entirely on LSP range formatting. rust-analyzer supports whole-document formatting but explicitly does not support range formatting, so pasted Rust snippets kept their original indentation instead of aligning with their destination. Whole-file pastes beginning with a module documentation comment could also remain uniformly indented.

What changed

  • Reindent register, system-clipboard, bracketed-terminal, and visual-line pastes with Red's existing Tree-sitter indentation engine, inside the original paste undo transaction.
  • Align pasted snippets to their surrounding block, remove shared indentation from whole-file pastes, and preserve relative nesting, configured tabs/spaces, cursor position, and multiline raw-string contents.
  • Keep LSP range formatting when available, honor formatting.on_paste = false, work when LSP is disabled, and bound local processing for large pastes.
  • Update the formatting configuration documentation and add focused coverage for nested snippets, leading //! comments, whole files, protected string contents, undo, and disabled formatting/LSP.

How to Test

  1. Open a Rust method with an indented body and paste:

    self.retry_revision = None;
    if self.in_flight_trigger == Some(RecapTrigger::Automatic) {
        self.clear_in_flight_request();
    }

    Expect the first two lines and closing brace to align with the method body, the inner call to indent one additional level, and a single undo to remove the entire paste.

  2. Paste a uniformly indented Rust module beginning with //! into an empty .rs file. Expect the module comment, use statements, and impl block to start at column zero while nested methods retain their relative indentation.

  3. Paste a multiline raw string and verify its contents remain unchanged. Repeat with [formatting] on_paste = false and verify the pasted whitespace remains untouched.

  4. Run cargo test --test editing rust_, cargo test --lib paste, and cargo test --lib syntax_indent. The complete editing suite also passes 413 tests with --skip dot_repeats_linewise_paste_and_visual_block_insert; that existing 2 MiB stack-overflow test fails unchanged on main. Strict all-target/all-feature Clippy passes.

@fcoury-oai
fcoury-oai merged commit ae9c323 into main Aug 24, 2026
11 checks passed
@fcoury-oai
fcoury-oai deleted the fcoury/paste-indentation-fallback branch August 24, 2026 16:25
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.

1 participant