[gnome-builder/wip/chergert/completion] vala: restore previous completion bits



commit efb01cf01b9ff1d40a7adb6f2d050d470b9cb0f4
Author: Christian Hergert <chergert redhat com>
Date:   Tue Jun 5 03:46:39 2018 -0700

    vala: restore previous completion bits
    
    This has various drawbacks, but things work more. If we really
    want to improve the quality of results, that can be done after
    the completion engine lands, as it's a distraction right now.

 .../vala-pack/ide-vala-completion-provider.vala    |  8 +-
 src/plugins/vala-pack/ide-vala-completion.vala     | 96 +++++++++++++++-------
 src/plugins/vala-pack/ide-vala-index.vala          | 10 ++-
 3 files changed, 81 insertions(+), 33 deletions(-)
---
diff --git a/src/plugins/vala-pack/ide-vala-completion-provider.vala 
b/src/plugins/vala-pack/ide-vala-completion-provider.vala
index 9a2afcddc..d571210e1 100644
--- a/src/plugins/vala-pack/ide-vala-completion-provider.vala
+++ b/src/plugins/vala-pack/ide-vala-completion-provider.vala
@@ -50,7 +50,7 @@ namespace Ide
                        Ide.ValaCompletionResults results = null;
                        var buffer = context.get_buffer () as Ide.Buffer;
                        var file = buffer.get_file ();
