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                 construct(Editor editor)
14                 {
15                     this.editor  = editor;
16                     this.windowstate = editor.window.windowstate;
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                         var buffer = context.completion.view.buffer;
39                         var  mark = buffer.get_insert ();
40                         TextIter end;
41
42                         buffer.get_iter_at_mark (out end, mark);
43                         var endpos = end;
44                         
45                         var searchpos = endpos;
46                         
47                         searchpos.backward_find_char(is_space, null);
48                         searchpos.forward_char();
49                         var search = endpos.get_text(searchpos);
50                         print("got search %s\n", search);
51                         
52                         if (search.length < 2) {
53                                 return;
54                         }
55                         // now do our magic..
56                         var filtered_proposals = windowstate.file.palete().suggestComplete(
57                                 this.windowstate.file,
58                                 this.editor.node,
59                                 this.editor.ptype,
60                                 this.editor.key,
61                                 search
62                         );
63                         
64                         
65
66                          
67                         //filtered_proposals.prepend (new SourceCompletionItem (search + "xx", search + "xx", null, "some info"));
68                         foreach(var i in this.proposals) {
69                                 //if(i.text.contains(search)) // starts??
70                                 //      this.filtered_proposals.prepend (new SourceCompletionItem (i.label, i.text, i.icon, i.info));
71                                 //}
72                         }
73                         context.add_proposals (this, filtered_proposals, true);
74                 }
75
76                 public bool activate_proposal (SourceCompletionProposal proposal, TextIter iter)
77                 {
78                         var istart = iter;
79                         istart.backward_find_char(is_space, null);
80                         istart.forward_char();
81
82                 //    var search = iter.get_text(istart);           
83                 
84                         var buffer = iter.get_buffer();
85                         buffer.delete(ref istart, ref iter);
86                         buffer.insert(ref istart, proposal.get_text(), -1);
87                 
88                         return true;
89                 }
90
91                 public SourceCompletionActivation get_activation ()
92                 {
93                         //if(SettingsManager.Get_Setting("complete_auto") == "true"){
94                                 return SourceCompletionActivation.INTERACTIVE | SourceCompletionActivation.USER_REQUESTED;
95                         //} else {
96                         //      return Gtk.SourceCompletionActivation.USER_REQUESTED;
97                         //}
98                 }
99
100                 public int get_interactive_delay ()
101                 {
102                         return -1;
103                 }
104
105                 public bool get_start_iter (SourceCompletionContext context, SourceCompletionProposal proposal, out TextIter iter)
106                 {
107                         return false;
108                 }
109
110                 public void update_info (SourceCompletionProposal proposal, SourceCompletionInfo info)
111                 {
112
113                 }
114
115                 private bool is_space(unichar space){
116                         return space.isspace() || space.to_string() == "";
117                 }
118                 
119                 private bool is_forward_space(unichar space){
120                         return !(
121                                 space.to_string() == " "
122                                 ||
123                                 space.to_string() == ""
124                                 ||
125                                 space.to_string() == "\n"
126                                 ||
127                                 space.to_string() == ")"
128                                 ||
129                                 space.to_string() == "("
130                                 
131                         );
132                 }
133         }
134
135
136