QOL column alignment

This commit is contained in:
Peter Slattery 2025-07-07 14:31:41 -07:00
parent ea389639b3
commit 322f690afc
5 changed files with 154 additions and 46 deletions

View File

@ -123,6 +123,85 @@ CUSTOM_COMMAND_SIG(cmd_alt_enter_behavior)
}
}
function i64
qol_seek_char(Application_Links *app, Buffer_ID buffer, Scan_Direction direction, i64 start_pos, u8 target_char){
Scratch_Block scratch(app);
i64 line = get_line_number_from_pos(app, buffer, start_pos);
Range_i64 range = get_line_pos_range(app, buffer, line);
range.max += 1;
String_Const_u8 string = push_buffer_range(app, scratch, buffer, range);
i64 pos = start_pos;
while(range_contains(range, pos)){
pos += direction;
u8 current_char = string.str[pos - range.min];
if (current_char == target_char){ return pos; }
}
return start_pos;
}
CUSTOM_COMMAND_SIG(qol_char_forward)
CUSTOM_DOC("[QOL] Seeks forward in current line to the selected char")
{
View_ID view = get_active_view(app, Access_ReadVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadVisible);
i64 cursor_pos = view_get_cursor_pos(app, view);
i64 pos = qol_seek_char(app, buffer, Scan_Forward, cursor_pos, qol_target_char);
view_set_cursor_and_preferred_x(app, view, seek_pos(pos));
}
CUSTOM_COMMAND_SIG(qol_char_backward)
CUSTOM_DOC("[QOL] Seeks back in current line to the selected char")
{
View_ID view = get_active_view(app, Access_ReadVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadVisible);
i64 cursor_pos = view_get_cursor_pos(app, view);
i64 pos = qol_seek_char(app, buffer, Scan_Backward, cursor_pos, qol_target_char);
view_set_cursor_and_preferred_x(app, view, seek_pos(pos));
}
CUSTOM_COMMAND_SIG(qol_column_toggle)
CUSTOM_DOC("[QOL] Toggles the column for bumping and selects hovered char")
{
View_ID view = get_active_view(app, Access_ReadVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadVisible);
if (qol_col_cursor.pos < 0){
i64 pos = view_get_cursor_pos(app, view);
qol_target_char = buffer_get_char(app, buffer, pos);
qol_col_cursor = buffer_compute_cursor(app, buffer, seek_pos(pos));
}
else{
qol_col_cursor.pos = -1;
}
}
CUSTOM_COMMAND_SIG(qol_write_space)
CUSTOM_DOC("[QOL] Writes as many spaces needed for bumping to column")
{
Scratch_Block scratch(app);
if (qol_col_cursor.pos > 0){
View_ID view = get_active_view(app, Access_ReadVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadVisible);
qol_col_cursor = buffer_compute_cursor(app, buffer, seek_line_col(qol_col_cursor.line, qol_col_cursor.col));
i64 pos = view_get_cursor_pos(app, view);
i64 line = get_line_number_from_pos(app, buffer, pos);
f32 col_x = view_relative_xy_of_pos(app, view, line, qol_col_cursor.pos).x;
f32 cur_x = view_relative_xy_of_pos(app, view, line, pos).x;
Face_ID face = get_face_id(app, buffer);
f32 space_advance = get_face_metrics(app, face).space_advance;
i64 N = i64((col_x - cur_x) / space_advance);
if (N < 0){ N = 1; }
String_Const_u8 spaces = string_const_u8_push(scratch, N);
block_fill_u8(spaces.str, N, ' ');
write_text(app, spaces);
}
else{
write_space(app);
}
}
function void
bindings_cmd_misc(Mapping* m, Command_Map* map)
@ -231,6 +310,11 @@ custom_keyboard_bindings()
Bind(unindent_range, KeyCode_Tab, KeyCode_Shift);
Bind(indent_range, KeyCode_Tab);
Bind(qol_column_toggle, KeyCode_BackwardSlash, key_alt);
Bind(qol_write_space, KeyCode_Space, key_alt, KeyCode_Shift);
Bind(qol_char_forward, KeyCode_L, key_alt);
Bind(qol_char_backward, KeyCode_J, key_alt);
// Macros
Bind(keyboard_macro_start_recording, KeyCode_1, key_alt);
Bind(keyboard_macro_finish_recording, KeyCode_2, key_alt);

View File

@ -400,6 +400,18 @@ String_Const_u8 lexeme = push_token_lexeme(app, scratch, buffer, token);
draw_scope_highlight(app, buffer, text_layout_id, cursor_pos, colors.vals, colors.count);
}
// NOTE(PS): QOL Column
if (qol_col_cursor.pos >= 0){
Buffer_Seek seek = seek_line_col(qol_col_cursor.line, qol_col_cursor.col);
Buffer_Cursor cursor = buffer_compute_cursor(app, buffer, seek);
Rect_f32 col_rect = text_layout_character_on_screen(app, text_layout_id, cursor.pos);
if (col_rect.x1 > 0.f){
col_rect.y0 = rect.y0;
col_rect.y1 = rect.y1;
draw_rectangle_fcolor(app, col_rect, 0.f, fcolor_id(defcolor_highlight_cursor_line));
}
}
b32 use_error_highlight = def_get_config_b32(vars_save_string_lit("use_error_highlight"));
b32 use_jump_highlight = def_get_config_b32(vars_save_string_lit("use_jump_highlight"));
if (use_error_highlight || use_jump_highlight){

View File

@ -64,6 +64,7 @@
#include "4coder_tutorial.h"
#include "4coder_search_list.h"
#include "4coder_modal.h"
#include "4coder_qol.h"
////////////////////////////////
@ -147,6 +148,8 @@
#include "4coder_default_hooks.cpp"
#include "4coder_qol.cpp"
#endif
// BOTTOM

View File

9
code/custom/4coder_qol.h Normal file
View File

@ -0,0 +1,9 @@
/* date = July 7th 2025 2:22 pm */
#ifndef FCODER_QOL_H
#define FCODER_QOL_H
global u8 qol_target_char;
global Buffer_Cursor qol_col_cursor = {-1};
#endif //FCODER_QOL_H