From a6854a0ea52d1f614eeacecf0ec396d8d2cd3d32 Mon Sep 17 00:00:00 2001 From: pommicket Date: Sat, 5 Aug 2023 00:05:09 -0400 Subject: fix orphan node problem --- main.c | 8 +++----- ted.c | 56 +++++++++++++++++++++++++++++++++++--------------------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/main.c b/main.c index d72e217..73e2213 100644 --- a/main.c +++ b/main.c @@ -1,12 +1,10 @@ /* +TODO: +- rename using LSP (textDocument/rename) + - warn if many files are changing FUTURE FEATURES: - autodetect indentation (tabs vs spaces) - robust find (results shouldn't move around when you type things) -- rename using LSP (textDocument/rename) - - we have request writing & response parsing support for it, but that hasn't been tested yet - - i'm putting this off for now since it seems hard to have undo support for it. - - possible idea: open all files altered, and create undo chains for each of them. - if there are too many files, give an error like "use a different tool for this" - config variables - bind key to multiple commands - convert macro to command list diff --git a/ted.c b/ted.c index e952ece..7c1743b 100644 --- a/ted.c +++ b/ted.c @@ -408,6 +408,26 @@ void ted_free_fonts(Ted *ted) { ted->font_bold = NULL; } +// get node and tab containing buffer +static Node *ted_buffer_location_in_node_tree(Ted *ted, TextBuffer *buffer, u16 *tab_idx) { + // now we need to figure out where this buffer is + u16 idx = (u16)(buffer - ted->buffers); + const bool *nodes_used = ted->nodes_used; + for (u16 i = 0; i < TED_MAX_NODES; ++i) { + if (!nodes_used[i]) continue; + Node *node = &ted->nodes[i]; + arr_foreach_ptr(node->tabs, u16, tab) { + if (idx == *tab) { + if (tab_idx) + *tab_idx = (u16)(tab - node->tabs); + return node; + } + } + } + assert(0); + return NULL; +} + void ted_switch_to_buffer(Ted *ted, TextBuffer *buffer) { TextBuffer *search_buffer = find_search_buffer(ted); ted->active_buffer = buffer; @@ -419,24 +439,11 @@ void ted_switch_to_buffer(Ted *ted, TextBuffer *buffer) { if (buffer >= ted->buffers && buffer < ted->buffers + TED_MAX_BUFFERS) { ted->prev_active_buffer = buffer; - - u16 idx = (u16)(buffer - ted->buffers); - // now we need to figure out where this buffer is - bool *nodes_used = ted->nodes_used; - for (u16 i = 0; i < TED_MAX_NODES; ++i) { - if (nodes_used[i]) { - Node *node = &ted->nodes[i]; - arr_foreach_ptr(node->tabs, u16, tab) { - if (idx == *tab) { - node->active_tab = (u16)(tab - node->tabs); - ted->active_node = node; - signature_help_retrigger(ted); - return; - } - } - } - } - assert(0); + u16 active_tab=0; + Node *node = ted_buffer_location_in_node_tree(ted, buffer, &active_tab); + node->active_tab = active_tab; + ted->active_node = node; + signature_help_retrigger(ted); } else { ted->active_node = NULL; } @@ -510,12 +517,19 @@ static Status ted_open_buffer(Ted *ted, u16 *buffer_idx, u16 *tab) { if (new_buffer_index >= 0) { Node *node = ted->active_node; if (!node) { - // no active node; let's create one! - i32 node_idx = ted_new_node(ted); - if (node_idx >= 0) { + if (!ted->nodes_used[0]) { + // no nodes open; create a root node + i32 node_idx = ted_new_node(ted); + assert(node_idx == 0); node = &ted->nodes[node_idx]; ted->active_node = node; + } else if (ted->prev_active_buffer) { + // opening a file while a menu is open + // it may happen.... (currently happens for rename symbol) + node = ted_buffer_location_in_node_tree(ted, ted->prev_active_buffer, NULL); } else { + // idk what is going on + ted_error(ted, "internal error: can't figure out where to put this buffer."); ted_delete_buffer(ted, (u16)new_buffer_index); return false; } -- cgit v1.2.3