src/Palete/ValaSourceCompiler.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  "test";
23                 }
24
25                 public int get_priority ()
26                 {
27                   return 1;
28                 }
29
30                 public bool match (SourceCompletionContext context)
31                 {
32                 
33                         return true;
34                 }
35
36                 public void populate (SourceCompletionContext context)
37                 {
38                         if (this.windowstate == null) {
39                                 this.windowstate = this.editor.window.windowstate;
40                         }
41                         
42                         
43                         var buffer = context.completion.view.buffer;
44                         var  mark = buffer.get_insert ();
45                         TextIter end;
46
47                         buffer.get_iter_at_mark (out end, mark);
48                         var endpos = end;
49                         
50                         var searchpos = endpos;
51                         
52                         searchpos.backward_find_char(is_space, null);
53                         searchpos.forward_char();
54                         var search = endpos.get_text(searchpos);
55                         print("got search %s\n", search);
56                         
57                         if (search.length < 2) {
58                                 return;
59                         }
60                         
61                         // now do our magic..
62                         var filtered_proposals = this.windowstate.file.palete().suggestComplete(
63                                 this.windowstate.file,
64                                 this.editor.node,
65                                 this.editor.ptype,
66                                 this.editor.key,
67                                 search
68                         );
69                     
70                         print("GOT %d results\n", (int) filtered_proposals.length()); 
71                     
72                         if (filtered_proposals.length() < 2) {
73                             return;
74                         }
75                     
76                         filtered_proposals.sort((a, b) => {
77                                 return ((string)(a.text)).collate((string)(b.text));
78                         });
79                          
80                         context.add_proposals (this, filtered_proposals, true);
81                 }
82
83
84
85                 public bool activate_proposal (SourceCompletionProposal proposal, TextIter iter)
86                 {
87                         var istart = iter;
88                         istart.backward_find_char(is_space, null);
89                         istart.forward_char();
90
91                 //    var search = iter.get_text(istart);           
92                 
93                         var buffer = iter.get_buffer();
94                         buffer.delete(ref istart, ref iter);
95                         buffer.insert(ref istart, proposal.get_text(), -1);
96                 
97                         return true;
98                 }
99
100                 public SourceCompletionActivation get_activation ()
101                 {
102                         //if(SettingsManager.Get_Setting("complete_auto") == "true"){
103                                 return SourceCompletionActivation.INTERACTIVE | SourceCompletionActivation.USER_REQUESTED;
104                         //} else {
105                         //      return Gtk.SourceCompletionActivation.USER_REQUESTED;
106                         //}
107                 }
108
109                 public int get_interactive_delay ()
110                 {
111                         return -1;
112                 }
113
114                 public bool get_start_iter (SourceCompletionContext context, SourceCompletionProposal proposal, out TextIter iter)
115                 {
116                         return false;
117                 }
118
119                 public void update_info (SourceCompletionProposal proposal, SourceCompletionInfo info)
120                 {
121
122                 }
123
124                 private bool is_space(unichar space){
125                         return space.isspace() || space.to_string() == "";
126                 }
127                 
128                 private bool is_forward_space(unichar space){
129                         return !(
130                                 space.to_string() == " "
131                                 ||
132                                 space.to_string() == ""
133                                 ||
134                                 space.to_string() == "\n"
135                                 ||
136                                 space.to_string() == ")"
137                                 ||
138                                 space.to_string() == "("
139                                 
140                         );
141                 }
142         }
143
144
145