-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathlang_php.py
More file actions
3494 lines (3177 loc) · 152 KB
/
lang_php.py
File metadata and controls
3494 lines (3177 loc) · 152 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 *****
#
# Contributors:
# Shane Caraveo (ShaneC@ActiveState.com)
# Trent Mick (TrentM@ActiveState.com)
# Todd Whiteman (ToddW@ActiveState.com)
"""codeintel support for PHP"""
from __future__ import absolute_import
from __future__ import print_function
import os
from os.path import isdir, join, basename, splitext, exists, dirname
import sys
import re
import logging
import time
import warnings
from six.moves import cStringIO as StringIO
import weakref
from glob import glob
from SilverCity.ScintillaConstants import (SCE_UDL_SSL_DEFAULT,
SCE_UDL_SSL_OPERATOR,
SCE_UDL_SSL_IDENTIFIER,
SCE_UDL_SSL_WORD,
SCE_UDL_SSL_VARIABLE,
SCE_UDL_SSL_STRING,
SCE_UDL_SSL_NUMBER,
SCE_UDL_SSL_COMMENT,
SCE_UDL_SSL_COMMENTBLOCK)
from codeintel2.parseutil import *
from codeintel2.phpdoc import phpdoc_tags
from codeintel2.citadel import ImportHandler, CitadelLangIntel
from codeintel2.udl import UDLBuffer, UDLLexer, UDLCILEDriver, is_udl_csl_style, is_udl_css_style, XMLParsingBufferMixin
from codeintel2.common import *
from codeintel2 import util
from codeintel2.indexer import PreloadBufLibsRequest, PreloadLibRequest
from codeintel2.gencix_utils import *
from codeintel2.tree_php import PHPTreeEvaluator
from codeintel2.langintel import (ParenStyleCalltipIntelMixin,
ProgLangTriggerIntelMixin)
from codeintel2.accessor import AccessorCache
if _xpcom_:
from xpcom.server import UnwrapObject
#---- global data
lang = "PHP"
log = logging.getLogger("codeintel.php")
# log.setLevel(logging.DEBUG)
util.makePerformantLogger(log)
#---- language support
class PHPLexer(UDLLexer):
lang = lang
def _walk_php_symbols(elem, _prefix=None):
if _prefix:
lpath = _prefix + (elem.get("name"), )
else:
lpath = (elem.get("name"), )
yield lpath
if not (elem.tag == "scope" and elem.get("ilk") == "function"):
for child in elem:
for child_lpath in _walk_php_symbols(child, lpath):
yield child_lpath
class PHPLangIntel(CitadelLangIntel, ParenStyleCalltipIntelMixin,
ProgLangTriggerIntelMixin):
lang = lang
# Used by ProgLangTriggerIntelMixin.preceding_trg_from_pos()
trg_chars = tuple('$>:(,@"\' \\')
calltip_trg_chars = tuple('(') # excluded ' ' for perf (bug 55497)
# named styles used by the class
whitespace_style = SCE_UDL_SSL_DEFAULT
operator_style = SCE_UDL_SSL_OPERATOR
identifier_style = SCE_UDL_SSL_IDENTIFIER
keyword_style = SCE_UDL_SSL_WORD
variable_style = SCE_UDL_SSL_VARIABLE
string_style = SCE_UDL_SSL_STRING
comment_styles = (SCE_UDL_SSL_COMMENT, SCE_UDL_SSL_COMMENTBLOCK)
comment_styles_or_whitespace = comment_styles + (whitespace_style, )
def _functionCalltipTrigger(self, ac, pos, DEBUG=False):
# Implicit calltip triggering from an arg separater ",", we trigger a
# calltip if we find a function open paren "(" and function identifier
# http://bugs.activestate.com/show_bug.cgi?id=70470
if DEBUG:
print("Arg separater found, looking for start of function")
# Move back to the open paren of the function
paren_count = 0
p = pos
min_p = max(0, p - 200) # look back max 200 chars
while p and p > min_p:
p, c, style = ac.getPrecedingPosCharStyle(ignore_styles=self.comment_styles)
if style == self.operator_style:
if c == ")":
paren_count += 1
elif c == "(":
if paren_count == 0:
# We found the open brace of the func
trg_from_pos = p+1
p, ch, style = ac.getPrevPosCharStyle()
if DEBUG:
print("Function start found, pos: %d" % (p, ))
if style in self.comment_styles_or_whitespace:
# Find previous non-ignored style then
p, c, style = ac.getPrecedingPosCharStyle(style, self.comment_styles_or_whitespace)
if style in (self.identifier_style, self.keyword_style):
return Trigger(lang, TRG_FORM_CALLTIP,
"call-signature",
trg_from_pos, implicit=True)
else:
paren_count -= 1
elif c in ";{}":
# Gone too far and noting was found
if DEBUG:
print("No function found, hit stop char: %s at p: %d" % (c, p))
return None
# Did not find the function open paren
if DEBUG:
print("No function found, ran out of chars to look at, p: %d" % (p,))
return None
#@util.hotshotit
def trg_from_pos(self, buf, pos, implicit=True, DEBUG=False, ac=None):
#DEBUG = True
if pos < 4:
return None
#DEBUG = True
# Last four chars and styles
if ac is None:
ac = AccessorCache(buf.accessor, pos, fetchsize=4)
last_pos, last_char, last_style = ac.getPrevPosCharStyle()
prev_pos, prev_char, prev_style = ac.getPrevPosCharStyle()
# Bump up how much text is retrieved when cache runs out
ac.setCacheFetchSize(20)
if DEBUG:
print("\nphp trg_from_pos")
print(" last_pos: %s" % last_pos)
print(" last_char: %s" % last_char)
print(" last_style: %r" % last_style)
ac.dump()
try:
# Note: If a "$" exists by itself, it's styled as whitespace.
# Generally we want it to be indicating a variable instead.
if last_style == self.whitespace_style and last_char != "$":
if DEBUG:
print("Whitespace style")
WHITESPACE = tuple(" \t\n\r\v\f")
if not implicit:
# If we're not already at the keyword style, find it
if prev_style != self.keyword_style:
prev_pos, prev_char, prev_style = ac.getPrecedingPosCharStyle(last_style, self.comment_styles)
if DEBUG:
print("Explicit: prev_pos: %d, style: %d, ch: %r" % (prev_pos, prev_style, prev_char))
else:
prev_pos = pos - 2
if last_char in WHITESPACE and \
(prev_style == self.keyword_style or
(prev_style == self.operator_style and prev_char == ",")):
p = prev_pos
style = prev_style
ch = prev_char
#print "p: %d" % p
while p > 0 and style == self.operator_style and ch == ",":
p, ch, style = ac.getPrecedingPosCharStyle(style, self.comment_styles_or_whitespace)
#print "p 1: %d" % p
if p > 0 and style == self.identifier_style:
# Skip the identifier too
p, ch, style = ac.getPrecedingPosCharStyle(style, self.comment_styles_or_whitespace)
#print "p 2: %d" % p
if DEBUG:
ac.dump()
p, text = ac.getTextBackWithStyle(style, self.comment_styles, max_text_len=len("implements"))
if DEBUG:
print("ac.getTextBackWithStyle:: pos: %d, text: %r" % (p, text))
if text in ("new", "extends"):
return Trigger(lang, TRG_FORM_CPLN, "classes", pos, implicit)
elif text in ("implements", ):
return Trigger(lang, TRG_FORM_CPLN, "interfaces", pos, implicit)
elif text in ("use", ):
return Trigger(lang, TRG_FORM_CPLN, "use", pos, implicit)
elif text in ("function", "const"):
# Check for a "use function" or "use const" expression.
p, ch, style = ac.getPrevPosCharStyle(ignore_styles=self.comment_styles)
if p > 0 and style == self.whitespace_style:
p, ch, style = ac.getPrecedingPosCharStyle(style, ignore_styles=self.comment_styles)
if p > 0 and style == self.keyword_style:
p, text = ac.getTextBackWithStyle(style, self.comment_styles, max_text_len=len("use "))
if text == "use":
return Trigger(lang, TRG_FORM_CPLN, "use", pos, implicit,
ilk=text)
elif prev_style == self.operator_style and \
prev_char == "," and implicit:
return self._functionCalltipTrigger(ac, prev_pos, DEBUG)
elif last_style == self.operator_style:
if DEBUG:
print(" lang_style is operator style")
print("Prev char: %r" % (prev_char))
ac.dump()
if last_char == ":":
if not prev_char == ":":
return None
ac.setCacheFetchSize(10)
p, c, style = ac.getPrecedingPosCharStyle(prev_style, self.comment_styles)
if DEBUG:
print("Preceding: %d, %r, %d" % (p, c, style))
if style is None:
return None
elif style == self.keyword_style:
# Check if it's a "self::" or "parent::" expression
p, text = ac.getTextBackWithStyle(self.keyword_style,
# Ensure we don't go too far
max_text_len=6)
if DEBUG:
print("Keyword text: %d, %r" % (p, text))
ac.dump()
if text not in ("parent", "self", "static"):
return None
return Trigger(lang, TRG_FORM_CPLN, "static-members",
pos, implicit)
elif last_char == ">":
if prev_char == "-":
p, c, style = ac.getPrevPosCharStyle(ignore_styles=self.comment_styles_or_whitespace)
if style in (self.variable_style, self.identifier_style) or \
(style == self.operator_style and c == ')'):
return Trigger(lang, TRG_FORM_CPLN, "object-members",
pos, implicit)
elif DEBUG:
print("Preceding style is not a variable, pos: %d, style: %d" % (p, style))
elif last_char in "(,":
# where to trigger from, updated by "," calltip handler
if DEBUG:
print("Checking for function calltip")
# Implicit calltip triggering from an arg separater ","
# http://bugs.activestate.com/show_bug.cgi?id=70470
if implicit and last_char == ',':
return self._functionCalltipTrigger(ac, prev_pos, DEBUG)
if prev_style in self.comment_styles_or_whitespace:
# Find previous non-ignored style then
p, c, prev_style = ac.getPrecedingPosCharStyle(prev_style, self.comment_styles_or_whitespace)
if prev_style in (self.identifier_style, self.keyword_style):
return Trigger(lang, TRG_FORM_CALLTIP, "call-signature",
pos, implicit)
elif last_char == "\\":
# Ensure does not trigger when defining a new namespace,
# i.e., do not trigger for:
# namespace foo\<|>
style = last_style
while style in (self.operator_style, self.identifier_style):
p, c, style = ac.getPrecedingPosCharStyle(style, max_look_back=30)
if style == self.whitespace_style:
p, c, style = ac.getPrecedingPosCharStyle(self.whitespace_style, max_look_back=30)
if style is None:
if DEBUG:
print("Triggering namespace completion")
return Trigger(lang, TRG_FORM_CPLN, "namespace-members",
pos, implicit)
prev_text = ac.getTextBackWithStyle(style, max_text_len=15)
if DEBUG:
print("prev_text: %r" % (prev_text, ))
if prev_text[1] == "use":
if DEBUG:
print("Triggering use-namespace completion")
return Trigger(lang, TRG_FORM_CPLN, "use-namespace",
pos, implicit)
elif prev_text[1] in ("const", "function"):
if DEBUG:
print("Triggering use-namespace completion with ilk %r" % (prev_text[1]))
return Trigger(lang, TRG_FORM_CPLN, "use-namespace",
pos, implicit, ilk=prev_text[1])
elif prev_text[1] != "namespace":
if DEBUG:
print("Triggering namespace completion")
return Trigger(lang, TRG_FORM_CPLN, "namespace-members",
pos, implicit)
elif last_style == self.variable_style or \
(not implicit and last_char == "$"):
if DEBUG:
print("Variable style")
# Completion for variables (builtins and user defined variables),
# must occur after a "$" character.
if not implicit and last_char == '$':
# Explicit call, move ahead one for real trigger position
pos += 1
if not implicit or prev_char == "$":
# Ensure we are not triggering over static class variables.
# Do this by checking that the preceding text is not "::"
# http://bugs.activestate.com/show_bug.cgi?id=78099
p, c, style = ac.getPrecedingPosCharStyle(last_style,
max_look_back=30)
if c == ":" and style == self.operator_style and \
ac.getTextBackWithStyle(style, max_text_len=3)[1] == "::":
return None
return Trigger(lang, TRG_FORM_CPLN, "variables",
pos-1, implicit)
elif last_style in (self.identifier_style, self.keyword_style):
if DEBUG:
if last_style == self.identifier_style:
print("Identifier style")
else:
print("Identifier keyword style")
# Completion for keywords,function and class names
# Works after first 3 characters have been typed
#if DEBUG:
# print "identifier_style: pos - 4 %s" % (accessor.style_at_pos(pos - 4))
#third_char, third_style = last_four_char_and_styles[2]
#fourth_char, fourth_style = last_four_char_and_styles[3]
if prev_style == last_style:
trig_pos, ch, style = ac.getPrevPosCharStyle()
if style == last_style:
p, ch, style = ac.getPrevPosCharStyle(ignore_styles=self.comment_styles)
# style is None if no change of style (not ignored) was
# found in the last x number of chars
#if not implicit and style == last_style:
# if DEBUG:
# print "Checking back further for explicit call"
# p, c, style = ac.getPrecedingPosCharStyle(style, max_look_back=100)
# if p is not None:
# trg_pos = p + 3
if style in (None, self.whitespace_style,
self.operator_style):
# Ensure we are not in another trigger zone, we do
# this by checking that the preceeding text is not
# one of "->", "::", "new", "function", "class", ...
if style == self.whitespace_style:
p, c, style = ac.getPrecedingPosCharStyle(self.whitespace_style, max_look_back=30)
if style is None:
return Trigger(lang, TRG_FORM_CPLN, "functions",
trig_pos, implicit)
prev_text = ac.getTextBackWithStyle(style, max_text_len=15)
if DEBUG:
print("prev_text: %r" % (prev_text, ))
if (prev_text[1] not in ("new", "function", "use",
"class", "interface", "implements",
"public", "private", "protected",
"final", "abstract", "instanceof",)
# For the operator styles, we must use
# endswith, as it could follow a "()",
# bug 90846.
and prev_text[1][-2:] not in ("->", "::",)
# Don't trigger when accessing a
# namespace - bug 88736.
and not prev_text[1].endswith("\\")):
return Trigger(lang, TRG_FORM_CPLN, "functions",
trig_pos, implicit)
# If we want implicit triggering on more than 3 chars
#elif style == self.identifier_style:
# p, c, style = ac.getPrecedingPosCharStyle(self.identifier_style)
# return Trigger(lang, TRG_FORM_CPLN, "functions",
# p+1, implicit)
elif DEBUG:
print("identifier preceeded by an invalid style: " \
"%r, p: %r" % (style, p, ))
elif last_char == '_' and prev_char == '_' and \
style == self.whitespace_style:
# XXX - Check the php version, magic methods only
# appeared in php 5.
p, ch, style = ac.getPrevPosCharStyle(ignore_styles=self.comment_styles)
if style == self.keyword_style and \
ac.getTextBackWithStyle(style, max_text_len=9)[1] == "function":
if DEBUG:
print("triggered:: complete magic-methods")
return Trigger(lang, TRG_FORM_CPLN, "magic-methods",
prev_pos, implicit)
# PHPDoc completions
elif last_char == "@" and last_style in self.comment_styles:
# If the preceeding non-whitespace character is a "*" or newline
# then we complete for phpdoc tag names
p = last_pos - 1
min_p = max(0, p - 50) # Don't look more than 50 chars
if DEBUG:
print("Checking match for phpdoc completions")
accessor = buf.accessor
while p >= min_p and \
accessor.style_at_pos(p) in self.comment_styles:
ch = accessor.char_at_pos(p)
p -= 1
#if DEBUG:
# print "Looking at ch: %r" % (ch)
if ch in "*\r\n":
break
elif ch not in " \t\v":
# Not whitespace, not a valid tag then
return None
else:
# Nothing found in the specified range
if DEBUG:
print("trg_from_pos: not a phpdoc")
return None
if DEBUG:
print("Matched trigger for phpdoc completion")
return Trigger("PHP", TRG_FORM_CPLN,
"phpdoc-tags", pos, implicit)
# PHPDoc calltip
elif last_char in " \t" and last_style in self.comment_styles:
# whitespace in a comment, see if it matches for phpdoc calltip
p = last_pos - 1
min_p = max(0, p - 50) # Don't look more than 50 chars
if DEBUG:
print("Checking match for phpdoc calltip")
ch = None
ident_found_pos = None
accessor = buf.accessor
while p >= min_p and \
accessor.style_at_pos(p) in self.comment_styles:
ch = accessor.char_at_pos(p)
p -= 1
if ident_found_pos is None:
#print "phpdoc: Looking for identifier, ch: %r" % (ch)
if ch in " \t":
pass
elif _isident(ch):
ident_found_pos = p+1
else:
if DEBUG:
print("No phpdoc, whitespace not preceeded " \
"by an identifer")
return None
elif ch == "@":
# This is what we've been looking for!
phpdoc_field = accessor.text_range(p+2,
ident_found_pos+1)
if DEBUG:
print("Matched trigger for phpdoc calltip: '%s'" % (
phpdoc_field, ))
return Trigger("PHP", TRG_FORM_CALLTIP,
"phpdoc-tags", ident_found_pos, implicit,
phpdoc_field=phpdoc_field)
elif not _isident(ch):
if DEBUG:
print("No phpdoc, identifier not preceeded by '@'")
# Not whitespace, not a valid tag then
return None
# Nothing found in the specified range
if DEBUG:
print("No phpdoc, ran out of characters to look at.")
# Array completions
elif last_style == self.string_style and last_char in '\'"':
if prev_char != '[':
if prev_style in self.comment_styles_or_whitespace:
# Look back further.
prev_pos, prev_char, prev_style = ac.getPrevPosCharStyle(ignore_styles=self.comment_styles_or_whitespace)
if prev_char == '[':
# We're good to go.
if DEBUG:
print("Matched trigger for array completions")
return Trigger("PHP", TRG_FORM_CPLN,
"array-members", pos, implicit,
bracket_pos=prev_pos,
trg_char=last_char)
# Variable completions inside of comments
elif prev_char == "$" and last_style in self.comment_styles:
if DEBUG:
print("Comment variable style")
# Completion for variables (builtins and user defined variables),
# must occur after a "$" character.
return Trigger(lang, TRG_FORM_CPLN, "comment-variables",
pos-1, implicit)
elif DEBUG:
print("trg_from_pos: no handle for style: %d" % last_style)
except IndexError:
# Not enough chars found, therefore no trigger
pass
return None
#@util.hotshotit
def preceding_trg_from_pos(self, buf, pos, curr_pos,
preceding_trg_terminators=None, DEBUG=False):
#DEBUG = True
# Try the default preceding_trg_from_pos handler
trg = ProgLangTriggerIntelMixin.preceding_trg_from_pos(
self, buf, pos, curr_pos, preceding_trg_terminators,
DEBUG=DEBUG)
if trg is not None:
return trg
# Else, let's try to work out some other options
accessor = buf.accessor
prev_style = accessor.style_at_pos(curr_pos - 1)
if prev_style in (self.identifier_style, self.keyword_style):
# We don't know what to trigger here... could be one of:
# functions:
# apache<$><|>_getenv()...
# if(get_e<$><|>nv()...
# classes:
# new Exce<$><|>ption()...
# extends Exce<$><|>ption()...
# interfaces:
# implements apache<$><|>_getenv()...
ac = AccessorCache(accessor, curr_pos)
pos_before_identifer, ch, prev_style = \
ac.getPrecedingPosCharStyle(prev_style)
if DEBUG:
print("\nphp preceding_trg_from_pos, first chance for identifer style")
print(" curr_pos: %d" % (curr_pos))
print(" pos_before_identifer: %d" % (pos_before_identifer))
print(" ch: %r" % ch)
print(" prev_style: %d" % prev_style)
ac.dump()
if pos_before_identifer < pos:
resetPos = min(pos_before_identifer + 4, accessor.length() - 1)
ac.resetToPosition(resetPos)
if DEBUG:
print("preceding_trg_from_pos:: reset to position: %d, ac now:" % (resetPos))
ac.dump()
# Trigger on the third identifier character
return self.trg_from_pos(buf, resetPos,
implicit=False, DEBUG=DEBUG, ac=ac)
elif DEBUG:
print("Out of scope of the identifier")
elif prev_style in self.comment_styles:
# Check if there is a PHPDoc to provide a calltip for, example:
# /** @param $foo foobar - This is field for <|>
if DEBUG:
print("\nphp preceding_trg_from_pos::phpdoc: check for calltip")
comment = accessor.text_range(max(0, curr_pos-200), curr_pos)
at_idx = comment.rfind("@")
if at_idx >= 0:
if DEBUG:
print("\nphp preceding_trg_from_pos::phpdoc: contains '@'")
space_idx = comment[at_idx:].find(" ")
if space_idx >= 0:
# Trigger after the space character.
trg_pos = (curr_pos - len(comment)) + at_idx + space_idx + 1
if DEBUG:
print("\nphp preceding_trg_from_pos::phpdoc: calltip at %d" % (trg_pos, ))
return self.trg_from_pos(buf, trg_pos,
implicit=False, DEBUG=DEBUG)
_phpdoc_cplns = [ ("variable", t) for t in sorted(phpdoc_tags) ]
#@util.hotshotit
def async_eval_at_trg(self, buf, trg, ctlr):
if _xpcom_:
trg = UnwrapObject(trg)
ctlr = UnwrapObject(ctlr)
pos = trg.pos
ctlr.start(buf, trg)
#print "trg.type: %r" % (trg.type)
# PHPDoc completions
if trg.id == ("PHP", TRG_FORM_CPLN, "phpdoc-tags"):
#TODO: Would like a "javadoc tag" completion image name.
ctlr.set_cplns(self._phpdoc_cplns)
ctlr.done("success")
return
# PHPDoc calltip
elif trg.id == ("PHP", TRG_FORM_CALLTIP, "phpdoc-tags"):
phpdoc_field = trg.extra.get("phpdoc_field")
if phpdoc_field:
#print "phpdoc_field: %r" % (phpdoc_field, )
calltip = phpdoc_tags.get(phpdoc_field)
if calltip:
ctlr.set_calltips([calltip])
ctlr.done("success")
return
elif trg.type in ("classes", "interfaces"):
# Triggers from zero characters, thus calling citdl_expr_from_trg
# is no help
line = buf.accessor.line_from_pos(pos)
evalr = PHPTreeEvaluator(ctlr, buf, trg, "", line)
buf.mgr.request_eval(evalr)
else:
try:
citdl_expr = self.citdl_expr_from_trg(buf, trg)
except CodeIntelError as ex:
ctlr.error(str(ex))
ctlr.done("error")
return
line = buf.accessor.line_from_pos(pos)
evalr = PHPTreeEvaluator(ctlr, buf, trg, citdl_expr, line)
buf.mgr.request_eval(evalr)
def _citdl_expr_from_pos(self, trg, buf, pos, implicit=True,
include_forwards=False, DEBUG=False):
#DEBUG = True
#PERF: Would dicts be faster for all of these?
WHITESPACE = tuple(" \t\n\r\v\f")
EOL = tuple("\r\n")
BLOCKCLOSES = tuple(")}]")
STOPOPS = tuple("({[,&$+=^|%/<;:->!.@?")
EXTRA_STOPOPS_PRECEDING_IDENT = BLOCKCLOSES # Might be others.
#TODO: This style picking is a problem for the LangIntel move.
if trg.type == "comment-variables":
# Dev note: skip_styles in the other cases below will be a dict.
skip_styles = set()
elif implicit:
skip_styles = buf.implicit_completion_skip_styles
else:
skip_styles = buf.completion_skip_styles
citdl_expr = []
accessor = buf.accessor
# Use a cache of characters, easy to keep track this way
i = pos
ac = AccessorCache(accessor, i)
if include_forwards:
try:
# Move ahead to include forward chars as well
while 1:
i, ch, style = ac.getNextPosCharStyle()
if DEBUG:
print("include_forwards:: i now: %d, ch: %r" % (i, ch))
if ch in WHITESPACE:
if DEBUG:
print("include_forwards:: ch in WHITESPACE")
break
if ch in STOPOPS:
if DEBUG:
print("include_forwards:: ch in STOPOPS, i:%d ch:%r" % (i, ch))
break
elif ch in BLOCKCLOSES:
if DEBUG:
print("include_forwards:: ch in BLOCKCLOSES, i:%d ch:%r" % (i, ch))
break
# Move back to last valid char
i -= 1
if DEBUG:
if i > pos:
print("include_forwards:: Including chars from pos %d up to %d" % (pos, i))
else:
print("include_forwards:: No valid chars forward from pos %d, i now: %d" % (pos, i))
except IndexError:
# Nothing forwards, user what we have then
i = min(i, accessor.length() - 1)
if DEBUG:
print("include_forwards:: No more buffer, i now: %d" % (i))
ac.resetToPosition(i)
ch = None
try:
while i >= 0:
if ch == None and include_forwards:
i, ch, style = ac.getCurrentPosCharStyle()
else:
i, ch, style = ac.getPrevPosCharStyle()
if DEBUG:
print("i now: %d, ch: %r" % (i, ch))
if ch in WHITESPACE:
if trg.type in ("use-namespace", "namespace-members"):
# Namespaces cannot be split over whitespace.
break
while ch in WHITESPACE:
# drop all whitespace
next_char = ch
i, ch, style = ac.getPrevPosCharStyle()
if ch in WHITESPACE \
or (ch == '\\' and next_char in EOL):
if DEBUG:
print("drop whitespace: %r" % ch)
# If there are two whitespace-separated words then this is
# (likely or always?) a language keyword or declaration
# construct at which we want to stop. E.g.
# if foo<|> and ...
# def foo<|>(...
# if \foo<|>(... # uses a namespace
if citdl_expr \
and (_isident(citdl_expr[-1]) or citdl_expr[-1] == '\\') \
and (_isident(ch) or _isdigit(ch)):
if DEBUG:
print("stop at (likely?) start of keyword or "\
"declaration: %r" % ch)
break
# Not whitespace anymore, move into the main checks below
if DEBUG:
print("Out of whitespace: i now: %d, ch: %s" % (i, ch))
if style in skip_styles: # drop styles to ignore
while i >= 0 and style in skip_styles:
i, ch, style = ac.getPrevPosCharStyle()
if DEBUG:
print("drop char of style to ignore: %r" % ch)
elif ch in ":>" and i > 0:
# Next char has to be ":" or "-" respectively
prev_pos, prev_ch, prev_style = ac.getPrevPosCharStyle()
if (ch == ">" and prev_ch == "-") or \
(ch == ":" and prev_ch == ":"):
citdl_expr.append(".")
if DEBUG:
print("Turning member accessor '%s%s' into '.'" % (prev_ch, ch))
i -= 2
else:
if DEBUG:
print("citdl_expr: %r" % (citdl_expr))
print("stop at special stop-operator %d: %r" % (i, ch))
break
elif (ch in STOPOPS or ch in EXTRA_STOPOPS_PRECEDING_IDENT) and \
(ch != ")" or (citdl_expr and citdl_expr[-1] != ".")):
if ch == '$':
# This may not be the end of the road, given static
# variables are accessed through "Class::$static".
prev_pos, prev_ch, prev_style = ac.peekPrevPosCharStyle()
if prev_ch == ":":
# Continue building up the citdl then.
continue
if DEBUG:
print("citdl_expr: %r" % (citdl_expr))
print("stop at stop-operator %d: %r" % (i, ch))
break
elif ch in BLOCKCLOSES:
if DEBUG:
print("found block at %d: %r" % (i, ch))
citdl_expr.append(ch)
BLOCKS = { # map block close char to block open char
')': '(',
']': '[',
'}': '{',
}
stack = [] # stack of blocks: (<block close char>, <style>)
stack.append( (ch, style, BLOCKS[ch], i) )
while i >= 0:
i, ch, style = ac.getPrevPosCharStyle()
if DEBUG:
print("finding matching brace: ch %r (%s), stack %r"\
% (ch, ', '.join(buf.style_names_from_style_num(style)), stack))
if ch in BLOCKS and style not in skip_styles:
stack.append( (ch, style, BLOCKS[ch]) )
elif ch == stack[-1][2] and style not in skip_styles:
#XXX Replace the second test with the following
# when LexPython+SilverCity styling bugs are fixed
# (spurious 'stderr' problem):
# and style == stack[-1][1]:
stack.pop()
if not stack:
if DEBUG:
print("jump to matching brace at %d: %r" % (i, ch))
citdl_expr.append(ch)
break
else:
# Didn't find the matching brace.
if DEBUG:
print("couldn't find matching brace")
raise EvalError("could not find matching brace for "
"'%s' at position %d"
% (stack[-1][0], stack[-1][3]))
else:
if DEBUG:
style_names = buf.style_names_from_style_num(style)
print("add char: %r (%s)" % (ch, ', '.join(style_names)))
citdl_expr.append(ch)
i -= 1
except IndexError:
# Nothing left to consume, return what we have
pass
# Remove any unecessary starting dots
while citdl_expr and citdl_expr[-1] == ".":
citdl_expr.pop()
citdl_expr.reverse()
citdl_expr = ''.join(citdl_expr)
if DEBUG:
print("return: %r" % citdl_expr)
print(util.banner("done"))
return citdl_expr
def citdl_expr_from_trg(self, buf, trg):
"""Return a PHP CITDL expression preceding the given trigger.
The expression drops newlines, whitespace, and function call
arguments -- basically any stuff that is not used by the codeintel
database system for determining the resultant object type of the
expression. For example (in which <|> represents the given position):
GIVEN RETURN
----- ------
foo-<|>> foo
Foo:<|>: Foo
foo(bar-<|>> bar
foo(bar,blam)-<|>> foo()
foo(bar, foo()
blam)-<|>>
foo(arg1, arg2)->bar-<|>> foo().bar
Foo(arg1, arg2)::bar-<|>> Foo().bar
Foo\bar:<|>: Foo\bar
Foo\bar::bam-<|>> Foo\bar.bam
Foo\bar(arg1, arg2)::bam-<|>> Foo\bar().bam
"""
#DEBUG = True
DEBUG = False
if DEBUG:
print(util.banner("%s citdl_expr_from_trg @ %r" % (buf.lang, trg)))
if trg.form == TRG_FORM_CPLN:
# "->" or "::"
if trg.type == "classes":
i = trg.pos + 1
elif trg.type == "functions":
i = trg.pos + 3 # 3-char trigger, skip over it
elif trg.type in ("variables", "comment-variables"):
i = trg.pos + 1 # triggered on the $, skip over it
elif trg.type == "array-members":
i = trg.extra.get("bracket_pos") # triggered on foo['
elif trg.type == "use":
i = trg.pos + 1
elif trg.type == "namespace-members" or \
trg.type == "use-namespace":
i = trg.pos - 1
else:
i = trg.pos - 2 # skip past the trigger char
return self._citdl_expr_from_pos(trg, buf, i, trg.implicit,
DEBUG=DEBUG)
elif trg.form == TRG_FORM_DEFN:
return self.citdl_expr_under_pos(trg, buf, trg.pos, DEBUG)
else: # trg.form == TRG_FORM_CALLTIP:
# (<|>
return self._citdl_expr_from_pos(trg, buf, trg.pos-1, trg.implicit,
DEBUG=DEBUG)
def citdl_expr_under_pos(self, trg, buf, pos, DEBUG=False):
"""Return a PHP CITDL expression around the given pos.
Similar to citdl_expr_from_trg(), but looks forward to grab additional
characters.
GIVEN RETURN
----- ------
foo-<|>> foo
F<|>oo:: Foo
foo->ba<|>r foo.bar
f<|>oo->bar foo
foo(bar-<|>> bar
foo(bar,blam)-<|>> foo()
foo(bar, foo()
blam)-<|>>
foo(arg1, arg2)->bar-<|>> foo().bar
Foo(arg1, arg2)::ba<|>r-> Foo().bar
Fo<|>o(arg1, arg2)::bar-> Foo
"""
#DEBUG = True
expr = self._citdl_expr_from_pos(trg, buf, pos-1, implicit=True,
include_forwards=True, DEBUG=DEBUG)
if expr:
# Chop off any trailing "." characters
return expr.rstrip(".")
return expr
def libs_from_buf(self, buf):
env = buf.env
# A buffer's libs depend on its env and the buf itself so
# we cache it on the env and key off the buffer.
if "php-buf-libs" not in env.cache:
env.cache["php-buf-libs"] = weakref.WeakKeyDictionary()
cache = env.cache["php-buf-libs"] # <buf-weak-ref> -> <libs>
if buf not in cache:
# - curdirlib
# Using the dirname of this buffer isn't always right, but
# hopefully is a good first approximation.
cwd = dirname(buf.path)
if cwd == "<Unsaved>":
libs = []
else:
libs = [ self.mgr.db.get_lang_lib("PHP", "curdirlib", [cwd], "PHP")]
libs += self._buf_indep_libs_from_env(env)
cache[buf] = libs
return cache[buf]
def lpaths_from_blob(self, blob):
"""Return <lpaths> for this blob
where,
<lpaths> is a set of externally referencable lookup-paths, e.g.
[("MyOwnClass",), ("MyOwnClass", "function1"), ...]
"""
return set(lpath for child in blob
for lpath in _walk_php_symbols(child))
def _php_from_env(self, env):
import which
path = [d.strip()
for d in env.get_envvar("PATH", "").split(os.pathsep)
if d.strip()]
for exe_name in ("php", "php4", "php-cgi", "php-cli"):
try:
return which.which(exe_name, path=path)
except which.WhichError:
pass
return None
def _php_info_from_php(self, php, env):
"""Call the given PHP and return:
(<version>, <include_path>)
Returns (None, []) if could not determine.
"""
import process
import tempfile
# Use a marker to separate the start of output from possible
# leading lines of PHP loading errors/logging.
marker = "--- Start of Good Stuff ---"
info_cmd = (b'<?php '
+ b'echo("%s\n");' % marker.encode('utf-8')
+ b'echo(phpversion()."\n");'
+ b'echo(ini_get("include_path")."\n");'
+ b' ?>')
argv = [php]
envvars = env.get_all_envvars()
php_ini_path = env.get_pref("phpConfigFile")
if php_ini_path:
envvars["PHPRC"] = php_ini_path
fd, filepath = tempfile.mkstemp(suffix=".php")
try:
os.write(fd, info_cmd)
os.close(fd)
argv.append(filepath)
p = process.ProcessOpen(argv, env=env.get_all_envvars())
stdout, stderr = p.communicate()
finally:
os.remove(filepath)
stdout_lines = stdout.splitlines(0)
retval = p.returncode
if retval:
log.warn("failed to determine PHP info:\n"
" path: %s\n"
" retval: %s\n"
" stdout:\n%s\n"
" stderr:\n%s\n",
php, retval, util.indent('\n'.join(stdout_lines)),
util.indent(stderr))
return None, []
stdout_lines = stdout_lines[stdout_lines.index(marker)+1:]
php_ver = stdout_lines[0]
include_path = [p.strip() for p in stdout_lines[1].split(os.pathsep)
if p.strip()]