diff --git a/src/core/core.odin b/src/core/core.odin index e304de6..5aec7fd 100644 --- a/src/core/core.odin +++ b/src/core/core.odin @@ -148,18 +148,42 @@ new_buffer_virtual :: proc(state: ^State) -> (id: int, buffer: ^FileBuffer, ok: return util.append(&state.buffers, new_virtual_file_buffer(context.allocator)) } -new_buffer_file :: proc(state: ^State, file_path: string) -> (id: int, buffer: ^FileBuffer, ok: bool) { - new_buffer, err := new_file_buffer(context.allocator, file_path, state.directory) +new_buffer_file :: proc(state: ^State, file_path: string, line: int = 0, col: int = 0) -> (id: int, buffer: ^FileBuffer, ok: bool) { + new_buffer, err := make_file_buffer(context.allocator, file_path, state.directory) if err.type != .None { ok = false return } + move_cursor_to_location(&new_buffer, line, col) + return util.append(&state.buffers, new_buffer) } new_buffer :: proc{new_buffer_file, new_buffer_virtual} +open_buffer_file :: proc(state: ^State, file_path: string, line: int = 0, col: int = 0) { + next_id := 0 + for { + if panel, ok := util.get(&state.panels, next_id).?; ok { + if type, ok := &panel.type.(FileBufferPanel); ok { + buffer_id, _, ok := new_buffer_file(state, file_path, line, col) + if ok { + type.buffer_id = buffer_id + state.current_panel = panel.id + } + + break + } else { + next_id := util.get_next(&state.panels, next_id) + continue + } + } + + break + } +} + get_buffer :: proc(state: ^State, buffer_id: int) -> Maybe(^FileBuffer) { return util.get(&state.buffers, buffer_id) } diff --git a/src/core/file_buffer.odin b/src/core/file_buffer.odin index 7be2a7b..3c6a7f2 100644 --- a/src/core/file_buffer.odin +++ b/src/core/file_buffer.odin @@ -752,7 +752,7 @@ new_virtual_file_buffer :: proc(allocator := context.allocator) -> FileBuffer { return buffer; } -new_file_buffer :: proc(allocator: mem.Allocator, file_path: string, base_dir: string = "") -> (FileBuffer, Error) { +make_file_buffer :: proc(allocator: mem.Allocator, file_path: string, base_dir: string = "") -> (FileBuffer, Error) { context.allocator = allocator; fmt.eprintln("attempting to open", file_path); @@ -996,7 +996,7 @@ update_file_buffer_scroll :: proc(buffer: ^FileBuffer, cursor: Maybe(^Cursor) = cursor = &buffer.history.cursor; } - if buffer.glyphs.height < 5 { + if buffer.glyphs.height <= 5 { buffer.top_line = cursor.?.line } else if cursor.?.line > (buffer.top_line + buffer.glyphs.height - 5) { buffer.top_line = math.max(cursor.?.line - buffer.glyphs.height + 5, 0); diff --git a/src/panels/file_buffer.odin b/src/panels/file_buffer.odin index eff259b..846a849 100644 --- a/src/panels/file_buffer.odin +++ b/src/panels/file_buffer.odin @@ -65,11 +65,7 @@ make_file_buffer_panel :: proc(file_path: string, line: int = 0, col: int = 0) - } } else { if buffer_id, buffer, ok := core.new_buffer(state, panel_state.file_path); ok { - buffer.history.cursor.line = panel_state.line - buffer.history.cursor.col = panel_state.col - buffer.top_line = buffer.history.cursor.line - core.update_file_buffer_index_from_cursor(buffer) - + core.move_cursor_to_location(buffer, panel_state.line, panel_state.col) panel_state.buffer_id = buffer_id } else { log.error("failed to create buffer") diff --git a/src/panels/grep.odin b/src/panels/grep.odin index 7ba6773..f294d9e 100644 --- a/src/panels/grep.odin +++ b/src/panels/grep.odin @@ -87,13 +87,8 @@ make_grep_panel :: proc() -> core.Panel { if panel_state.query_results != nil { selected_result := &panel_state.query_results[panel_state.selected_result] - if panel_id, ok := open(state, make_file_buffer_panel(selected_result.file_path, selected_result.line, selected_result.col)); ok { - close(state, this_panel.id) - - state.current_panel = panel_id - } else { - log.error("failed to open file buffer in new panel") - } + core.open_buffer_file(state, selected_result.file_path, selected_result.line, selected_result.col) + close(state, this_panel.id) } } }, "Open File"); diff --git a/src/pkg/grep_lib/src/lib.rs b/src/pkg/grep_lib/src/lib.rs index 4bee332..dfcc5b3 100644 --- a/src/pkg/grep_lib/src/lib.rs +++ b/src/pkg/grep_lib/src/lib.rs @@ -298,7 +298,7 @@ extern "C" fn grep_buffer( #[unsafe(no_mangle)] extern "C" fn free_grep_results(results: GrepResults) { unsafe { - let mut array = std::slice::from_raw_parts_mut( + let array = std::slice::from_raw_parts_mut( results.results as *mut GrepResult, results.len as usize, );