forked from SublimeCodeIntel/SublimeCodeIntel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlang_ruby.py
More file actions
1698 lines (1527 loc) · 68.7 KB
/
lang_ruby.py
File metadata and controls
1698 lines (1527 loc) · 68.7 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 *****
"""Ruby support for CodeIntel"""
import os
from os.path import basename, splitext, isdir, join, normcase, \
normpath, exists, dirname
import time
import sys
import logging
import re
from pprint import pformat
from glob import glob
import weakref
from ciElementTree import Element, SubElement, tostring
import SilverCity
from SilverCity.Lexer import Lexer
from SilverCity import ScintillaConstants
from SilverCity.ScintillaConstants import (
SCLEX_RUBY, SCE_RB_DEFAULT, SCE_RB_COMMENTLINE,
SCE_RB_REGEX, SCE_RB_IDENTIFIER, SCE_RB_WORD, SCE_RB_OPERATOR,
SCE_RB_CLASSNAME, SCE_RB_DEFNAME, SCE_RB_MODULE_NAME,
SCE_UDL_M_OPERATOR, SCE_UDL_SSL_DEFAULT, SCE_UDL_SSL_IDENTIFIER,
SCE_UDL_SSL_OPERATOR, SCE_UDL_SSL_VARIABLE, SCE_UDL_SSL_WORD,
SCE_UDL_TPL_OPERATOR
)
from SilverCity.Keywords import ruby_keywords
from codeintel2.common import *
from codeintel2.citadel import (ImportHandler, CitadelBuffer, CitadelEvaluator,
CitadelLangIntel)
from codeintel2.citadel_common import ScanRequest
from codeintel2.indexer import PreloadLibRequest
from codeintel2.parseutil import urlencode_path
from codeintel2.udl import UDLBuffer
from codeintel2.accessor import AccessorCache
from codeintel2 import rubycile
from codeintel2.langintel import (ParenStyleCalltipIntelMixin,
ProgLangTriggerIntelMixin)
from codeintel2.lang_ruby_common import RubyCommonBufferMixin
from codeintel2.util import (isident, isdigit, banner, indent, markup_text,
hotshotit, makePerformantLogger)
from codeintel2.tree import tree_2_0_from_tree_0_1
from codeintel2.tree_ruby import RubyTreeEvaluator
if _xpcom_:
from xpcom.server import UnwrapObject
#---- globals
lang = "Ruby"
log = logging.getLogger("codeintel.ruby")
# log.setLevel(logging.DEBUG)
makePerformantLogger(log)
CACHING = True # XXX obsolete, kill it
_trg_type_to_trg_char = {'lib-paths': ['\'', True],
'lib-subpaths': ['/', True],
'object-methods': ['.', False],
'literal-methods': ['.', False],
'available-modules': [' ', False],
'available-modules-and-classes': ['<', False],
'module-names': [':', False],
'instance-vars': ['@', True],
'class-vars': ['@', True],
'global-vars': ['$', True],
}
#---- language support
class RubyLexer(Lexer):
lang = "Ruby"
def __init__(self):
self._properties = SilverCity.PropertySet()
self._lexer = SilverCity.find_lexer_module_by_id(SCLEX_RUBY)
self._keyword_lists = [
SilverCity.WordList(ruby_keywords)
]
# TODO: This isn't used. Drop it.
class RubyCitadelEvaluator(CitadelEvaluator):
def __init__(self, ctlr, buf, trg, expr, line, converted_dot_new=None):
CitadelEvaluator.__init__(self, ctlr, buf, trg, expr, line)
self.converted_dot_new = converted_dot_new
def post_process_calltips(self, calltips):
if self.converted_dot_new:
# "Foo.new(" is really using "Foo.initialize(". We swapped
# that for calltip evaluation, now swap it back.
for i, c in enumerate(calltips):
if c.startswith("initialize"):
calltips[i] = "new" + c[len("initialize"):]
return calltips
def post_process_cplns(self, cplns):
"""Skip operator-typical methods like "+", "===", etc.
XXX Should we skip methods beginning with "_" and or "__foo__"
as well?
"""
cplns = [c for c in cplns if isident(c[1][0])]
cplns.sort(key=lambda c: c[1].upper())
return cplns
class RubyImportsEvaluator(Evaluator):
def __init__(self, ctlr, buf, trg, import_handler, prefix):
Evaluator.__init__(self, ctlr, buf, trg)
self.import_handler = import_handler
self.prefix = prefix
def __str__(self):
return "Ruby subimports of '%s'" % self.prefix
def eval(self, mgr):
self.ctlr.set_desc("subimports of '%s'" % self.prefix)
cwd = dirname(self.buf.path) # XXX Should eventually use relevant env
# TODO:XXX Update to use the newer `find_importables_in_dir`.
# `findSubImportsOnDisk` is deprecated.
subimports = self.import_handler.findSubImportsOnDisk(
self.prefix, cwd)
if subimports:
cplns = []
for subimport in subimports:
if subimport[-1] == '/':
cplns.append(("directory", subimport[:-1]))
else:
cplns.append(("module", subimport))
cplns.sort(key=lambda c: c[1].upper())
self.ctlr.set_cplns(cplns)
self.ctlr.done("success")
class RubyBuffer(CitadelBuffer, RubyCommonBufferMixin):
lang = "Ruby"
sce_prefixes = ["SCE_RB_"]
cb_show_if_empty = True
# Characters that should automatically invoke the current completion item:
# XXX Figure out the appropriate set (get Eric to help).
# - Cannot be '!' or '?' or '=' (I think) because those can be part of a
# method name.
# - Cannot be "'" or '"' because that can get in the way. E.g.:
# require 'cgi<|>
# At this point the selected item can be "cgi-lib". Filling up
# with a quote would select "cgi-lib" instead of the possibly
# intended "cgi".
# - Cannot be '.' because of '..' operator for ranges. E.g.:
# 1..1000
# would result in (or whatever the first Fixnum method is):
# 1.abs.1000
cpln_fillup_chars = "~`@#$%^&*(+}[]|\\;:,<>/ "
cpln_stop_chars = cpln_fillup_chars + "'\"."
def __init__(self, mgr, accessor, env=None, path=None, *args, **kwargs):
CitadelBuffer.__init__(self, mgr, accessor, env, path, *args, **kwargs)
self.check_for_rails_app_path(path)
# Skip styles are a bit different for Ruby:
# - for *some* cases autocomplete *is* automatically triggered
# in a string
# - you *can* trigger on a number
self.implicit_completion_skip_styles = dict(
(s, True) for s in self.comment_styles())
self.completion_skip_styles = self.implicit_completion_skip_styles.copy(
)
self.completion_skip_styles[SCE_RB_REGEX] = True
@property
def libs(self):
return self.langintel.libs_from_buf(self)
class _CommonStyleClassifier(object):
def __init__(self, buf):
self.buf = buf
@property
def ignore_styles(self):
return self.ignoreStyles
class _RubyStyleClassifier(_CommonStyleClassifier):
# This class delegates mostly to its Buffer object.
# class-specific methods first...
def is_ruby_style_at_pos(self, pos):
# All chars are in Ruby code in pure Ruby buffers,
# no need to get the style at position pos
return True
def is_identifier_or_word_style(self, style):
return style == SCE_RB_IDENTIFIER or style == SCE_RB_WORD
def is_identifier_style(self, style):
return style == SCE_RB_IDENTIFIER
def is_operator_style(self, style):
return style == SCE_RB_OPERATOR
def is_default_style(self, style):
return style == SCE_RB_DEFAULT
def is_identifier_or_definition_style(self, style):
""" Check if a style is either a identifier reference or a definition
of a thing that can be referenced by an identifier """
return style in (SCE_RB_IDENTIFIER, SCE_RB_CLASSNAME,
SCE_RB_DEFNAME, SCE_RB_MODULE_NAME)
def __getattr__(self, name):
return getattr(self.buf, name)
ignoreStyles = (SCE_RB_DEFAULT, SCE_RB_COMMENTLINE)
class _UDLStyleClassifier(_CommonStyleClassifier):
def __init__(self, buf):
_CommonStyleClassifier.__init__(self, buf)
self._implicit_completion_skip_styles = None
self._completion_skip_styles = None
def is_ruby_style_at_pos(self, pos):
style = self.buf.accessor.style_at_pos(pos)
return SCE_UDL_SSL_DEFAULT <= style <= SCE_UDL_SSL_VARIABLE
@property
def implicit_completion_skip_styles(self):
if self._implicit_completion_skip_styles is None:
# XXX - do we have access to the language info?
self._implicit_completion_skip_styles = {
ScintillaConstants.SCE_UDL_SSL_COMMENT: True,
ScintillaConstants.SCE_UDL_SSL_COMMENTBLOCK: True,
}
return self._implicit_completion_skip_styles
@property
def completion_skip_styles(self):
if self._completion_skip_styles is None:
self._completion_skip_styles = {
ScintillaConstants.SCE_UDL_SSL_COMMENT: True,
ScintillaConstants.SCE_UDL_SSL_COMMENTBLOCK: True,
ScintillaConstants.SCE_UDL_SSL_REGEX: True,
}
return self._completion_skip_styles
def is_identifier_or_word_style(self, style):
return style == SCE_UDL_SSL_IDENTIFIER or style == SCE_UDL_SSL_WORD
def is_identifier_style(self, style):
return style == SCE_UDL_SSL_IDENTIFIER
def is_identifier_or_definition_style(self, style):
return style == SCE_UDL_SSL_IDENTIFIER
def is_operator_style(self, style):
return style == SCE_UDL_SSL_OPERATOR
# These aren't properties in buffer.py, so they can't be here either.
def comment_styles(self):
return (ScintillaConstants.SCE_UDL_SSL_COMMENT,)
def number_styles(self):
return (ScintillaConstants.SCE_UDL_SSL_NUMBER,)
def string_styles(self):
return (ScintillaConstants.SCE_UDL_SSL_STRING, )
def is_default_style(self, style):
return style == SCE_UDL_SSL_DEFAULT
ignoreStyles = (ScintillaConstants.SCE_UDL_SSL_DEFAULT,
ScintillaConstants.SCE_UDL_SSL_COMMENT)
class RubyLangIntel(CitadelLangIntel,
ParenStyleCalltipIntelMixin,
ProgLangTriggerIntelMixin):
lang = "Ruby"
calltip_region_terminators = tuple(']});\r\n')
# newlines are for ending calltips triggered on a space
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 "ruby-buf-libs" not in env.cache:
env.cache["ruby-buf-libs"] = weakref.WeakKeyDictionary()
cache = env.cache["ruby-buf-libs"] # <buf-weak-ref> -> <libs>
if buf not in cache:
libs = self._buf_indep_libs_from_env(env)[:]
# - curdirlib (in Ruby '.' is *last* in the import path)
cwd = dirname(buf.path)
if cwd != "<Unsaved>":
libs.append(self.mgr.db.get_lang_lib(
"Ruby", "curdirlib", [cwd]))
cache[buf] = libs
return cache[buf]
def _ruby_from_env(self, env):
import which
path = [d.strip()
for d in env.get_envvar("PATH", "").split(os.pathsep)
if d.strip()]
try:
return which.which("ruby", path=path)
except which.WhichError:
return None
_gem_ver_ptn = re.compile(r'(.+?)(-[\d+\.]+)$')
def _ruby_info_from_ruby(self, ruby, env):
"""Call the given Ruby and return:
(<version>, <lib-dir>, <site-lib-dir>, <import-dirs>, <gem-dirs>)
TODO: Unicode path issues?
"""
import process
# Ruby 1.5.2 does not support sys.version_info.
info_cmd = "puts RUBY_VERSION; puts $:"
argv = [ruby, "-e", info_cmd]
log.debug("run `%s -e ...'", ruby)
p = process.ProcessOpen(argv, env=env.get_all_envvars(), stdin=None)
stdout, stderr = p.communicate()
stdout_lines = stdout.splitlines(0)
retval = p.returncode
if retval:
log.warn("failed to determine Ruby info:\n"
" path: %s\n"
" retval: %s\n"
" stdout:\n%s\n"
" stderr:\n%s\n",
ruby, retval, indent('\n'.join(stdout_lines)),
indent(stderr))
ruby_ver = stdout_lines[0]
_ver_parts = ruby_ver.split('.', 2)
short_ver = '.'.join(_ver_parts[:2])
if len(_ver_parts) >= 3:
sub_minor_ver = int(_ver_parts[2])
else:
sub_minor_ver = 0
prefix = dirname(dirname(ruby))
libdir = join(prefix, "lib", "ruby", short_ver)
sitelibdir = join(prefix, "lib", "ruby", "site_ruby")
# Need to normpath() these because they come out, e.g.:
# c:/ruby184/lib/ruby/site_ruby/1.8
# on Windows.
import_path = [normpath(p) for p in stdout_lines[1:]]
def get_first_gem_dir_candidate():
gems_dir_part1 = join(prefix, "lib", "ruby", "gems")
gems_dir_version = join(gems_dir_part1, short_ver)
if exists(gems_dir_version):
return join(gems_dir_version, "gems")
for i in range(sub_minor_ver + 1):
cand_ver = "%s.%d" % (gems_dir_version, i)
if exists(cand_ver):
return join(cand_ver, "gems")
return None
# Get the list of relevant Gem lib dirs.
def gem_ver_from_ver_str(ver_str):
parts = ver_str.split('.')
try:
parts = list(map(int, parts))
except ValueError:
return ver_str
else:
return tuple(parts)
gems_dir = get_first_gem_dir_candidate()
gem_ver_and_dir_from_name = {
# "actionmailer": ((1,2,5), ".../actionmailer-1.2.5"),
}
if gems_dir:
for dir in glob(join(gems_dir, "*-*")):
if not isdir(dir):
continue
m = self._gem_ver_ptn.match(basename(dir))
if not m:
continue
name, ver_str = m.groups()
gem_ver = gem_ver_from_ver_str(ver_str)
if name in gem_ver_and_dir_from_name:
if gem_ver > gem_ver_and_dir_from_name[name][0]:
gem_ver_and_dir_from_name[name] = (gem_ver, dir)
else:
gem_ver_and_dir_from_name[name] = (gem_ver, dir)
log.debug("ruby gem dir info: %s", pformat(gem_ver_and_dir_from_name))
gem_lib_dirs = []
for name, (gem_ver, dir) in sorted(gem_ver_and_dir_from_name.items()):
gem_lib_dir = join(dir, "lib")
if isdir(gem_lib_dir):
gem_lib_dirs.append(gem_lib_dir)
return ruby_ver, libdir, sitelibdir, import_path, gem_lib_dirs
#def _extra_dirs_from_env(self, env):
# extra_dirs = set()
# for pref in env.get_all_prefs("rubyExtraPaths"):
# if not pref:
# continue
# # TODO: need to support Gems specially?
# extra_dirs.update(d.strip() for d in pref.split(os.pathsep) if exists(d.strip()))
# return tuple(extra_dirs)
def _buf_indep_libs_from_env(self, env):
"""Create the buffer-independent list of libs."""
cache_key = "ruby-libs"
if cache_key not in env.cache:
env.add_pref_observer("ruby", self._invalidate_cache)
env.add_pref_observer("rubyExtraPaths",
self._invalidate_cache_and_rescan_extra_dirs)
env.add_pref_observer("codeintel_selected_catalogs",
self._invalidate_cache)
db = self.mgr.db
# Gather information about the current ruby.
ruby = None
if env.has_pref("ruby"):
ruby = env.get_pref("ruby").strip() or None
if not ruby or not exists(ruby):
ruby = self._ruby_from_env(env)
if not ruby:
log.warn("no Ruby was found from which to determine the "
"import path")
ver, libdir, sitelibdir, import_path, gem_lib_dirs \
= None, None, None, [], []
else:
ver, libdir, sitelibdir, import_path, gem_lib_dirs \
= self._ruby_info_from_ruby(ruby, env)
libs = []
# - extradirslib
extra_dirs = self._extra_dirs_from_env(env)
if extra_dirs:
log.debug("Ruby extra lib dirs: %r", extra_dirs)
libs.append(db.get_lang_lib("Ruby", "extradirslib",
extra_dirs))
# Figure out which sys.path dirs belong to which lib.
paths_from_libname = {"sitelib": [], "envlib": [], "stdlib": []}
STATE = "envlib"
canon_libdir = libdir and normcase(libdir) or None
canon_sitelibdir = sitelibdir and normcase(sitelibdir) or None
for dir in import_path:
canon_dir = normcase(dir)
if dir == ".": # -> curdirlib (handled separately)
continue
# TODO: need to support gems specially?
elif dir.startswith(sitelibdir):
STATE = "sitelib"
elif dir.startswith(libdir):
STATE = "stdlib"
if not exists(dir):
continue
paths_from_libname[STATE].append(dir)
log.debug("Ruby %s paths for each lib:\n%s", ver, indent(
pformat(paths_from_libname)))
# - envlib, sitelib, gemlib, cataloglib, stdlib
if paths_from_libname["envlib"]:
libs.append(db.get_lang_lib("Ruby", "envlib",
paths_from_libname["envlib"]))
if paths_from_libname["sitelib"]:
libs.append(db.get_lang_lib("Ruby", "sitelib",
paths_from_libname["sitelib"]))
if gem_lib_dirs:
libs.append(db.get_lang_lib("Ruby", "gemlib", gem_lib_dirs))
catalog_selections = env.get_pref("codeintel_selected_catalogs")
libs += [
db.get_catalog_lib("Ruby", catalog_selections),
db.get_stdlib("Ruby", ver)
]
env.cache[cache_key] = libs
return env.cache[cache_key]
def _invalidate_cache(self, env, pref_name):
for key in ("ruby-buf-libs", "ruby-libs"):
if key in env.cache:
log.debug("invalidate '%s' cache on %r", key, env)
del env.cache[key]
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(
"Ruby", "extradirslib", extra_dirs)
request = PreloadLibRequest(extradirslib)
self.mgr.idxr.stage_request(request, 1.0)
# All Ruby trigger points occur at one of these characters:
# '.' (period) [eval implemented]
# ' ' (space)
# '(' (open paren) [eval implemented]
# ':' (colon) "::" actually [eval implemented]
# '@' (at-sign) "@..." for instance var,
# "@@..." for class vars
# '$' (dollar sign)
# "'" (single-quote) [eval implemented]
# '"' (double-quote) [eval implemented]
# '/' (slash) [eval implemented]
#
# At least three characters into an identifier (\w_)+
#
# spaces -- for class inheritance, include, & call-tips
#
trg_chars = tuple('. (:@$"\'/') # the full set
calltip_trg_chars = tuple('( ')
RUBY_KEYWORDS = dict((k, True) for k in ruby_keywords.split())
def _get_prev_token_skip_ws(self, pos, accessor, styleClassifier):
prev_start_pos, prev_end_pos = accessor.contiguous_style_range_from_pos(
pos - 1)
if prev_start_pos == 0 or not styleClassifier.is_ruby_style_at_pos(prev_start_pos):
return prev_start_pos, prev_end_pos
prev_style = accessor.style_at_pos(prev_start_pos)
if styleClassifier.is_default_style(prev_style):
prev_start_pos, prev_end_pos = accessor.contiguous_style_range_from_pos(
prev_start_pos - 1)
return prev_start_pos, prev_end_pos
def _get_token_before_namelist(self, pos, accessor, styleClassifier, lim=-1):
""" Walk backwards skipping (name, comma) pairs.
If lim is given and is positive stop once we reach 0, to avoid spending
too much time here
"""
while True:
prev_start_pos, prev_end_pos = self._get_prev_token_skip_ws(
pos, accessor, styleClassifier)
if prev_start_pos == 0 or not styleClassifier.is_ruby_style_at_pos(prev_start_pos):
return 0, 0
prev_style = accessor.style_at_pos(prev_start_pos)
if not styleClassifier.is_identifier_or_word_style(prev_style):
return 0, 0
prev_start_pos, prev_end_pos = self._get_prev_token_skip_ws(
prev_start_pos, accessor, styleClassifier)
if prev_start_pos == 0 or not styleClassifier.is_ruby_style_at_pos(prev_start_pos):
return 0, 0
elif not styleClassifier.is_operator_style(prev_style):
return 0, 0
op = accessor.text_range(prev_start_pos, prev_end_pos)
if op != ",":
return prev_start_pos, prev_end_pos
lim -= 1
if lim == 0:
return 0, 0
def _is_completable_name(self, pos, accessor, styleClassifier):
"""
Ensure we are not in another trigger zone, we do
this by checking that the preceeding text is not
one of "." or "::"
Also ignore words in block var names:
{do or "{"}, "|", {name, ","}*
"""
if pos == 0:
return True
prev_start_pos, prev_end_pos = self._get_prev_token_skip_ws(
pos, accessor, styleClassifier)
if prev_start_pos == 0 or not styleClassifier.is_ruby_style_at_pos(prev_start_pos):
return True
prev_style = accessor.style_at_pos(prev_start_pos)
if not styleClassifier.is_operator_style(prev_style):
return True
op = accessor.text_range(prev_start_pos, prev_end_pos)
if op in (".", "::"):
return False
if op == ",":
prev_start_pos, prev_end_pos = self._get_token_before_namelist(
prev_start_pos,
accessor, styleClassifier,
lim=5)
if prev_start_pos <= 0:
return False
prev_style = accessor.style_at_pos(prev_start_pos)
if not styleClassifier.is_operator_style(prev_style):
return True
op = accessor.text_range(prev_start_pos, prev_end_pos)
if op[-1] != "|": # last character
return True
elif op == "{|":
# Special case due to the way the accessor combines tokens of same
# style
return False
# Now look back for either a brace or a 'do'
prev_start_pos, prev_end_pos = self._get_prev_token_skip_ws(
prev_start_pos, accessor, styleClassifier)
if prev_start_pos == 0 or not styleClassifier.is_ruby_style_at_pos(prev_start_pos):
return False
op = accessor.text_range(prev_start_pos, prev_end_pos)
return op not in ("{", "do")
def trg_from_pos(self, buf, pos, implicit=True, DEBUG=False):
"""If the given position is a _likely_ trigger point, return the
trigger type. Otherwise return None.
"""
if pos <= 0:
return None
styleClassifier = (isinstance(buf, UDLBuffer) and _UDLStyleClassifier
or _RubyStyleClassifier)(buf)
DEBUG = False # not using 'logging' system, because want to be fast
if DEBUG:
print(banner("Ruby trg_from_pos(pos=%r, implicit=%r)"
% (pos, implicit)))
accessor = buf.accessor
last_pos = pos - 1
last_ch = accessor.char_at_pos(last_pos)
if DEBUG:
print(" last_pos: %s" % last_pos)
print(" last_ch: %r" % last_ch)
# All Ruby trigger points occur at one of the trg_chars.
# Also some require specific two (or more) character combos that
# we can use to filter quickly.
if last_ch not in self.trg_chars:
# Can we do a complete-names?
last_style = accessor.style_at_pos(last_pos)
if last_ch.isalnum() or last_ch == '_':
# Gather as long as they're identifier or word chars
MIN_LENGTH = 3
if styleClassifier.is_identifier_or_word_style(last_style):
start_pos, end_pos = accessor.contiguous_style_range_from_pos(
last_pos)
# XXX Is end_pos pointing one past the end?
if pos - start_pos == MIN_LENGTH or not implicit:
ident = accessor.text_range(start_pos, end_pos)
prefix = ident[:pos - start_pos]
if self._is_completable_name(start_pos, accessor, styleClassifier):
return Trigger("Ruby", TRG_FORM_CPLN,
"names",
start_pos, implicit, length=0, prefix=prefix)
if DEBUG:
print("no: %r is not in %r"\
% (last_ch, self.trg_chars))
return None
elif last_ch == ' ':
if last_pos <= 0:
return None
penultimate_ch = accessor.char_at_pos(last_pos-1)
prev_style = accessor.style_at_pos(last_pos - 1)
# Complex conditions, so express them this way to simplify
if styleClassifier.is_operator_style(prev_style) and penultimate_ch == "<":
pass
elif styleClassifier.is_identifier_or_word_style(prev_style):
# XXX Reject keywords
pass
else:
if DEBUG:
print("no: %r is not '< ' or ending a word"\
"(i.e. 'include ')" % (penultimate_ch+last_ch))
return None
elif last_ch == ':' \
and not (last_pos > 0
and accessor.char_at_pos(last_pos-1) == ':'):
if DEBUG:
penultimate_ch = (last_pos > 0
and accessor.char_at_pos(last_pos-1) or '')
print("no: %r is not '::'"\
% (penultimate_ch+last_ch))
return None
# Suppress triggering in some styles.
TRIGGER_IN_STRING_CHARS = tuple('\'"/')
last_style = accessor.style_at_pos(last_pos)
if DEBUG:
style_names = buf.style_names_from_style_num(last_style)
print(" style: %s %s" % (last_style, style_names))
suppress = False
if implicit:
if last_style in styleClassifier.implicit_completion_skip_styles:
suppress = True
elif last_style in styleClassifier.string_styles():
if last_ch not in TRIGGER_IN_STRING_CHARS:
# The ', ", and / trigger chars *always* trigger in
# a string.
suppress = True
elif last_ch in TRIGGER_IN_STRING_CHARS:
suppress = True
elif last_style in styleClassifier.completion_skip_styles:
# If the user requests code-completion and previous char is
# in this style, suppress it.
suppress = True
if suppress:
if DEBUG:
print("no: completion is suppressed in style at %s: %s %s"\
% (last_pos, last_style, style_names))
return None
WHITESPACE = tuple(' \t\n\r')
EOL = tuple('\n\r')
if last_ch == ' ':
# This may be one of the following:
# class FOO < | complete-available-modules-and-classes
# not implemented yet, "<" not in trg-char tuple.
# include | complete-available-modules
# method calltip-call-signature
# Simplifying assumptions:
# With whitespace allow for a completion list after '<'
# in {class Name <}, but allow for any calltip after an identifier.
# (above) that the preceding char (stored in
# 'penultimate_ch') is '<' or a word or identifier.
# - The construct doesn't have to be on one line.
LIMIT = 50
text = accessor.text_range(max(
0, last_pos-LIMIT), last_pos) # working text
if DEBUG:
print(" working text: %r" % text)
i = len(text)-1
while i > 0: # Skip back to start of line.
if text[i] in EOL:
break
i -= 1
line = text[i:].lstrip()
if DEBUG:
print(" line: %r" % line)
if penultimate_ch == "<":
if not line.startswith("class"):
if DEBUG:
print("no: line does not start with 'class'")
return None
if DEBUG:
print("complete-available-modules-and-classes")
return Trigger("Ruby", TRG_FORM_CPLN,
"available-modules-and-classes",
pos, implicit)
elif line.strip() == "include":
if DEBUG:
print("complete-available-modules")
return Trigger("Ruby", TRG_FORM_CPLN, "available-modules",
pos, implicit)
else: # maybe a calltip on a paren-free call
if DEBUG:
print("calltip-call-signature")
return Trigger("Ruby", TRG_FORM_CALLTIP, "call-signature",
pos, implicit)
elif last_ch == '.':
# This can be:
# FOO.| complete-object-methods
# LITERAL.| complete-literal-methods
# but bug62397: not for a fixnum
# Examples:
# Foo.
# foo.
# @foo.
# ::foo.
# (foo + 1). <-- We don't bother with this one for now
# because CITDL processing won't resolve
# the expression anyway.
# foo2.
# Allow literals too:
# 0. 3.14.
# 1e6. [].
# {}. 'foo'. "bar".
# Weird literals (pickaxe p319):
# %W{...} # also q, Q, r, w, x, <empty> instead of 'W'
# 0d123456, 0xff, -0b10_1010 # specific base laterals
# ?\M-a # char literals
# here docs
# symbols
# Counter examples:
# foo . Don't allow whitespace in between.
# No examples in Ruby stdlib that I can
# see and more often interferes with range
# operator.
# foo['bar']. would need to find matching '[' and
# ensure no ident char immediately
# preceding (that's a heuristic)
# %W{foo}. don't want to go there yet
#
# def\w+CLASSNAME. Don't trigger on CLASSNAME, as we're in
# a definition context, not a use one.
if last_pos > 0:
last_last_pos = last_pos - 1
last_last_ch = accessor.char_at_pos(last_last_pos)
if DEBUG:
print(" prev char = %r" % last_last_ch)
if last_last_ch in '"\'':
return Trigger("Ruby", TRG_FORM_CPLN,
"literal-methods", pos, implicit,
literal="String")
elif last_last_ch == '}':
# Might be Hash literal:
# @tk_windows = {}.<|>taint
# Need to rule out counter examples:
# attributes.collect{|name, value| ...}.<|>to_s
# FileTest.exist?("#{@filename}.<|>#{i}")
# @result = Thread.new { perform_with_block }.<|>value
# Simplifying assumption (because too many counter
# examples are more common): only trigger on exactly
# {}.<|>
if last_pos > 1 \
and accessor.char_at_pos(last_pos-2) == '{':
return Trigger("Ruby", TRG_FORM_CPLN,
"literal-methods", pos, implicit,
literal="Hash")
else:
return None
elif last_last_ch == ']':
# Might be Array literal:
# @tk_table_list = [].<|>taint
# [1,2,3].<|>
# Need to rule out counter examples:
# @@services[host][port].stop
# foo[blah].bang
# Algorithm: Look back on currently line for
# matching '['. If the char before that is a space,
# then consider it an Array. If can't find matching
# '[' on this line, then consider it an Array.
wrk_line = accessor.text_range(
accessor.line_start_pos_from_pos(last_pos),
last_last_pos)
block_count = 1
for ch in reversed(wrk_line):
if not block_count:
if ch in WHITESPACE or ch in '=,(':
return Trigger("Ruby", TRG_FORM_CPLN,
"literal-methods", pos,
implicit, literal="Array")
return None
if ch == '[':
block_count -= 1
elif ch == ']':
block_count += 1
else:
return Trigger("Ruby", TRG_FORM_CPLN,
"literal-methods", pos,
implicit, literal="Array")
return None
elif isident(last_last_ch):
LIMIT = 50
text = accessor.text_range(max(
0, last_pos-LIMIT), last_pos) # working text
if text.find("def") > -1:
# String.find fails faster than Regex.search
idx = max(text.rfind("\n"), text.rfind("\r"))
if idx > -1:
line = text[idx + 1:]
else:
line = text
if self._method_def_header.search(line):
if DEBUG:
print("==> bailing out, defining something")
return None
return Trigger("Ruby", TRG_FORM_CPLN,
"object-methods", pos, implicit)
elif isdigit(last_last_ch):
# Could be a numeric literal or an ident.
wrk_line = accessor.text_range(
accessor.line_start_pos_from_pos(last_pos),
last_pos)
if DEBUG:
print("'<digit>.': numeric literal or identifier")
print("check for leading number in %r" % wrk_line)
if self._leading_float_re.search(wrk_line):
return Trigger("Ruby", TRG_FORM_CPLN,
"literal-methods", pos,
implicit, literal="Float")
elif self._leading_number_re.search(wrk_line):
return (not implicit and
Trigger("Ruby", TRG_FORM_CPLN,
"literal-methods", pos,
implicit, literal="Fixnum")) or None
else:
return Trigger("Ruby", TRG_FORM_CPLN,
"object-methods", pos, implicit)
else:
return None
elif last_ch == '(':
# This may be:
# FOO(| calltip-call-signature
# - Want to be sure to exclude precedence parens after
# keywords: "if (", "while (", etc.
# - XXX Are there instances of this trigger that we just
# want to drop here because of practical limitations in the
# Ruby codeintel handling -- as there are with Perl?
LIMIT = 100
text = accessor.text_range(max(
0, last_pos-LIMIT), last_pos) # working text
if DEBUG:
print(" working text: %r" % text)
i = len(text)-1
while i >= 0 and text[i] in WHITESPACE: # parse off whitespace
i -= 1
RUBY_SPECIAL_METHOD_END_CHARS = "?!" # XXX what about '='?
if i >= 0 and not (isident(text[i]) or isdigit(text[i])
or text[i] in RUBY_SPECIAL_METHOD_END_CHARS):
if DEBUG:
print("no: first non-ws char before "\
"trigger point is not an ident char: '%s'" % text[i])
return None
end = i+1
if text[i] in RUBY_SPECIAL_METHOD_END_CHARS:
i -= 1
while i >= 0: # parse out the preceding identifier
if isdigit(text[i]):
# might be an identifier, need to keep looking
pass
elif not isident(text[i]):
# Identifier can be prefixed with '$', '@' or '@@'.
if i >= 1 and text[i-1:i+1] == "@@":
start = i-1
elif text[i] in "$@":
start = i
else:
start = i+1
identifier = text[start:end]
break
i -= 1
else:
identifier = text[:end]
if DEBUG:
print(" identifier: %r" % identifier)
if not identifier:
if DEBUG:
print("no: no identifier preceding trigger point")
return None
elif isdigit(identifier[0]):
if DEBUG:
print("no: token preceding trigger "\
"point is not a legal identifier")
return None
if identifier in self.RUBY_KEYWORDS:
if DEBUG:
print("no: no trigger on paren "\
"after keyword: %r" % identifier)
return None
# Now we want to rule out subroutine definition lines, e.g.:
# def foo(
# def ClassName.foo(
# def self.foo(
# def (wacked+out).foo(
line = text[:end].splitlines(0)[-1]
if DEBUG:
print(" trigger line: %r" % line)
if line.lstrip().startswith("def"):
if DEBUG:
print("no: no trigger on Ruby func definition")
return None
if DEBUG:
print("calltip-call-signature")
return Trigger("Ruby", TRG_FORM_CALLTIP, "call-signature",