Skip to content

Commit 3a9ef2a

Browse files
committed
index support
1 parent 2fe6e0a commit 3a9ef2a

File tree

8 files changed

+1050
-59
lines changed

8 files changed

+1050
-59
lines changed

CHANGELOG.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,70 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
1717

1818
## [Unreleased]
1919

20+
## [0.5.0]
21+
22+
### Added
23+
24+
- **Auto-Indentation Module**: Comprehensive automatic indentation for improved coding experience
25+
- **New Module Structure**:
26+
- Created `src/loki_indent.c` (~280 lines) and `src/loki_indent.h` (public API)
27+
- Opaque `indent_config` structure for clean encapsulation
28+
- Test-only accessor functions for internal state verification
29+
- **Core Features**:
30+
- **Indent Preservation**: Automatically copies indentation from previous line when pressing Enter
31+
- **Smart Auto-Indent**: Adds extra indent level after opening braces `{`, brackets `[`, and parentheses `(`
32+
- **Electric Dedent**: Automatically dedents when typing closing `}`, `]`, `)` on whitespace-only lines
33+
- **Bracket Matching**: Finds matching opening bracket to determine correct dedent target
34+
- **Nested Brace Support**: Correctly handles deeply nested code structures
35+
- **Indentation Detection**:
36+
- **Level Detection**: Counts leading whitespace in spaces (tabs converted based on width)
37+
- **Style Detection**: Heuristic-based detection of tabs vs spaces (samples up to 100 lines)
38+
- **Mixed Indentation**: Handles files with both tabs and spaces
39+
- **Configuration Options**:
40+
- **Enable/Disable**: Toggle auto-indent on or off (default: enabled)
41+
- **Indent Width**: Configurable width from 1-8 spaces (default: 4)
42+
- **Tab/Space Style**: Auto-detected or manually configured
43+
- **Electric Dedent Toggle**: Can disable electric behavior independently
44+
- **Implementation Details**:
45+
- **Integration Points**:
46+
- `editor_insert_newline()` calls `indent_apply()` after creating new line
47+
- `editor_insert_char()` calls `indent_electric_char()` after inserting character
48+
- `editor_ctx_init()` calls `indent_init()` to set up defaults
49+
- `editor_ctx_free()` cleans up indent_config
50+
- **Smart Trailing Whitespace**: Ignores trailing spaces when detecting line-ending braces
51+
- **Respects Disabled State**: Both general and electric-specific disable flags honored
52+
- **Efficient Deletion**: Uses `editor_del_char()` for proper backspace behavior
53+
- **API Functions** (8 total):
54+
- `indent_init(ctx)` - Initialize with sensible defaults
55+
- `indent_get_level(ctx, row)` - Get indentation level in spaces
56+
- `indent_detect_style(ctx)` - Detect tabs vs spaces heuristically
57+
- `indent_apply(ctx)` - Apply indentation to current line
58+
- `indent_electric_char(ctx, c)` - Handle electric dedent for closing chars
59+
- `indent_set_enabled(ctx, enabled)` - Enable/disable auto-indent
60+
- `indent_set_width(ctx, width)` - Set indentation width
61+
- Plus 3 test-only accessors for verification
62+
- **Comprehensive Testing**:
63+
- **25 unit tests** covering all features and edge cases:
64+
- 5 tests for `indent_get_level()` (spaces, tabs, mixed, stops at content, empty)
65+
- 3 tests for `indent_detect_style()` (tabs, spaces, empty file)
66+
- 2 tests for configuration (width, enabled state)
67+
- 7 tests for `indent_apply()` (basic, after brace/bracket/paren, trailing space, first line, disabled)
68+
- 8 tests for `indent_electric_char()` (dedent brace/bracket/paren, content before cursor, disabled, non-closing, nested, mismatched)
69+
- **All tests pass** (25/25, 100% pass rate)
70+
- **Zero regressions**: Full test suite passes (12/12 test suites)
71+
- **Files Modified**:
72+
- Added: `src/loki_indent.c`, `src/loki_indent.h`, `tests/test_indent.c`
73+
- Modified: `src/loki_core.c` (added integration calls), `src/loki_internal.h` (added indent_config field and editor_insert_row declaration), `CMakeLists.txt` (added module to build)
74+
- **Build Quality**:
75+
- Zero compiler warnings
76+
- Clean compilation with `-Wall -Wextra -pedantic`
77+
- Proper opaque structure usage for encapsulation
78+
- **User Experience**:
79+
- Seamless integration with existing editor workflow
80+
- Professional coding experience similar to Vim/VS Code
81+
- Works across all supported languages (C, Python, Lua, JavaScript, etc.)
82+
- No configuration required - sensible defaults work immediately
83+
2084
## [0.4.8]
2185

