Compare commits

..

2 Commits

Author SHA1 Message Date
Krzosa Karol
7b47f2f6f5 Indent lines make selection nicer 2026-01-24 14:12:06 +01:00
Krzosa Karol
f5825f050c Fix EndEdit when edits->len == 0 2026-01-24 14:11:40 +01:00
4 changed files with 44 additions and 4 deletions

View File

@@ -35,7 +35,6 @@ New UI Session
FEATURE Some decl/function indexing in fuzzy format
FEATURE dump text editor state to file, restore state
- Search and replace
- 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
- 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
- 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

@@ -1064,6 +1064,10 @@ API void EndEdit(Buffer *buffer, Array<Edit> *edits, Array<Caret> *carets, bool
Assert(buffer->edit_phase == 2);
buffer->edit_phase -= 2;
if (edits->len == 0) {
return;
}
#if BUFFER_DEBUG
if (buffer->no_history == false) {
HistoryEntry *entry = GetLast(buffer->undo_stack);

View File

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

View File

@@ -172,7 +172,7 @@ void IndentedNewLine(View *view) {
For(view->carets) {
Int front = GetFront(it);
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);
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
struct XYPair { XY front; XY back; };
// :PreserveXYCarets (kind of)
Array<XYPair> saved_xy = {scratch};
For (view->carets) {
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);
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);
For (line_ranges_to_indent) {
for (Int i = it.min; i < it.max; i += 1) {
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" ";
indent_string.len = IndentSize;
if (!shift) {
AddEdit(&edits, {pos_range_of_line.min, pos_range_of_line.min}, indent_string);
} else {
String16 string = GetString(buffer, pos_range_of_line);
Int whitespace_len = 0;
for (Int i = 0; i < IndentSize && i < string.len && string.data[i] == u' '; i += 1) {
whitespace_len += 1;
@@ -583,6 +591,14 @@ void IndentSelectedLines(View *view, bool shift = false) {
}
}
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;
}