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
58                         var filtered_proposals = new List<SourceCompletionItem> ();
59                         
60                         filtered_proposals.prepend (new SourceCompletionItem (search + "xx", search + "xx", null, "some info"));
61                         foreach(var i in this.proposals) {
62                                 //if(i.text.contains(search)) // starts??
63                                 //      this.filtered_proposals.prepend (new SourceCompletionItem (i.label, i.text, i.icon, i.info));
64                                 //}
65                         }
66                         context.add_proposals (this, filtered_proposals, true);
67                 }
68
69                 public bool activate_proposal (SourceCompletionProposal proposal, TextIter iter)
70                 {
71                         var istart = iter;
72                         istart.backward_find_char(is_space, null);
73                         istart.forward_char();
74
75                 //    var search = iter.get_text(istart);           
76                 
77                         var buffer = iter.get_buffer();
78                         buffer.delete(ref istart, ref iter);
79                         buffer.insert(ref istart, proposal.get_text(), -1);
80                 
81                         return true;
82                 }
83
84                 public SourceCompletionActivation get_activation ()
85                 {
86                         //if(SettingsManager.Get_Setting("complete_auto") == "true"){
87                                 return SourceCompletionActivation.INTERACTIVE | SourceCompletionActivation.USER_REQUESTED;
88                         //} else {
89                         //      return Gtk.SourceCompletionActivation.USER_REQUESTED;
90                         //}
91                 }
92
93                 public int get_interactive_delay ()
94                 {
95                         return -1;
96                 }
97
98                 public bool get_start_iter (SourceCompletionContext context, SourceCompletionProposal proposal, out TextIter iter)
99                 {
100                         return false;
101                 }
102
103                 public void update_info (SourceCompletionProposal proposal, SourceCompletionInfo info)
104                 {
105
106                 }
107
108                 private bool is_space(unichar space){
109                         return space.isspace() || space.to_string() == "";
110                 }
111                 
112                 private bool is_forward_space(unichar space){
113                         return !(
114                                 space.to_string() == " "
115                                 ||
116                                 space.to_string() == ""
117                                 ||
118                                 space.to_string() == "\n"
119                                 ||
120                                 space.to_string() == ")"
121                                 ||
122                                 space.to_string() == "("
123                                 
124                         );
125                 }
126         }
127
128
129