Fix indenting

This commit is contained in:
Krzosa Karol
2026-01-06 14:15:20 +01:00
parent 73a19e2260
commit a96cead179

View File

@@ -150,26 +150,24 @@ String16 GetIndentString(Allocator allocator, Int indent_size) {
char16_t *result = AllocArray(allocator, char16_t, indent_size + 1); char16_t *result = AllocArray(allocator, char16_t, indent_size + 1);
char16_t c = GetIndentChar(); char16_t c = GetIndentChar();
for (int i = 0; i < IndentSize; i += 1) { for (int i = 0; i < indent_size; i += 1) {
result[i] = c; result[i] = c;
} }
result[IndentSize] = 0; result[indent_size] = 0;
String16 res = {result, indent_size}; String16 res = {result, indent_size};
return res; return res;
} }
Int FindScopeIndent(Buffer *buffer, Int pos) { String GetIndentString8(Allocator allocator, Int indent_size) {
for (Int i = pos - 1; i >= 0; i -= 1) { char *result = AllocArray(allocator, char, indent_size + 1);
if (buffer->str[i] == '{' || buffer->str[i] == '[' || buffer->str[i] == '(') { char c = (char)GetIndentChar();
Int line = PosToLine(buffer, pos);
return GetLineIndent(buffer, line) + IndentSize; for (int i = 0; i < indent_size; i += 1) {
result[i] = c;
} }
if (buffer->str[i] == '}' || buffer->str[i] == ']' || buffer->str[i] == ')') { result[indent_size] = 0;
Int line = PosToLine(buffer, pos); String res = {result, indent_size};
return GetLineIndent(buffer, line); return res;
}
}
return 0;
} }
void IndentedNewLine(View *view) { void IndentedNewLine(View *view) {
@@ -180,7 +178,8 @@ 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", GetIndentString(scratch, indent)); String indent_string = GetIndentString8(scratch, indent);
String string = Format(scratch, "\n%S", indent_string);
String16 string16 = ToString16(scratch, string); String16 string16 = ToString16(scratch, string);
AddEdit(&edits, it.range, string16); AddEdit(&edits, it.range, string16);
} }
@@ -604,7 +603,9 @@ Array<Edit> ReplaceEx(Allocator scratch, View *view, String16 string) {
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
Array<Edit> edits = BeginEdit(scratch, buffer, view->carets); Array<Edit> edits = BeginEdit(scratch, buffer, view->carets);
MergeCarets(buffer, &view->carets); MergeCarets(buffer, &view->carets);
For(view->carets) AddEdit(&edits, it.range, string); For(view->carets) {
AddEdit(&edits, it.range, string);
}
EndEdit(buffer, &edits, &view->carets, KILL_SELECTION); EndEdit(buffer, &edits, &view->carets, KILL_SELECTION);
return edits; return edits;
} }