forked from SublimeCodeIntel/SublimeCodeIntel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_php.py
More file actions
1759 lines (1633 loc) · 81.8 KB
/
tree_php.py
File metadata and controls
1759 lines (1633 loc) · 81.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is Komodo code.
#
# The Initial Developer of the Original Code is ActiveState Software Inc.
# Portions created by ActiveState Software Inc are Copyright (C) 2000-2007
# ActiveState Software Inc. All Rights Reserved.
#
# Contributor(s):
# ActiveState Software Inc
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
"""Completion evaluation code for PHP"""
from codeintel2.common import *
from codeintel2.tree import TreeEvaluator
from codeintel2.util import make_short_name_dict, banner
php_magic_global_method_data = {
"__autoload": "__autoload(string $className)\n"
"This function is automatically called when a class\n"
"is being called, but which hasn't been defined yet.",
}
php_magic_class_method_data = {
"__construct": "__construct([mixed $args [, $...]])\n"
"Initializes a newly created class instance.\n"
"Note: parent constructors will need to be implictly\n"
"called.",
"__destruct": "__destruct()\n"
"This function is called when all references to this\n"
"particular class instance are removed or when the\n"
"object is explicitly destroyed.\n"
"Note: parent destructors will need to be implictly\n"
"called.",
"__call": "__call(string $name, array $arguments) -> mixed\n"
"Is triggered when your class instance (and inherited\n"
"classes) does not contain the method name.",
"__callStatic": "__callStatic(string $name, array $arguments) -> mixed\n"
"Is triggered when your class is accessed statically\n"
"and it does not contain the method name.",
"__get": "__get(string $name) -> mixed\n"
"Is triggered when your class instance (and inherited\n"
"classes) does not contain the member or method name.",
"__set": "__set(string $name, mixed $value)\n"
"Is triggered when your class instance (and inherited\n"
"classes) does not contain the member or method name.",
"__isset": "__isset(string $name) -> bool\n"
"Overload the isset function for the class instance.",
"__unset": "__unset(string $name)\n"
"Overload the unset function for the class instance.",
"__sleep": "__sleep() -> array\n"
"Called through serialize in order to generate a\n"
"storable representation of a class instance. Returns\n"
"an array of the variable names that need to\n"
"be stored.",
"__wakeup": "__wakeup()\n"
"Called through unserialize after restoring a\n"
"a class instance with it's serialized values.",
"__toString": "__toString() -> string\n"
"Returns a string representation of the class instance.",
"__set_state": "__set_state(array $properties)\n"
"Static method that is called to restore a class\n"
"instance's state that was previously exported\n"
"using the var_export() function. Properties are\n"
"in the form array('property' => value, ...).",
"__clone": "__clone()\n"
"When the class instance is cloned using the\n"
"clone($object) function call, a new class instance\n"
"is created with shallow copies of all $object's\n"
"values. This function is then called after to allow\n"
"the new instance to update any of these values.",
}
php_keyword_calltip_data = {
"array": "array(<list>)\n"
"Create a PHP array.",
"declare": "declare(directive)\n"
"Set execution directives for a block of code.\n",
"echo": "echo($arg1 [, $arg2... ])\n"
"Output one or more strings.",
"eval": "eval($code) => mixed\n"
"Evaluate the $code string as PHP code.",
"exit": "exit($status)\n"
"$status can be either a string or an int.\n"
"Outputs $status and then terminates the current script.\n"
"If status is an integer, that value will also be used as\n"
"the exit status. Exit statuses should be in the range 0\n"
"to 254, the exit status 255 is reserved by PHP and shall\n"
"not be used. The status 0 is used to terminate the\n"
"program successfully. PHP >= 4.2.0 does NOT print the\n"
"status if it is an integer.",
"include": "include(file_path)\n"
"Includes and evaluates the specified file, produces a\n"
"Fatal Error on error.",
"include_once": "include_once(file_path)\n"
"Includes and evaluates the specified file if it hasn't\n"
"been included before, produces a Fatal Error on error.",
"print": "print($arg)\n"
"Output the $arg string.",
"require": "require(file_path)\n"
"Includes and evaluates the specified file, produces a\n"
"Fatal Error on error.",
"require_once": "require_once(file_path)\n"
"Includes and evaluates the specified file if it hasn't\n"
"been included before, produces a Fatal Error on error.",
}
class PHPTreeEvaluator(TreeEvaluator):
"""
scoperef: (<blob>, <lpath>) where <lpath> is list of names
self._elem_from_scoperef()
hit: (<elem>, <scoperef>)
"""
# Calltips with this expression value are ignored. See bug:
# http://bugs.activestate.com/show_bug.cgi?id=61497
php_ignored_calltip_expressions = (
"if",
"elseif",
"for",
"foreach",
"while",
"switch",
"array",
"list",
"die",
"exit",
"header",
"require",
"require_once",
"echo",
"var_dump",
"print_r",
"var_export",
)
php_magic_global_method_cplns = [("function", name) for name in
sorted(php_magic_global_method_data.keys())]
# Classes can use both global and class specific functions.
php_magic_class_method_cplns = [("function", name) for name in
sorted(php_magic_class_method_data.keys())]
# A variable used to track the recursion depth of _hit_from_citdl().
eval_depth = 0
# TODO: candidate for base TreeEvaluator class
_langintel = None
@property
def langintel(self):
if self._langintel is None:
self._langintel = self.mgr.langintel_from_lang(self.trg.lang)
return self._langintel
# TODO: candidate for base TreeEvaluator class
_libs = None
@property
def libs(self):
if self._libs is None:
self._libs = self.langintel.libs_from_buf(self.buf)
return self._libs
def get_next_scoperef(self, scoperef):
blob, lpath = scoperef
elem = self._elem_from_scoperef(scoperef)
linenum = self.line + 1 # convert to 1-based
for subelem in elem.getchildren():
start = int(subelem.get("line"))
if start > linenum:
if subelem.tag == "scope":
lpath.append(subelem.get("name"))
break
return (blob, lpath)
# Un-comment for an indented log record, each indented level will represent
# a layer of recursion - which is quite useful for debugging.
# def log_start(self):
# TreeEvaluator.log_start(self)
# self.eval_depth = 0
# def debug(self, msg, *args):
# msg = ' ' * self.eval_depth + msg
# TreeEvaluator.debug(self, msg, *args)
# def info(self, msg, *args):
# msg = ' ' * self.eval_depth + msg
# TreeEvaluator.info(self, msg, *args)
def eval_cplns(self):
self.log_start()
self._imported_blobs = {}
start_scope = self.get_start_scoperef()
trg = self.trg
if trg.type == "variables":
return self._variables_from_scope(self.expr, start_scope)
elif trg.type == "comment-variables":
next_scope = self.get_next_scoperef(start_scope)
return self._comment_variables_from_scope(self.expr, next_scope)
elif trg.type == "functions":
# The 3-character trigger, which not actually specific to
# functions.
retval = self._keywords_from_scope(self.expr, start_scope) + \
self._functions_from_scope(self.expr, start_scope) + \
self._constants_from_scope(self.expr, start_scope) + \
self._classes_from_scope(self.expr[:3], start_scope) + \
self._imported_namespaces_from_scope(
self.expr, start_scope)
# if self.ctlr.is_aborted():
# return None
return retval
elif trg.type == "classes":
return self._classes_from_scope(None, start_scope) + \
self._imported_namespaces_from_scope(None, start_scope)
elif trg.type == "use-global-namespaces":
return self._namespaces_from_scope(None, start_scope)
elif trg.type == "use":
# When inside of a trait or a class - return trait completions, else
# look for namespace completions.
global_scoperef = self._get_global_scoperef(start_scope)
elem = self._elem_from_scoperef(start_scope)
if elem.get("ilk") in ("class", "trait"):
return self._traits_from_scope(None, global_scoperef)
else:
# All available namespaces and all available/global classes.
return self._namespaces_from_scope(None, start_scope) + \
self._classes_from_scope(None, global_scoperef,
allowGlobalClasses=True)
elif trg.type == "namespace-members" and (not self.expr or self.expr == "\\"):
# All available namespaces and include all available global
# functions/classes/constants as well.
global_scoperef = self._get_global_scoperef(start_scope)
cplns = self._namespaces_from_scope(self.expr, start_scope)
cplns += self._functions_from_scope(None, global_scoperef)
cplns += self._constants_from_scope(None, global_scoperef)
cplns += self._classes_from_scope(None, global_scoperef)
return cplns
elif trg.type == "interfaces":
return self._interfaces_from_scope(self.expr, start_scope) + \
self._imported_namespaces_from_scope(self.expr, start_scope)
elif trg.type == "magic-methods":
elem = self._elem_from_scoperef(start_scope)
if elem.get("ilk") == "function":
# Use the parent for our check.
blob, lpath = start_scope
if len(lpath) > 1:
elem = self._elem_from_scoperef((blob, lpath[:-1]))
if elem.get("ilk") in ("class", "trait"):
# All looks good, return the magic class methods.
return self.php_magic_class_method_cplns
else:
# Return global magic methods.
return self.php_magic_global_method_cplns
elif trg.type in ["namespace-members", "use-namespace", "namespace-members-nmspc-only"]:
# Find the completions:
cplns = []
expr = self.expr
if trg.type in ["use-namespace", "namespace-members-nmspc-only"] and expr and not expr.startswith("\\"):
# Importing a namespace, uses a FQN name - bug 88736.
expr = "\\" + expr
fqn = self._fqn_for_expression(expr, start_scope)
hits = self._hits_from_namespace(fqn, start_scope)
if hits:
# self.log("self.expr: %r", self.expr)
# self.log("hits: %r", hits)
cplns = list(self._members_from_hits(hits))
# self.log("cplns: %r", cplns)
if trg.type == "namespace-members-nmspc-only":
cplns = [i for i in cplns if i[0] == "namespace"]
# Return additional sub-namespaces that start with this prefix.
if hits and hits[0][0] is not None:
# We hit a namespace, return additional namespaces that
# start with the hit's fully qualified name (fqn).
fqn = hits[0][0].get("name")
self.debug("eval_cplns:: adding namespace hits that start with "
"fqn: %r", fqn)
namespaces = self._namespaces_from_scope(fqn, start_scope)
# self.log("namespaces: %r", namespaces)
for ilk, n in namespaces:
namespace = n[len(fqn):]
if namespace and namespace.startswith("\\"):
cplns.append((ilk, namespace.strip("\\")))
return cplns
else:
hit = self._hit_from_citdl(self.expr, start_scope)
if hit[0] is not None:
self.log("self.expr: %r", self.expr)
# special handling for parent, explicitly set the
# protected and private member access for this case
if self.expr == "parent" or \
self.expr.startswith("parent."):
self.log("Allowing protected parent members")
return list(
self._members_from_hit(hit, allowProtected=True,
allowPrivate=False))
elif self.trg.type == "array-members":
return list(self._members_from_array_hit(hit, self.trg.extra.get('trg_char')))
else:
return list(self._members_from_hit(hit))
def eval_calltips(self):
self.log_start()
self._imported_blobs = {}
start_scope = self.get_start_scoperef()
# Ignore doing a lookup on certain expressions
# XXX - TODO: Might be better to do this in trg_from_pos.
expr = self.expr
if expr in self.php_ignored_calltip_expressions:
return None
elif expr in php_keyword_calltip_data:
return [php_keyword_calltip_data.get(expr)]
# XXX - Check the php version, magic methods only appeared in php 5.
elif expr in php_magic_class_method_data:
elem = self._elem_from_scoperef(start_scope)
if elem.get("ilk") == "function":
# Use the parent for our check.
blob, lpath = start_scope
if len(lpath) > 1:
elem = self._elem_from_scoperef((blob, lpath[:-1]))
if elem.get("ilk") in ("class", "trait"):
# Use the class magic methods.
return [php_magic_class_method_data[expr]]
# Else, let the tree work it out.
elif expr in php_magic_global_method_data:
elem = self._elem_from_scoperef(start_scope)
if elem.get("ilk") == "function":
# Check the parent is not a class. See:
# http://bugs.activestate.com/show_bug.cgi?id=69758
blob, lpath = start_scope
if len(lpath) > 1:
elem = self._elem_from_scoperef((blob, lpath[:-1]))
if elem.get("ilk") in ("class", "trait"):
# Not available inside a class.
return []
return [php_magic_global_method_data[expr]]
hit = self._hit_from_citdl(expr, start_scope)
return self._calltips_from_hit(hit)
def eval_defns(self):
self.log_start()
self._imported_blobs = {}
start_scoperef = self.get_start_scoperef()
self.info("start scope is %r", start_scoperef)
hit = self._hit_from_citdl(self.expr, start_scoperef, defn_only=True)
return [self._defn_from_hit(hit)]
# Determine if the hit is valid
def _return_with_hit(self, hit, nconsumed):
# Added special attribute __not_yet_defined__ to the PHP ciler,
# this is used to indicate the variable was made but is not yet
# assigned a type, i.e. it's not yet defined.
elem, scoperef = hit
attributes = elem.get("attributes")
if attributes:
attr_split = attributes.split(" ")
if "__not_yet_defined__" in attr_split:
self.log(
"_return_with_hit:: hit was a not_yet_defined, ignoring it: %r", hit)
return False
self.log("_return_with_hit:: hit is okay: %r", hit)
return True
def _element_names_from_scope_starting_with_expr(self, expr, scoperef,
elem_type, scope_names,
elem_retriever):
"""Return all available elem_type names beginning with expr"""
self.log(
"%s_names_from_scope_starting_with_expr:: expr: %r, scoperef: %r for %r",
elem_type, expr, scoperef, scope_names)
global_blob = self._elem_from_scoperef(
self._get_global_scoperef(scoperef))
# Get all of the imports
# Start making the list of names
all_names = set()
for scope_type in scope_names:
elemlist = []
if scope_type == "locals":
elemlist = [self._elem_from_scoperef(scoperef)]
elif scope_type == "globals":
elemlist = [global_blob]
elif scope_type == "namespace":
elemlist = []
namespace = self._namespace_elem_from_scoperef(scoperef)
if namespace is not None:
elemlist.append(namespace)
# Note: This namespace may occur across multiple files, so we
# iterate over all known libs that use this namespace.
fqn = namespace.get("name")
lpath = (fqn, )
for lib in self.libs:
hits = lib.hits_from_lpath(lpath, self.ctlr,
curr_buf=self.buf)
elemlist += [elem for elem, scoperef in hits]
elif scope_type == "builtins":
lib = self.buf.stdlib
# Find the matching names (or all names if no expr)
# log.debug("Include builtins: elem_type: %s", elem_type)
names = lib.toplevel_cplns(prefix=expr, ilk=elem_type)
all_names.update([n for ilk, n in names])
# "Include everything" includes the builtins already
elif scope_type == "imports":
# Iterate over all known libs
for lib in self.libs:
# Find the matching names (or all names if no expr)
# log.debug("Include everything: elem_type: %s", elem_type)
names = lib.toplevel_cplns(prefix=expr, ilk=elem_type)
all_names.update([n for ilk, n in names])
# Standard imports, specified through normal import semantics
elemlist = self._get_all_import_blobs_for_elem(global_blob)
for elem in elemlist:
names = elem_retriever(elem)
if expr:
if isinstance(names, dict):
try:
names = names[expr]
except KeyError:
# Nothing in the dict matches, thats okay
names = []
else:
# Iterate over the names and keep the ones containing
# the given prefix.
names = [x for x in names if x.startswith(expr)]
self.log(
"%s_names_from_scope_starting_with_expr:: adding %s: %r",
elem_type, scope_type, names)
all_names.update(names)
return self._convertListToCitdl(elem_type, all_names)
def _variables_from_scope(self, expr, scoperef):
"""Return all available variable names beginning with expr"""
# The current scope determines what is visible, see bug:
# http://bugs.activestate.com/show_bug.cgi?id=65159
vars = []
blob, lpath = scoperef
if len(lpath) > 0:
# Inside a function or class, don't get to see globals
scope_chain = ("locals", "builtins", )
if len(lpath) > 1:
# Current scope is inside a class function?
elem = self._elem_from_scoperef(scoperef)
if elem is not None and elem.get("ilk") == "function":
p_elem = self._elem_from_scoperef((blob, lpath[:-1]))
if p_elem is not None and p_elem.get("ilk") == "class":
# Include "$this" in the completions.
vars = [("variable", "this")]
else:
# Already global scope, so get to see them all
scope_chain = ("locals", "globals", "imports", )
# XXX - TODO: Move to 3 char trigger (if we want/need to)
vars += self._element_names_from_scope_starting_with_expr(None,
scoperef,
"variable",
scope_chain,
self.variable_names_from_elem)
if expr:
# XXX - TODO: Use VARIABLE_TRIGGER_LEN instead of hard coding 1
expr = expr[:1]
return [(ilk, name) for ilk, name in vars if name.startswith(expr)]
else:
return [(ilk, name) for ilk, name in vars]
def _comment_variables_from_scope(self, expr, scoperef):
"""Return all available local variable names beginning with expr"""
blob, lpath = scoperef
# Only care about the local variables.
scope_chain = ("locals", )
vars = self._element_names_from_scope_starting_with_expr(None,
scoperef,
"variable",
scope_chain,
self.variable_names_from_elem)
# XXX - TODO: Use VARIABLE_TRIGGER_LEN instead of hard coding 1
expr = expr[:1]
return [(ilk, name) for ilk, name in vars if name.startswith(expr)]
def _constants_from_scope(self, expr, scoperef):
"""Return all available constant names beginning with expr"""
# XXX - TODO: Use FUNCTION_TRIGGER_LEN instead of hard coding 3
return self._element_names_from_scope_starting_with_expr(
expr and expr[:3] or None,
scoperef,
"constant",
("globals", "imports", "builtins"),
self.constant_shortnames_from_elem)
def _keywords_from_scope(self, expr, scoperef):
"""Return all available keyword names beginning with expr"""
keyword_completions = []
keywords = self.langintel.langinfo.keywords
expr = expr and expr[:3]
for name in keywords:
if not expr or name.startswith(expr):
keyword_completions.append(("keyword", name))
return keyword_completions
def _functions_from_scope(self, expr, scoperef):
"""Return all available function names beginning with expr"""
# XXX - TODO: Use FUNCTION_TRIGGER_LEN instead of hard coding 3
return self._element_names_from_scope_starting_with_expr(
expr and expr[:3] or None,
scoperef,
"function",
("locals", "namespace", "globals", "imports",),
self.function_shortnames_from_elem)
def _classes_from_scope(self, expr, scoperef, allowGlobalClasses=False):
"""Return all available class names beginning with expr"""
lookup_scopes = ("locals", "namespace", "globals", "imports",)
if not allowGlobalClasses and self._namespace_elem_from_scoperef(scoperef):
# When inside a namespace, don't include global classes - bug
# 83192.
lookup_scopes = ("locals", "namespace",)
return self._element_names_from_scope_starting_with_expr(expr,
scoperef,
"class",
lookup_scopes,
self.class_names_from_elem)
def _traits_from_scope(self, expr, scoperef, allowGlobalClasses=False):
"""Return all available class names beginning with expr"""
return self._element_names_from_scope_starting_with_expr(expr,
scoperef,
"trait",
("globals", ),
self.trait_names_from_elem)
def _imported_namespaces_from_scope(self, expr, scoperef):
"""Return all available class names beginning with expr"""
return self._element_names_from_scope_starting_with_expr(expr,
scoperef,
"namespace",
("namespace",
"globals"),
self.imported_namespace_names_from_elem)
def _namespaces_from_scope(self, expr, scoperef):
"""Return all available namespaces beginning with expr"""
if expr:
expr = expr.strip("\\")
namespaces = self._element_names_from_scope_starting_with_expr(expr,
scoperef,
"namespace",
("globals", "imports",
"builtins",),
self.namespace_names_from_elem)
# We only want to see the next sub-namespace, i.e. if the expr is
# \mynamespace\<|>
# then only return the immediate child namespaces, such as:
# \mynamespace\foo
# but not any deeper, i.e. not:
# \mynamespace\foo\bar
if expr:
namespace_depth = expr.count("\\") + 2
else:
namespace_depth = 1
filtered_namespaces = set()
for ilk, n in namespaces:
s = n.split("\\")
filtered_namespaces.add((ilk, "\\".join(s[:namespace_depth])))
return list(filtered_namespaces)
def _interfaces_from_scope(self, expr, scoperef):
"""Return all available interface names beginning with expr"""
# Need to work from the global scope for this one
return self._element_names_from_scope_starting_with_expr(expr or None,
scoperef,
"interface",
("namespace", "globals", "imports",),
self.interface_names_from_elem)
# c.f. tree_python.py::PythonTreeEvaluator
def _calltips_from_hit(self, hit):
# TODO: compare with CitadelEvaluator._getSymbolCallTips()
elem, scoperef = hit
calltips = []
if elem.tag == "variable":
XXX
elif elem.tag == "scope":
ilk = elem.get("ilk")
if ilk == "function":
calltips.append(self._calltip_from_func(elem, scoperef))
elif ilk == "class":
calltips.append(self._calltip_from_class(elem, scoperef))
else:
raise NotImplementedError("unexpected scope ilk for "
"calltip hit: %r" % elem)
elif elem.tag == "alias":
# Find the resolving function to get the signature, then we replace
# the function name in the signature with the aliased name.
funcelem, scoperef = self._hit_from_variable_type_inference(
elem, scoperef)
ctip = self._calltip_from_func(funcelem, scoperef)
ctip = ctip.replace(funcelem.get("name"), elem.get("name"), 1)
calltips.append(ctip)
else:
raise NotImplementedError("unexpected elem for calltip "
"hit: %r" % elem)
return calltips
def _calltip_from_class(self, node, scoperef):
# If the class has a defined signature then use that.
signature = node.get("signature")
doc = node.get("doc")
if signature:
ctlines = signature.splitlines(0)
if doc:
ctlines += doc.splitlines(0)
return '\n'.join(ctlines)
# Alternatively we use calltip information on the class'
# constructor. PHP does not automatically inherit class contructors,
# so we just return the one on the current class.
else:
# In PHP5 our CIX classes may have a special constructor function
# with the name "__construct".
ctor = node.names.get("__construct")
if ctor is not None:
self.log("_calltip_from_class:: ctor is %r", ctor)
return self._calltip_from_func(ctor, scoperef)
# In PHP4 the contructor is a function that has the same name as
# the class name.
name = node.get("name")
ctor = node.names.get(name)
if ctor is not None:
self.log("_calltip_from_class:: ctor is %r", ctor)
return self._calltip_from_func(ctor, scoperef)
self.log("_calltip_from_class:: no ctor in class %r", name)
# See if there is a parent class that has a constructor - bug
# 90156.
for classref in node.get("classrefs", "").split():
# TODO: update _hit_from_citdl to accept optional node type,
# i.e. to only return classes in this case.
self.log("_calltip_from_class:: looking for parent class: %r",
classref)
base_elem, base_scoperef \
= self._hit_from_citdl(classref, scoperef)
if base_elem is not None and base_elem.get("ilk") == "class":
return self._calltip_from_class(base_elem, base_scoperef)
else:
self.log("_calltip_from_class:: no parent class found: %r",
base_elem)
return "%s()" % (name)
def _members_from_array_hit(self, hit, trg_char):
"""Retrieve members from the given array element.
@param hit {tuple} (elem, scoperef)
@param trg_char {string} The character that triggered the event.
"""
self.log("_members_from_array_hit:: %r", hit)
elem, scoperef = hit
members = set()
for child in elem:
# Should look like: SERVER_NAME']
members.add((child.get("ilk") or child.tag,
#"%s%s]" % (child.get("name"), trg_char)) )
child.get("name")))
return members
def _members_from_elem(self, elem, name_prefix=''):
"""Return the appropriate set of autocomplete completions for
the given element. Typically this is just one, but can be more for
'*'-imports
"""
members = set()
tag = elem.tag
if tag == "import":
module_name = elem.get("module")
cpln_name = module_name.split('.', 1)[0]
members.add(("module", cpln_name))
elif tag == "alias":
members.add(("function",
name_prefix + elem.get("name")))
else:
members.add((elem.get("ilk") or tag,
name_prefix + elem.get("name")))
return members
def _isElemInsideScoperef(self, elem, scoperef):
blob, lpath = scoperef
i = 0
# self.log("_isElemInsideScoperef, elem: %r, lpath: %r", elem, lpath)
for i in range(len(lpath)):
name = lpath[i]
if name == elem.get("name"):
# XXX - It would be nice to confirm that this element is the
# actual elem, but this won't work correctly when there
# is an alternative match being used. i.e.
# http://bugs.activestate.com/show_bug.cgi?id=70015
# check_elem = self._elem_from_scoperef((blob, lpath[:i+1]))
# if check_elem == elem:
# It's in the scope
return True
return False
def _members_from_hit(self, hit, allowProtected=None, allowPrivate=None):
"""Retrieve members from the given hit.
@param hit {tuple} (elem, scoperef)
"""
elem, scoperef = hit
# Namespaces completions only show for namespace elements.
elem_type = elem.get("ilk") or elem.tag
namespace_cplns = (self.trg.type == "namespace-members")
if namespace_cplns and elem_type != "namespace":
raise CodeIntelError("%r resolves to type %r, which is not a "
"namespace" % (self.expr, elem_type, ))
members = set()
elem_name = elem.get("name")
static_cplns = (self.trg.type == "static-members")
for child in elem:
# self.debug("_members_from_hit: checking child: %r", child)
if child.tag == "import":
continue
name_prefix = '' # Used to add "$" for static variable names.
attributes = child.get("attributes", "").split()
if "__hidden__" in attributes:
continue
if not allowProtected:
# Protected and private vars can only be shown from inside
# the class scope
if "protected" in attributes:
if allowProtected is None:
# Need to check if it's allowed
allowProtected = self._isElemInsideScoperef(
elem, self.get_start_scoperef())
if not allowProtected:
# Checked scope already and it does not allow protected
# Thats means it also does not allow private
allowPrivate = False
self.log("hit '%s.%s' is protected, not including",
elem_name, child.get("name"))
continue
if not allowPrivate:
# we now know protected is allowed, now check private
if "private" in attributes:
if allowPrivate is None:
# Need to check if it's allowed
allowPrivate = self._isElemInsideScoperef(
elem, self.get_start_scoperef())
if not allowPrivate:
# Checked scope already and it does not allow private
self.log("hit '%s.%s' is private, not including",
elem_name, child.get("name"))
continue
if child.tag == "variable":
if static_cplns:
# Static variables use the '$' prefix, constants do not.
if "static" in attributes:
name_prefix = '$'
child.tag = "static property"
elif child.get("ilk") != "constant":
continue
elif "static" in attributes:
continue
# Only namespaces allow access to constants.
elif child.get("ilk") == "constant" and not namespace_cplns:
continue
# add the element, we've already checked private|protected scopes
members.update(self._members_from_elem(child, name_prefix))
elem_ilk = elem.get("ilk")
if elem_ilk == "class" or elem_ilk == "trait":
for classref in elem.get("classrefs", "").split() + \
elem.get("traitrefs", "").split():
ns_elem = self._namespace_elem_from_scoperef(scoperef)
if ns_elem is not None:
# For class reference inside a namespace, *always* use the
# fully qualified name - bug 85643.
classref = "\\" + self._fqn_for_expression(
classref, scoperef)
self.debug(
"_members_from_hit: Getting members for inherited %r: %r",
elem_ilk, classref)
try:
subhit = self._hit_from_citdl(classref, scoperef)
except CodeIntelError as ex:
# Continue with what we *can* resolve.
self.warn(str(ex))
else:
if allowProtected is None:
# Need to check if it's allowed
allowProtected = self._isElemInsideScoperef(
elem, self.get_start_scoperef())
# Checking the parent class, private is not allowed for
# sure
members.update(self._members_from_hit(
subhit, allowProtected, allowPrivate=False))
return members
def _members_from_hits(self, hits, allowProtected=None, allowPrivate=None):
"""Retrieve members for the given hits.
@param hits - a list of hits: [(elem, scoperef), ...]
"""
members = set()
for hit in hits:
members.update(self._members_from_hit(hit))
return members
def _hit_from_citdl(self, expr, scoperef, defn_only=False):
"""Resolve the given CITDL expression (starting at the given
scope) down to a non-import/non-variable hit.
"""
self.eval_depth += 1
self.log("_hit_from_citdl:: expr: %r, scoperef: %r", expr, scoperef)
try:
self._check_infinite_recursion(expr)
except EvalError:
# In the case of a recursion error, it is likely due to a class
# variable having the same name as the class itself, so to try
# to get better completions for this case we do not abort here,
# but rather try from the parent scope instead. See bug:
# http://bugs.activestate.com/show_bug.cgi?id=67774
scoperef = self.parent_scoperef_from_scoperef(scoperef)
if scoperef is None:
# When we run out of scope, raise an error
raise
self.debug("_hit_from_citdl: recursion found for '%s', "
"moving to parent scope %r",
expr, scoperef)
tokens = list(self._tokenize_citdl_expr(expr))
self.log("_hit_from_citdl:: expr tokens: %r", tokens)
# First part...
hits, nconsumed = self._hits_from_first_part(tokens, scoperef)
if not hits:
# TODO: Add the fallback Buffer-specific near-by hunt
# for a symbol for the first token. See my spiral-bound
# book for some notes.
if self.trg.type == "namespace-members":
# If this is a namespace-members completion trigger, then this
# namespace may not exist, but one of the sub-namespaces may:
# namespace foo\bar\bam {}
# \foo\<|> # namespace 'foo' doesn't exist
# in this case we want to return all namespaces starting with
# this expr, which is handled in the eval_cplns() method.
return None, None
raise CodeIntelError("could not resolve first part of '%s'" % expr)
self.debug("_hit_from_citdl:: first part: %r -> %r",
tokens[:nconsumed], hits)
# ...the remainder.
remaining_tokens = tokens[nconsumed:]
while remaining_tokens:
new_hits = []
for hit in hits:
new_hit = None
try:
self.debug("_hit_from_citdl:: resolve %r on %r in %r",
remaining_tokens, *hit)
if remaining_tokens[0] == "()":
# TODO: impl this function
# _hit_from_call(elem, scoperef) -> hit or raise
# CodeIntelError("could resolve call on %r: %s",
# hit[0], ex)
new_hit = self._hit_from_call(*hit)
nconsumed = 1
else:
new_hit, nconsumed \
= self._hit_from_getattr(remaining_tokens, *hit)
remaining_tokens = remaining_tokens[nconsumed:]
except CodeIntelError as ex:
self.debug("error %s", ex)
if new_hit:
new_hits.append(new_hit)
if not new_hits:
break
hits = new_hits
if not hits:
raise CodeIntelError("could not resolve first part of '%s'" % expr)
if len(hits) > 2:
self.debug(
"_hit_from_citdl:: multiple hits found, returning the first hit")
hit = hits[0]
# Resolve any variable type inferences.
# TODO: Need to *recursively* resolve hits.
elem, scoperef = hit
if elem.tag == "variable" and not defn_only:
elem, scoperef = self._hit_from_variable_type_inference(
elem, scoperef)
self.info(
"_hit_from_citdl:: found '%s' => %s on %s", expr, elem, scoperef)
return (elem, scoperef)
def _alternative_elem_from_scoperef(self, traversed_items):
"""Find an alternative named element for the given traversal items"""
for i in range(len(traversed_items)-1, -1, -1):
elem, lname, failed_elems = traversed_items[i]
self.log(
"Checking for alt elem: %r, lpath: %r, failed_elems: %r", elem, lname, failed_elems)
for child in elem.getchildren():
if child.attrib.get("name") == lname and child not in failed_elems:
return i, child
return None
def _elem_from_scoperef(self, scoperef):
"""A scoperef is (<blob>, <lpath>). Return the actual elem in
the <blob> ciElementTree being referred to.
"""
elem, lpath = scoperef
traversed_items = []
for i in range(len(lpath)):
lname = lpath[i]
try:
child = elem.names[lname]
except KeyError:
# There is likely an alternative element that has the same name.
# Try and find it now.
# http://bugs.activestate.com/show_bug.cgi?id=70015
child = None
if i > 0:
# self.log("i now: %r, traversed_items: %r", i,
# traversed_items)
traversed_items[i-1][2].append(elem)
i, child = self._alternative_elem_from_scoperef(
traversed_items)
traversed_items = traversed_items[:i]
if child is None:
self.info("elem %r is missing lname: %r", elem, lname)
raise
self.info(
"elem %r is missing lname: %r, found alternative: %r",
elem, lname, child)
traversed_items.append((elem, lname, []))
elem = child
return elem
def _namespace_elem_from_scoperef(self, scoperef):
"""A scoperef is (<blob>, <lpath>). Return the current namespace in
the <blob> ciElementTree, walking up the scope as necessary.
"""
blob, lpath = scoperef
if len(lpath) >= 1:
elem_chain = []
elem = blob
for i in range(len(lpath)):
try:
elem = elem.names[lpath[i]]
elem_chain.append(elem)
except KeyError:
break
for elem in reversed(elem_chain):
if elem.tag == "scope" and elem.get("ilk") == "namespace":
return elem
return None
def _imported_namespace_elements_from_scoperef(self, scoperef):
namespace_elems = {}
possible_elems = [self._namespace_elem_from_scoperef(scoperef),
self._get_global_scoperef(scoperef)[0]]
for elem in possible_elems:
if elem is not None:
for child in elem:
if child.tag == "import":
symbol = child.get("symbol")
alias = child.get("alias")
if symbol is not None:
namespace_elems[alias or symbol] = child
return namespace_elems
def _fqn_for_expression(self, expr, scoperef):