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 var ty = wbit[wbit.length - 2];
63 ty = ty.has_suffix("?") ? "?" : "";
64 word += ty + wbit[wbit.length-1]; // property type..?
66 word += args.length > 0 ? " )" : ")";
72 /* If the insertion cursor is within a word and the trailing characters
73 * of the word match the suffix of the proposal, then limit how much
74 * text we insert so that the word is completed properly.
76 if (!end.ends_line() &&
77 !end.get_char().isspace() &&
82 if (word_end.forward_word_end ()) {
83 var text = end.get_slice(word_end);
84 if (text.length > word.length) {
87 if (word.has_suffix (text)) {
88 //g_assert (strlen (word) >= strlen (text));
89 len = word.length - text.length;
90 end_mark = buffer.create_mark (null, word_end, false);
95 buffer.begin_user_action();
96 buffer.delete (ref begin, ref end);
97 buffer.insert ( ref begin, word, len);
98 buffer.end_user_action ();
100 if (end_mark != null)
102 buffer.get_iter_at_mark(out end, end_mark);
103 buffer.select_range(end, end);
104 buffer.delete_mark(end_mark);
111 public void display (GtkSource.CompletionContext context, GtkSource.CompletionProposal proposal, GtkSource.CompletionCell cell)
113 //GLib.debug("compelte display");
114 var col = cell.get_column();
116 var p = (CompletionProposal) proposal;
118 case GtkSource.CompletionColumn.TYPED_TEXT:
119 cell.set_text(p.label);
121 case GtkSource.CompletionColumn.ICON:
122 //cell.set_icon_name("lang-define-symbolic");return;
123 //cell.set_icon_name("lang-include-symbolic");return;
124 //cell.set_icon_name("lang-typedef-symbolic");return;
125 //cell.set_icon_name("lang-union-symbolic");return;
128 case Lsp.CompletionItemKind.Text: cell.set_icon_name("completion-snippet-symbolic");return;
129 case Lsp.CompletionItemKind.Method: cell.set_icon_name("lang-method-symbolic");return;
130 case Lsp.CompletionItemKind.Function: cell.set_icon_name("lang-function-symbolic");return;
131 case Lsp.CompletionItemKind.Constructor: cell.set_icon_name("lang-method-symbolic");return;
132 case Lsp.CompletionItemKind.Field: cell.set_icon_name("lang-struct-field-symbolic");return;
133 case Lsp.CompletionItemKind.Variable: cell.set_icon_name("lang-variable-symbolic");return;
134 case Lsp.CompletionItemKind.Class: cell.set_icon_name("lang-class-symbolic");return;
135 case Lsp.CompletionItemKind.Interface: cell.set_icon_name("lang-class-symbolic");return;
136 case Lsp.CompletionItemKind.Module: cell.set_icon_name("lang-namespace-symbolic");return;
137 case Lsp.CompletionItemKind.Property:cell.set_icon_name("lang-struct-field-symbolic");return;
138 case Lsp.CompletionItemKind.Unit: cell.set_icon_name("lang-variable-symbolic");return;
139 case Lsp.CompletionItemKind.Value: cell.set_icon_name("lang-variable-symbolic");return;
140 case Lsp.CompletionItemKind.Enum: cell.set_icon_name("lang-enum-symbolic");return;
141 case Lsp.CompletionItemKind.Keyword: cell.set_icon_name("completion-word-symbolic");return;
142 case Lsp.CompletionItemKind.Snippet: cell.set_icon_name("completion-snippet-symbolic");return;
144 case Lsp.CompletionItemKind.Color: cell.set_icon_name("lang-typedef-symbolic");return;
145 case Lsp.CompletionItemKind.File:cell.set_icon_name("lang-typedef-symbolic");return;
146 case Lsp.CompletionItemKind.Reference: cell.set_icon_name("lang-typedef-symbolic");return;
147 case Lsp.CompletionItemKind.Folder:cell.set_icon_name("lang-typedef-symbolic");return;
148 case Lsp.CompletionItemKind.EnumMember: cell.set_icon_name("lang-typedef-symbolic");return;
149 case Lsp.CompletionItemKind.Constant:cell.set_icon_name("lang-typedef-symbolic");return;
150 case Lsp.CompletionItemKind.Struct: cell.set_icon_name("lang-struct-symbolic");return;
151 case Lsp.CompletionItemKind.Event:cell.set_icon_name("lang-typedef-symbolic");return;
152 case Lsp.CompletionItemKind.Operator:cell.set_icon_name("lang-typedef-symbolic");return;
153 case Lsp.CompletionItemKind.TypeParameter:cell.set_icon_name("lang-typedef-symbolic");return;
159 cell.set_icon_name("completion-snippet-symbolic");
164 case GtkSource.CompletionColumn.COMMENT:
165 cell.set_text(p.text);
167 case GtkSource.CompletionColumn.DETAILS:
168 if (p.ci.documentation != null) {
169 cell.set_text(p.ci.documentation.value);
173 cell.set_text(p.text);
181 bool in_populate = false;
183 internal async GLib.ListModel populate_async (GtkSource.CompletionContext context, GLib.Cancellable? cancellable)
185 GLib.debug("pupoulate async");
186 /*if (!this.in_populate) {
187 GLib.debug("pupoulate async - skipped waiting for reply");
190 this.in_populate = true;
192 global::Gtk.TextIter begin, end;
193 Lsp.CompletionList res;
194 if (context.get_bounds (out begin, out end)) {
195 var line = end.get_line();
196 var offset = end.get_line_offset();
197 if (this.editor.prop != null) {
198 // tried line -1 (does not work)
199 GLib.debug("node pad = '%s' %d", this.editor.node.node_pad, this.editor.node.node_pad.length);
201 line += this.editor.prop.start_line ;
202 // this is based on Gtk using tabs (hence 1/2 chars);
203 offset += this.editor.node.node_pad.length;
204 // javascript listeners are indented 2 more spaces.
205 if (this.editor.prop.ptype == JsRender.NodePropType.LISTENER) {
210 yield this.file.getLanguageServer().document_change_force(this.file, this.editor.tempFileContents());
212 GLib.debug("sending request to language server %s", this.file.getLanguageServer().get_type().name());
214 res = yield this.file.getLanguageServer().completion(this.file, line, offset, 1);
215 } catch (GLib.Error e) {
216 GLib.debug("got error %s", e.message);
224 GLib.debug("pupoulate async - got reply");
225 this.model = new CompletionModel(this, context, res, cancellable);
226 var word = context.get_word();
229 var expression = new global::Gtk.PropertyExpression(typeof(CompletionProposal), null, "label");
230 this.filter = new global::Gtk.StringFilter(expression);
231 this.filter.set_search( word);
232 var filter_model = new global::Gtk.FilterListModel(this.model, this.filter);
233 filter.match_mode = global::Gtk.StringFilterMatchMode.PREFIX;
234 filter_model.set_incremental(true);
235 this.in_populate = false;
242 internal void refilter (GtkSource.CompletionContext context, GLib.ListModel in_model)
245 //GLib.debug("pupoulate refilter");
248 var word = context.get_word();
249 this.filter.set_search(word);
256 public bool activate_proposal (GtkSource.CompletionProposal proposal, TextIter iter)
259 istart.backward_find_char(is_space, null);
260 istart.forward_char();
262 // var search = iter.get_text(istart);
264 var buffer = iter.get_buffer();
265 buffer.delete(ref istart, ref iter);
266 buffer.insert(ref istart, proposal.get_text(), -1);
273 private bool is_space(unichar space){
274 return space.isspace() || space.to_string() == "";
279 public class CompletionModel : Object, GLib.ListModel
281 CompletionProvider provider;
282 Gee.ArrayList<CompletionProposal> items;
284 int minimum_word_size = 2;
286 public Cancellable? cancellable;
288 public CompletionModel(CompletionProvider provider, GtkSource.CompletionContext context, Lsp.CompletionList? res, Cancellable? cancellable)
290 this.provider = provider;
291 this.cancellable = cancellable;
292 this.items = new Gee.ArrayList<CompletionProposal>();
294 var word = context.get_word();
295 GLib.debug("looking for %s", word);
298 foreach(var comp in res.items) {
300 this.items.add(new CompletionProposal(comp));
304 print("GOT %d results\n", (int) items.size);
306 if (this.items.size < this.minimum_word_size) {
310 items.sort((a, b) => {
311 return ((string)(a.label)).collate((string)(b.label));
318 public GLib.Object? get_item (uint pos)
320 return (Object) this.items.get((int) pos);
322 public GLib.Type get_item_type ()
324 return typeof(GtkSource.CompletionProposal);
326 public uint get_n_items ()
328 return this.items.size;
330 public bool can_filter (string word)
332 if (word == null || word[0] == 0) {
336 if (word.length < this.minimum_word_size) {
340 /* If the new word starts with our initial word, then we can simply
341 * refilter outside this model using a GtkFilterListModel.
344 return word.has_prefix(this.search);
346 public void cancel ()
348 if (this.cancellable != null) {
349 this.cancellable.cancel();
356 public class CompletionProposal : Object, GtkSource.CompletionProposal
359 public string label { get; set; default = ""; }
361 public string text { get; set; default = ""; }
362 public string info { get; set; default = ""; }
364 public Lsp.CompletionItem ci;
366 public CompletionProposal(Lsp.CompletionItem ci) //string label, string text, string info)
370 this.text = ci.detail == null ? "" : ci.detail ;
371 this.label = ci.label;
372 this.info = ci.documentation == null ? "": ci.documentation.value;
373 //GLib.debug("SET: detail =%s, label = %s; info =%s", ci.detail, ci.label, "to long..");