|
3 | 3 | ## Current Encoding Usage |
4 | 4 |
|
5 | 5 | ### APIs (15+ functions with encoding parameter) |
| 6 | + |
6 | 7 | - `get_overview(absolute_file_path, encoding="utf-8")` |
7 | 8 | - `search_content(..., encoding="utf-8", ...)` |
8 | 9 | - `read_content(..., encoding="utf-8", ...)` |
9 | 10 | - `edit_content(..., encoding="utf-8", ...)` |
10 | 11 |
|
11 | 12 | ### Internal Functions |
| 13 | + |
12 | 14 | - `src/file_access.py`: 6 functions with encoding |
13 | | -- `src/search_engine.py`: `search_file()` |
| 15 | +- `src/search_engine.py`: `search_file()` |
14 | 16 | - `src/editor.py`: 2 functions with encoding |
15 | 17 | - `src/data_models.py`: `FileOverview.encoding` |
16 | 18 |
|
17 | 19 | ## Implementation Plan |
18 | 20 |
|
19 | 21 | #### Phase 1: Add chardet and detection |
20 | | -- [x] Add chardet dependency |
| 22 | + |
| 23 | +- [x] Add chardet dependency |
21 | 24 | - [x] Create `detect_file_encoding(file_path: str) -> str` in `file_access.py` |
22 | 25 | - [x] Fallback to utf-8 on detection failure |
23 | 26 |
|
24 | 27 | **Implementation Notes:** |
| 28 | + |
25 | 29 | - Added 0.7 confidence threshold to prevent poor chardet guesses |
26 | 30 | - Detection function handles empty files, exceptions, and low confidence cases |
27 | 31 | - Test coverage for UTF-8, Latin-1, UTF-16, empty files, and non-existent files |
28 | 32 |
|
29 | 33 | #### Phase 2: Update file access layer |
| 34 | + |
30 | 35 | - [x] Remove encoding parameter from `read_file_content()` |
31 | | -- [x] Remove encoding parameter from `read_file_lines()` |
| 36 | +- [x] Remove encoding parameter from `read_file_lines()` |
32 | 37 | - [x] Remove encoding parameter from `write_file_content()` |
33 | 38 | - [x] Update internal strategy functions |
34 | 39 | - [x] Add encoding cache per file session |
35 | 40 |
|
36 | 41 | **Implementation Notes:** |
| 42 | + |
37 | 43 | - `read_file_content()` and `read_file_lines()` now call `detect_file_encoding()` internally |
38 | 44 | - `write_file_content()` preserves existing file encoding or defaults to utf-8 for new files |
39 | 45 | - Internal strategy functions (`_read_file_memory`, etc.) still accept encoding parameter |
40 | 46 | - **Skipped caching**: Detection is fast (~1-5ms), avoiding session management complexity |
41 | 47 | - All existing tests pass with auto-detection |
42 | 48 |
|
43 | 49 | #### Phase 3: Update search and edit layers |
| 50 | + |
44 | 51 | - [x] Remove encoding parameter from `search_file()` |
45 | 52 | - [x] Remove encoding parameter from `atomic_edit_file()` |
46 | 53 | - [x] Remove encoding parameter from `_preview_edit()` |
47 | 54 |
|
48 | 55 | **Implementation Notes:** |
| 56 | + |
49 | 57 | - `search_file()` now uses `read_file_lines()` without encoding parameter |
50 | 58 | - `atomic_edit_file()` and `replace_content()` updated to use auto-detection |
51 | 59 | - **No `_preview_edit()` function**: Preview functionality is built into `replace_content()` |
52 | 60 | - All edit operations now preserve original file encoding automatically |
53 | 61 |
|
54 | 62 | #### Phase 4: Update public APIs |
55 | | -- [ ] Remove encoding parameter from all 4 MCP tools |
56 | | -- [ ] Update MCP schema definitions |
57 | | -- [ ] Update `FileOverview.encoding` to show detected value |
| 63 | + |
| 64 | +- [x] Remove encoding parameter from all 4 MCP tools |
| 65 | +- [x] Update MCP schema definitions |
| 66 | +- [x] Update `FileOverview.encoding` to show detected value |
| 67 | + |
| 68 | +**Implementation Notes:** |
| 69 | +- All 4 MCP tools (`get_overview`, `search_content`, `read_content`, `edit_content`) no longer accept encoding |
| 70 | +- MCP schema definitions updated to remove encoding parameter from all tools |
| 71 | +- `FileOverview.encoding` now shows the actual detected encoding value |
| 72 | +- **Breaking change**: All tool signatures simplified, auto-detection is now transparent to users |
58 | 73 |
|
59 | 74 | #### Phase 5: Documentation and testing |
| 75 | + |
60 | 76 | - [ ] Update API.md, design.md, examples |
61 | 77 | - [ ] Add encoding detection tests |
62 | 78 | - [ ] Test various encodings and edge cases |
63 | 79 |
|
64 | 80 | #### Phase 6: Quality assurance |
| 81 | + |
65 | 82 | - [ ] Run pre-push script |
66 | 83 | - [ ] Verify all tests pass |
67 | 84 | - [ ] Update plan as complete |
68 | 85 |
|
69 | 86 | ## Technical Implementation |
70 | 87 |
|
71 | 88 | ### Detection Function |
| 89 | + |
72 | 90 | ```python |
73 | 91 | def detect_file_encoding(file_path: str) -> str: |
74 | 92 | """Detect file encoding using chardet with utf-8 fallback.""" |
75 | 93 | try: |
76 | 94 | with open(file_path, 'rb') as f: |
77 | 95 | sample = f.read(65536) # 64KB sample |
78 | | - |
| 96 | + |
79 | 97 | if not sample: |
80 | 98 | return 'utf-8' |
81 | | - |
| 99 | + |
82 | 100 | result = chardet.detect(sample) |
83 | | - |
| 101 | + |
84 | 102 | # Use detection only if confidence is reasonable (>= 0.7) |
85 | 103 | if result and result.get('confidence', 0) >= 0.7: |
86 | 104 | return result['encoding'] or 'utf-8' |
87 | 105 | else: |
88 | 106 | return 'utf-8' |
89 | | - |
| 107 | + |
90 | 108 | except Exception: |
91 | 109 | return 'utf-8' |
92 | 110 | ``` |
93 | 111 |
|
94 | 112 | ## Testing |
95 | 113 |
|
96 | 114 | ### Encodings to Test |
| 115 | + |
97 | 116 | - UTF-8, UTF-16 LE/BE, Latin-1, ASCII, Windows-1252 |
98 | 117 |
|
99 | | -### Edge Cases |
| 118 | +### Edge Cases |
| 119 | + |
100 | 120 | - Empty files, binary files, small files, BOM markers |
101 | 121 |
|
102 | 122 | ## Code Quality |
103 | 123 |
|
104 | 124 | ### Requirements |
| 125 | + |
105 | 126 | - Simple, clear, concise code |
106 | 127 | - Avoid complexity and premature optimization |
107 | 128 | - Self-documenting functions |
108 | 129 | - Minimal abstraction layers |
109 | 130 | - Direct error handling |
110 | 131 |
|
111 | 132 | ### Implementation Guidance |
112 | | -- Document scope changes and decisions made during each phase |
113 | | -- Note what was skipped and why (e.g., complexity vs benefit trade-offs) |
| 133 | + |
| 134 | +- Document scope changes and decisions made during each phase as Implementation Notes below the phase |
114 | 135 | - Keep implementation notes concise but informative for future reference |
| 136 | +- Note what was skipped and why (e.g., complexity vs benefit trade-offs) |
| 137 | +- ALWAYS use a todo when implementing a phase, the last step of the todo being to mark checklist items as complete when done. |
115 | 138 |
|
116 | 139 | ### API After Implementation |
| 140 | + |
117 | 141 | ```python |
118 | 142 | # Simplified APIs |
119 | 143 | get_overview("/path/to/file.py") |
120 | 144 | search_content("/path/to/file.py", "pattern") |
121 | 145 | read_content("/path/to/file.py", target) |
122 | 146 | edit_content("/path/to/file.py", search_text, replace_text) |
123 | | -``` |
| 147 | +``` |
0 commit comments