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;
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)
36 public void activate (GtkSource.CompletionContext context, GtkSource.CompletionProposal proposal)
38 GLib.debug("compelte activate");
40 var p = (CompletionProposal) proposal;
41 GLib.debug("lsp says use %s", p.ci.insertText);
44 global::Gtk.TextMark end_mark = null;
45 global::Gtk.TextIter begin, end;
47 if (!context.get_bounds(out begin, out end)) {
50 var buffer = begin.get_buffer();
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(",");
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) {
66 var ty = wbit[wbit.length - 2];
67 ty = ty.has_suffix("?") ? "?" : "";
68 word += ty + wbit[wbit.length-1]; // property type..?
70 word += args.length > 0 ? " )" : ")";
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.
80 if (!end.ends_line() &&
81 !end.get_char().isspace() &&
86 if (word_end.forward_word_end ()) {
87 var text = end.get_slice(word_end);
88 if (text.length > word.length) {
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);
99 buffer.begin_user_action();
100 buffer.delete (ref begin, ref end);
101 buffer.insert ( ref begin, word, len);
102 buffer.end_user_action ();
104 if (end_mark != null)
106 buffer.get_iter_at_mark(out end, end_mark);
107 buffer.select_range(end, end);
108 buffer.delete_mark(end_mark);
115 public void display (GtkSource.CompletionContext context, GtkSource.CompletionProposal proposal, GtkSource.CompletionCell cell)
117 //GLib.debug("compelte display");
118 var col = cell.get_column();
120 var p = (CompletionProposal) proposal;
122 case GtkSource.CompletionColumn.TYPED_TEXT:
123 cell.set_text(p.label);
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;
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;
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;
163 cell.set_icon_name("completion-snippet-symbolic");
168 case GtkSource.CompletionColumn.COMMENT:
169 cell.set_text(p.text);
171 case GtkSource.CompletionColumn.DETAILS:
172 if (p.ci.documentation != null) {
173 cell.set_text(p.ci.documentation.value);
177 cell.set_text(p.text);
185 bool in_populate = false;
187 internal async GLib.ListModel populate_async (GtkSource.CompletionContext context, GLib.Cancellable? cancellable)
189 GLib.debug("pupoulate async");
190 /*if (!this.in_populate) {
191 GLib.debug("pupoulate async - skipped waiting for reply");
194 this.in_populate = true;
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);
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) {
214 yield this.file.getLanguageServer().document_change_force(this.file, this.editor.tempFileContents());
216 GLib.debug("sending request to language server %s", this.file.getLanguageServer().get_type().name());
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);
228 GLib.debug("pupoulate async - got reply");
229 this.model = new CompletionModel(this, context, res, cancellable);
230 var word = context.get_word();
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;
246 internal void refilter (GtkSource.CompletionContext context, GLib.ListModel in_model)
249 //GLib.debug("pupoulate refilter");
252 var word = context.get_word();
253 this.filter.set_search(word);
260 public bool activate_proposal (GtkSource.CompletionProposal proposal, TextIter iter)
263 istart.backward_find_char(is_space, null);
264 istart.forward_char();
266 // var search = iter.get_text(istart);
268 var buffer = iter.get_buffer();
269 buffer.delete(ref istart, ref iter);
270 buffer.insert(ref istart, proposal.get_text(), -1);
277 private bool is_space(unichar space){
278 return space.isspace() || space.to_string() == "";
283 public class CompletionModel : Object, GLib.ListModel
285 CompletionProvider provider;
286 Gee.ArrayList<CompletionProposal> items;
288 int minimum_word_size = 2;
290 public Cancellable? cancellable;
292 public CompletionModel(CompletionProvider provider, GtkSource.CompletionContext context, Lsp.CompletionList? res, Cancellable? cancellable)
294 this.provider = provider;
295 this.cancellable = cancellable;
296 this.items = new Gee.ArrayList<CompletionProposal>();
298 var word = context.get_word();
299 GLib.debug("looking for %s", word);
302 foreach(var comp in res.items) {
304 this.items.add(new CompletionProposal(comp));
308 print("GOT %d results\n", (int) items.size);
310 if (this.items.size < this.minimum_word_size) {
314 items.sort((a, b) => {
315 return ((string)(a.label)).collate((string)(b.label));
322 public GLib.Object? get_item (uint pos)
324 return (Object) this.items.get((int) pos);
326 public GLib.Type get_item_type ()
328 return typeof(GtkSource.CompletionProposal);
330 public uint get_n_items ()
332 return this.items.size;
334 public bool can_filter (string word)
336 if (word == null || word[0] == 0) {
340 if (word.length < this.minimum_word_size) {
344 /* If the new word starts with our initial word, then we can simply
345 * refilter outside this model using a GtkFilterListModel.
348 return word.has_prefix(this.search);
350 public void cancel ()
352 if (this.cancellable != null) {
353 this.cancellable.cancel();
360 public class CompletionProposal : Object, GtkSource.CompletionProposal
363 public string label { get; set; default = ""; }
365 public string text { get; set; default = ""; }
366 public string info { get; set; default = ""; }
368 public Lsp.CompletionItem ci;
370 public CompletionProposal(Lsp.CompletionItem ci) //string label, string text, string info)
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..");