From 2b5f441e92e185f71b81132917d7adca2baad40a Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 4 Aug 2024 08:28:42 +0200 Subject: [PATCH] Indented new line --- src/basic/string16.cpp | 12 +++++++++++ src/text_editor/buffer.cpp | 12 ----------- src/text_editor/buffer_helpers.cpp | 32 ++++++++++++++++++++++++++++- src/text_editor/commands_window.cpp | 28 ++++++++++++++++++++++--- src/text_editor/generated.cpp | 7 ++++++- src/text_editor/todo.txt | 2 +- 6 files changed, 75 insertions(+), 18 deletions(-) diff --git a/src/basic/string16.cpp b/src/basic/string16.cpp index 55a9941..822bd76 100644 --- a/src/basic/string16.cpp +++ b/src/basic/string16.cpp @@ -207,3 +207,15 @@ String16 TrimEnd(String16 string) { String16 result = GetPrefix(string, whitespace_end); return result; } + +bool EndsWith(String16 a, String16 end, unsigned ignore_case = false) { + String16 a_end = GetPostfix(a, end.len); + bool result = AreEqual(end, a_end, ignore_case); + return result; +} + +bool StartsWith(String16 a, String16 start, unsigned ignore_case = false) { + String16 a_start = GetPrefix(a, start.len); + bool result = AreEqual(start, a_start, ignore_case); + return result; +} diff --git a/src/text_editor/buffer.cpp b/src/text_editor/buffer.cpp index 41fdef9..70f53f8 100644 --- a/src/text_editor/buffer.cpp +++ b/src/text_editor/buffer.cpp @@ -120,15 +120,3 @@ void Appendf(Buffer *buffer, const char *fmt, ...) { String16 string16 = ToString16(scratch, string); ReplaceText(buffer, GetEndAsRange(*buffer), string16); } - -String16 GetLineString(Buffer &buffer, Int line, Int *end_of_buffer = NULL) { - Range range = GetLineRange(buffer, line, end_of_buffer); - String16 string = GetString(buffer, range); - return string; -} - -String16 GetLineStringWithoutNL(Buffer &buffer, Int line) { - Range range = GetLineRangeWithoutNL(buffer, line); - String16 string = GetString(buffer, range); - return string; -} diff --git a/src/text_editor/buffer_helpers.cpp b/src/text_editor/buffer_helpers.cpp index 2ca080a..0b995f8 100644 --- a/src/text_editor/buffer_helpers.cpp +++ b/src/text_editor/buffer_helpers.cpp @@ -167,6 +167,18 @@ Range GetLineRangeWithoutNL(Buffer &buffer, Int line) { return line_range; } +String16 GetLineString(Buffer &buffer, Int line, Int *end_of_buffer = NULL) { + Range range = GetLineRange(buffer, line, end_of_buffer); + String16 string = GetString(buffer, range); + return string; +} + +String16 GetLineStringWithoutNL(Buffer &buffer, Int line) { + Range range = GetLineRangeWithoutNL(buffer, line); + String16 string = GetString(buffer, range); + return string; +} + Int PosToLine(Buffer &buffer, Int pos) { Add(&buffer.line_starts, buffer.len + 1); @@ -376,4 +388,22 @@ Int GetNextChar(Buffer *buffer, Int pos) { Int GetPrevChar(Buffer *buffer, Int pos) { Int result = Clamp(pos - 1, (Int)0, buffer->len); return result; -} \ No newline at end of file +} + +Int GetLineIndent(Buffer *buffer, Int line) { + String16 string = GetLineStringWithoutNL(*buffer, line); + Int indent = 0; + for (Int i = 0; i < string.len; i += 1) { + if (IsWhitespace(string.data[i])) { + indent += 1; + } else { + break; + } + } + + // string = TrimEnd(string); + // if (EndsWith(string, L"{")) indent += StyleIndentSize; + // if (EndsWith(string, L"(")) indent += StyleIndentSize; + + return indent; +} diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index 2d635b4..15966c9 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -497,6 +497,24 @@ Array FindAllInBuffer(Allocator allocator, Buffer *buffer, String16 needl return result; } +void Command_IdentedNewLine(View *view) { + Buffer *buffer = GetBuffer(view->active_buffer); + Scratch scratch; + BeforeEdit(buffer, view->carets); + MergeCarets(view); + Array edits = {scratch}; + For(view->carets) { + Int front = GetFront(it); + Int line = PosToLine(*buffer, front); + Int indent = GetLineIndent(buffer, line); + String string = Format(scratch, "\n%.*s", (int)indent, " "); + String16 string16 = ToString16(scratch, string); + AddEdit(&edits, it.range, string16); + } + ApplyEdits(buffer, edits); + AfterEdit(buffer, &edits, &view->carets); +} + void WindowCommand(Event event, Window *window, View *view) { ProfileFunction(); Buffer *buffer = GetBuffer(view->active_buffer); @@ -740,11 +758,15 @@ void WindowCommand(Event event, Window *window, View *view) { } } else if (window->id.id == SearchWindowID.id) { } else { - if (Ctrl(SDLK_RETURN)) { + if (CtrlShift(SDLK_RETURN)) { + Command_MoveCursorsToSide(view, DIR_LEFT); + Command_IdentedNewLine(view); + Command_Move(view, DIR_UP); + } else if (Ctrl(SDLK_RETURN)) { Command_MoveCursorsToSide(view, DIR_RIGHT); - Command_Replace(view, L"\n"); + Command_IdentedNewLine(view); } else if (Press(SDLK_RETURN)) { - Command_Replace(view, L"\n"); + Command_IdentedNewLine(view); } } diff --git a/src/text_editor/generated.cpp b/src/text_editor/generated.cpp index b21246e..44a2c24 100644 --- a/src/text_editor/generated.cpp +++ b/src/text_editor/generated.cpp @@ -172,7 +172,12 @@ function GenericTextFileRule(_s) end -- other_random_filename - return GetCurrentBufferDir()..'/'..s + buffer_dir = GetCurrentBufferDir() + if buffer_dir:len() == 0 then + return s + else + return buffer_dir..'/'..s + end end line, col, _s = ExtractLineAndColumn(_s) diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index 698a4ee..933369e 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -34,7 +34,7 @@ - select space between parens,braces - color parens, braces -- maybe my mouse selection is wrong? seems like on double click lite and sublime start to enclosing words!! +- double click - start enclosing word, triple click - start enclosing lines, merge 2 cursor selection - start treating it as anchor - Windows - layout using a tree!!