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