4 // not sure why - but extending Gtk.SourceCompletionProvider seems to give an error..
7 public class CompletionProvider : Object, SourceCompletionProvider
10 WindowState windowstate;
11 //public List<Gtk.SourceCompletionItem> filtered_proposals;
13 public CompletionProvider(Editor editor)
16 this.windowstate = null; // not ready until the UI is built.
20 public string get_name ()
22 return "roojsbuilder";
25 public int get_priority ()
30 public bool match (SourceCompletionContext context)
32 bool has_matches = false;
33 this.fetchMatches(context, out has_matches);
37 public List<SourceCompletionItem>? fetchMatches(SourceCompletionContext context, out bool has_matches)
41 if (this.windowstate == null) {
42 this.windowstate = this.editor.window.windowstate;
46 var buffer = context.completion.view.buffer;
47 var mark = buffer.get_insert ();
50 buffer.get_iter_at_mark (out end, mark);
53 var searchpos = endpos;
55 searchpos.backward_find_char(is_space, null);
56 searchpos.forward_char();
57 var search = endpos.get_text(searchpos);
58 print("got search %s\n", search);
60 if (search.length < 2) {
65 var filtered_proposals = this.windowstate.file.palete().suggestComplete(
66 this.windowstate.file,
73 print("GOT %d results\n", (int) filtered_proposals.length());
75 if (filtered_proposals.length() < 2) {
79 filtered_proposals.sort((a, b) => {
80 return ((string)(a.text)).collate((string)(b.text));
83 return filtered_proposals;
87 public void populate (SourceCompletionContext context)
89 bool has_matches = false;
90 var filtered_proposals = this.fetchMatches(context, out has_matches);
92 context.add_proposals (this, null, true);
95 // add proposals triggers a critical error in Gtk - try running gtksourceview/tests/test-completion.
96 // see https://bugzilla.gnome.org/show_bug.cgi?id=758646
97 var fe = GLib.Log.set_always_fatal(0);
98 context.add_proposals (this, filtered_proposals, true);
99 GLib.Log.set_always_fatal(fe);
104 public bool activate_proposal (SourceCompletionProposal proposal, TextIter iter)
107 istart.backward_find_char(is_space, null);
108 istart.forward_char();
110 // var search = iter.get_text(istart);
112 var buffer = iter.get_buffer();
113 buffer.delete(ref istart, ref iter);
114 buffer.insert(ref istart, proposal.get_text(), -1);
119 public SourceCompletionActivation get_activation ()
121 //if(SettingsManager.Get_Setting("complete_auto") == "true"){
122 return SourceCompletionActivation.INTERACTIVE | SourceCompletionActivation.USER_REQUESTED;
124 // return Gtk.SourceCompletionActivation.USER_REQUESTED;
128 public int get_interactive_delay ()
133 public bool get_start_iter (SourceCompletionContext context, SourceCompletionProposal proposal, out TextIter iter)
138 public void update_info (SourceCompletionProposal proposal, SourceCompletionInfo info)
143 private bool is_space(unichar space){
144 return space.isspace() || space.to_string() == "";
147 private bool is_forward_space(unichar space){
149 space.to_string() == " "
151 space.to_string() == ""
153 space.to_string() == "\n"
155 space.to_string() == ")"
157 space.to_string() == "("