From 0c488bc31350ce24f555ac63febb6d8b907b62b3 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 25 Jan 2026 17:53:29 +0100 Subject: [PATCH] Fix indenting, dedenting --- src/text_editor/plugin_word_complete.cpp | 3 +++ src/text_editor/text_editor.cpp | 2 +- src/text_editor/view.cpp | 15 ++++++++------- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/text_editor/plugin_word_complete.cpp b/src/text_editor/plugin_word_complete.cpp index 8fe13b2..7dc9766 100644 --- a/src/text_editor/plugin_word_complete.cpp +++ b/src/text_editor/plugin_word_complete.cpp @@ -62,6 +62,9 @@ void CWSLexIdentifiers(Array *out_idents, Buffer *buffer) { } if (StartsWith(token, CWS.prefix_string) && token != CWS.prefix_string) { Int pos = token.data - buffer->str; + // Here we are computing distance based on position from currently open + // buffer but should be fine. We are sorting then putting into the array + // and it doesn't displace the original ones so it's fine Int distance = Absolute(CWS.original_caret_pos - pos); int64_t value_index = FindValueIndex(&idents, token); if (value_index != -1) { diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 0353ce4..eb9fdca 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -250,7 +250,7 @@ void OnCommand(Event event) { } } - Int p = ScreenSpaceToBufferPos(selected.window, selected.view, selected.buffer, mouse); + Int p = ScreenSpaceToBufferPos(selected.window, selected.view, selected.buffer, mouse); Caret &caret = selected.view->carets[0]; caret = SetFrontWithAnchor(caret, DocumentAnchor, p); } diff --git a/src/text_editor/view.cpp b/src/text_editor/view.cpp index cca2309..6007118 100644 --- a/src/text_editor/view.cpp +++ b/src/text_editor/view.cpp @@ -618,11 +618,19 @@ void IndentSelectedLines(View *view, bool shift = false) { indent_string.len = IndentSize; if (!shift) { AddEdit(&edits, {pos_range_of_line.min, pos_range_of_line.min}, indent_string); + For (saved_xy) { + if (it.front.y == i) it.front.x += indent_string.len; + if (it.back.y == i) it.back.x += indent_string.len; + } } else { Int whitespace_len = 0; for (Int i = 0; i < IndentSize && i < string.len && string.data[i] == u' '; i += 1) { whitespace_len += 1; } + For (saved_xy) { + if (it.front.y == i) it.front.x -= whitespace_len; + if (it.back.y == i) it.back.x -= whitespace_len; + } AddEdit(&edits, {pos_range_of_line.min, pos_range_of_line.min + whitespace_len}, u""); } @@ -633,13 +641,6 @@ void IndentSelectedLines(View *view, bool shift = false) { for (Int i = 0; i < saved_xy.len; i += 1) { Caret &caret = view->carets[i]; XYPair &xypair = saved_xy[i]; - if (!shift) { - xypair.front.x += IndentSize; - xypair.back.x += IndentSize; - } else { - xypair.front.x -= IndentSize; - xypair.back.x -= IndentSize; - } Int front = XYToPos(buffer, xypair.front); Int back = XYToPos(buffer, xypair.back); caret = MakeCaret(front, back);