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