Commit f89e47d
refactor: modularize codebase and enhance Mix formatter integration (#8)
# Modularize Codebase and Enhance Mix Formatter Integration
This PR implements comprehensive refactoring to improve code
organization while maintaining the Mix.Tasks.Format plugin integration
that allows formatting both pure markdown and embedded markdown in
Elixir files.
## Mix.Tasks.Format Integration
This formatter works as a **Mix formatter plugin** via `@behaviour
Mix.Tasks.Format`:
- **Plugin Priority**: When configured, it processes `.ex/.exs` files
**BEFORE** the standard `elixir_format` function
- **Smart Processing**: Two-phase approach for Elixir files:
1. Formats markdown content in module attributes (`@moduledoc`, `@doc`,
etc.)
2. Calls `Code.format_string\!` to ensure proper Elixir code formatting
- **File Support**: Handles pure markdown (`.md/.markdown`) AND embedded
markdown in Elixir files
- **Sigil Support**: Provides `~M` sigil for embedded markdown
formatting
## Refactoring Impact
- **Main module**: 515 → 313 lines (40% reduction)
- **New modules**: 3 focused modules (AstProcessor, PatchBuilder,
StringUtils)
- **Code duplication**: Eliminated 150+ lines
- **Tests**: All pass (13 doctests, 80 tests, 0 failures, 1 skipped)
## Changes by Phase
### ✅ Validator Pattern Matching
- Consolidated repetitive validation using shared
`validate_atom_choice/3` and `validate_string_choice/3`
- Reduced function count from 15 to 8, eliminated 69 duplicate lines
### ✅ AST Processing Module
- Extracted `DprintMarkdownFormatter.AstProcessor` for parsing and patch
creation
- Moved `parse_elixir_source`, `collect_patches_for_doc_attributes`,
etc.
### ✅ Control Flow Simplification
- Replaced `cond` with pattern matching, combined function heads
- Improved error handling consistency
### ✅ Patch Builder Logic
- Created `DprintMarkdownFormatter.PatchBuilder` for string replacements
- Comprehensive documentation with examples
### ✅ Rust NIF Improvements
- Extracted config building functions, reduced duplication by 30+ lines
- Cleaner match statements for enum mapping
## Quality Assurance
✅ All tests pass without modification
✅ Full quality checks (Credo, Dialyzer, Cargo fmt/clippy)
✅ No breaking changes to public API
✅ Performance maintained
## Benefits
- **Maintainability**: Clear separation of concerns, focused modules
- **Developer Experience**: Better organization, comprehensive
documentation
- **Future-proof**: Modular structure enables easier enhancements
---
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 69119c4 commit f89e47d
13 files changed
Lines changed: 626 additions & 488 deletions
File tree
- lib
- dprint_markdown_formatter
- native/dprint_markdown_formatter/src
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | 5 | | |
9 | 6 | | |
10 | 7 | | |
| |||
0 commit comments