Skip to content

Feat/autofix poc#22

Draft
kevv87 wants to merge 25 commits intomainfrom
feat/autofix_poc
Draft

Feat/autofix poc#22
kevv87 wants to merge 25 commits intomainfrom
feat/autofix_poc

Conversation

@kevv87
Copy link
Owner

@kevv87 kevv87 commented Jul 29, 2025

PR Summary: Add CodeAction Support and Autofix Functionality

Overview

This PR implements LSP CodeAction support with quick fixes for DML linting errors, plus a new --autofix CLI flag that automatically applies
fixes to files.

Key Features

  1. CodeAction Protocol Support
 - Added textDocument/codeAction request handler
 - Returns quick fixes for diagnostics that have autofix data
 - Supports quickfix CodeAction kind
 - Marks fixes as preferred when they're the only fix available
  1. Autofix Data in Diagnostics
 - Extended DMLError struct with optional fix: Option<DMLFix> field
 - Diagnostics now include fix information in the data field (JSON serialized)
 - Linter automatically generates fixes where possible
  1. DFA CLI Autofix
 - New --autofix flag applies fixes directly to files
 - New --backup flag creates .bak copies before modifying
 - New --dry-run flag shows what would change without modifying files
 - Validates that backup only works with autofix enabled
  1. High-Performance Text Surgery Module
 - Uses ropey crate for efficient text manipulation on large files
 - Detects and prevents conflicting edits
 - Caches Rope instances to avoid redundant parsing
 - Optimized file I/O (single read, conditional write)
 - Early exit when no fixes are available

@kevv87 kevv87 changed the base branch from main to feature/55-indentation-rules July 29, 2025 20:49
kevv87 and others added 23 commits August 20, 2025 18:02
- Add DLS_TEST_DEBUG flag to control test output logs
- Add DLS_CHILD_DEBUG flag to control child process logs
- Allows granular control over logging during tests
- Default behavior is now clean output without logs
- Add fix field to DMLError struct to carry TextEdit fixes
- Update DMLError::to_diagnostic() to serialize fix into data field
- Modify LinterAnalysis::new() to preserve fix from DMLStyleError
- Add test_diagnostic_has_autofix_data to verify fixes in diagnostics
- Create autofix.dml example file for testing
- Add SOURCE_AUTOFIX and MOCK_URI_AUTOFIX test fixtures

The diagnostic.data field now contains the serialized TextEdit when a
lint error has an available autofix. This enables CodeAction handlers
to extract these fixes and return them as quick fix actions.
- Implement CodeActionRequest handler to extract fixes from diagnostic.data
- Deserialize TextEdit from diagnostic data and create CodeAction responses
- Return QuickFix actions with WorkspaceEdit containing the fixes
- Set is_preferred to None to let client decide preference
- Add comprehensive test to verify CodeAction fixes match diagnostics
- Test validates: action count, type, titles, edits, and exact fix matching
- Update existing test to use autofix.dml with actual fixable errors

CodeAction handler now reads fixes from diagnostic.data field and converts
them into proper LSP CodeActions that clients can execute to apply fixes.
- Add analyze_files() as main public API for DFA functionality
- Create AnalysisRequest and AnalysisResult structs for clear interface
- Extract business logic from main_inner() into focused functions:
  - setup_client(): Configure DLS client with request parameters
  - open_and_analyze_files(): Open files and wait for analysis
  - collect_diagnostics(): Gather results from analysis
- Add client.shutdown() to properly cleanup DLS process
- Add integration test validating analyze_files() behavior
- Add tempfile as dev dependency for test infrastructure
- Keep ClientInterface as internal implementation detail

This refactoring separates CLI argument parsing from business logic,
making the DFA functionality testable and reusable. The test validates
the refactor maintains existing behavior.
- Create text_edit module with utilities for applying TextEdits
- Implement apply_edit_to_content() to apply single edit to string
- Implement apply_edits_to_content() to apply multiple edits
- Implement has_conflicting_edits() to detect overlapping edits
- Calculate line positions from content for accurate byte offsets
- Sort edits in reverse order to avoid offset shifting issues
- Add comprehensive tests for all text edit functions
- All functions are pub(crate) - internal to dfa module

These utilities work entirely in memory with strings and will be used
by the autofix functionality to apply fixes to file contents before
writing them back to disk.
- Add request_code_actions() method to ClientInterface
- Method sends textDocument/codeAction request to LSP server
- Receives and parses CodeAction responses
- Filters CodeAction items from CodeActionOrCommand enum
- Returns Vec<CodeAction> with all available quick fixes
- Method is pub(crate) for internal dfa module use
- Add anyhow macro import for error handling
- Keep ClientInterface as pub for dfa binary compatibility

This enables requesting code actions from the DLS server with
diagnostic context, which will be used by autofix functionality
to obtain fixes that can be applied to files.
- Add autofix field to AnalysisRequest
- Implement apply_fixes_to_files with subfunctions for clarity
- Add text edit utilities for applying LSP TextEdits
- Add CodeAction request capability to ClientInterface
- Fix collect_diagnostics to populate diagnostics field
- Add comprehensive tests for analyze_files with/without autofix
- Add conflict detection for overlapping edits
- Replace custom text editing with Ropey rope data structure
- Create text_surgery module with TextBuffer for O(log n) edits
- Consolidate text manipulation into single module
- Remove text_edit.rs in favor of efficient rope-based approach
- Maintain public API: apply_edits_to_file() and has_conflicting_edits()
Adds --dry-run flag to preview autofix changes without modifying files.
When enabled, shows what fixes would be applied but skips file writes.
Only available when --autofix is enabled.
@kevv87 kevv87 changed the base branch from feature/55-indentation-rules to main November 28, 2025 04:17
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