Indent on open scope

This commit is contained in:
Krzosa Karol
2026-01-05 19:40:05 +01:00
parent 753c9199b6
commit ecbc800fdd
5 changed files with 19 additions and 56 deletions

View File

@@ -9,7 +9,6 @@
- Window position: vscode opens in fullscreen and then remembers what position it was close in (could be a config option)
- On Linux: Try to implement is_debugger_present()
- Probably shouldn't emit (Key pressed when ctrl etc. is not clicked!!)
- Brace, bracket, paren indenting?
- OnUpdate view hooks!
- OnSave buffer hooks which will execute the config on save

View File

@@ -519,47 +519,20 @@ API Int SkipSpaces(Buffer *buffer, Int seek) {
return seek;
}
API Int FindScopeEnd(Buffer *buffer, Int seek, Int max_seek, char16_t open, char16_t close) {
char16_t right = GetChar(buffer, seek);
if (right == open) {
int scope = 1;
Int i = seek + 1;
for (; i < seek + max_seek && i < buffer->len; i += 1) {
char16_t c = GetChar(buffer, i);
if (open == close && c == '\\') {
i += 1;
} else if (open == close && c == open) {
scope -= 1;
} else if (c == open) {
scope += 1;
} else if (c == close) {
scope -= 1;
API Int FindAnyScopeBegin(Buffer *buffer, Int pos) {
int inner_scope = 0;
for (Int i = pos - 1; i >= 0; i -= 1) {
if (buffer->str[i] == '{' || buffer->str[i] == '[' || buffer->str[i] == '(') {
if (inner_scope == 0) {
return i;
}
if (c == u'\n' || scope == 0) break;
inner_scope -= 1;
}
if (scope == 0) seek = i;
}
return seek;
}
API Range EncloseScope(Buffer *buffer, Int pos_min, Int pos_max, char16_t open, char16_t close) {
Range result = {pos_min, pos_max};
for (Int i = pos_min - 1; i >= 0; i -= 1) {
if (buffer->str[i] == open) {
result.min = i;
break;
if (buffer->str[i] == '}' || buffer->str[i] == ']' || buffer->str[i] == ')') {
inner_scope += 1;
}
}
for (Int i = pos_max; i < buffer->len; i += 1) {
if (buffer->str[i] == close) {
result.max = i + 1;
break;
}
}
return result;
return 0;
}
API Range EncloseLine(Buffer *buffer, Int pos) {

View File

@@ -120,7 +120,6 @@ API Int GetPrevEmptyLineStart(Buffer *buffer, Int pos);
API Range EncloseWord(Buffer *buffer, Int pos);
API Int SkipSpaces(Buffer *buffer, Int seek);
API Int FindScopeEnd(Buffer *buffer, Int seek, Int max_seek, char16_t open, char16_t close);
API Range EncloseScope(Buffer *buffer, Int pos_min, Int pos_max, char16_t open, char16_t close);
API Range EncloseLine(Buffer *buffer, Int pos);
API Range EncloseFullLine(Buffer *buffer, Int pos);
API Int OffsetByLine(Buffer *buffer, Int pos, Int line_offset);

View File

@@ -134,15 +134,16 @@ String16 FetchFuzzyViewLoadLine(View *view) {
return string;
}
void IdentedNewLine(View *view) {
void IndentedNewLine(View *view) {
Buffer *buffer = GetBuffer(view->active_buffer);
Scratch scratch;
Array<Edit> edits = BeginEdit(scratch, buffer, view->carets);
MergeCarets(buffer, &view->carets);
For(view->carets) {
Int front = GetFront(it);
Int line = PosToLine(buffer, front);
Int indent = GetLineIndent(buffer, line);
Int scope_begin = FindAnyScopeBegin(buffer, front); // @todo: this could be problematic in large source files!
Int line = PosToLine(buffer, scope_begin);
Int indent = GetLineIndent(buffer, line) + IndentSize;
String string = Format(scratch, "\n%.*s", (int)indent, " ");
String16 string16 = ToString16(scratch, string);
AddEdit(&edits, it.range, string16);
@@ -662,15 +663,6 @@ void EncloseLine(View *view) {
}
}
void EncloseScope(View *view) {
Buffer *buffer = GetBuffer(view->active_buffer);
For (view->carets) {
it.range = EncloseScope(buffer, it.range.min - 1, it.range.max + 1, u'(', u')');
it.range.min = Clamp(buffer, it.range.min + 1);
it.range.max = Clamp(buffer, it.range.max - 1);
}
}
void SelectAllOccurences(View *view, String16 needle) {
Buffer *buffer = GetBuffer(view->active_buffer);
Scratch scratch;

View File

@@ -43,7 +43,7 @@ void CMD_ShowDebugBufferList() {
}
command_bar.view->update_scroll = true;
SelectRange(command_bar.view, GetBufferBeginAsRange(command_bar.buffer));
} RegisterCommand(CMD_ShowDebugBufferList, "", "Show full list of buffers, including the special ones that normally just clutter list");
} RegisterCommand(CMD_ShowDebugBufferList, "ctrl-shift-alt-p", "Show full list of buffers, including the special ones that normally just clutter list");
void CMD_ShowBufferList() {
if (ActiveWindowID == CommandWindowID && LastExecutedManualCommand == CMD_ShowBufferList) {