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