4 // not sure why - but extending Gtk.SourceCompletionProvider seems to give an error..
7 public class CompletionProvider : Object, GtkSource.CompletionProvider
9 public JsRender.JsRender file {
10 get { return this.editor.file; }
14 //public WindowState windowstate;
15 public CompletionModel model;
16 global::Gtk.StringFilter? filter = null;
18 public CompletionProvider(Editor editor)
22 // this.windowstate = null; // not ready until the UI is built.
26 public string get_name ()
28 return "roojsbuilder";
31 public int get_priority (GtkSource.CompletionContext context)
37 public bool is_trigger(global::Gtk.TextIter iter, unichar ch)
39 if (this.in_populate || ch == 32) {
42 GLib.debug("should trigger? %c", (int) ch);
48 public void activate (GtkSource.CompletionContext context, GtkSource.CompletionProposal proposal)
50 GLib.debug("compelte activate");
52 var p = (CompletionProposal) proposal;
53 GLib.debug("lsp says use %s", p.ci.insertText);
56 global::Gtk.TextMark end_mark = null;
57 global::Gtk.TextIter begin, end;
59 if (!context.get_bounds(out begin, out end)) {
62 var buffer = begin.get_buffer();
65 if (p.ci.kind == Lsp.CompletionItemKind.Method || p.ci.kind == Lsp.CompletionItemKind.Function) {
66 var bits = p.text.split("(");
67 var abits = bits[1].split(")");
68 var args = abits[0].split(",");
71 for(var i = 0 ; i < args.length; i++) {
72 word += i > 0 ? ", " : " ";
73 var wbit = args[i].strip().split(" ");
74 if (wbit.length < 2) {
78 var ty = wbit[wbit.length - 2];
79 ty = ty.has_suffix("?") ? "?" : "";
80 word += ty + wbit[wbit.length-1]; // property type..?
82 word += args.length > 0 ? " )" : ")";
88 /* If the insertion cursor is within a word and the trailing characters
89 * of the word match the suffix of the proposal, then limit how much
90 * text we insert so that the word is completed properly.
92 if (!end.ends_line() &&
93 !end.get_char().isspace() &&
98 if (word_end.forward_word_end ()) {
99 var text = end.get_slice(word_end);
100 if (text.length > word.length) {
103 if (word.has_suffix (text)) {
104 //g_assert (strlen (word) >= strlen (text));
105 len = word.length - text.length;
106 end_mark = buffer.create_mark (null, word_end, false);
111 buffer.begin_user_action();
112 buffer.delete (ref begin, ref end);
113 buffer.insert ( ref begin, word, len);
114 buffer.end_user_action ();
116 if (end_mark != null)
118 buffer.get_iter_at_mark(out end, end_mark);
119 buffer.select_range(end, end);
120 buffer.delete_mark(end_mark);
127 public void display (GtkSource.CompletionContext context, GtkSource.CompletionProposal proposal, GtkSource.CompletionCell cell)
129 //GLib.debug("compelte display");
130 var col = cell.get_column();
132 var p = (CompletionProposal) proposal;
134 case GtkSource.CompletionColumn.TYPED_TEXT:
135 cell.set_text(p.label);
137 case GtkSource.CompletionColumn.ICON:
138 //cell.set_icon_name("lang-define-symbolic");return;
139 //cell.set_icon_name("lang-include-symbolic");return;
140 //cell.set_icon_name("lang-typedef-symbolic");return;
141 //cell.set_icon_name("lang-union-symbolic");return;
144 case Lsp.CompletionItemKind.Text: cell.set_icon_name("completion-snippet-symbolic");return;
145 case Lsp.CompletionItemKind.Method: cell.set_icon_name("lang-method-symbolic");return;
146 case Lsp.CompletionItemKind.Function: cell.set_icon_name("lang-function-symbolic");return;
147 case Lsp.CompletionItemKind.Constructor: cell.set_icon_name("lang-method-symbolic");return;
148 case Lsp.CompletionItemKind.Field: cell.set_icon_name("lang-struct-field-symbolic");return;
149 case Lsp.CompletionItemKind.Variable: cell.set_icon_name("lang-variable-symbolic");return;
150 case Lsp.CompletionItemKind.Class: cell.set_icon_name("lang-class-symbolic");return;
151 case Lsp.CompletionItemKind.Interface: cell.set_icon_name("lang-class-symbolic");return;
152 case Lsp.CompletionItemKind.Module: cell.set_icon_name("lang-namespace-symbolic");return;
153 case Lsp.CompletionItemKind.Property:cell.set_icon_name("lang-struct-field-symbolic");return;
154 case Lsp.CompletionItemKind.Unit: cell.set_icon_name("lang-variable-symbolic");return;
155 case Lsp.CompletionItemKind.Value: cell.set_icon_name("lang-variable-symbolic");return;
156 case Lsp.CompletionItemKind.Enum: cell.set_icon_name("lang-enum-symbolic");return;
157 case Lsp.CompletionItemKind.Keyword: cell.set_icon_name("completion-word-symbolic");return;
158 case Lsp.CompletionItemKind.Snippet: cell.set_icon_name("completion-snippet-symbolic");return;
160 case Lsp.CompletionItemKind.Color: cell.set_icon_name("lang-typedef-symbolic");return;
161 case Lsp.CompletionItemKind.File:cell.set_icon_name("lang-typedef-symbolic");return;
162 case Lsp.CompletionItemKind.Reference: cell.set_icon_name("lang-typedef-symbolic");return;
163 case Lsp.CompletionItemKind.Folder:cell.set_icon_name("lang-typedef-symbolic");return;
164 case Lsp.CompletionItemKind.EnumMember: cell.set_icon_name("lang-typedef-symbolic");return;
165 case Lsp.CompletionItemKind.Constant:cell.set_icon_name("lang-typedef-symbolic");return;
166 case Lsp.CompletionItemKind.Struct: cell.set_icon_name("lang-struct-symbolic");return;
167 case Lsp.CompletionItemKind.Event:cell.set_icon_name("lang-typedef-symbolic");return;
168 case Lsp.CompletionItemKind.Operator:cell.set_icon_name("lang-typedef-symbolic");return;
169 case Lsp.CompletionItemKind.TypeParameter:cell.set_icon_name("lang-typedef-symbolic");return;
175 cell.set_icon_name("completion-snippet-symbolic");
180 case GtkSource.CompletionColumn.COMMENT:
181 cell.set_text(p.text);
183 case GtkSource.CompletionColumn.DETAILS:
184 if (p.ci.documentation != null) {
185 cell.set_text(p.ci.documentation.value);
189 cell.set_text(p.text);
197 bool in_populate = false;
199 internal async GLib.ListModel populate_async (GtkSource.CompletionContext context, GLib.Cancellable? cancellable)
201 GLib.debug("pupoulate async");
202 var ret = new GLib.ListStore(typeof(CompletionProposal));
204 if (this.in_populate) {
205 GLib.debug("pupoulate async - skipped waiting for reply");
208 this.in_populate = true;
210 global::Gtk.TextIter begin, end;
211 Lsp.CompletionList res;
212 if (context.get_bounds (out begin, out end)) {
213 var line = end.get_line();
214 var offset = end.get_line_offset();
215 if (this.editor.prop != null) {
216 // tried line -1 (does not work)
217 GLib.debug("node pad = '%s' %d", this.editor.node.node_pad, this.editor.node.node_pad.length);
219 line += this.editor.prop.start_line ;
220 // this is based on Gtk using tabs (hence 1/2 chars);
221 offset += this.editor.node.node_pad.length;
222 // javascript listeners are indented 2 more spaces.
223 if (this.editor.prop.ptype == JsRender.NodePropType.LISTENER) {
227 // this should not really be slow, as it's a quick repsonse
228 yield this.file.getLanguageServer().document_change_force(this.file, this.editor.tempFileContents());
230 GLib.debug("sending request to language server %s", this.file.getLanguageServer().get_type().name());
232 res = yield this.file.getLanguageServer().completion(this.file, line, offset, 1);
233 } catch (GLib.Error e) {
234 GLib.debug("got error %s", e.message);
235 this.in_populate = false;
240 this.in_populate = false;
244 GLib.debug("pupoulate async - got reply");
245 this.model = new CompletionModel(this, context, res, cancellable);
246 var word = context.get_word();
249 var expression = new global::Gtk.PropertyExpression(typeof(CompletionProposal), null, "label");
250 this.filter = new global::Gtk.StringFilter(expression);
251 this.filter.set_search( word);
252 var filter_model = new global::Gtk.FilterListModel(this.model, this.filter);
253 filter.match_mode = global::Gtk.StringFilterMatchMode.PREFIX;
254 filter_model.set_incremental(true);
255 this.in_populate = false;
262 internal void refilter (GtkSource.CompletionContext context, GLib.ListModel in_model)
265 //GLib.debug("pupoulate refilter");
266 if (this.filter == null) {
270 var word = context.get_word();
271 this.filter.set_search(word);
278 public bool activate_proposal (GtkSource.CompletionProposal proposal, TextIter iter)
281 istart.backward_find_char(is_space, null);
282 istart.forward_char();
284 // var search = iter.get_text(istart);
286 var buffer = iter.get_buffer();
287 buffer.delete(ref istart, ref iter);
288 buffer.insert(ref istart, proposal.get_text(), -1);
295 private bool is_space(unichar space){
296 return space.isspace() || space.to_string() == "";
301 public class CompletionModel : Object, GLib.ListModel
303 CompletionProvider provider;
304 Gee.ArrayList<CompletionProposal> items;
306 int minimum_word_size = 2;
308 public Cancellable? cancellable;
310 public CompletionModel(CompletionProvider provider, GtkSource.CompletionContext context, Lsp.CompletionList? res, Cancellable? cancellable)
312 this.provider = provider;
313 this.cancellable = cancellable;
314 this.items = new Gee.ArrayList<CompletionProposal>();
316 var word = context.get_word();
317 GLib.debug("looking for %s", word);
320 foreach(var comp in res.items) {
322 this.items.add(new CompletionProposal(comp));
326 print("GOT %d results\n", (int) items.size);
328 if (this.items.size < this.minimum_word_size) {
332 items.sort((a, b) => {
333 return ((string)(a.label)).collate((string)(b.label));
340 public GLib.Object? get_item (uint pos)
342 return (Object) this.items.get((int) pos);
344 public GLib.Type get_item_type ()
346 return typeof(GtkSource.CompletionProposal);
348 public uint get_n_items ()
350 return this.items.size;
352 public bool can_filter (string word)
354 if (word == null || word[0] == 0) {
358 if (word.length < this.minimum_word_size) {
362 /* If the new word starts with our initial word, then we can simply
363 * refilter outside this model using a GtkFilterListModel.
366 return word.has_prefix(this.search);
368 public void cancel ()
370 if (this.cancellable != null) {
371 this.cancellable.cancel();
378 public class CompletionProposal : Object, GtkSource.CompletionProposal
381 public string label { get; set; default = ""; }
383 public string text { get; set; default = ""; }
384 public string info { get; set; default = ""; }
386 public Lsp.CompletionItem ci;
388 public CompletionProposal(Lsp.CompletionItem ci) //string label, string text, string info)
392 this.text = ci.detail == null ? "" : ci.detail ;
393 this.label = ci.label;
394 this.info = ci.documentation == null ? "": ci.documentation.value;
395 //GLib.debug("SET: detail =%s, label = %s; info =%s", ci.detail, ci.label, "to long..");