98f1d53a68562316b4ba2a27ff1a8da23f511972
[roobuilder] / src / Palete / LanguageClientJavascript.vala
1
2 namespace Palete {
3         public class LanguageClientJavascript : LanguageClient {
4         
5         
6                 public LanguageClientJavascript(Project.Project project)
7                 {
8                         // extend versions will proably call initialize to start and connect to server.
9                         base(project);
10                         
11                 
12                 }
13                 public override   void  initialize_server()   {
14                         GLib.debug("initialize javascript server");                     
15                 }
16                 public override void startServer()
17                 {
18                 }
19                  
20                 
21                 
22                 
23                 public new bool isReady() 
24                 {
25                         return false;
26                 }
27                 public new void document_open (JsRender.JsRender file)  
28                 {
29                         
30                         Javascript.singleton().validate(file.toSourceCode(), file );
31                         BuilderApplication.updateCompileResults();
32                 
33                 }
34                 public new void document_save (JsRender.JsRender file)  
35                 {
36                         Javascript.singleton().validate(file.toSourceCode(), file );
37                         BuilderApplication.updateCompileResults();
38                 }
39                 public new void document_change (JsRender.JsRender file   )    
40                 {
41                         Javascript.singleton().validate(file.toSourceCode(), file );
42                         BuilderApplication.updateCompileResults();
43                 }
44                 
45                 public async Lsp.CompletionList?  completion(JsRender.JsRender file, int line, int offset , int triggerType = 1) throws GLib.Error 
46                 {
47                 
48                         var ret = new Lsp.CompletionList();     
49                  
50                         
51                         var ar = file.toSource().split("\n");
52                         var ln = line >= ar.length ? "" :  ar[line];
53                         if (ln.length >= offset) {
54                                 return ret;
55                         }
56                         var start = -1;
57                         for (var i = offset; i > 0; i--) {
58                                 GLib.debug("check char %c", ln[i]);
59                                 if (ln[i].isalpha() || ln[i] == '.') {
60                                         start = i;
61                                         continue;
62                                 }
63                                 break;
64                         }
65                         var complete_string = ln.substring(start, offset - start);
66                         GLib.debug("complete string = %s", complete_string);
67                         
68                         
69                  
70                         // completion rules??
71                         
72                         // Roo......
73                         
74                         // this. (based on the node type)
75                         // this.xxx // Node and any determination.
76                         
77                         // keywords... // text does not contains "."
78                         
79                         if (!complete_string.contains(".")) {
80                                 // string does not have a '.'
81                                 // offer up this / Roo / javascript keywords... / look for var string = .. in the code..
82                                 for(var i = 0; i <  JsRender.Lang.match_strings.size ; i++) {
83                                         var str = JsRender.Lang.match_strings.get(i);
84                                         var sci = new  Lsp.CompletionItem.keyword(str, str, "keywords : %s".printf(str));
85                                         ret.items.add(sci);
86                                                  
87                                         
88                                         
89                                         
90                                 }
91                                 if (complete_string != "Roo" && "Roo".has_prefix(complete_string)  ) { 
92                                         // should we ignore exact matches... ???
93                                         var sci =  new Lsp.CompletionItem.keyword("Roo", "Roo", "Roo - A Roo class" );
94                                         ret.items.add(sci);
95
96                                          
97                                 }
98                                 if (complete_string != "_this" && "_this".has_prefix(  complete_string) ) { 
99                                         // should we ignore exact matches... ???
100                                         
101                                         var sci =  new Lsp.CompletionItem.keyword("_this", "_this",   "Reference to the global pointer to the files main class instance");
102                                         ret.items.add(sci);
103                                          
104                                          
105                                 }
106                                 return ret;
107                         }
108                         // got at least one ".".
109                         var parts = complete_string.split(".");
110                         var curtype = "";
111                         var cur_instance = false;
112                         if (parts[0] == "this") {
113                                 // work out from the node, what the type is...
114                                 // fetch node from element.
115                                 //if (node == null) {
116                                         GLib.debug("node is empty - no return\n");
117                                         return ret; // no idea..
118                                 //}
119                                 //curtype = node.fqn();
120                                 //cur_instance = true;
121                         }
122                         if (parts[0] == "Roo") {        
123                                 curtype = "Roo";
124                                 cur_instance = false;
125                         }
126                                         
127                         var prevbits = parts[0] + ".";
128                         for(var i =1; i < parts.length; i++) {
129                                 GLib.debug("matching %d/%d\n", i, parts.length);
130
131                                 var is_last = i == parts.length -1;     
132                                 // look up all the properties of the type...
133                                 var cls = this.project.palete.getClass(curtype);
134                                 if (cls == null) {
135                                         GLib.debug("could not get class of curtype %s\n", curtype);
136                                         return ret;
137                                 }
138
139                                 if (!is_last) {
140                                 
141                                         // only exact matches from here on...
142                                         if (cur_instance) {
143                                                 if (cls.props.has_key(parts[i])) {
144                                                         var prop = cls.props.get(parts[i]);
145                                                         if (prop.type.index_of(".",0) > -1) {
146                                                                 // type is another roo object..
147                                                                 curtype = prop.type;
148                                                                 prevbits += parts[i] + ".";
149                                                                 continue;
150                                                         }
151                                                         return ret;
152                                                 }
153                                                 
154                                                 
155                                                 
156                                                 // check methods?? - we do not export that at present..
157                                                 return ret;      //no idea...
158                                         }
159                                 
160                                         // not a instance..
161                                         //look for child classes.
162                                         var citer = this.project.palete.classes.map_iterator();
163                                         var foundit = false;
164                                         while (citer.next()) {
165                                                 var scls = citer.get_key();
166                                                 var look = prevbits + parts[i];
167                                                 if (scls.index_of(look,0) != 0) {
168                                                         continue;
169                                                 }
170                                                 // got a starting match..
171                                                 curtype = look;
172                                                 cur_instance = false;
173                                                 foundit =true;
174                                                 break;
175                                         }
176                                         if (!foundit) {
177                                                 return ret;
178                                         }
179                                         prevbits += parts[i] + ".";
180                                         continue;
181                                 }
182                                 // got to the last element..
183                                 GLib.debug("Got last element\n");
184                                 if (curtype == "") { // should not happen.. we would have returned already..
185                                         return ret;
186                                 }
187                                 GLib.debug("Got last element type %s\n",curtype);
188                                 if (!cur_instance) {
189                                         GLib.debug("matching instance");
190                                         // it's a static reference..
191                                         var citer = this.project.palete.classes.map_iterator();
192                                         while (citer.next()) {
193                                                 var scls = citer.get_key();
194                                                 var look = prevbits + parts[i];
195                                                 if (parts[i].length > 0 && scls.index_of(look,0) != 0) {
196                                                         continue;
197                                                 }
198                                                 var sci =  new Lsp.CompletionItem.keyword(scls,scls, "doc??" );
199                                                 ret.items.add(sci);
200
201                                                  
202                                         }
203                                         return ret;
204                                 }
205                                 GLib.debug("matching property");
206                                 
207                                 
208                                 
209                                 var citer = cls.methods.map_iterator();
210                                 while (citer.next()) {
211                                         var prop = citer.get_value();
212                                         // does the name start with ...
213                                         if (parts[i].length > 0 && prop.name.index_of(parts[i],0) != 0) {
214                                                 continue;
215                                         }
216                                         // got a matching property...
217                                         // return type?
218                                         
219                                         var sci =  new Lsp.CompletionItem.keyword( prevbits + prop.name + "(", prop.name + "(" , prop.doctxt );
220                                         ret.items.add(sci);
221
222                                  
223                                          
224                                 }
225                                 
226                                 // get the properties / methods and subclasses.. of cls..
227                                 // we have cls.. - see if the string matches any of the properties..
228                                 citer = cls.props.map_iterator();
229                                 while (citer.next()) {
230                                         var prop = citer.get_value();
231                                         // does the name start with ...
232                                         if (parts[i].length > 0 && prop.name.index_of(parts[i],0) != 0) {
233                                                 continue;
234                                         }
235                                         
236                                         var sci =  new Lsp.CompletionItem.keyword( prevbits +  prop.name + "(", prop.name + "(" , prop.doctxt );
237                                         ret.items.add(sci);
238  
239                                 
240                                 }
241                                          
242                                         
243                                 return ret;     
244                                         
245                                         
246                                 
247                                         
248                                 
249                         }
250                         
251                          
252                         
253                         
254                         
255                         
256                         return ret;
257                 
258                 }
259                 
260         }
261         
262 }