9ef4c63fbf2665a7f55184e416a0b266a921ab62
[roobuilder] / src / Palete / CompletionProvider.vala
1  
2 //using Gtk;
3
4 // not sure why - but extending Gtk.SourceCompletionProvider seems to give an error..
5 namespace Palete {
6
7     public class CompletionProvider : Object, GtkSource.CompletionProvider
8     {
9                 public JsRender.JsRender file {
10                         get { return this.editor.file; }
11                         private set {}
12                 }
13                 public Editor editor; 
14                 //public WindowState windowstate;
15                 public CompletionModel model;
16                 global::Gtk.StringFilter filter;
17
18                 public CompletionProvider(Editor editor)
19                 {
20                     this.editor  = editor;
21                  
22                    // this.windowstate = null; // not ready until the UI is built.
23                     
24                 }
25
26                 public string get_name ()
27                 {
28                   return  "roojsbuilder";
29                 }
30
31                 public int get_priority (GtkSource.CompletionContext context)
32                 {
33                   return 200;
34                 }
35                 
36                 public  void activate (GtkSource.CompletionContext context, GtkSource.CompletionProposal proposal)
37                 {
38                         GLib.debug("compelte activate");
39                         
40                         var  p = (CompletionProposal) proposal;
41                         GLib.debug("lsp says use %s", p.ci.insertText);
42                         
43                         
44                         global::Gtk.TextMark end_mark = null;
45                         global::Gtk.TextIter begin, end;
46
47                         if (!context.get_bounds(out begin, out end)) {
48                                 return;
49                         }  
50                         var buffer = begin.get_buffer();
51                 
52                         var  word = p.label;
53                         if (p.ci.kind == Lsp.CompletionItemKind.Method || p.ci.kind == Lsp.CompletionItemKind.Function) {
54                                 var bits = p.text.split("(");
55                                 var abits = bits[1].split(")");
56                                 var args = abits[0].split(",");
57                                 
58                                 word += "(";
59                                 for(var i = 0 ; i < args.length; i++) {
60                                         word += i > 0 ? ", " : " ";
61                                         var wbit = args[i].strip().split(" ");
62                                         if (wbit.length < 2) {
63                                                 word += wbit[0];
64                                                 continue;
65                                         }
66                                         var ty = wbit[wbit.length - 2];
67                                         ty = ty.has_suffix("?") ? "?" : "";  
68                                         word += ty + wbit[wbit.length-1]; // property type..?
69                                 }
70                                 word += args.length > 0 ? " )" : ")";
71                         }
72                         
73                         var len = -1;
74                         
75
76                         /* If the insertion cursor is within a word and the trailing characters
77                          * of the word match the suffix of the proposal, then limit how much
78                          * text we insert so that the word is completed properly.
79                          */
80                         if (!end.ends_line() &&
81                                 !end.get_char().isspace() &&
82                                 !end.ends_word ())
83                         {
84                                 var word_end = end;
85
86                                 if (word_end.forward_word_end ()) {
87                                         var text = end.get_slice(word_end);
88                                         if (text.length > word.length) {
89                                                 return;
90                                         }
91                                         if (word.has_suffix (text)) {
92                                                 //g_assert (strlen (word) >= strlen (text));
93                                                 len = word.length - text.length;
94                                                 end_mark = buffer.create_mark (null, word_end, false); 
95                                         }
96                                 }
97                         }
98
99                         buffer.begin_user_action();
100                         buffer.delete (ref begin, ref end);
101                         buffer.insert ( ref begin, word, len);
102                         buffer.end_user_action ();
103
104                         if (end_mark != null)
105                         {
106                                 buffer.get_iter_at_mark(out end, end_mark);
107                                 buffer.select_range(end,  end);
108                                 buffer.delete_mark(end_mark);
109                         }
110                 
111
112                 }
113
114
115                 public  void display (GtkSource.CompletionContext context, GtkSource.CompletionProposal proposal, GtkSource.CompletionCell cell)
116                 {
117                         //GLib.debug("compelte display");
118                         var col = cell.get_column();
119                         
120                         var p = (CompletionProposal) proposal;
121                         switch(col) {
122                                 case GtkSource.CompletionColumn.TYPED_TEXT:
123                                         cell.set_text(p.label);
124                                         break;
125                                 case GtkSource.CompletionColumn.ICON:
126 //cell.set_icon_name("lang-define-symbolic");return;
127 //cell.set_icon_name("lang-include-symbolic");return;
128 //cell.set_icon_name("lang-typedef-symbolic");return;
129 //cell.set_icon_name("lang-union-symbolic");return;                      
130                                         switch (p.ci.kind) {
131                                         
132                                                 case    Lsp.CompletionItemKind.Text: cell.set_icon_name("completion-snippet-symbolic");return;
133                                                 case    Lsp.CompletionItemKind.Method: cell.set_icon_name("lang-method-symbolic");return;
134                                                 case    Lsp.CompletionItemKind.Function: cell.set_icon_name("lang-function-symbolic");return;
135                                                 case    Lsp.CompletionItemKind.Constructor: cell.set_icon_name("lang-method-symbolic");return;
136                                                 case    Lsp.CompletionItemKind.Field: cell.set_icon_name("lang-struct-field-symbolic");return;
137                                                 case    Lsp.CompletionItemKind.Variable: cell.set_icon_name("lang-variable-symbolic");return;
138                                                 case    Lsp.CompletionItemKind.Class: cell.set_icon_name("lang-class-symbolic");return;
139                                                 case    Lsp.CompletionItemKind.Interface: cell.set_icon_name("lang-class-symbolic");return;
140                                                 case    Lsp.CompletionItemKind.Module: cell.set_icon_name("lang-namespace-symbolic");return;
141                                                 case    Lsp.CompletionItemKind.Property:cell.set_icon_name("lang-struct-field-symbolic");return;
142                                                 case    Lsp.CompletionItemKind.Unit: cell.set_icon_name("lang-variable-symbolic");return;
143                                                 case    Lsp.CompletionItemKind.Value: cell.set_icon_name("lang-variable-symbolic");return;
144                                                 case    Lsp.CompletionItemKind.Enum: cell.set_icon_name("lang-enum-symbolic");return;
145                                                 case    Lsp.CompletionItemKind.Keyword: cell.set_icon_name("completion-word-symbolic");return;
146                                                 case    Lsp.CompletionItemKind.Snippet: cell.set_icon_name("completion-snippet-symbolic");return;
147
148                                                 case    Lsp.CompletionItemKind.Color: cell.set_icon_name("lang-typedef-symbolic");return;
149                                                 case    Lsp.CompletionItemKind.File:cell.set_icon_name("lang-typedef-symbolic");return;
150                                                 case    Lsp.CompletionItemKind.Reference: cell.set_icon_name("lang-typedef-symbolic");return;
151                                                 case    Lsp.CompletionItemKind.Folder:cell.set_icon_name("lang-typedef-symbolic");return;
152                                                 case    Lsp.CompletionItemKind.EnumMember: cell.set_icon_name("lang-typedef-symbolic");return;
153                                                 case    Lsp.CompletionItemKind.Constant:cell.set_icon_name("lang-typedef-symbolic");return;
154                                                 case    Lsp.CompletionItemKind.Struct: cell.set_icon_name("lang-struct-symbolic");return;
155                                                 case    Lsp.CompletionItemKind.Event:cell.set_icon_name("lang-typedef-symbolic");return;
156                                                 case    Lsp.CompletionItemKind.Operator:cell.set_icon_name("lang-typedef-symbolic");return;
157                                                 case    Lsp.CompletionItemKind.TypeParameter:cell.set_icon_name("lang-typedef-symbolic");return;
158                                                 default:
159
160
161
162                                         
163                                                         cell.set_icon_name("completion-snippet-symbolic");
164                                                         return;
165                                                 }
166                                                 
167                                         
168                                 case  GtkSource.CompletionColumn.COMMENT:
169                                         cell.set_text(p.text);
170                                         break;
171                                 case GtkSource.CompletionColumn.DETAILS:
172                                         if (p.ci.documentation != null) {
173                                                 cell.set_text(p.ci.documentation.value);
174                                                 return;
175                                         }
176                                 
177                                         cell.set_text(p.text);
178                                         break;
179                                 default:
180                                         cell.set_text(null);
181                                         break;
182                         }       
183                 }
184
185                 bool in_populate = false;
186          
187                 internal  async GLib.ListModel populate_async (GtkSource.CompletionContext context, GLib.Cancellable? cancellable) 
188                 {
189                         GLib.debug("pupoulate async");
190                         /*if (!this.in_populate) {
191                                 GLib.debug("pupoulate async  - skipped waiting for reply");
192                                 return null;
193                         }
194                         this.in_populate = true;
195 */
196                         global::Gtk.TextIter begin, end;
197                         Lsp.CompletionList res;
198                         if (context.get_bounds (out begin, out end)) {
199                                 var line = end.get_line();
200                                 var offset =  end.get_line_offset();
201                                 if (this.editor.prop != null) {
202                                 //      tried line -1 (does not work)
203                                         GLib.debug("node pad = '%s' %d", this.editor.node.node_pad, this.editor.node.node_pad.length);
204                                         
205                                         line += this.editor.prop.start_line ; 
206                                         // this is based on Gtk using tabs (hence 1/2 chars);
207                                         offset += this.editor.node.node_pad.length;
208                                         // javascript listeners are indented 2 more spaces.
209                                         if (this.editor.prop.ptype == JsRender.NodePropType.LISTENER) {
210                                                 offset += 2;
211                                         }
212                                 } 
213                                 
214                                 yield this.file.getLanguageServer().document_change_force(this.file, this.editor.tempFileContents());                           
215                                 try {
216                                         GLib.debug("sending request to language server %s", this.file.getLanguageServer().get_type().name());
217                                         
218                                         res = yield this.file.getLanguageServer().completion(this.file, line, offset, 1);
219                                 } catch (GLib.Error e) {
220                                         GLib.debug("got error %s", e.message);
221                                         res = null;
222                                 }
223                                 
224                         } else {
225                                 res = null;
226                         }
227                         
228                         GLib.debug("pupoulate async  - got reply");
229                         this.model = new CompletionModel(this, context, res, cancellable); 
230                         var word = context.get_word();
231                         
232                         
233                         var expression = new global::Gtk.PropertyExpression(typeof(CompletionProposal), null, "label");
234                         this.filter = new global::Gtk.StringFilter(expression);
235                         this.filter.set_search( word);
236                         var  filter_model = new global::Gtk.FilterListModel(this.model, this.filter); 
237                         filter.match_mode = global::Gtk.StringFilterMatchMode.PREFIX;
238                         filter_model.set_incremental(true);
239                         this.in_populate = false;
240                         return filter_model; 
241                         
242                          
243                         
244                 }
245
246                 internal  void refilter (GtkSource.CompletionContext context, GLib.ListModel in_model)
247                 {
248  
249                         //GLib.debug("pupoulate refilter");
250          
251
252                         var word = context.get_word();
253                         this.filter.set_search(word);
254                  
255                 
256                 }
257
258
259 /*
260                 public bool activate_proposal (GtkSource.CompletionProposal proposal, TextIter iter)
261                 {
262                         var istart = iter;
263                         istart.backward_find_char(is_space, null);
264                         istart.forward_char();
265
266                 //    var search = iter.get_text(istart);           
267                 
268                         var buffer = iter.get_buffer();
269                         buffer.delete(ref istart, ref iter);
270                         buffer.insert(ref istart, proposal.get_text(), -1);
271                 
272                         return true;
273                 }
274   
275          
276
277                 private bool is_space(unichar space){
278                         return space.isspace() || space.to_string() == "";
279                 }
280                 */
281                  
282         }
283         public class CompletionModel : Object, GLib.ListModel 
284         {
285                 CompletionProvider provider;
286                 Gee.ArrayList<CompletionProposal> items;
287                 string search;
288                 int minimum_word_size = 2;
289                 
290                 public Cancellable? cancellable;
291                 
292                 public CompletionModel(CompletionProvider provider, GtkSource.CompletionContext context, Lsp.CompletionList? res, Cancellable? cancellable)
293                 {
294                         this.provider = provider;
295                         this.cancellable = cancellable;
296                         this.items = new Gee.ArrayList<CompletionProposal>();
297                         
298                         var word = context.get_word();
299                         GLib.debug("looking for %s", word);
300                         this.search = word;
301                         if (res != null) {
302                                 foreach(var comp in res.items) {
303                                          
304                                         this.items.add(new CompletionProposal(comp));   
305                                         
306                                 }
307                         }
308                     print("GOT %d results\n", (int) items.size); 
309                         // WHY TWICE?
310                     if (this.items.size < this.minimum_word_size) {
311                                 return;
312                     }
313                 
314                     items.sort((a, b) => {
315                             return ((string)(a.label)).collate((string)(b.label));
316                     });
317                 
318                 }
319                 
320                  
321                 
322                 public GLib.Object? get_item (uint pos)
323                 {
324                         return (Object) this.items.get((int) pos);
325                 }
326                 public GLib.Type  get_item_type ()
327                 {
328                         return typeof(GtkSource.CompletionProposal);
329                 }
330                 public   uint get_n_items () 
331                 {
332                         return this.items.size;
333                 }
334                 public bool can_filter (string word) 
335                 {
336                         if (word == null || word[0] == 0) {
337                                 return false;
338                         }
339  
340                         if (word.length < this.minimum_word_size) {
341                                 return false;
342                         }
343
344                         /* If the new word starts with our initial word, then we can simply
345                          * refilter outside this model using a GtkFilterListModel.
346                          */
347                          
348                          return word.has_prefix(this.search); 
349                 }
350                 public void  cancel ()
351                 {
352                         if (this.cancellable != null) {
353                                 this.cancellable.cancel();
354                         }
355                 }
356
357
358                 
359         }
360         public class CompletionProposal : Object, GtkSource.CompletionProposal 
361         {
362                 
363                 public string label { get; set; default = ""; }
364                 
365                 public string text  { get; set; default = ""; }
366                 public string info  { get; set; default = ""; }
367                 
368                 public Lsp.CompletionItem ci;
369                 
370                 public CompletionProposal(Lsp.CompletionItem ci) //string label, string text, string info)
371                 {
372                         
373                         this.ci = ci;
374                         this.text = ci.detail == null ? "" : ci.detail ;
375                         this.label = ci.label;
376                         this.info = ci.documentation == null ? "": ci.documentation.value;
377                         //GLib.debug("SET: detail =%s, label = %s; info =%s", ci.detail, ci.label, "to long..");
378                 }
379                 
380         }
381
382
383