open file at location

memory-refactor
Patrick Cleaveliln 2025-07-10 00:03:28 +00:00
parent 06d9750cd2
commit a8dbc09085
4 changed files with 51 additions and 14 deletions

View File

@ -299,7 +299,7 @@ main :: proc() {
if args, ok := core.attempt_read_command_args(Args, state.command_args[:]); ok { if args, ok := core.attempt_read_command_args(Args, state.command_args[:]); ok {
log.info("attempting to open file", args.file_path) log.info("attempting to open file", args.file_path)
panels.open_file_buffer_in_new_panel(state, args.file_path) panels.open_file_buffer_in_new_panel(state, args.file_path, 0, 0)
} }
} }
) )
@ -315,7 +315,7 @@ main :: proc() {
if len(os.args) > 1 { if len(os.args) > 1 {
for arg in os.args[1:] { for arg in os.args[1:] {
panels.open_file_buffer_in_new_panel(&state, arg) panels.open_file_buffer_in_new_panel(&state, arg, 0, 0)
} }
} else { } else {
buffer := core.new_virtual_file_buffer(context.allocator); buffer := core.new_virtual_file_buffer(context.allocator);

View File

@ -46,8 +46,8 @@ rs_grep_as_results :: proc(results: ^RS_GrepResults, allocator := context.alloca
query_results[i] = core.GrepQueryResult { query_results[i] = core.GrepQueryResult {
file_context = strings.clone_from_ptr(r.text, int(r.text_len), allocator) or_continue, file_context = strings.clone_from_ptr(r.text, int(r.text_len), allocator) or_continue,
file_path = strings.clone_from_ptr(r.path, int(r.path_len), allocator) or_continue, file_path = strings.clone_from_ptr(r.path, int(r.path_len), allocator) or_continue,
line = int(r.line_number), line = int(r.line_number) - 1,
col = int(r.column), col = int(r.column) - 1,
} }
} }
@ -65,12 +65,16 @@ register_default_leader_actions :: proc(input_map: ^core.InputActions) {
}, "Grep Workspace") }, "Grep Workspace")
} }
open :: proc(state: ^core.State, panel: core.Panel, make_active: bool = true) { open :: proc(state: ^core.State, panel: core.Panel, make_active: bool = true) -> (panel_id: int, ok: bool) {
if panel_id, ok := util.append_static_list(&state.panels, panel).?; ok && make_active { if panel_id, ok := util.append_static_list(&state.panels, panel).?; ok && make_active {
state.current_panel = panel_id state.current_panel = panel_id
core.reset_input_map(state) core.reset_input_map(state)
return panel_id, true
} }
return -1, false
} }
close :: proc(state: ^core.State, panel_id: int) { close :: proc(state: ^core.State, panel_id: int) {
@ -81,21 +85,32 @@ close :: proc(state: ^core.State, panel_id: int) {
util.delete(&state.panels, panel_id) util.delete(&state.panels, panel_id)
// TODO: keep track of the last active panel instead of focusing back to the first one
state.current_panel = util.get_first_active_index(&state.panels).?
core.reset_input_map(state) core.reset_input_map(state)
} }
} }
open_file_buffer_in_new_panel :: proc(state: ^core.State, file_path: string) { open_file_buffer_in_new_panel :: proc(state: ^core.State, file_path: string, line, col: int) -> (panel_id, buffer_index: int, ok: bool) {
buffer, err := core.new_file_buffer(context.allocator, file_path, state.directory); buffer, err := core.new_file_buffer(context.allocator, file_path, state.directory);
if err.type != .None { if err.type != .None {
log.error("Failed to create file buffer:", err); log.error("Failed to create file buffer:", err);
return; return;
} }
buffer_index := len(state.buffers) buffer.cursor.line = line
buffer.cursor.col = col
core.update_file_buffer_index_from_cursor(&buffer)
buffer_index = len(state.buffers)
runtime.append(&state.buffers, buffer); runtime.append(&state.buffers, buffer);
open(state, make_file_buffer_panel(buffer_index)) if panel_id, ok := open(state, make_file_buffer_panel(buffer_index)); ok {
return panel_id, buffer_index, true
}
return -1, -1, false
} }
render_file_buffer :: proc(state: ^core.State, s: ^ui.State, buffer: ^core.FileBuffer) { render_file_buffer :: proc(state: ^core.State, s: ^ui.State, buffer: ^core.FileBuffer) {
@ -211,12 +226,14 @@ make_grep_panel :: proc(state: ^core.State) -> core.Panel {
if panel_state.query_results != nil { if panel_state.query_results != nil {
selected_result := &panel_state.query_results[panel_state.selected_result] selected_result := &panel_state.query_results[panel_state.selected_result]
open_file_buffer_in_new_panel(state, selected_result.file_path) if panel_id, buffer, ok := open_file_buffer_in_new_panel(state, selected_result.file_path, selected_result.line, selected_result.col); ok {
mem.arena_free_all(&panel_state.query_arena)
panel_state.query_results = nil
close(state, this_panel) close(state, this_panel)
state.current_panel = panel_id
state.current_buffer = buffer
} else {
log.error("failed to open file buffer in new panel")
}
} }
} }
} }

View File

@ -47,6 +47,16 @@ get_static_list_elem :: proc(list: ^StaticList($T), index: int) -> Maybe(^T) {
return nil return nil
} }
get_first_active_index :: proc(list: ^StaticList($T)) -> Maybe(int) {
for i in 0..<len(list.data) {
if list.data[i].active {
return i
}
}
return nil
}
get :: proc{get_static_list_elem} get :: proc{get_static_list_elem}
delete_static_list_elem :: proc(list: ^StaticList($T), index: int) { delete_static_list_elem :: proc(list: ^StaticList($T), index: int) {

12
todo.md
View File

@ -4,6 +4,13 @@
- Odd scrolling behavior on small screen heights - Odd scrolling behavior on small screen heights
# Planned Features # Planned Features
- Save/Load files
- [ ] Save
- [ ] Load when changed on disk
- [ ] Simple File Search (vim /)
- [ ] Auto-indent
- Testing Harness
- [ ] Replay user inputs and assert buffer contents/changes
- LSP Integration - LSP Integration
- [ ] Language Server Configurations - [ ] Language Server Configurations
- [ ] Diagnostics - [ ] Diagnostics
@ -16,11 +23,14 @@
- [ ] Integrate tree-sitter - [ ] Integrate tree-sitter
- [ ] Bootleg Telescope - [ ] Bootleg Telescope
- [ ] Grepping Files - [ ] Grepping Files
- [x] Query across project
- [x] Open file in new buffer
- [x] Open file in new buffer at found location
- [ ] Preview file with context (instead of just the single matched line)
- [ ] Open Buffer Search - [ ] Open Buffer Search
- Re-write the UI (again) - Re-write the UI (again)
- [x] New UI - [x] New UI
- [ ] Styling - [ ] Styling
- Save/Load files
- Undo/Redo - Undo/Redo
- [ ] Edit History Tree - [ ] Edit History Tree
- [ ] Undo history saved to disk - [ ] Undo history saved to disk