2286
### Changed

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ add_library(libloki ${LOKI_LIBRARY_TYPE}
9393
src/loki_lua.c
9494
src/loki_editor.c
9595
src/loki_syntax.c
96+
src/loki_indent.c
9697
src/loki_languages.c
9798
src/loki_selection.c
9899
src/loki_search.c
@@ -268,3 +269,14 @@ target_include_directories(test_buffers PRIVATE
268269
)
269270
target_link_libraries(test_buffers PRIVATE libloki test_framework)
270271
add_test(NAME test_buffers COMMAND test_buffers)
272+
273+
# Unit tests for auto-indentation
274+
add_executable(test_indent tests/test_indent.c)
275+
target_include_directories(test_indent PRIVATE
276+
${CMAKE_CURRENT_SOURCE_DIR}/include
277+
${CMAKE_CURRENT_SOURCE_DIR}/src
278+
${CMAKE_CURRENT_SOURCE_DIR}/tests
279+
${LUA_INCLUDE_DIR}
280+
)
281+
target_link_libraries(test_indent PRIVATE libloki test_framework)
282+
add_test(NAME test_indent COMMAND test_indent)

ROADMAP.md

Lines changed: 96 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
This roadmap outlines future development for Loki, organized around the **modular architecture principle**: keep core components focused on essential infrastructure while adding capabilities through feature modules.
44

5-
## Current Status (v0.4.8)
5+
## Current Status (v0.5.0)
66

77
**Project Metrics:**
88

99
- **Core:** **891 lines** (loki_core.c) - **33% below 1,000 line milestone!** 🎉
10-
- **Total codebase:** ~10,000 lines across modular components
10+
- **Total codebase:** ~10,500 lines across modular components
1111
- **Binary size:** ~300KB (editor), ~316KB (REPL)
12-
- **Module count:** 12 separate modules (syntax, buffers, undo, modal, search, markdown, etc.)
12+
- **Module count:** 13 separate modules (syntax, indent, buffers, undo, modal, search, markdown, etc.)
13+
- **Test coverage:** 12 test suites, 100% pass rate
1314

14-
**Recently Completed (v0.4.8):**
15+
**Recently Completed (v0.5.0):**
1516

17+
-**Auto-indentation module** - Smart indentation with electric dedent (`loki_indent.c`, 280 lines, 25 tests)
1618
-**Syntax highlighting extraction** - Moved to dedicated `loki_syntax.c` module (291 lines)
1719
- ✅ Undo/Redo system with circular buffer
1820
- ✅ Multiple buffers (tabs) with Ctrl-T/Ctrl-W navigation
@@ -23,9 +25,10 @@ This roadmap outlines future development for Loki, organized around the **modula
2325
- ✅ Dynamic language registration system
2426
- ✅ Advanced Lua scripting with modular configuration
2527

26-
**Architectural Milestone:**
28+
**Architectural Milestones:**
2729

28-
The syntax highlighting extraction brings `loki_core.c` below 900 lines (891 lines), achieving a **22.6% reduction** from the previous 1,152 lines. This is the **first time the core has been below 1,000 lines**, demonstrating the success of the modular architecture strategy.
30+
1. **Core Below 1,000 Lines**: The syntax highlighting extraction brought `loki_core.c` to 891 lines (22.6% reduction from 1,152 lines)
31+
2. **13 Modules**: Auto-indent module marks the 13th feature module, demonstrating sustained modular architecture
2932

3033
## Philosophy
3134

@@ -41,16 +44,17 @@ The syntax highlighting extraction brings `loki_core.c` below 900 lines (891 lin
4144

4245
1. **`loki_core.c`** (891 lines) - Terminal I/O, buffer management, file I/O, rendering
4346
2. **`loki_syntax.c`** (291 lines) - Syntax highlighting and color formatting
44-
3. **`loki_languages.c`** (494 lines) - Language definitions and markdown highlighting
45-
4. **`loki_buffers.c`** (426 lines) - Multiple buffer (tab) management
46-
5. **`loki_undo.c`** (474 lines) - Undo/redo with circular buffer
47-
6. **`loki_modal.c`** (510 lines) - Vim-like modal editing
48-
7. **`loki_selection.c`** (156 lines) - Text selection and OSC 52 clipboard
49-
8. **`loki_search.c`** (128 lines) - Incremental search
50-
9. **`loki_command.c`** (491 lines) - Ex-mode command system
51-
10. **`loki_terminal.c`** (125 lines) - Terminal control and window size
52-
11. **`loki_markdown.c`** (421 lines) - CommonMark parsing and rendering
53-
12. **`loki_lua.c`** (1,400+ lines) - Lua integration and API bindings
47+
3. **`loki_indent.c`** (280 lines) - Auto-indentation and electric dedent
48+
4. **`loki_languages.c`** (494 lines) - Language definitions and markdown highlighting
49+
5. **`loki_buffers.c`** (426 lines) - Multiple buffer (tab) management
50+
6. **`loki_undo.c`** (474 lines) - Undo/redo with circular buffer
51+
7. **`loki_modal.c`** (510 lines) - Vim-like modal editing
52+
8. **`loki_selection.c`** (156 lines) - Text selection and OSC 52 clipboard
53+
9. **`loki_search.c`** (128 lines) - Incremental search
54+
10. **`loki_command.c`** (491 lines) - Ex-mode command system
55+
11. **`loki_terminal.c`** (125 lines) - Terminal control and window size
56+
12. **`loki_markdown.c`** (421 lines) - CommonMark parsing and rendering
57+
13. **`loki_lua.c`** (1,400+ lines) - Lua integration and API bindings
5458

5559
**Module Design Guidelines:**
5660

@@ -61,40 +65,45 @@ The syntax highlighting extraction brings `loki_core.c` below 900 lines (891 lin
6165

6266
---
6367

64-
## Near-Term Improvements (v0.5.x)
68+
## Near-Term Improvements (v0.5.x - v0.6.x)
6569

66-
### Priority Features
70+
### Completed in v0.5.0
6771

68-
#### 1. Auto-Indent Module (`loki_indent.c`) **HIGHEST PRIORITY**
72+
#### Auto-Indent Module (`loki_indent.c`) - **COMPLETED**
6973

70-
**Impact:** Essential developer quality-of-life
71-
**Complexity:** Low-Medium (~150-200 lines)
72-
**Status:** Not started
74+
**Implementation achieved:**
7375

74-
**Implementation:**
76+
- ✅ Copy indentation from previous line on Enter
77+
- ✅ Electric dedent for closing braces `}`, `]`, `)`
78+
- ✅ Tab/space detection (smart tabs vs spaces heuristic)
79+
- ✅ Bracket matching for correct dedent target
80+
- ✅ Nested brace support
81+
- ✅ Configurable width (1-8 spaces, default 4)
82+
- ✅ Enable/disable toggles
7583

76-
- Copy indentation from previous line on Enter
77-
- Electric dedent for closing braces `}`, `]`, `)`
78-
- Tab/space detection (smart tabs vs spaces)
79-
- Language-specific rules (optional, via hooks)
80-
- Preserve indent on blank lines
84+
**Actual implementation:** 280 lines (exceeded initial estimate of 150-200)
8185

82-
**API:**
86+
**Testing:** 25 unit tests, 100% pass rate
87+
88+
**API delivered:**
8389

8490
```c
91+
void indent_init(editor_ctx_t *ctx);
8592
int indent_get_level(editor_ctx_t *ctx, int row);
86-
void indent_apply(editor_ctx_t *ctx, int row);
87-
void indent_electric_char(editor_ctx_t *ctx, char c);
88-
int indent_detect_style(editor_ctx_t *ctx); // tabs vs spaces
93+
int indent_detect_style(editor_ctx_t *ctx);
94+
void indent_apply(editor_ctx_t *ctx);
95+
int indent_electric_char(editor_ctx_t *ctx, int c);
96+
void indent_set_enabled(editor_ctx_t *ctx, int enabled);
97+
void indent_set_width(editor_ctx_t *ctx, int width);
8998
```
9099
91-
**Integration:** Hook into `editor_insert_newline()` and `editor_insert_char()`
92-
93-
**Why highest priority:** Most requested feature by developers, relatively simple to implement
100+
**Integration:** Hooked into `editor_insert_newline()` and `editor_insert_char()` as planned
94101
95102
---
96103
97-
#### 2. Enhanced Clipboard Integration (`loki_clipboard.c`) ⭐ **HIGH PRIORITY**
104+
### Priority Features for v0.6.x
105+
106+
#### 1. Enhanced Clipboard Integration (`loki_clipboard.c`) ⭐ **HIGHEST PRIORITY**
98107
99108
**Status:** Partial (OSC 52 in selection module, needs expansion)
100109
@@ -120,7 +129,7 @@ int clipboard_system_available(void); // Check if OSC 52 works
120129

121130
---
122131

123-
#### 3. Configuration File System ⭐ **HIGH PRIORITY**
132+
#### 2. Configuration File System ⭐ **HIGH PRIORITY**
124133

125134
**Impact:** User customization without recompilation
126135
**Complexity:** Medium (~200-250 lines)
@@ -689,17 +698,20 @@ Things we explicitly **won't** add (preserves minimalist identity):
689698
690699
## Implementation Strategy
691700
692-
### Prioritization Framework (v0.5.x)
701+
### Prioritization Framework
693702
694-
**Immediate Priorities (v0.5.0 - Next Release):**
703+
**Completed in v0.5.0:**
695704
696-
1. ✅ Auto-indent module - Most requested feature
697-
2. ✅ Configuration file system (TOML) - Easier onboarding
698-
3. ✅ Enhanced clipboard - Better workflow
705+
1. ✅ Auto-indent module - Most requested feature (**COMPLETED** - 280 lines, 25 tests)
699706
700-
**Secondary Priorities (v0.5.x series):**
707+
**Immediate Priorities (v0.6.0 - Next Release):**
701708
709+
2. Enhanced clipboard - Better workflow
710+
3. Configuration file system (TOML) - Easier onboarding
702711
4. Line numbers module - Low complexity, high value
712+
713+
**Secondary Priorities (v0.6.x series):**
714+
703715
5. Search enhancements (regex, replace) - Improve existing feature
704716
6. Bracket matching visualization - Quick win
705717
@@ -737,17 +749,20 @@ Things we explicitly **won't** add (preserves minimalist identity):
737749
738750
**Release cycle:**
739751
740-
- **v0.5.x** - Focus: Auto-indent, config system, enhanced clipboard (~2-3 months)
741-
- **v0.6.x** - Focus: Split windows, line numbers, search improvements (~3-4 months)
742-
- **v0.7.x** - Focus: Git integration, macros, plugin formalization (~4-6 months)
752+
- **v0.5.0** - ✅ **COMPLETED** - Auto-indent module with 25 comprehensive tests
753+
- **v0.6.x** - Focus: Enhanced clipboard, config system, line numbers (~2-3 months)
754+
- **v0.7.x** - Focus: Split windows, search improvements, bracket matching (~3-4 months)
755+
- **v0.8.x** - Focus: Git integration, macros, plugin formalization (~4-6 months)
743756
- **v1.0.0** - Stability milestone: Feature-complete for core use cases
744757
745758
**v1.0 criteria (proposed):**
746759
747-
- Auto-indent working ✓
748-
- Config system (TOML) ✓
749-
- Split windows ✓
750-
- Git awareness (phase 1) ✓
760+
- Auto-indent working ✅ **COMPLETED in v0.5.0**
761+
- Enhanced clipboard (copy/paste)
762+
- Config system (TOML)
763+
- Split windows
764+
- Line numbers
765+
- Git awareness (phase 1)
751766
- Comprehensive test coverage (>80%)
752767
- Documentation complete
753768
- No known critical bugs
@@ -829,11 +844,12 @@ Things we explicitly **won't** add (preserves minimalist identity):
829844
- ✅ Lua scripting with extensive API
830845
- ✅ Async HTTP for AI integration
831846
- ✅ Tab completion in REPL
847+
- ✅ **Auto-indent** (electric dedent, bracket matching) - **v0.5.0**
832848
833849
**What needs work:**
834850
835-
- 🔨 Auto-indent (most requested)
836851
- 🔨 Configuration file system (ease of use)
852+
- 🔨 Enhanced clipboard (better copy/paste)
837853
- 🔨 Line numbers (common request)
838854
- 🔨 Search & replace (enhance existing)
839855
- 🔨 Split windows (complex but valuable)
@@ -847,20 +863,39 @@ Things we explicitly **won't** add (preserves minimalist identity):
847863
848864
---
849865
850-
## Next Steps (v0.5.0)
866+
## v0.5.0 Release - ✅ COMPLETED
867+
868+
**Implemented:**
869+
870+
1. ✅ **Auto-indent module** - Comprehensive implementation with electric dedent, bracket matching, and 25 tests
871+
- **Impact:** Highest user value achieved
872+
- **Quality:** 100% test pass rate, zero compiler warnings
873+
- **Integration:** Seamless integration with existing editor workflow
874+
875+
**Success criteria met:**
876+
877+
- ✅ Auto-indent works for all common languages (C, Python, Lua, JavaScript, etc.)
878+
- ✅ Smart bracket matching and electric dedent
879+
- ✅ Configurable width and enable/disable toggles
880+
- ✅ Tab/space detection heuristic
881+
- ✅ Comprehensive test coverage
882+
883+
---
884+
885+
## Next Steps (v0.6.0)
851886
852887
**Immediate focus (prioritized by impact/effort ratio):**
853888
854-
1. **Auto-indent module** (~2 weeks) - Highest user value
855-
2. **TOML config system** (~1-2 weeks) - Better onboarding
856-
3. **Line numbers module** (~1 week) - Quick win
857-
4. **Enhanced search** (~2 weeks) - Improve existing feature
889+
1. **Enhanced clipboard** (~1-2 weeks) - Better copy/paste workflow
890+
2. **TOML config system** (~1-2 weeks) - Easier onboarding
891+
3. **Line numbers module** (~1 week) - Quick win, common request
892+
4. **Enhanced search** (~2 weeks) - Regex and replace support
858893
859-
**Total estimated time for v0.5.0:** 6-7 weeks
894+
**Total estimated time for v0.6.0:** 5-7 weeks
860895
861-
**Success criteria for v0.5.0:**
896+
**Success criteria for v0.6.0:**
862897
863-
- Auto-indent works for all common languages
898+
- Enhanced clipboard with multiple registers
864899
- Config file replaces most Lua config needs
865900
- Line numbers toggle-able and performant
866901
- Search supports regex and replace
@@ -869,4 +904,6 @@ Things we explicitly **won't** add (preserves minimalist identity):
869904
870905
The modular architecture has proven successful. By maintaining discipline around core separation and module boundaries, Loki can evolve into a powerful editor while preserving its minimalist, hackable nature.
871906
907+
The v0.5.0 release demonstrates that complex features can be added without compromising core simplicity - the auto-indent module added 280 lines of well-tested code in a completely separate module.
908+
872909
Contributions welcome! See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines (if it exists), or open an issue to discuss new features.

0 commit comments

Comments
 (0)