Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash in vala outline when entering some invalid code #1356

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Services/Document.vala
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ namespace Scratch.Services {
this.file = file;
}

~Document () {
debug ("Destruct Document");
}

static construct {
var fontpath = Path.build_filename (
Constants.DATADIR, Constants.PROJECT_NAME, "fonts", "BuilderBlocks.ttf"
Expand Down Expand Up @@ -522,6 +526,10 @@ namespace Scratch.Services {
// Delete backup copy file
closing = true; // Stops recreating backup when trailing space stripped
delete_backup ();
// Need to destoy outline for Document to destruct
outline_widget_pane.get_child2 ().destroy ();
outline = null;

notify["tab.loading"].disconnect (on_tab_loading_change);
doc_closed ();
}
Expand Down
62 changes: 40 additions & 22 deletions src/SymbolPane/Vala/ValaSymbolOutline.vala
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
*/

public class Scratch.Services.ValaSymbolOutline : Scratch.Services.SymbolOutline {
private Code.Plugins.ValaSymbolResolver resolver;
private Vala.Parser parser;
private GLib.Cancellable cancellable;
private GLib.Thread<void*> current_thread;
public ValaSymbolOutline (Scratch.Services.Document _doc) {
Object (
orientation: Gtk.Orientation.VERTICAL,
Expand All @@ -44,9 +43,6 @@ public class Scratch.Services.ValaSymbolOutline : Scratch.Services.SymbolOutline
}

construct {
parser = new Vala.Parser ();
resolver = new Code.Plugins.ValaSymbolResolver ();

store.item_selected.connect ((selected) => {
doc.goto (((ValaSymbolItem)selected).symbol.source_reference.begin.line);
});
Expand All @@ -55,40 +51,56 @@ public class Scratch.Services.ValaSymbolOutline : Scratch.Services.SymbolOutline
}

~ValaSymbolOutline () {
debug ("Destroy symbol out line");
debug ("Destruct ValaSymbolOutline");
}

void doc_closed (Scratch.Services.Document doc) {
doc.doc_closed.disconnect (doc_closed);
if (cancellable != null) {
cancel ();
}

private void cancel () {
if (cancellable != null && !cancellable.is_cancelled ()) {
cancellable.cancel ();
cancellable = null;
}

cancellable = null;
}

public override void parse_symbols () {
var context = new Vala.CodeContext ();
#if VALA_0_50
context.set_target_profile (Vala.Profile.GOBJECT, false);
#else
context.profile = Vala.Profile.GOBJECT;
#endif
context.add_source_filename (doc.file.get_path ());
context.report = new Report ();
if (cancellable != null && !cancellable.is_cancelled ()) {
cancellable.cancel ();
cancel ();
if (current_thread != null) {
warning ("THREAD NOT FINISHED");
// TODO Show something in symbol pane to indicate parser stalled
// TODO Provide a way of resetting parser
return;
}

cancellable = new GLib.Cancellable ();
new Thread<void*> ("parse-symbols", () => {
warning ("parse vala symbols in %s", doc.file.get_basename ());
current_thread = new Thread<void*> ("parse-symbols", () => {
var context = new Vala.CodeContext ();
#if VALA_0_50
context.set_target_profile (Vala.Profile.GOBJECT, false);
#else
context.profile = Vala.Profile.GOBJECT;
#endif
context.add_source_filename (doc.file.get_path ());
context.report = new Report ();

Vala.CodeContext.push (context);

var parser = new Vala.Parser ();
var resolver = new Code.Plugins.ValaSymbolResolver ();

parser.parse (context);
resolver.clear ();

resolver.resolve (context);
Vala.CodeContext.pop ();

var new_root = construct_tree (cancellable);
var new_root = construct_tree (resolver, cancellable);
if (!cancellable.is_cancelled ()) {
cancellable = null;
Idle.add (() => {
double adjustment_value = store.vadjustment.value;
var root_children = store.root.children; // Keep reference to children for later destruction
Expand All @@ -106,6 +118,8 @@ public class Scratch.Services.ValaSymbolOutline : Scratch.Services.SymbolOutline
} else {
destroy_all_children (new_root);
}

current_thread = null;
return null;
});
}
Expand All @@ -124,7 +138,10 @@ public class Scratch.Services.ValaSymbolOutline : Scratch.Services.SymbolOutline
parent.remove (item);
}

private Code.Widgets.SourceList.ExpandableItem construct_tree (GLib.Cancellable cancellable) {
private Code.Widgets.SourceList.ExpandableItem construct_tree (
Code.Plugins.ValaSymbolResolver resolver,
GLib.Cancellable cancellable
) {
var fields = resolver.get_properties_fields ();
var symbols = resolver.get_symbols ();
// Remove fake fields created by the vala parser.
Expand All @@ -144,6 +161,7 @@ public class Scratch.Services.ValaSymbolOutline : Scratch.Services.SymbolOutline

construct_child (symbol, new_root, cancellable);
}

return new_root;
}

Expand Down