4 // not sure why - but extending Gtk.SourceCompletionProvider seems to give an error..
7 public class CompletionProvider : Object, GtkSource.CompletionProvider
10 public JsRender.JsRender file {
11 get { return this.editor.file; }
15 //public WindowState windowstate;
16 public CompletionModel model;
17 global::Gtk.StringFilter filter;
19 public CompletionProvider(Editor editor)
23 // this.windowstate = null; // not ready until the UI is built.
27 public string get_name ()
29 return "roojsbuilder";
32 public int get_priority (GtkSource.CompletionContext context)
37 public void activate (GtkSource.CompletionContext context, GtkSource.CompletionProposal proposal)
39 GLib.debug("compelte activate");
41 var p = (CompletionProposal) proposal;
42 GLib.debug("lsp says use %s", p.ci.insertText);
45 global::Gtk.TextMark end_mark = null;
46 global::Gtk.TextIter begin, end;
48 if (!context.get_bounds(out begin, out end)) {
51 var buffer = begin.get_buffer();
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(",");
60 for(var i = 0 ; i < args.length; i++) {
61 word += i > 0 ? ", " : " ";
62 var wbit = args[i].split(" ");
65 word += args.length > 0 ? " )" : ")";
71 /* If the insertion cursor is within a word and the trailing characters
72 * of the word match the suffix of the proposal, then limit how much
73 * text we insert so that the word is completed properly.
75 if (!end.ends_line() &&
76 !end.get_char().isspace() &&
81 if (word_end.forward_word_end ()) {
82 var text = end.get_slice(word_end);
83 if (text.length > word.length) {
86 if (word.has_suffix (text)) {
87 //g_assert (strlen (word) >= strlen (text));
88 len = word.length - text.length;
89 end_mark = buffer.create_mark (null, word_end, false);
94 buffer.begin_user_action();
95 buffer.delete (ref begin, ref end);
96 buffer.insert ( ref begin, word, len);
97 buffer.end_user_action ();
101 buffer.get_iter_at_mark(out end, end_mark);
102 buffer.select_range(end, end);
103 buffer.delete_mark(end_mark);
110 public void display (GtkSource.CompletionContext context, GtkSource.CompletionProposal proposal, GtkSource.CompletionCell cell)
112 //GLib.debug("compelte display");
113 var col = cell.get_column();
115 var p = (CompletionProposal) proposal;
117 case GtkSource.CompletionColumn.TYPED_TEXT:
118 cell.set_text(p.label);
120 case GtkSource.CompletionColumn.ICON:
121 //cell.set_icon_name("lang-define-symbolic");return;
122 //cell.set_icon_name("lang-include-symbolic");return;
123 //cell.set_icon_name("lang-typedef-symbolic");return;
124 //cell.set_icon_name("lang-union-symbolic");return;
127 case Lsp.CompletionItemKind.Text: cell.set_icon_name("completion-snippet-symbolic");return;
128 case Lsp.CompletionItemKind.Method: cell.set_icon_name("lang-method-symbolic");return;
129 case Lsp.CompletionItemKind.Function: cell.set_icon_name("lang-function-symbolic");return;
130 case Lsp.CompletionItemKind.Constructor: cell.set_icon_name("lang-method-symbolic");return;
131 case Lsp.CompletionItemKind.Field: cell.set_icon_name("lang-struct-field-symbolic");return;
132 case Lsp.CompletionItemKind.Variable: cell.set_icon_name("lang-variable-symbolic");return;
133 case Lsp.CompletionItemKind.Class: cell.set_icon_name("lang-class-symbolic");return;
134 case Lsp.CompletionItemKind.Interface: cell.set_icon_name("lang-class-symbolic");return;
135 case Lsp.CompletionItemKind.Module: cell.set_icon_name("lang-namespace-symbolic");return;
136 case Lsp.CompletionItemKind.Property:cell.set_icon_name("lang-struct-field-symbolic");return;
137 case Lsp.CompletionItemKind.Unit: cell.set_icon_name("lang-variable-symbolic");return;
138 case Lsp.CompletionItemKind.Value: cell.set_icon_name("lang-variable-symbolic");return;
139 case Lsp.CompletionItemKind.Enum: cell.set_icon_name("lang-enum-symbolic");return;
140 case Lsp.CompletionItemKind.Keyword: cell.set_icon_name("completion-word-symbolic");return;
141 case Lsp.CompletionItemKind.Snippet: cell.set_icon_name("completion-snippet-symbolic");return;
143 case Lsp.CompletionItemKind.Color: cell.set_icon_name("lang-typedef-symbolic");return;
144 case Lsp.CompletionItemKind.File:cell.set_icon_name("lang-typedef-symbolic");return;
145 case Lsp.CompletionItemKind.Reference: cell.set_icon_name("lang-typedef-symbolic");return;
146 case Lsp.CompletionItemKind.Folder:cell.set_icon_name("lang-typedef-symbolic");return;
147 case Lsp.CompletionItemKind.EnumMember: cell.set_icon_name("lang-typedef-symbolic");return;
148 case Lsp.CompletionItemKind.Constant:cell.set_icon_name("lang-typedef-symbolic");return;
149 case Lsp.CompletionItemKind.Struct: cell.set_icon_name("lang-struct-symbolic");return;
150 case Lsp.CompletionItemKind.Event:cell.set_icon_name("lang-typedef-symbolic");return;
151 case Lsp.CompletionItemKind.Operator:cell.set_icon_name("lang-typedef-symbolic");return;
152 case Lsp.CompletionItemKind.TypeParameter:cell.set_icon_name("lang-typedef-symbolic");return;
158 cell.set_icon_name("completion-snippet-symbolic");
163 case GtkSource.CompletionColumn.COMMENT:
164 cell.set_text(p.text);
166 case GtkSource.CompletionColumn.DETAILS:
167 if (p.ci.documentation != null) {
168 cell.set_text(p.ci.documentation.value);
172 cell.set_text(p.text);
180 bool in_populate = false;
182 internal async GLib.ListModel populate_async (GtkSource.CompletionContext context, GLib.Cancellable? cancellable)
184 GLib.debug("pupoulate async");
185 /*if (!this.in_populate) {
186 GLib.debug("pupoulate async - skipped waiting for reply");
189 this.in_populate = true;
191 global::Gtk.TextIter begin, end;
192 Lsp.CompletionList res;
193 if (context.get_bounds (out begin, out end)) {
194 var line = end.get_line();
195 var offset = end.get_line_offset();
196 if (this.editor.prop != null) {
197 // tried line -1 (does not work)
199 line += this.editor.prop.start_line ;
200 // this is based on Gtk using tabs (hence 1/2 chars);
201 offset += this.editor.file.file_namespace == "" ? 1 : 2;
204 this.file.getLanguageServer().document_change_real(this.file, this.editor.tempFileContents());
206 yield this.file.getLanguageServer().completion(this.file, line, offset, 1, out res);
207 } catch (GLib.Error e) {
208 GLib.debug("got error %s", e.message);
216 GLib.debug("pupoulate async - got reply");
217 this.model = new CompletionModel(this, context, res, cancellable);
218 var word = context.get_word();
221 var expression = new global::Gtk.PropertyExpression(typeof(CompletionProposal), null, "label");
222 this.filter = new global::Gtk.StringFilter(expression);
223 this.filter.set_search( word);
224 var filter_model = new global::Gtk.FilterListModel(this.model, this.filter);
225 filter.match_mode = global::Gtk.StringFilterMatchMode.PREFIX;
226 filter_model.set_incremental(true);
227 this.in_populate = false;
234 internal void refilter (GtkSource.CompletionContext context, GLib.ListModel in_model)
237 //GLib.debug("pupoulate refilter");
240 var word = context.get_word();
241 this.filter.set_search(word);
248 public bool activate_proposal (GtkSource.CompletionProposal proposal, TextIter iter)
251 istart.backward_find_char(is_space, null);
252 istart.forward_char();
254 // var search = iter.get_text(istart);
256 var buffer = iter.get_buffer();
257 buffer.delete(ref istart, ref iter);
258 buffer.insert(ref istart, proposal.get_text(), -1);
265 private bool is_space(unichar space){
266 return space.isspace() || space.to_string() == "";
271 public class CompletionModel : Object, GLib.ListModel
273 CompletionProvider provider;
274 Gee.ArrayList<CompletionProposal> items;
276 int minimum_word_size = 2;
278 public Cancellable? cancellable;
280 public CompletionModel(CompletionProvider provider, GtkSource.CompletionContext context, Lsp.CompletionList? res, Cancellable? cancellable)
282 this.provider = provider;
283 this.cancellable = cancellable;
284 this.items = new Gee.ArrayList<CompletionProposal>();
286 var word = context.get_word();
287 GLib.debug("looking for %s", word);
290 foreach(var comp in res.items) {
292 this.items.add(new CompletionProposal(comp));
296 print("GOT %d results\n", (int) items.size);
298 if (this.items.size < this.minimum_word_size) {
302 items.sort((a, b) => {
303 return ((string)(a.label)).collate((string)(b.label));
310 public GLib.Object? get_item (uint pos)
312 return (Object) this.items.get((int) pos);
314 public GLib.Type get_item_type ()
316 return typeof(GtkSource.CompletionProposal);
318 public uint get_n_items ()
320 return this.items.size;
322 public bool can_filter (string word)
324 if (word == null || word[0] == 0) {
328 if (word.length < this.minimum_word_size) {
332 /* If the new word starts with our initial word, then we can simply
333 * refilter outside this model using a GtkFilterListModel.
336 return word.has_prefix(this.search);
338 public void cancel ()
340 if (this.cancellable != null) {
341 this.cancellable.cancel();
348 public class CompletionProposal : Object, GtkSource.CompletionProposal
351 public string label { get; set; default = ""; }
353 public string text { get; set; default = ""; }
354 public string info { get; set; default = ""; }
356 public Lsp.CompletionItem ci;
358 public CompletionProposal(Lsp.CompletionItem ci) //string label, string text, string info)
362 this.text = ci.detail == null ? "" : ci.detail ;
363 this.label = ci.label;
364 this.info = ci.documentation == null ? "": ci.documentation.value;
365 //GLib.debug("SET: detail =%s, label = %s; info =%s", ci.detail, ci.label, "to long..");