From 80edd22ef7514c176876f283bcd5baa33c86c79c Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Mon, 20 Jan 2025 18:43:18 +0000 Subject: [PATCH] Timeout tree construction --- src/SymbolPane/SymbolOutline.vala | 7 +++++ src/SymbolPane/Vala/ValaSymbolOutline.vala | 35 ++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/SymbolPane/SymbolOutline.vala b/src/SymbolPane/SymbolOutline.vala index a42a79f28..eed99485a 100644 --- a/src/SymbolPane/SymbolOutline.vala +++ b/src/SymbolPane/SymbolOutline.vala @@ -79,6 +79,13 @@ public class Scratch.Services.SymbolOutline : Gtk.Box { protected Code.Widgets.SourceList.ExpandableItem root; protected Gtk.CssProvider source_list_style_provider; public Gtk.Widget get_widget () { return this; } + public bool tool_box_sensitive { + set { + search_entry.sensitive = value; + filter_button.sensitive = value; + } + } + public virtual void parse_symbols () {} Gtk.MenuButton filter_button; diff --git a/src/SymbolPane/Vala/ValaSymbolOutline.vala b/src/SymbolPane/Vala/ValaSymbolOutline.vala index f33520a50..e46357ad6 100644 --- a/src/SymbolPane/Vala/ValaSymbolOutline.vala +++ b/src/SymbolPane/Vala/ValaSymbolOutline.vala @@ -17,6 +17,7 @@ */ public class Scratch.Services.ValaSymbolOutline : Scratch.Services.SymbolOutline { + public const int PARSE_TIME_MAX_MSEC = 1000; private Code.Plugins.ValaSymbolResolver resolver; private Vala.Parser parser; private GLib.Cancellable cancellable; @@ -66,7 +67,9 @@ public class Scratch.Services.ValaSymbolOutline : Scratch.Services.SymbolOutline } } + private uint parse_timeout_id = 0; public override void parse_symbols () { + tool_box_sensitive = true; var context = new Vala.CodeContext (); #if VALA_0_50 context.set_target_profile (Vala.Profile.GOBJECT, false); @@ -87,8 +90,20 @@ public class Scratch.Services.ValaSymbolOutline : Scratch.Services.SymbolOutline resolver.resolve (context); Vala.CodeContext.pop (); + bool took_too_long = false; + parse_timeout_id = Timeout.add_full (Priority.LOW, PARSE_TIME_MAX_MSEC, () => { + parse_timeout_id = 0; + took_too_long = true; + cancellable.cancel (); + return Source.REMOVE; + }); + var new_root = construct_tree (cancellable); - if (!cancellable.is_cancelled ()) { + if (parse_timeout_id > 0) { + Source.remove (parse_timeout_id); + } + + if (!cancellable.is_cancelled () || took_too_long) { Idle.add (() => { double adjustment_value = store.vadjustment.value; var root_children = store.root.children; // Keep reference to children for later destruction @@ -97,7 +112,21 @@ public class Scratch.Services.ValaSymbolOutline : Scratch.Services.SymbolOutline destroy_all_children ((Code.Widgets.SourceList.ExpandableItem)child); } - store.root.add (new_root); + if (took_too_long) { + var warning_item = new Code.Widgets.SourceList.Item () { + icon = new ThemedIcon ("dialog-warning"), + markup = "%s".printf (_("Too Many Symbols")), + tooltip = _("%s contains too many Vala symbols.\nParsing and showing them took long").printf (doc.file.get_basename ()), + selectable = false + }; + + store.root.add (warning_item); + tool_box_sensitive = false; + + } else { + store.root.add (new_root); + } + store.root.expand_all (); store.vadjustment.set_value (adjustment_value); @@ -106,6 +135,7 @@ public class Scratch.Services.ValaSymbolOutline : Scratch.Services.SymbolOutline } else { destroy_all_children (new_root); } + return null; }); } @@ -144,6 +174,7 @@ public class Scratch.Services.ValaSymbolOutline : Scratch.Services.SymbolOutline construct_child (symbol, new_root, cancellable); } + return new_root; }