add debug panel, don't open same file twice

main
Patrick Cleaveliln 2025-07-26 23:05:26 +00:00
parent ba312efe1a
commit d27c566ff1
5 changed files with 218 additions and 29 deletions

View File

@ -96,10 +96,14 @@ Panel_VTable :: struct {
}
PanelType :: union {
DebugPanel,
FileBufferPanel,
GrepPanel,
}
DebugPanel :: struct {
}
FileBufferPanel :: struct {
buffer_id: int,
viewed_symbol: Maybe(string),
@ -136,8 +140,10 @@ GrepQueryResult :: struct {
current_buffer :: proc(state: ^State) -> ^FileBuffer {
if current_panel, ok := state.current_panel.?; ok {
if panel, ok := util.get(&state.panels, current_panel).?; ok {
buffer, _ := panel->buffer(state)
return buffer
if panel.buffer != nil {
buffer, _ := panel->buffer(state)
return buffer
}
}
}
@ -149,6 +155,15 @@ new_buffer_virtual :: proc(state: ^State) -> (id: int, buffer: ^FileBuffer, ok:
}
new_buffer_file :: proc(state: ^State, file_path: string, line: int = 0, col: int = 0) -> (id: int, buffer: ^FileBuffer, ok: bool) {
for i in 0..<len(state.buffers.data) {
if buffer, ok := util.get(&state.buffers, i).?; ok {
if buffer.file_path == file_path {
move_cursor_to_location(buffer, line, col)
return i, buffer, true
}
}
}
new_buffer, err := make_file_buffer(context.allocator, file_path, state.directory)
if err.type != .None {
ok = false

View File

@ -749,6 +749,8 @@ new_virtual_file_buffer :: proc(allocator := context.allocator) -> FileBuffer {
glyphs = make_glyph_buffer(width, height),
};
push_new_snapshot(&buffer.history)
return buffer;
}
@ -812,6 +814,7 @@ make_file_buffer :: proc(allocator: mem.Allocator, file_path: string, base_dir:
glyphs = make_glyph_buffer(width, height),
};
push_new_snapshot(&buffer.history)
ts.parse_buffer(&buffer.tree, tree_sitter_file_buffer_input(&buffer))
return buffer, error();
@ -905,8 +908,8 @@ color_character :: proc(buffer: ^FileBuffer, start: Cursor, end: Cursor, palette
}
draw_file_buffer :: proc(state: ^State, buffer: ^FileBuffer, x, y, w, h: int, show_line_numbers: bool = true, show_cursor: bool = true) {
glyph_width := math.min(256, int(w / state.source_font_width));
glyph_height := math.min(256, int(h / state.source_font_height)) + 1;
glyph_width := math.max(math.min(256, int(w / state.source_font_width)), 1);
glyph_height := math.max(math.min(256, int(h / state.source_font_height)) + 1, 1);
update_glyph_buffer(buffer, glyph_width, glyph_height);

View File

@ -265,6 +265,10 @@ main :: proc() {
panels.open(&state, panels.make_file_buffer_panel(""))
}
panels.open(&state, panels.make_debug_panel())
panels.open(&state, panels.make_debug_panel())
if sdl2.Init({.VIDEO}) < 0 {
log.error("SDL failed to initialize:", sdl2.GetError());
return;
@ -331,8 +335,6 @@ main :: proc() {
sdl2.AddEventWatch(expose_event_watcher, &state);
core.push_new_snapshot(&core.current_buffer(&state).history)
control_key_pressed: bool;
for !state.should_close {
{

140
src/panels/debug.odin Normal file
View File

@ -0,0 +1,140 @@
package panels
import "base:runtime"
import "core:mem"
import "core:fmt"
import "core:strings"
import "core:log"
import "vendor:sdl2"
import "../core"
import "../util"
import "../ui"
make_debug_panel :: proc() -> core.Panel {
return core.Panel {
type = core.DebugPanel {},
create = proc(panel: ^core.Panel, state: ^core.State) {
context.allocator = panel.allocator
panel_state := &panel.type.(core.DebugPanel)
panel.input_map = core.new_input_map(show_help = true)
panel_actions := core.new_input_actions(show_help = true)
register_default_panel_actions(&panel_actions)
core.register_ctrl_key_action(&panel.input_map.mode[.Normal], .W, panel_actions, "Panel Navigation")
},
render = proc(panel: ^core.Panel, state: ^core.State) -> (ok: bool) {
if panel_state, ok := &panel.type.(core.DebugPanel); ok {
s := transmute(^ui.State)state.ui
ui.open_element(s, nil,
{
dir = .TopToBottom,
kind = {ui.Fit{}, ui.Grow{}},
},
style = {
background_color = .Background1,
},
)
{
render_buffer_list(state, s)
ui.open_element(s, nil, {
kind = {ui.Fit{}, ui.Exact(8)},
})
ui.close_element(s)
render_panel_list(state, s)
}
ui.close_element(s)
}
return true
}
}
}
render_buffer_list :: proc(state: ^core.State, s: ^ui.State) {
ui.open_element(s, nil,
{
dir = .TopToBottom,
kind = {ui.Fit{}, ui.Fit{}},
},
style = {
background_color = .Background1,
},
)
{
ui.open_element(s, "Open Buffers",
{
kind = {ui.Grow{}, ui.Fit{}},
},
style = {
border = {.Bottom},
border_color = .Background4,
}
)
ui.close_element(s)
ui.open_element(s, nil, {
kind = {ui.Fit{}, ui.Exact(8)},
})
ui.close_element(s)
for i in 0..<len(state.buffers.data) {
if buffer, ok := util.get(&state.buffers, i).?; ok {
buffer_label := fmt.tprintf("buf '%v' - %v", i, buffer.file_path[len(state.directory):])
ui.open_element(s, buffer_label, {})
ui.close_element(s)
}
}
}
ui.close_element(s)
}
render_panel_list :: proc(state: ^core.State, s: ^ui.State) {
ui.open_element(s, nil,
{
dir = .TopToBottom,
kind = {ui.Fit{}, ui.Fit{}},
},
style = {
background_color = .Background1,
},
)
{
ui.open_element(s, "Open Panels",
{
kind = {ui.Grow{}, ui.Fit{}},
},
style = {
border = {.Bottom},
border_color = .Background4,
}
)
ui.close_element(s)
ui.open_element(s, nil, {
kind = {ui.Fit{}, ui.Exact(8)},
})
ui.close_element(s)
for i in 0..<len(state.panels.data) {
if panel, ok := util.get(&state.panels, i).?; ok {
info: string
switch v in &panel.type {
case core.DebugPanel: info = "DebugPanel"
case core.FileBufferPanel: info = "FileBufferPanel"
case core.GrepPanel: info = "GrepPanel"
}
panel_label := fmt.tprintf("panel id '%v' - %v", i, info)
ui.open_element(s, panel_label, {})
ui.close_element(s)
}
}
}
ui.close_element(s)
}

View File

@ -136,20 +136,34 @@ close_element :: proc(state: ^State, loc := #caller_location) -> UI_Layout {
case Exact: { e.layout.size.x = int(v) }
case Fit: {
it := e.first
for child in iterate_siblings(state, &it) {
if child.layout.floating { continue }
switch e.layout.dir {
case .RightToLeft: fallthrough
case .LeftToRight: {
e.layout.size.x += child.layout.size.x
}
if it != nil {
for child in iterate_siblings(state, &it) {
if child.layout.floating { continue }
case .BottomToTop: fallthrough
case .TopToBottom: {
e.layout.size.x = math.max(e.layout.size.x, child.layout.size.x)
switch e.layout.dir {
case .RightToLeft: fallthrough
case .LeftToRight: {
e.layout.size.x += child.layout.size.x
}
case .BottomToTop: fallthrough
case .TopToBottom: {
e.layout.size.x = math.max(e.layout.size.x, child.layout.size.x)
}
}
}
} else {
switch v in e.kind {
case UI_Element_Kind_Text: {
// FIXME: properly use font size
e.layout.size.x = len(v) * 12
}
case UI_Element_Kind_Image: {
// TODO
}
case UI_Element_Kind_Custom: { }
}
}
}
case Grow: {
@ -177,20 +191,35 @@ close_element :: proc(state: ^State, loc := #caller_location) -> UI_Layout {
case Exact: { e.layout.size.y = int(v) }
case Fit: {
it := e.first
for child in iterate_siblings(state, &it) {
if child.layout.floating { continue }
switch e.layout.dir {
case .RightToLeft: fallthrough
case .LeftToRight: {
e.layout.size.y = math.max(e.layout.size.y, child.layout.size.y)
}
if it != nil {
for child in iterate_siblings(state, &it) {
if child.layout.floating { continue }
case .BottomToTop: fallthrough
case .TopToBottom: {
e.layout.size.y += child.layout.size.y
switch e.layout.dir {
case .RightToLeft: fallthrough
case .LeftToRight: {
e.layout.size.y = math.max(e.layout.size.y, child.layout.size.y)
}
case .BottomToTop: fallthrough
case .TopToBottom: {
e.layout.size.y += child.layout.size.y
}
}
}
} else {
switch v in e.kind {
case UI_Element_Kind_Text: {
// TODO: wrap text
// FIXME: properly use font size
e.layout.size.y = 16
}
case UI_Element_Kind_Image: {
// TODO
}
case UI_Element_Kind_Custom: { }
}
}
}
case Grow: {
@ -316,7 +345,7 @@ grow_children :: proc(state: ^State, index: int) {
}
}
if num_growing.x > 0 || num_growing.y > 0 {
if true || num_growing.x > 0 || num_growing.y > 0 {
remaining_size := [2]int{ x_e, y_e } - children_size
to_grow: [2]int
to_grow.x = 0 if num_growing.x < 1 else remaining_size.x/num_growing.x
@ -348,9 +377,9 @@ grow_children :: proc(state: ^State, index: int) {
_, x_growing := child.layout.kind.x.(Grow)
_, y_growing := child.layout.kind.y.(Grow)
if x_growing || y_growing || child.layout.floating {
// if x_growing || y_growing || child.layout.floating {
grow_children(state, child_index)
}
// }
}
}
}