Indent lines make selection nicer

This commit is contained in:
Krzosa Karol
2026-01-24 14:12:06 +01:00
parent f5825f050c
commit 7b47f2f6f5
3 changed files with 40 additions and 4 deletions

View File

@@ -35,7 +35,6 @@ New UI Session
FEATURE Some decl/function indexing in fuzzy format FEATURE Some decl/function indexing in fuzzy format
FEATURE dump text editor state to file, restore state FEATURE dump text editor state to file, restore state
- Search and replace
- word complete - word complete
- escapeing multiple cursor after ctrl + d should put the cursor where it was (probably will need to swap secondary and primary cursor for new cursor - escapeing multiple cursor after ctrl + d should put the cursor where it was (probably will need to swap secondary and primary cursor for new cursor
- draw indentation levels like in sublime (those lines) - we render chars one by one so seems relatively easy to figure out if whitespace belongs to beginning of line (make sure to add max value like 40 because of big files) - draw indentation levels like in sublime (those lines) - we render chars one by one so seems relatively easy to figure out if whitespace belongs to beginning of line (make sure to add max value like 40 because of big files)
@@ -54,3 +53,21 @@ FEATURE dump text editor state to file, restore state
- gap buffer - gap buffer
- optimize rendering - command buffer, and vertice buffer instead of vertice buffer with scissor - optimize rendering - command buffer, and vertice buffer instead of vertice buffer with scissor
WORD COMPLETE
1. Tokenize the file (or maybe couple last viewed files)
2. Match the tokens (identifiers) with prefix string and produce a list
3. Remove duplicates
4. Sort by proximity to cursor
We save the iterator, it's enough for it to be global with unique for it arena
If prefix ask is the same - reuse the iterator go to next word, if there are none go to next buffer ...
else - dump old, create new

View File

@@ -10,6 +10,9 @@ union XY {
struct {Int col; Int line;}; struct {Int col; Int line;};
struct {Int x; Int y; }; struct {Int x; Int y; };
}; };
struct XYPair { XY front; XY back; };
typedef void Function(); typedef void Function();
struct FunctionData { struct FunctionData {

View File

@@ -172,7 +172,7 @@ void IndentedNewLine(View *view) {
For(view->carets) { For(view->carets) {
Int front = GetFront(it); Int front = GetFront(it);
Int indent = GetLineIndent(buffer, PosToLine(buffer, front)); Int indent = GetLineIndent(buffer, PosToLine(buffer, front));
String string = Format(scratch, "\n%.*s", IndentSize, " "); String string = Format(scratch, "\n%.*s", indent, " ");
String16 string16 = ToString16(scratch, string); String16 string16 = ToString16(scratch, string);
AddEdit(&edits, it.range, string16); AddEdit(&edits, it.range, string16);
} }
@@ -400,7 +400,7 @@ void MoveCaretsLine(View *view, int direction) {
// Save caret positions to fix them at end to the expected incremented by one positions // Save caret positions to fix them at end to the expected incremented by one positions
struct XYPair { XY front; XY back; }; // :PreserveXYCarets (kind of)
Array<XYPair> saved_xy = {scratch}; Array<XYPair> saved_xy = {scratch};
For (view->carets) { For (view->carets) {
Add(&saved_xy, {PosToXY(buffer, GetFront(it)), PosToXY(buffer, GetBack(it))}); Add(&saved_xy, {PosToXY(buffer, GetFront(it)), PosToXY(buffer, GetBack(it))});
@@ -562,17 +562,25 @@ void IndentSelectedLines(View *view, bool shift = false) {
Array<Edit> edits = BeginEdit(scratch, buffer, view->carets); Array<Edit> edits = BeginEdit(scratch, buffer, view->carets);
MergeCarets(buffer, &view->carets); MergeCarets(buffer, &view->carets);
// :PreserveXYCarets - maybe make it as one of the strategies of adjusting carets?
Array<XYPair> saved_xy = {scratch};
For (view->carets) Add(&saved_xy, {PosToXY(buffer, GetFront(it)), PosToXY(buffer, GetBack(it))});
Array<Range> line_ranges_to_indent = GetSelectedLinesSortedExclusive(scratch, view); Array<Range> line_ranges_to_indent = GetSelectedLinesSortedExclusive(scratch, view);
For (line_ranges_to_indent) { For (line_ranges_to_indent) {
for (Int i = it.min; i < it.max; i += 1) { for (Int i = it.min; i < it.max; i += 1) {
Range pos_range_of_line = GetLineRange(buffer, i); Range pos_range_of_line = GetLineRange(buffer, i);
String16 string = GetString(buffer, pos_range_of_line);
String16 without_whitespace = Trim(string);
if (without_whitespace.len == 0) {
continue;
}
String16 indent_string = u" "; String16 indent_string = u" ";
indent_string.len = IndentSize; indent_string.len = IndentSize;
if (!shift) { if (!shift) {
AddEdit(&edits, {pos_range_of_line.min, pos_range_of_line.min}, indent_string); AddEdit(&edits, {pos_range_of_line.min, pos_range_of_line.min}, indent_string);
} else { } else {
String16 string = GetString(buffer, pos_range_of_line);
Int whitespace_len = 0; Int whitespace_len = 0;
for (Int i = 0; i < IndentSize && i < string.len && string.data[i] == u' '; i += 1) { for (Int i = 0; i < IndentSize && i < string.len && string.data[i] == u' '; i += 1) {
whitespace_len += 1; whitespace_len += 1;
@@ -583,6 +591,14 @@ void IndentSelectedLines(View *view, bool shift = false) {
} }
} }
EndEdit(buffer, &edits, &view->carets, !EndEdit_KillSelection); EndEdit(buffer, &edits, &view->carets, !EndEdit_KillSelection);
for (Int i = 0; i < saved_xy.len; i += 1) {
Caret &caret = view->carets[i];
XYPair &xypair = saved_xy[i];
Int front = XYToPos(buffer, xypair.front);
Int back = XYToPos(buffer, xypair.back);
caret = MakeCaret(front, back);
}
view->update_scroll = false; view->update_scroll = false;
} }