src/Palete/CompletionProvider.vala
[app.Builder.js] / 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, SourceCompletionProvider
8     {
9                 Editor editor; 
10                 WindowState windowstate;
11                 //public List<Gtk.SourceCompletionItem> filtered_proposals;
12
13                 public CompletionProvider(Editor editor)
14                 {
15                     this.editor  = editor;
16                     this.windowstate = null; // not ready until the UI is built.
17                     
18                 }
19
20                 public string get_name ()
21                 {
22                   return  "roojsbuilder";
23                 }
24
25                 public int get_priority ()
26                 {
27                   return 200;
28                 }
29
30                 public bool match (SourceCompletionContext context)
31                 {
32                         bool has_matches = false;
33                         this.fetchMatches(context, out has_matches);
34                         return has_matches;
35                 }
36
37                 public List<SourceCompletionItem>? fetchMatches(SourceCompletionContext context, out bool has_matches)
38                 {
39                      has_matches = false;
40
41                     if (this.windowstate == null) {
42                             this.windowstate = this.editor.window.windowstate;
43                     }
44                 
45                 
46                     var buffer = context.completion.view.buffer;
47                     var  mark = buffer.get_insert ();
48                     TextIter end;
49
50                     buffer.get_iter_at_mark (out end, mark);
51                     var endpos = end;
52                 
53                     var searchpos = endpos;
54                 
55                     searchpos.backward_find_char(is_space, null);
56                     searchpos.forward_char();
57                     var search = endpos.get_text(searchpos);
58                     print("got search %s\n", search);
59                 
60                     if (search.length < 2) {
61                             return null;
62                     }
63                 
64                     // now do our magic..
65                     var filtered_proposals = this.windowstate.file.palete().suggestComplete(
66                             this.windowstate.file,
67                             this.editor.node,
68                             this.editor.ptype,
69                             this.editor.key,
70                             search
71                     );
72                 
73                     print("GOT %d results\n", (int) filtered_proposals.length()); 
74                 
75                     if (filtered_proposals.length() < 2) {
76                         return null;
77                     }
78                 
79                     filtered_proposals.sort((a, b) => {
80                             return ((string)(a.text)).collate((string)(b.text));
81                     });
82                     has_matches = true;
83                     return filtered_proposals;
84
85                 }
86         
87                 public void populate (SourceCompletionContext context)
88                 {
89                         bool has_matches = false;
90                         var filtered_proposals = this.fetchMatches(context, out has_matches);
91                         if (!has_matches) {
92                             context.add_proposals (this, null, true);
93                             return;
94                         }
95                         // add proposals triggers a critical error in Gtk - try running gtksourceview/tests/test-completion.
96                         GLib.Log.set_always_fatal(0); 
97                         context.add_proposals (this, filtered_proposals, true);
98                         GLib.Log.set_always_fatal(LogLevelFlags.LEVEL_ERROR | LogLevelFlags.LEVEL_CRITICAL);
99                 }
100
101
102
103                 public bool activate_proposal (SourceCompletionProposal proposal, TextIter iter)
104                 {
105                         var istart = iter;
106                         istart.backward_find_char(is_space, null);
107                         istart.forward_char();
108
109                 //    var search = iter.get_text(istart);           
110                 
111                         var buffer = iter.get_buffer();
112                         buffer.delete(ref istart, ref iter);
113                         buffer.insert(ref istart, proposal.get_text(), -1);
114                 
115                         return true;
116                 }
117
118                 public SourceCompletionActivation get_activation ()
119                 {
120                         //if(SettingsManager.Get_Setting("complete_auto") == "true"){
121                                 return SourceCompletionActivation.INTERACTIVE | SourceCompletionActivation.USER_REQUESTED;
122                         //} else {
123                         //      return Gtk.SourceCompletionActivation.USER_REQUESTED;
124                         //}
125                 }
126
127                 public int get_interactive_delay ()
128                 {
129                         return -1;
130                 }
131
132                 public bool get_start_iter (SourceCompletionContext context, SourceCompletionProposal proposal, out TextIter iter)
133                 {
134                         return false;
135                 }
136
137                 public void update_info (SourceCompletionProposal proposal, SourceCompletionInfo info)
138                 {
139
140                 }
141
142                 private bool is_space(unichar space){
143                         return space.isspace() || space.to_string() == "";
144                 }
145                 
146                 private bool is_forward_space(unichar space){
147                         return !(
148                                 space.to_string() == " "
149                                 ||
150                                 space.to_string() == ""
151                                 ||
152                                 space.to_string() == "\n"
153                                 ||
154                                 space.to_string() == ")"
155                                 ||
156                                 space.to_string() == "("
157                                 
158                         );
159                 }
160         }
161
162
163
164