src/Palete/Palete.vala
[app.Builder.js] / src / Palete / CompletionProvider.vala
index 766ed7a..36b9c99 100644 (file)
  
+using Gtk;
 
+// not sure why - but extending Gtk.SourceCompletionProvider seems to give an error..
 namespace Palete {
 
-    class CompletionProvider : Gtk.SourceCompletionProvider, Object
+    public class CompletionProvider : Object, SourceCompletionProvider
     {
-               
-                public List<Gtk.SourceCompletionItem> proposals;
-               //public List<Gtk.SourceCompletionItem> filtered_proposals;
+               Editor editor; 
+               WindowState windowstate;
+               //public List<Gtk.SourceCompletionItem> filtered_proposals;
 
-               construct
+               public CompletionProvider(Editor editor)
                {
-                  
-                       this.proposals = new List<Gtk.SourceCompletionItem> ();
-               }
+                   this.editor  = editor;
+                   this.windowstate = null; // not ready until the UI is built.
+                   
+               }
 
                public string get_name ()
                {
-                 return  "test";
+                 return  "roojsbuilder";
                }
 
                public int get_priority ()
                {
-                 return 1;
+                 return 200;
                }
 
-               public bool match (Gtk.SourceCompletionContext context)
+               public bool match (SourceCompletionContext context)
                {
-               
-                       return true;
+                       bool has_matches = false;
+                       this.fetchMatches(context, out has_matches);
+                       return has_matches;
                }
 
-               public void populate (Gtk.SourceCompletionContext context)
+               public List<SourceCompletionItem>? fetchMatches(SourceCompletionContext context, out bool has_matches)
                {
-                       var buffer = context.completion.view.buffer;
-                       var  mark = buffer.get_insert ();
-                       TextIter end;
-
-                       buffer.get_iter_at_mark (out end, mark);
-                       var endpos = end;
-                       
-                       var searchpos = endpos;
-                       
-                       searchpos.backward_find_char(is_space, null);
-                       searchpos.forward_char();
-                       var search = endpos.get_text(searchpos);
-                       print("got search %s\n");
-                       
-
-                       var filtered_proposals = new List<Gtk.SourceCompletionItem> ();
-                       foreach(var i in this.proposals) {
-                               //if(i.text.contains(search)) // starts??
-                               //      this.filtered_proposals.prepend (new Gtk.SourceCompletionItem (i.label, i.text, i.icon, i.info));
-                               //}
+                    has_matches = false;
+
+                   if (this.windowstate == null) {
+                           this.windowstate = this.editor.window.windowstate;
+                   }
+               
+               
+                   var buffer = context.completion.view.buffer;
+                   var  mark = buffer.get_insert ();
+                   TextIter end;
+
+                   buffer.get_iter_at_mark (out end, mark);
+                   var endpos = end;
+               
+                   var searchpos = endpos;
+               
+                   searchpos.backward_find_char(is_space, null);
+                   searchpos.forward_char();
+                   var search = endpos.get_text(searchpos);
+                   print("got search %s\n", search);
+               
+                   if (search.length < 2) {
+                           return null;
+                   }
+               
+                   // now do our magic..
+                   var filtered_proposals = this.windowstate.file.palete().suggestComplete(
+                           this.windowstate.file,
+                           this.editor.node,
+                           this.editor.ptype,
+                           this.editor.key,
+                           search
+                   );
+               
+                   print("GOT %d results\n", (int) filtered_proposals.length()); 
+               
+                   if (filtered_proposals.length() < 2) {
+                       return null;
+                   }
+               
+                   filtered_proposals.sort((a, b) => {
+                           return ((string)(a.text)).collate((string)(b.text));
+                   });
+                   has_matches = true;
+                   return filtered_proposals;
+
+               }
+       
+               public void populate (SourceCompletionContext context)
+               {
+                       bool has_matches = false;
+                       var filtered_proposals = this.fetchMatches(context, out has_matches);
+                       if (!has_matches) {
+                           context.add_proposals (this, null, true);
+                           return;
                        }
+                       // add proposals triggers a critical error in Gtk - try running gtksourceview/tests/test-completion.
+                       // see https://bugzilla.gnome.org/show_bug.cgi?id=758646
+                       var fe = GLib.Log.set_always_fatal(0); 
                        context.add_proposals (this, filtered_proposals, true);
+                       GLib.Log.set_always_fatal(fe);
                }
 
-               public bool activate_proposal (Gtk.SourceCompletionProposal proposal, Gtk.TextIter iter)
+
+
+               public bool activate_proposal (SourceCompletionProposal proposal, TextIter iter)
                {
                        var istart = iter;
                        istart.backward_find_char(is_space, null);
@@ -71,10 +116,10 @@ namespace Palete {
                        return true;
                }
 
-               public Gtk.SourceCompletionActivation get_activation ()
+               public SourceCompletionActivation get_activation ()
                {
                        //if(SettingsManager.Get_Setting("complete_auto") == "true"){
-                               return Gtk.SourceCompletionActivation.INTERACTIVE | Gtk.SourceCompletionActivation.USER_REQUESTED;
+                               return SourceCompletionActivation.INTERACTIVE | SourceCompletionActivation.USER_REQUESTED;
                        //} else {
                        //      return Gtk.SourceCompletionActivation.USER_REQUESTED;
                        //}
@@ -85,12 +130,12 @@ namespace Palete {
                        return -1;
                }
 
-               public bool get_start_iter (Gtk.SourceCompletionContext context, Gtk.SourceCompletionProposal proposal, Gtk.TextIter iter)
+               public bool get_start_iter (SourceCompletionContext context, SourceCompletionProposal proposal, out TextIter iter)
                {
                        return false;
                }
 
-               public void update_info (Gtk.SourceCompletionProposal proposal, Gtk.SourceCompletionInfo info)
+               public void update_info (SourceCompletionProposal proposal, SourceCompletionInfo info)
                {
 
                }
@@ -117,3 +162,4 @@ namespace Palete {
 
 
 } 
+