Skip to content

Wire Document Formatting to editor (Shift+Alt+F) #231

@mitchdenny

Description

@mitchdenny

Summary

Wire the existing Formatting LSP infrastructure to the editor so users can auto-format the current document with Shift+Alt+F.

Trigger

  • Hotkey: Shift+Alt+F - Format entire document (standard VS Code binding)
  • Selection hotkey: Ctrl+K Ctrl+F - Format selection (optional, uses range formatting)

Existing Infrastructure

Layer Location Method Status
LSP Client LanguageServerClient.cs RequestFormattingAsync(), RequestRangeFormattingAsync() Exists
Feature Controller LspFeatureController.cs RequestFormattingAsync() Exists
Decoration Provider LanguageServerDecorationProvider.cs RequestFormattingAsync() Exists
Editor Binding EditorNode.cs Not bound Missing

Implementation

1. Add Shift+Alt+F binding in SetupInputBindings (EditorNode.cs)

bindings.Shift().Alt().Key(Hex1bKey.F).Action(TriggerFormattingAsync, "Format document");

2. Add TriggerFormattingAsync method

  1. Call provider.RequestFormattingAsync(tabSize, insertSpaces) with editor settings
  2. Receive TextEdit[] back from the LS
  3. Sort edits in reverse document order (bottom-to-top) to preserve positions
  4. Apply each edit to the document via State.ReplaceRange(start, end, newText)
  5. If ReplaceRange does not exist, add it to EditorState

3. Apply TextEdits helper

Create a reusable ApplyTextEdits(TextEdit[]) method since rename, code actions, and formatting all need it:

private void ApplyTextEdits(TextEdit[] edits)
{
    // Sort reverse to avoid position shifting
    var sorted = edits.OrderByDescending(e => e.Range.Start.Line)
                      .ThenByDescending(e => e.Range.Start.Character);
    foreach (var edit in sorted)
    {
        // Convert LSP 0-based positions to 1-based document positions
        // Replace range with new text
    }
}

4. Update README

  • Document Shift+Alt+F hotkey

Visual Behavior

  1. User presses Shift+Alt+F
  2. The entire document is reformatted in-place
  3. Cursor position is preserved as closely as possible

Tests

  • Shift+Alt+F applies formatting edits
  • Edits are applied in correct reverse order
  • Cursor position is preserved after formatting
  • No-op when LS returns no edits

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions