Delete indent in multiples of indent size

This commit is contained in:
Krzosa Karol
2024-08-04 10:40:03 +02:00
parent c3bd96d5d6
commit b267000394
3 changed files with 32 additions and 3 deletions

View File

@@ -402,6 +402,11 @@ Int GetPrevChar(Buffer *buffer, Int pos) {
return result;
}
wchar_t GetChar(Buffer *buffer, Int pos) {
if (pos >= 0 && pos < buffer->len) return buffer->str[pos];
return 0;
}
Int GetLineIndent(Buffer *buffer, Int line) {
String16 string = GetLineStringWithoutNL(*buffer, line);
Int indent = 0;
@@ -421,6 +426,13 @@ Int GetIndentAtPos(Buffer *buffer, Int pos) {
return result;
}
Range GetIndentRangeAtPos(Buffer *buffer, Int pos) {
Int line = PosToLine(*buffer, pos);
Int indent = GetLineIndent(buffer, line);
Range range = GetLineRangeWithoutNL(*buffer, line);
return {range.min, range.min + indent};
}
Range EncloseScope(Buffer *buffer, Int pos, wchar_t open, wchar_t close) {
Range result = {pos, pos};
for (Int i = pos; i >= 0; i -= 1) {

View File

@@ -342,7 +342,25 @@ void Command_Delete(View *view, int direction, bool ctrl = false) {
// Select things to delete
For(view->carets) {
if (GetSize(it.range)) continue;
it = MoveCaret(buffer, it, direction, ctrl, SHIFT_PRESSED);
if (direction == DIR_LEFT) {
// Delete indent in multiple of IndentSize
Range indent_range = GetIndentRangeAtPos(buffer, it.range.min);
if (it.range.min > indent_range.min && it.range.max <= indent_range.max) {
Int offset = it.range.min - indent_range.min;
Int to_delete = (offset % (StyleIndentSize));
if (to_delete == 0) to_delete = StyleIndentSize;
to_delete = Clamp(to_delete, (Int)1, StyleIndentSize);
for (Int i = 0; i < to_delete; i += 1) {
it = MoveCaret(buffer, it, direction, ctrl, SHIFT_PRESSED);
}
} else {
it = MoveCaret(buffer, it, direction, ctrl, SHIFT_PRESSED);
}
} else {
it = MoveCaret(buffer, it, direction, ctrl, SHIFT_PRESSED);
}
}
MergeCarets(view);

View File

@@ -9,8 +9,7 @@
- fix the bug where when we create a new view the anchor value on new view is compromised because and incongruent with position so we select from start of buffer
- hand cursor on hover over underlined word
- ctrl + q to open, alt + q to go back
- delete multiple spaces (based on indent size) on delete instead one by one
- delete multiple spaces (based on indent size) on delete instead one by one. It should only work at beginning of line, in indent area
- switch to previous view (ctrl + tab)
- save location on open and allow for revert (buffer id? or buffer name? - buffer name can change, buffer id is harder to serialize, I guess if internal only then buffer id)
- we could rewrite kill lines with simpler commands - extend selection to encompass lines->replace