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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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

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

static construct {
var fontpath = Path.build_filename (
Constants.DATADIR, Constants.PROJECT_NAME, "fonts", "BuilderBlocks.ttf"
Expand Down Expand Up @@ -470,6 +474,9 @@ namespace Scratch.Services {
if (ret_value) {
// Delete backup copy file
delete_backup ();
// Need to destoy outline for Document to destruct
outline_widget_pane.get_child2 ().destroy ();
outline = null;
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,19 +17,15 @@
*/

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 (
doc: _doc
);
}

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 @@ -38,40 +34,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 @@ -89,6 +101,8 @@ public class Scratch.Services.ValaSymbolOutline : Scratch.Services.SymbolOutline
} else {
destroy_all_children (new_root);
}

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

private Granite.Widgets.SourceList.ExpandableItem construct_tree (GLib.Cancellable cancellable) {
private Granite.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 @@ -127,6 +144,7 @@ public class Scratch.Services.ValaSymbolOutline : Scratch.Services.SymbolOutline

construct_child (symbol, new_root, cancellable);
}

return new_root;
}

Expand Down
1 change: 1 addition & 0 deletions src/Widgets/DocumentView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ public class Scratch.Widgets.DocumentView : Granite.Widgets.DynamicNotebook {
doc.do_close.begin (false, (obj, res) => {
if (doc.do_close.end (res)) {
remove_tab (doc);
doc.destroy ();
}
});
}
Expand Down