-                       Gtk.TextIter iter;
+                       Gtk.TextIter iter, begin;
 
                        if (file.is_temporary) {
                                throw new GLib.IOError.NOT_SUPPORTED ("Cannot complete on temporary files");
@@ -59,6 +59,11 @@ namespace Ide
                        buffer.sync_to_unsaved_files ();
 
                        context.get_bounds (out iter, null);
+
+                       begin = iter;
+                       begin.set_line_offset (0);
+                       var line_text = begin.get_slice (iter);
+
                        var line = iter.get_line ();
                        var line_offset = iter.get_line_offset ();
 
@@ -74,6 +79,7 @@ namespace Ide
                                results = index.code_complete (file.file,
                                                               line + 1,
                                                               line_offset + 1,
+                                                              line_text,
                                                               unsaved_files_copy,
                                                               cancellable,
                                                               out res_line,
diff --git a/src/plugins/vala-pack/ide-vala-completion.vala b/src/plugins/vala-pack/ide-vala-completion.vala
index 356d5b24f..e11f13d09 100644
--- a/src/plugins/vala-pack/ide-vala-completion.vala
+++ b/src/plugins/vala-pack/ide-vala-completion.vala
@@ -25,39 +25,58 @@ using Vala;
 
 namespace Ide
 {
-       public class ValaCompletion : GLib.Object
+       public class ValaCompletion: GLib.Object
        {
+               static Regex member_access;
+               static Regex member_access_split;
+
                Vala.CodeContext context;
                Vala.SourceLocation location;
+               string current_text;
                Vala.Block? nearest;
 
+               static construct {
+                       try {
+                               member_access = new Regex("""((?:\w+(?:\s*\([^()]*\))?\.)*)(\w*)$""");
+                               member_access_split = new Regex ("""(\s*\([^()]*\))?\.""");
+                       } catch (RegexError err) {
+                               critical("Regular expressions failed to compile : %s", err.message);
+                       }
+               }
+
                public ValaCompletion (Vala.CodeContext context,
                                       Vala.SourceLocation location,
+                                      string current_text,
                                       Vala.Block? nearest)
                {
                        this.context = context;
                        this.location = location;
+                       this.current_text = current_text;
                        this.nearest = nearest;
                }
 
-               public void run (Ide.ValaCompletionResults ret, ref Vala.SourceLocation start_pos)
+               public GLib.List<Vala.Symbol>? run (ref Vala.SourceLocation start_pos)
                {
+                       MatchInfo match_info;
+
+                       if (!member_access.match (current_text, 0, out match_info))
+                               return null;
+                       else if (match_info.fetch(0).length < 2)
+                               return null;
+
                        start_pos.line = this.location.line;
-                       start_pos.column = this.location.column;
+                       start_pos.column = this.location.column - (int)match_info.fetch (2).length;
 
-                       if (nearest != null) {
-                               for (var sym = (Vala.Symbol) nearest; sym != null; sym = sym.parent_symbol) {
-                                       symbol_lookup_inherited (ret, sym);
-                               }
+                       var names = member_access_split.split (match_info.fetch (1));
 
-                               foreach (var ns in nearest.source_reference.file.current_using_directives) {
-                                       symbol_lookup_inherited (ret, ns.namespace_symbol);
-                               }
-                       }
+                       var syms = lookup_symbol (construct_member_access (names),
+                                                 match_info.fetch (2),
+                                                 nearest);
+
+                       return syms;
                }
 
-#if 0
-               GLib.List<Vala.Symbol> lookup_symbol (Vala.Expression? inner, Vala.Block? block)
+               GLib.List<Vala.Symbol> lookup_symbol (Vala.Expression? inner, string name, Vala.Block? block)
                {
                        var matching_symbols = new GLib.List<Vala.Symbol> ();
 
@@ -76,14 +95,14 @@ namespace Ide
                                        matching_symbols.concat (symbol_lookup_inherited 
(inner.symbol_reference));
                        } else if (inner is Vala.MemberAccess) {
                                var inner_ma = (Vala.MemberAccess) inner;
-                               var matching = lookup_symbol (inner_ma.inner, inner_ma.member_name, false, 
block);
+                               var matching = lookup_symbol (inner_ma.inner, inner_ma.member_name, block);
                                if (matching != null)
                                        matching_symbols.concat (symbol_lookup_inherited (matching.data));
                        } else if (inner is Vala.MethodCall) {
                                var inner_inv = (Vala.MethodCall) inner;
                                var inner_ma = inner_inv.call as Vala.MemberAccess;
                                if (inner_ma != null) {
-                                       var matching = lookup_symbol (inner_ma.inner, inner_ma.member_name, 
false, block);
+                                       var matching = lookup_symbol (inner_ma.inner, inner_ma.member_name, 
block);
                                        if (matching != null)
                                                matching_symbols.concat (symbol_lookup_inherited 
(matching.data, true));
                                }
@@ -91,53 +110,72 @@ namespace Ide
 
                        return matching_symbols;
                }
-#endif
 
-               void symbol_lookup_inherited (Ide.ValaCompletionResults results,
-                                             Vala.Symbol? sym,
-                                             bool invocation = false)
+               GLib.List<Vala.Symbol> symbol_lookup_inherited (Vala.Symbol? sym,
+                                                               bool invocation = false)
                {
+                       GLib.List<Vala.Symbol> result = null;
+
                        // This may happen if we cannot find all the needed packages
                        if (sym == null)
-                               return;
+                               return result;
 
                        var symbol_table = sym.scope.get_symbol_table ();
 
                        if (symbol_table != null) {
                                foreach (string key in symbol_table.get_keys()) {
-                                       results.add (symbol_table[key]);
+                                       result.append (symbol_table[key]);
                                }
                        }
 
                        if (invocation && sym is Vala.Method) {
                                var func = (Vala.Method) sym;
-                               symbol_lookup_inherited (results, func.return_type.data_type);
+                               result.concat (symbol_lookup_inherited (func.return_type.data_type));
                        } else if (sym is Vala.Class) {
                                var cl = (Vala.Class) sym;
                                foreach (var base_type in cl.get_base_types ()) {
-                                       symbol_lookup_inherited (results, base_type.data_type);
+                                       result.concat (symbol_lookup_inherited (base_type.data_type));
                                }
                        } else if (sym is Vala.Struct) {
                                var st = (Vala.Struct) sym;
-                               symbol_lookup_inherited (results, st.base_type.data_type);
+                               result.concat (symbol_lookup_inherited (st.base_type.data_type));
                        } else if (sym is Vala.Interface) {
                                var iface = (Vala.Interface) sym;
                                foreach (var prerequisite in iface.get_prerequisites ()) {
-                                       symbol_lookup_inherited (results, prerequisite.data_type);
+                                       result.concat (symbol_lookup_inherited (prerequisite.data_type));
                                }
                        } else if (sym is Vala.LocalVariable) {
                                var variable = (Vala.LocalVariable) sym;
-                               symbol_lookup_inherited (results, variable.variable_type.data_type);
+                               result.concat (symbol_lookup_inherited (variable.variable_type.data_type));
                        } else if (sym is Vala.Field) {
                                var field = (Vala.Field) sym;
-                               symbol_lookup_inherited (results, field.variable_type.data_type);
+                               result.concat (symbol_lookup_inherited (field.variable_type.data_type));
                        } else if (sym is Vala.Property) {
                                var prop = (Vala.Property) sym;
-                               symbol_lookup_inherited (results, prop.property_type.data_type);
+                               result.concat (symbol_lookup_inherited (prop.property_type.data_type));
                        } else if (sym is Vala.Parameter) {
                                var fp = (Vala.Parameter) sym;
-                               symbol_lookup_inherited (results, fp.variable_type.data_type);
+                               result.concat (symbol_lookup_inherited (fp.variable_type.data_type));
                        }
+
+                       return result;
+               }
+
+               Vala.Expression construct_member_access (string[] names)
+               {
+                       Vala.Expression expr = null;
+
+                       for (var i = 0; names[i] != null; i++) {
+                               if (names[i] != "") {
+                                       expr = new Vala.MemberAccess (expr, names[i]);
+                                       if (names[i+1] != null && names[i+1].chug ().has_prefix ("(")) {
+                                               expr = new Vala.MethodCall (expr);
+                                               i++;
+                                       }
+                               }
+                       }
+
+                       return expr;
                }
        }
 }
diff --git a/src/plugins/vala-pack/ide-vala-index.vala b/src/plugins/vala-pack/ide-vala-index.vala
index 06d2f88d8..14f5ec00f 100644
--- a/src/plugins/vala-pack/ide-vala-index.vala
+++ b/src/plugins/vala-pack/ide-vala-index.vala
@@ -329,6 +329,7 @@ namespace Ide
                public Ide.ValaCompletionResults code_complete (GLib.File file,
                                                                int line,
                                                                int column,
+                                                               string? line_text,
                                                                GLib.GenericArray<Ide.UnsavedFile>? 
unsaved_files,
                                                                GLib.Cancellable? cancellable,
                                                                out int result_line,
@@ -355,7 +356,7 @@ namespace Ide
                                                var locator = new Ide.ValaLocator ();
                                                var nearest = locator.locate (source_file, line, column);
 
-                                               this.add_completions (source_file, ref line, ref column, 
nearest, result);
+                                               this.add_completions (source_file, ref line, ref column, 
line_text, nearest, result);
                                        }
 
                                        Vala.CodeContext.pop ();
@@ -418,15 +419,18 @@ namespace Ide
                void add_completions (Ide.ValaSourceFile source_file,
                                      ref int line,
                                      ref int column,
+                                     string? line_text,
                                      Vala.Symbol? nearest,
                                      Ide.ValaCompletionResults results)
                {
                        var block = nearest as Vala.Block;
                        Vala.SourceLocation cursor = Vala.SourceLocation (null, line, column);
 
-                       var completion = new Ide.ValaCompletion (this.code_context, cursor, block);
+                       var completion = new Ide.ValaCompletion (this.code_context, cursor, line_text, block);
 
-                       completion.run (results, ref cursor);
+                       foreach (var symbol in completion.run (ref cursor)) {
+                               results.add (symbol);
+                       }
 
                        line = cursor.line;
                        column = cursor.column;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]