Indented new line

This commit is contained in:
Krzosa Karol
2024-08-04 08:28:42 +02:00
parent e761761108
commit 2b5f441e92
6 changed files with 75 additions and 18 deletions

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}
}
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;
}

View File

@@ -497,6 +497,24 @@ Array<Range> 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<Edit> 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);
}
}

View File

@@ -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)

View File

@@ -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!!