forked from SublimeCodeIntel/SublimeCodeIntel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlang_javascript.py
More file actions
4341 lines (4023 loc) · 197 KB
/
lang_javascript.py
File metadata and controls
4341 lines (4023 loc) · 197 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 *****
"""JavaScript support for Code Intelligence
This is a Language Engine for the Code Intelligence (codeintel) system.
Code Intelligence XML format. See:
http://specs.tl.activestate.com/kd/kd-0100.html#xml-based-import-export-syntax-cix
Future ideas and changes for the JavaScript ciler:
* give anonymous functions a unique identifier, so referencing code
is able to look up the correct anonymous function.
* create a class prototype structure
* separate class prototype variables and functions from the local variables
* use better push/pop handling
"""
import os
from os.path import splitext, basename, exists, dirname, normpath
import sys
import types
import logging
import operator
from io import StringIO
import weakref
from glob import glob
from collections import deque
from ciElementTree import Element, ElementTree, SubElement
import SilverCity
from SilverCity.Lexer import Lexer
from SilverCity import ScintillaConstants
from SilverCity.ScintillaConstants import (
SCE_C_COMMENT, SCE_C_COMMENTDOC, SCE_C_COMMENTDOCKEYWORD,
SCE_C_COMMENTDOCKEYWORDERROR, SCE_C_COMMENTLINE,
SCE_C_COMMENTLINEDOC, SCE_C_DEFAULT, SCE_C_IDENTIFIER, SCE_C_NUMBER,
SCE_C_OPERATOR, SCE_C_STRING, SCE_C_CHARACTER, SCE_C_STRINGEOL, SCE_C_WORD,
SCE_UDL_CSL_COMMENT, SCE_UDL_CSL_COMMENTBLOCK, SCE_UDL_CSL_DEFAULT,
SCE_UDL_CSL_IDENTIFIER, SCE_UDL_CSL_NUMBER, SCE_UDL_CSL_OPERATOR,
SCE_UDL_CSL_REGEX, SCE_UDL_CSL_STRING, SCE_UDL_CSL_WORD,
)
from codeintel2.citadel import CitadelBuffer, ImportHandler, CitadelLangIntel
from codeintel2.buffer import Buffer
from codeintel2.tree_javascript import JavaScriptTreeEvaluator
from codeintel2 import util
from codeintel2.common import *
from codeintel2.indexer import PreloadBufLibsRequest, PreloadLibRequest
from codeintel2.jsdoc import JSDoc, JSDocParameter, jsdoc_tags
from codeintel2.gencix_utils import *
from codeintel2.database.langlib import LangDirsLib
from codeintel2.udl import UDLBuffer, is_udl_csl_style
from codeintel2.accessor import AccessorCache, KoDocumentAccessor
from codeintel2.langintel import (ParenStyleCalltipIntelMixin,
ProgLangTriggerIntelMixin,
PythonCITDLExtractorMixin)
if _xpcom_:
from xpcom.server import UnwrapObject
#---- globals
lang = "JavaScript"
# Setup the logger
log = logging.getLogger("codeintel.javascript.lang")
# log.setLevel(logging.DEBUG)
# log.setLevel(logging.INFO)
util.makePerformantLogger(log)
# When enabled, will add a specific path attribute to each cix element for where
# this element was originally created from, good for debugging gencix JavaScript
# API catalogs (YUI, ExtJS).
ADD_PATH_CIX_INFO = False
# ADD_PATH_CIX_INFO = True
# States used by JavaScriptScanner when parsing information
S_DEFAULT = 0
S_IN_ARGS = 1
S_IN_ASSIGNMENT = 2
S_IGNORE_SCOPE = 3
S_OBJECT_ARGUMENT = 4
# Types used by JavaScriptScanner when parsing information
TYPE_NONE = 0
TYPE_FUNCTION = 1
TYPE_VARIABLE = 2
TYPE_GETTER = 3
TYPE_SETTER = 4
TYPE_MEMBER = 5
TYPE_OBJECT = 6
TYPE_CLASS = 7
TYPE_PARENT = 8
TYPE_ALIAS = 9
#---- language support
class JavaScriptLexer(Lexer):
lang = lang
def __init__(self, mgr):
self._properties = SilverCity.PropertySet()
self._lexer = SilverCity.find_lexer_module_by_id(
ScintillaConstants.SCLEX_CPP)
jsli = mgr.lidb.langinfo_from_lang(self.lang)
self._keyword_lists = [
SilverCity.WordList(' '.join(jsli.keywords)),
SilverCity.WordList(),
SilverCity.WordList(),
SilverCity.WordList(),
SilverCity.WordList()
]
class PureJavaScriptStyleClassifier:
def __init__(self):
self.is_udl = False
self.operator_style = SCE_C_OPERATOR
self.identifier_style = SCE_C_IDENTIFIER
self.keyword_style = SCE_C_WORD
self.comment_styles = (SCE_C_COMMENT,
SCE_C_COMMENTDOC,
SCE_C_COMMENTLINE,
SCE_C_COMMENTLINEDOC,
SCE_C_COMMENTDOCKEYWORD,
SCE_C_COMMENTDOCKEYWORDERROR)
self.string_styles = (SCE_C_STRING, SCE_C_CHARACTER, SCE_C_STRINGEOL)
self.whitespace_style = SCE_C_DEFAULT
self.ignore_styles = self.comment_styles + (self.whitespace_style, )
class UDLJavaScriptStyleClassifier:
def __init__(self):
self.is_udl = True
self.operator_style = SCE_UDL_CSL_OPERATOR
self.identifier_style = SCE_UDL_CSL_IDENTIFIER
self.keyword_style = SCE_UDL_CSL_WORD
self.comment_styles = (SCE_UDL_CSL_COMMENT,
SCE_UDL_CSL_COMMENTBLOCK,)
self.string_styles = (SCE_UDL_CSL_STRING, )
self.whitespace_style = SCE_UDL_CSL_DEFAULT
self.ignore_styles = self.comment_styles + (self.whitespace_style, )
pureJSClassifier = PureJavaScriptStyleClassifier()
udlJSClassifier = UDLJavaScriptStyleClassifier()
class JavaScriptLangIntel(CitadelLangIntel,
ParenStyleCalltipIntelMixin,
ProgLangTriggerIntelMixin,
PythonCITDLExtractorMixin):
lang = lang
_evaluatorClass = JavaScriptTreeEvaluator
extraPathsPrefName = "javascriptExtraPaths"
# The way namespacing is done with variables in JS means that grouping
# global vars is just annoying.
cb_group_global_vars = False
# Define the trigger chars we use, used by ProgLangTriggerIntelMixin
trg_chars = tuple(".(,@'\" ")
calltip_trg_chars = tuple('(') # excluded ' ' for perf (bug 55497)
# Define literal mapping to citdl member, used in PythonCITDLExtractorMixin
citdl_from_literal_type = {"string": "String"}
def cb_variable_data_from_elem(self, elem):
"""Use the 'namespace' image in the Code Browser for a variable
acting as one.
"""
data = CitadelLangIntel.cb_variable_data_from_elem(self, elem)
if len(elem) and data["img"].startswith("variable"):
data["img"] = data["img"].replace("variable", "namespace")
return data
def _functionCalltipTrigger(self, ac, jsClassifier, 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=93864
if DEBUG:
print("Arg separater found, looking for start of function, pos: %r" % (pos, ))
ac.getPrevPosCharStyle()
# Move back to the open paren of the function
paren_count = 0
p = pos
min_p = max(0, p - 100) # look back max 100 chars
while p > min_p:
p, c, style = ac.getPrecedingPosCharStyle(
ignore_styles=jsClassifier.comment_styles)
if DEBUG:
print(' p: %r, ch: %r, st: %d' % (p, c, style))
loopcount = 0
while style == jsClassifier.operator_style and loopcount < 10:
loopcount += 1
if c == ")":
paren_count += 1
if DEBUG:
print(' paren_count: %r' % (paren_count, ))
elif c == "(":
if DEBUG:
print(' paren_count: %r' % (paren_count, ))
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 jsClassifier.ignore_styles:
# Find previous non-ignored style then
p, c, style = ac.getPrecedingPosCharStyle(
style, jsClassifier.ignore_styles)
if style == jsClassifier.identifier_style:
# Ensure that this isn't a new function
# declaration.
p, c, style = ac.getPrecedingPosCharStyle(
style, jsClassifier.ignore_styles)
if style == jsClassifier.keyword_style:
p, prev_text = ac.getTextBackWithStyle(
style, jsClassifier.ignore_styles, max_text_len=len("function")+1)
if prev_text in ("function", ):
# Don't trigger here
return None
return Trigger(lang, TRG_FORM_CALLTIP,
"call-signature",
trg_from_pos, implicit=True)
return None
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
p, c, style = ac.getPrevPosCharStyle()
if DEBUG:
print(' p: %r, ch: %r, st: %d' % (p, c, style))
# 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
def trg_from_pos(self, buf, pos, implicit=True,
lang=None):
DEBUG = False # not using 'logging' system, because want to be fast
# DEBUG = True
# if DEBUG:
# print util.banner("JavaScript trg_from_pos(pos=%r, implicit=%r)"
# % (pos, implicit))
if pos == 0:
return None
if lang is None:
lang = self.lang
if isinstance(buf, UDLBuffer):
jsClassifier = udlJSClassifier
else:
jsClassifier = pureJSClassifier
accessor = buf.accessor
last_pos = pos - 1
last_char = accessor.char_at_pos(last_pos)
last_style = accessor.style_at_pos(last_pos)
if DEBUG:
print(" last_pos: %s" % last_pos)
print(" last_ch: %r" % last_char)
print(" last_style: %r" % last_style)
if (jsClassifier.is_udl and last_char == '/'
and last_pos > 0 and accessor.char_at_pos(last_pos-1) == '<'
and last_style not in (SCE_UDL_CSL_STRING,
SCE_UDL_CSL_COMMENTBLOCK,
SCE_UDL_CSL_COMMENT,
SCE_UDL_CSL_REGEX)):
# Looks like start of closing '</script>' tag. While typing this
# the styling will still be in the CSL range.
return Trigger(buf.m_lang, TRG_FORM_CPLN,
"end-tag", pos, implicit)
# JSDoc completions
elif last_char == "@" and last_style in jsClassifier.comment_styles:
# If the preceeding non-whitespace character is a "*" or newline
# then we complete for jsdoc tag names
p = last_pos - 1
min_p = max(
0, p - 50) # Don't bother looking more than 50 chars
if DEBUG:
print("Checking match for jsdoc completions")
while p >= min_p and \
accessor.style_at_pos(p) in jsClassifier.comment_styles:
ch = accessor.char_at_pos(p)
p -= 1
# if DEBUG:
# print "Looking at ch: %r" % (ch)
if ch == "*" or 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 jsdoc")
return None
if DEBUG:
print("Matched trigger for jsdoc completion")
return Trigger(lang, TRG_FORM_CPLN,
"jsdoc-tags", pos, implicit)
# JSDoc calltip
elif last_char in " \t" and last_style in jsClassifier.comment_styles:
# whitespace in a comment, see if it matches for jsdoc calltip
p = last_pos - 1
min_p = max(
0, p - 50) # Don't bother looking more than 50 chars
if DEBUG:
print("Checking match for jsdoc calltip")
ch = None
ident_found_pos = None
while p >= min_p and \
accessor.style_at_pos(p) in jsClassifier.comment_styles:
ch = accessor.char_at_pos(p)
p -= 1
if ident_found_pos is None:
# print "jsdoc: Looking for identifier, at ch: %r" % (ch)
if ch in " \t":
pass
elif _isident(ch):
ident_found_pos = p+1
else:
if DEBUG:
print("No jsdoc, whitespace not preceeded by an " \
"identifer")
return None
elif ch == "@":
# This is what we've been looking for!
jsdoc_field = accessor.text_range(p+2, ident_found_pos+1)
if DEBUG:
print("Matched trigger for jsdoc calltip: '%s'" % (jsdoc_field, ))
return Trigger(lang, TRG_FORM_CALLTIP,
"jsdoc-tags", ident_found_pos, implicit,
jsdoc_field=jsdoc_field)
elif not _isident(ch):
if DEBUG:
print("No jsdoc, identifier not preceeded by an '@'")
# Not whitespace, not a valid tag then
return None
# Nothing found in the specified range
if DEBUG:
print("No jsdoc, ran out of characters to look at.")
elif last_char not in self.trg_chars:
# Check if this could be a 'complete-names' trigger, this is
# an implicit 3-character trigger i.e. " win<|>" or an explicit
# trigger on 3 or more characters. We cannot support less than than
# 3-chars without involving a big penalty hit (as our codeintel
# database uses a 3-char index).
if last_pos >= 2 and (last_style == jsClassifier.identifier_style or
last_style == jsClassifier.keyword_style):
if DEBUG:
print("Checking for 'names' three-char-trigger")
# The previous two characters must be the same style.
p = last_pos - 1
min_p = max(0, p - 1)
citdl_expr = [last_char]
while p >= min_p:
if accessor.style_at_pos(p) != last_style:
if DEBUG:
print("No 'names' trigger, inconsistent style: " \
"%d, pos: %d" % (accessor.style_at_pos(p), p))
break
citdl_expr += accessor.char_at_pos(p)
p -= 1
else:
# Now check the third character back.
# "p < 0" is for when we are at the start of a document.
if p >= 0:
ac = None
style = accessor.style_at_pos(p)
if style == last_style:
if implicit:
if DEBUG:
print("No 'names' trigger, third char " \
"style: %d, pos: %d" % (style, p))
return None
else:
# explicit can be longer than 3-chars, skip over
# the rest of the word/identifier.
ac = AccessorCache(accessor, p)
p, ch, style = ac.getPrecedingPosCharStyle(last_style,
jsClassifier.ignore_styles,
max_look_back=80)
# Now we know that we are three identifier characters
# preceeded by something different, which is not that
# common, we now need to be a little more cpu-intensive
# with our search to ensure we're not preceeded by a
# dot ".", as this would mean a different cpln type!
if style == jsClassifier.whitespace_style:
# Find out what occurs before the whitespace,
# ignoring whitespace and comments.
if ac is None:
ac = AccessorCache(accessor, p)
p, ch, style = ac.getPrecedingPosCharStyle(style,
jsClassifier.ignore_styles,
max_look_back=80)
if style is not None:
ch = accessor.char_at_pos(p)
if ch == ".":
if DEBUG:
print("No 'names' trigger, third char " \
"is a dot")
return None
elif style == jsClassifier.keyword_style:
p, prev_text = ac.getTextBackWithStyle(
style, jsClassifier.ignore_styles, max_text_len=len("function")+1)
if prev_text in ("function", ):
# We don't trigger after function, this is
# defining a new item that does not exist.
if DEBUG:
print("No 'names' trigger, preceeding "\
"text is 'function'")
return None
if DEBUG:
print("triggering 'javascript-complete-names' at " \
"pos: %d" % (last_pos - 2, ))
return Trigger(self.lang, TRG_FORM_CPLN,
"names", last_pos - 2, implicit,
citdl_expr="".join(reversed(citdl_expr)))
if DEBUG:
print("trg_from_pos: no: %r is not in %r" % (
last_char, "".join(self.trg_chars), ))
return None
elif last_style == jsClassifier.operator_style:
# Go back and check what we are operating on, should be
# an identifier or a close brace type ")]}".
p = last_pos - 1
while p >= 0:
style = accessor.style_at_pos(p)
if style == jsClassifier.identifier_style:
break
elif style == jsClassifier.keyword_style and last_char == ".":
break
elif style == jsClassifier.operator_style and \
last_char == "." and accessor.char_at_pos(p) in ")]}":
break
elif style in jsClassifier.string_styles:
break
elif style not in jsClassifier.ignore_styles:
# Else, wrong style for calltip
if DEBUG:
print("not a trigger: unexpected style: %d at pos: %d" \
% (style, p))
return None
p -= 1
else:
# Did not find the necessary style, no completion/calltip then
return None
if last_char == ".":
if style in jsClassifier.string_styles:
return Trigger(lang, TRG_FORM_CPLN,
"literal-members", pos, implicit,
citdl_expr="String")
elif style == jsClassifier.keyword_style:
# Check if it's a "this." expression
isThis = False
if last_pos >= 4:
word = []
p = last_pos - 1
p_end = last_pos - 5
while p > p_end:
word.insert(0, accessor.char_at_pos(p))
p -= 1
if "".join(word) == "this":
isThis = True
if not isThis:
return None
return Trigger(lang, TRG_FORM_CPLN,
"object-members", pos, implicit)
elif last_char in "(,":
# p is now at the end of the identifier, go back and check
# that we are not defining a function
ac = AccessorCache(accessor, p)
# Implicit calltip triggering from an arg separater ","
# http://bugs.activestate.com/show_bug.cgi?id=93864
if implicit and last_char == ',':
return self._functionCalltipTrigger(ac, jsClassifier, p, DEBUG)
# Get the previous style, if it's a keyword style, check that
# the keyword is not "function"
prev_pos, prev_char, prev_style = ac.getPrecedingPosCharStyle(
jsClassifier.identifier_style, jsClassifier.ignore_styles)
if prev_style == jsClassifier.keyword_style:
p, prev_text = ac.getTextBackWithStyle(
prev_style, jsClassifier.ignore_styles, max_text_len=len("function")+1)
if prev_text in ("function", ):
# Don't trigger here
return None
return Trigger(lang, TRG_FORM_CALLTIP,
"call-signature", pos, implicit)
elif last_style in jsClassifier.string_styles and last_char in "\"'":
prev_pos = last_pos - 1
prev_char = accessor.char_at_pos(prev_pos)
if prev_char != '[':
prev_style = accessor.style_at_pos(prev_pos)
ac = AccessorCache(accessor, prev_pos)
if prev_style in jsClassifier.ignore_styles:
# Look back further.
prev_pos, prev_char, prev_style = ac.getPrevPosCharStyle(
ignore_styles=jsClassifier.ignore_styles)
if prev_char == '[':
# We're good to go.
if DEBUG:
print("Matched trigger for array completions")
return Trigger(lang, TRG_FORM_CPLN,
"array-members", pos, implicit,
bracket_pos=prev_pos, trg_char=last_char)
return None
def preceding_trg_from_pos(self, buf, pos, curr_pos,
preceding_trg_terminators=None, DEBUG=False):
DEBUG = False
if DEBUG:
print("pos: %d" % (pos, ))
print("ch: %r" % (buf.accessor.char_at_pos(pos), ))
print("curr_pos: %d" % (curr_pos, ))
# Check if we can match on either of the 3-character trigger or on the
# normal preceding_trg_terminators.
# Try the default preceding_trg_from_pos handler
if pos != curr_pos and self._last_trg_type == "names":
# The last trigger type was a 3-char trigger "names", we must try
# triggering from the same point as before to get other available
# trigger types defined at the same poisition or before.
trg = ProgLangTriggerIntelMixin.preceding_trg_from_pos(
self, buf, pos+2, curr_pos, preceding_trg_terminators,
DEBUG=DEBUG)
else:
trg = ProgLangTriggerIntelMixin.preceding_trg_from_pos(
self, buf, pos, curr_pos, preceding_trg_terminators,
DEBUG=DEBUG)
# Now try the 3-char trigger, if we get two triggers, take the closest
# match.
names_trigger = None
if isinstance(buf, UDLBuffer):
jsClassifier = udlJSClassifier
else:
jsClassifier = pureJSClassifier
style = None
if pos > 0:
accessor = buf.accessor
if pos == curr_pos:
# We actually care about whats left of the cursor.
pos -= 1
style = accessor.style_at_pos(pos)
if style in (jsClassifier.identifier_style, jsClassifier.keyword_style):
ac = AccessorCache(accessor, pos)
prev_pos, prev_ch, prev_style = ac.getPrecedingPosCharStyle(
style)
if prev_style is not None and (pos - prev_pos) > 3:
# We need at least 3 character for proper completion
# handling.
names_trigger = self.trg_from_pos(
buf, prev_pos + 4, implicit=False)
if DEBUG:
print("trg: %r" % (trg, ))
print("names_trigger: %r" % (names_trigger, ))
print("last_trg_type: %r" % (self._last_trg_type, ))
if names_trigger:
if not trg:
trg = names_trigger
# Two triggers, choose the best one.
elif trg.pos == names_trigger.pos:
if self._last_trg_type != "names":
# The names trigger gets priority over the other trigger
# types, unless the previous trigger was also a names trg.
trg = names_trigger
elif trg.pos < names_trigger.pos:
trg = names_trigger
elif trg is None and style in jsClassifier.comment_styles:
# Check if there is a JSDoc to provide a calltip for, example:
# /** @param foobar {sometype} This is field for <|>
if DEBUG:
print("\njs preceding_trg_from_pos::jsdoc: 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("\njs preceding_trg_from_pos::jsdoc: 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("\njs preceding_trg_from_pos::jsdoc: calltip at %d" % (trg_pos, ))
trg = self.trg_from_pos(buf, trg_pos, implicit=False)
if trg:
self._last_trg_type = trg.type
return trg
_jsdoc_cplns = [("variable", t) for t in sorted(jsdoc_tags)]
def async_eval_at_trg(self, buf, trg, ctlr):
if _xpcom_:
trg = UnwrapObject(trg)
ctlr = UnwrapObject(ctlr)
ctlr.start(buf, trg)
# JSDoc completions
if trg.id == (self.lang, TRG_FORM_CPLN, "jsdoc-tags"):
# TODO: Would like a "javadoc tag" completion image name.
ctlr.set_cplns(self._jsdoc_cplns)
ctlr.done("success")
return
# JSDoc calltip
elif trg.id == (self.lang, TRG_FORM_CALLTIP, "jsdoc-tags"):
# TODO: Would like a "javadoc tag" completion image name.
jsdoc_field = trg.extra.get("jsdoc_field")
if jsdoc_field:
# print "jsdoc_field: %r" % (jsdoc_field, )
calltip = jsdoc_tags.get(jsdoc_field)
if calltip:
ctlr.set_calltips([calltip])
ctlr.done("success")
return
if trg.type == "literal-members":
# We could leave this to citdl_expr_from_trg, but this is a
# little bit faster, since we already know the citdl expr.
citdl_expr = trg.extra.get("citdl_expr")
elif trg.type == "names":
# We could leave this to citdl_expr_from_trg, but since we already
# know the citdl expr, use it.
citdl_expr = trg.extra.get("citdl_expr")
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(trg.pos)
evalr = self._evaluatorClass(ctlr, buf, trg, citdl_expr, line)
buf.mgr.request_eval(evalr)
def _expand_extra_dirs(self, env, extra_dirs):
max_depth = env.get_pref("codeintel_max_recursive_dir_depth", 10)
# Always include common JS associations - bug 91915.
js_assocs = env.assoc_patterns_from_lang("JavaScript")
if self.lang != "JavaScript":
# Add any language specific associations.
js_assocs = set(js_assocs)
js_assocs.update(env.assoc_patterns_from_lang(self.lang))
js_assocs = list(js_assocs)
return util.gen_dirs_under_dirs(extra_dirs,
max_depth=max_depth,
interesting_file_patterns=js_assocs)
#def _extra_dirs_from_env(self, env):
# extra_dirs = set()
# include_project = env.get_pref("codeintel_scan_files_in_project", True)
# if include_project:
# proj_base_dir = env.get_proj_base_dir()
# if proj_base_dir is not None:
# extra_dirs.add(proj_base_dir) # Bug 68850.
# for pref in env.get_all_prefs(self.extraPathsPrefName):
# if not pref:
# continue
# extra_dirs.update(d.strip() for d in pref.split(os.pathsep)
# if exists(d.strip()))
# if extra_dirs:
# log.debug("%s extra lib dirs: %r", self.lang, extra_dirs)
# max_depth = env.get_pref("codeintel_max_recursive_dir_depth", 10)
# # Always include common JS associations - bug 91915.
# js_assocs = env.assoc_patterns_from_lang("JavaScript")
# if self.lang != "JavaScript":
# # Add any language specific associations.
# js_assocs = set(js_assocs)
# js_assocs.update(env.assoc_patterns_from_lang(self.lang))
# js_assocs = list(js_assocs)
# extra_dirs = tuple(
# util.gen_dirs_under_dirs(extra_dirs,
# max_depth=max_depth,
# interesting_file_patterns=js_assocs)
# )
# # TODO Why doesn't it pick-up the value in the setting file???
# exclude_patterns = env.get_pref("codeintel_scan_exclude_dir", ["/build/"])
# if not exclude_patterns is None:
# for p in exclude_patterns:
# extra_dirs = [d for d in extra_dirs if not re.search(p, d)]
# else:
# extra_dirs = () # ensure retval is a tuple
# return extra_dirs
def _get_stdlibs_from_env(self, env=None):
return [self.mgr.db.get_stdlib(self.lang)]
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 "javascript-buf-libs" not in env.cache:
env.cache["javascript-buf-libs"] = weakref.WeakKeyDictionary()
cache = env.cache["javascript-buf-libs"] # <buf-weak-ref> -> <libs>
if buf not in cache:
env.add_pref_observer(self.extraPathsPrefName,
self._invalidate_cache_and_rescan_extra_dirs)
env.add_pref_observer("codeintel_selected_catalogs",
self._invalidate_cache)
env.add_pref_observer("codeintel_max_recursive_dir_depth",
self._invalidate_cache)
env.add_pref_observer("codeintel_scan_files_in_project",
self._invalidate_cache)
env.add_pref_observer("codeintel_scan_exclude_dir",
self._invalidate_cache)
db = self.mgr.db
libs = []
# - extradirslib
extra_dirs = self._extra_dirs_from_env(env)
if extra_dirs:
libs.append(db.get_lang_lib(self.lang, "extradirslib",
extra_dirs))
# Warn the user if there is a huge number of import dirs that
# might slow down completion.
num_import_dirs = len(extra_dirs)
if num_import_dirs > 100:
msg = "This buffer is configured with %d %s import dirs: " \
"this may result in poor completion performance" % \
(num_import_dirs, self.lang)
self.mgr.report_message(msg, "\n".join(extra_dirs))
if buf.lang == self.lang:
# - curdirlib (before extradirslib; only if pure JS file)
cwd = dirname(normpath(buf.path))
if cwd not in extra_dirs:
libs.insert(0, db.get_lang_lib(
self.lang, "curdirlib", [cwd]))
# - cataloglibs
if buf.lang == "HTML5":
# Implicit HTML 5 catalog additions.
libs.append(db.get_catalog_lib("JavaScript", ["html5"]))
catalog_selections = env.get_pref("codeintel_selected_catalogs")
libs.append(db.get_catalog_lib(self.lang, catalog_selections))
# - stdlibs
libs += self._get_stdlibs_from_env(env)
cache[buf] = libs
return cache[buf]
def _invalidate_cache(self, env, pref_name):
if "javascript-buf-libs" in env.cache:
log.debug("invalidate 'javascript-buf-libs' cache on %r", env)
del env.cache["javascript-buf-libs"]
def _invalidate_cache_and_rescan_extra_dirs(self, env, pref_name):
self._invalidate_cache(env, pref_name)
extra_dirs = self._extra_dirs_from_env(env)
if extra_dirs:
extradirslib = self.mgr.db.get_lang_lib(
self.lang, "extradirslib", extra_dirs)
request = PreloadLibRequest(extradirslib)
self.mgr.idxr.stage_request(request, 1.0)
def lpaths_from_blob(self, blob):
"""Return <lpaths> for this blob
where,
<lpaths> is a set of externally referencable lookup-paths, e.g.
[("YAHOO",), ("YAHOO", "util"), ...]
Note: The jury is out on whether this should include imports.
However, currently this is only being used for JS (no imports)
so it doesn't yet matter.
"""
return set(lpath for child in blob
for lpath in _walk_js_symbols(child))
def citdl_expr_from_trg(self, buf, trg):
"""Return a CITDL expression preceding the given trigger. This is the
JavaScript specialization; all it does is set array_as_attr to True.
"""
DEBUG = False
if trg.form != TRG_FORM_DEFN:
if trg.type == 'array-members':
# Get everything before the bracket position.
pos = trg.extra.get('bracket_pos') - 1
else:
pos = trg.pos - 2 # skip past the trigger char
return self._citdl_expr_from_pos(buf, pos, implicit=trg.implicit,
DEBUG=DEBUG, trg=trg,
array_as_attr=True)
return PythonCITDLExtractorMixin.citdl_expr_from_trg(self, buf, trg)
class JavaScriptBuffer(CitadelBuffer):
lang = lang
# Fillup chars for JavaScript: basically, any non-identifier char.
# XXX - '@' removed in order to better support XPCOM completions:
# Components.interfaces['@mozilla.]
# Whilst not an ideal solution, as when the '.' is hit we run into the
# same problem again... the ideal solution would be to override the
# cpln_fillup_chars to be only "\"'" for the 'array-members' trigger
# event. But this is not yet possible...
# - dropped ' ' It gets in the way of common usage: "var " (bug 77950).
cpln_fillup_chars = "~`!#%^&*()-=+{}[]|\\;:'\",.<>?/"
cpln_stop_chars = "~`!@#%^&*()-=+{}[]|\\;:'\",.<>?/ "
sce_prefixes = ["SCE_C_"]
cb_show_if_empty = True
@property
def libs(self):
return self.langintel.libs_from_buf(self)
@property
def stdlib(self):
return self.libs[-1]
def scoperef_from_blob_and_line(self, blob, line):
"""Return the scope for the given position in this buffer.
"line" is 1-based.
See CitadelBuffer.scoperef_from_pos() for details.
JavaScript has two differences here:
- <variable>'s are scopes if they have child tags. This CIX
technique is used in JavaScript to define customized object
instances.
- Currently a JavaScript "class" line range may not include its
methods in some cases.
function Foo() {
}
Foo.prototype.bar = function() {
}
Class "Foo" has a line range that does not include method "bar".
c.f. test javascript/cpln/intermixed_class_definitions
"""
DEBUG = False
if DEBUG:
print("scoperef_from_pos: look for line %d in %r" % (line, blob))
best_fit_lpath = None
for scope, lpath in _walk_js_scopes(blob):
start = int(scope.get("line"))
# JS CIX <scope> should alway have lineend. The default is
# because JS <variable>'s with content (i.e. anonymous
# custom Object instances) do not typically have lineend.
# Note: not sure the fallback is correct.
end = int(scope.get("lineend", start))
if DEBUG:
print("scoperef_from_pos: scope %r (%r-%r)?"\
% (scope, start, end), end=' ')
if line < start:
if DEBUG:
print("no, before start")
continue
elif line > end:
if DEBUG:
print("no, after end")
continue
elif line <= end:
if DEBUG:
print("yes, could be")
best_fit_lpath = lpath
else:
if DEBUG:
print("no, passed end")
if best_fit_lpath is not None:
break
if best_fit_lpath is not None:
return (blob, best_fit_lpath)
else:
return (blob, [])
class JavaScriptImportHandler(ImportHandler):
# The file extensions that this import handler will use when importing.
import_file_extensions = (".js", )
ignore_file_extensions = (".min.js", "-min.js", ".pkg.js", "-pkg.js", )
def setCorePath(self, compiler=None, extra=None):
self.corePath = []
def _findScannableFiles(self, xxx_todo_changeme, dirname, names):
(files, searchedDirs) = xxx_todo_changeme
if sys.platform.startswith("win"):
cpath = dirname.lower()
else:
cpath = dirname
if cpath in searchedDirs:
while names:
del names[0]
return
else:
searchedDirs[cpath] = 1
for i in range(len(names)-1, -1, -1): # backward so can del from list
path = os.path.join(dirname, names[i])
if os.path.isdir(path):
pass
elif names[i].endswith(self.import_file_extensions) and not names[i].endswith(self.ignore_file_extensions):
# XXX The list of extensions should be settable on
# the ImportHandler and Komodo should set whatever is
# set in prefs.
# XXX This check for files should probably include
# scripts, which might likely not have the
# extension: need to grow filetype-from-content smarts.
files.append(path)
def find_importables_in_dir(self, dir):
"""See citadel.py::ImportHandler.find_importables_in_dir() for
details.
Importables for JavaScript look like this:
{"foo.js": ("foo.js", None, False),
"somedir": (None, None, True)}
TODO: log the fs-stat'ing a la codeintel.db logging.
"""
from os.path import join, isdir, splitext
if dir == "<Unsaved>":
# TODO: stop these getting in here.
return {}
# TODO: log the fs-stat'ing a la codeintel.db logging.
try:
names = os.listdir(dir)
except OSError as ex:
return {}
dirs, nondirs = set(), set()
for name in names:
try:
if isdir(join(dir, name)):
dirs.add(name)
else:
nondirs.add(name)
except UnicodeDecodeError: