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