forked from ipython/ipython
/
completer.py
2093 lines (1665 loc) · 71.6 KB
/
completer.py
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
"""Completion for IPython.
This module started as fork of the rlcompleter module in the Python standard
library. The original enhancements made to rlcompleter have been sent
upstream and were accepted as of Python 2.3,
This module now support a wide variety of completion mechanism both available
for normal classic Python code, as well as completer for IPython specific
Syntax like magics.
Latex and Unicode completion
============================
IPython and compatible frontends not only can complete your code, but can help
you to input a wide range of characters. In particular we allow you to insert
a unicode character using the tab completion mechanism.
Forward latex/unicode completion
--------------------------------
Forward completion allows you to easily type a unicode character using its latex
name, or unicode long description. To do so type a backslash follow by the
relevant name and press tab:
Using latex completion:
.. code::
\\alpha<tab>
α
or using unicode completion:
.. code::
\\greek small letter alpha<tab>
α
Only valid Python identifiers will complete. Combining characters (like arrow or
dots) are also available, unlike latex they need to be put after the their
counterpart that is to say, `F\\\\vec<tab>` is correct, not `\\\\vec<tab>F`.
Some browsers are known to display combining characters incorrectly.
Backward latex completion
-------------------------
It is sometime challenging to know how to type a character, if you are using
IPython, or any compatible frontend you can prepend backslash to the character
and press `<tab>` to expand it to its latex form.
.. code::
\\α<tab>
\\alpha
Both forward and backward completions can be deactivated by setting the
``Completer.backslash_combining_completions`` option to ``False``.
Experimental
============
Starting with IPython 6.0, this module can make use of the Jedi library to
generate completions both using static analysis of the code, and dynamically
inspecting multiple namespaces. Jedi is an autocompletion and static analysis
for Python. The APIs attached to this new mechanism is unstable and will
raise unless use in an :any:`provisionalcompleter` context manager.
You will find that the following are experimental:
- :any:`provisionalcompleter`
- :any:`IPCompleter.completions`
- :any:`Completion`
- :any:`rectify_completions`
.. note::
better name for :any:`rectify_completions` ?
We welcome any feedback on these new API, and we also encourage you to try this
module in debug mode (start IPython with ``--Completer.debug=True``) in order
to have extra logging information if :any:`jedi` is crashing, or if current
IPython completer pending deprecations are returning results not yet handled
by :any:`jedi`
Using Jedi for tab completion allow snippets like the following to work without
having to execute any code:
>>> myvar = ['hello', 42]
... myvar[1].bi<tab>
Tab completion will be able to infer that ``myvar[1]`` is a real number without
executing any code unlike the previously available ``IPCompleter.greedy``
option.
Be sure to update :any:`jedi` to the latest stable version or to try the
current development version to get better completions.
"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
#
# Some of this code originated from rlcompleter in the Python standard library
# Copyright (C) 2001 Python Software Foundation, www.python.org
import __main__
import builtins as builtin_mod
import glob
import time
import inspect
import itertools
import keyword
import os
import re
import sys
import unicodedata
import string
import warnings
from contextlib import contextmanager
from importlib import import_module
from typing import Iterator, List, Tuple, Iterable
from types import SimpleNamespace
from traitlets.config.configurable import Configurable
from IPython.core.error import TryNext
from IPython.core.inputtransformer2 import ESC_MAGIC
from IPython.core.latex_symbols import latex_symbols, reverse_latex_symbol
from IPython.core.oinspect import InspectColors
from IPython.utils import generics
from IPython.utils.dir2 import dir2, get_real_method
from IPython.utils.process import arg_split
from traitlets import Bool, Enum, observe, Int
# skip module docstests
skip_doctest = True
try:
import jedi
jedi.settings.case_insensitive_completion = False
import jedi.api.helpers
import jedi.api.classes
JEDI_INSTALLED = True
except ImportError:
JEDI_INSTALLED = False
#-----------------------------------------------------------------------------
# Globals
#-----------------------------------------------------------------------------
# Public API
__all__ = ['Completer','IPCompleter']
if sys.platform == 'win32':
PROTECTABLES = ' '
else:
PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
# Protect against returning an enormous number of completions which the frontend
# may have trouble processing.
MATCHES_LIMIT = 500
_deprecation_readline_sentinel = object()
class ProvisionalCompleterWarning(FutureWarning):
"""
Exception raise by an experimental feature in this module.
Wrap code in :any:`provisionalcompleter` context manager if you
are certain you want to use an unstable feature.
"""
pass
warnings.filterwarnings('error', category=ProvisionalCompleterWarning)
@contextmanager
def provisionalcompleter(action='ignore'):
"""
This context manager has to be used in any place where unstable completer
behavior and API may be called.
>>> with provisionalcompleter():
... completer.do_experimental_things() # works
>>> completer.do_experimental_things() # raises.
.. note:: Unstable
By using this context manager you agree that the API in use may change
without warning, and that you won't complain if they do so.
You also understand that, if the API is not to your liking, you should report
a bug to explain your use case upstream.
We'll be happy to get your feedback, feature requests, and improvements on
any of the unstable APIs!
"""
with warnings.catch_warnings():
warnings.filterwarnings(action, category=ProvisionalCompleterWarning)
yield
def has_open_quotes(s):
"""Return whether a string has open quotes.
This simply counts whether the number of quote characters of either type in
the string is odd.
Returns
-------
If there is an open quote, the quote character is returned. Else, return
False.
"""
# We check " first, then ', so complex cases with nested quotes will get
# the " to take precedence.
if s.count('"') % 2:
return '"'
elif s.count("'") % 2:
return "'"
else:
return False
def protect_filename(s, protectables=PROTECTABLES):
"""Escape a string to protect certain characters."""
if set(s) & set(protectables):
if sys.platform == "win32":
return '"' + s + '"'
else:
return "".join(("\\" + c if c in protectables else c) for c in s)
else:
return s
def expand_user(path:str) -> Tuple[str, bool, str]:
"""Expand ``~``-style usernames in strings.
This is similar to :func:`os.path.expanduser`, but it computes and returns
extra information that will be useful if the input was being used in
computing completions, and you wish to return the completions with the
original '~' instead of its expanded value.
Parameters
----------
path : str
String to be expanded. If no ~ is present, the output is the same as the
input.
Returns
-------
newpath : str
Result of ~ expansion in the input path.
tilde_expand : bool
Whether any expansion was performed or not.
tilde_val : str
The value that ~ was replaced with.
"""
# Default values
tilde_expand = False
tilde_val = ''
newpath = path
if path.startswith('~'):
tilde_expand = True
rest = len(path)-1
newpath = os.path.expanduser(path)
if rest:
tilde_val = newpath[:-rest]
else:
tilde_val = newpath
return newpath, tilde_expand, tilde_val
def compress_user(path:str, tilde_expand:bool, tilde_val:str) -> str:
"""Does the opposite of expand_user, with its outputs.
"""
if tilde_expand:
return path.replace(tilde_val, '~')
else:
return path
def completions_sorting_key(word):
"""key for sorting completions
This does several things:
- Demote any completions starting with underscores to the end
- Insert any %magic and %%cellmagic completions in the alphabetical order
by their name
"""
prio1, prio2 = 0, 0
if word.startswith('__'):
prio1 = 2
elif word.startswith('_'):
prio1 = 1
if word.endswith('='):
prio1 = -1
if word.startswith('%%'):
# If there's another % in there, this is something else, so leave it alone
if not "%" in word[2:]:
word = word[2:]
prio2 = 2
elif word.startswith('%'):
if not "%" in word[1:]:
word = word[1:]
prio2 = 1
return prio1, word, prio2
class _FakeJediCompletion:
"""
This is a workaround to communicate to the UI that Jedi has crashed and to
report a bug. Will be used only id :any:`IPCompleter.debug` is set to true.
Added in IPython 6.0 so should likely be removed for 7.0
"""
def __init__(self, name):
self.name = name
self.complete = name
self.type = 'crashed'
self.name_with_symbols = name
self.signature = ''
self._origin = 'fake'
def __repr__(self):
return '<Fake completion object jedi has crashed>'
class Completion:
"""
Completion object used and return by IPython completers.
.. warning:: Unstable
This function is unstable, API may change without warning.
It will also raise unless use in proper context manager.
This act as a middle ground :any:`Completion` object between the
:any:`jedi.api.classes.Completion` object and the Prompt Toolkit completion
object. While Jedi need a lot of information about evaluator and how the
code should be ran/inspected, PromptToolkit (and other frontend) mostly
need user facing information.
- Which range should be replaced replaced by what.
- Some metadata (like completion type), or meta information to displayed to
the use user.
For debugging purpose we can also store the origin of the completion (``jedi``,
``IPython.python_matches``, ``IPython.magics_matches``...).
"""
__slots__ = ['start', 'end', 'text', 'type', 'signature', '_origin']
def __init__(self, start: int, end: int, text: str, *, type: str=None, _origin='', signature='') -> None:
warnings.warn("``Completion`` is a provisional API (as of IPython 6.0). "
"It may change without warnings. "
"Use in corresponding context manager.",
category=ProvisionalCompleterWarning, stacklevel=2)
self.start = start
self.end = end
self.text = text
self.type = type
self.signature = signature
self._origin = _origin
def __repr__(self):
return '<Completion start=%s end=%s text=%r type=%r, signature=%r,>' % \
(self.start, self.end, self.text, self.type or '?', self.signature or '?')
def __eq__(self, other)->Bool:
"""
Equality and hash do not hash the type (as some completer may not be
able to infer the type), but are use to (partially) de-duplicate
completion.
Completely de-duplicating completion is a bit tricker that just
comparing as it depends on surrounding text, which Completions are not
aware of.
"""
return self.start == other.start and \
self.end == other.end and \
self.text == other.text
def __hash__(self):
return hash((self.start, self.end, self.text))
_IC = Iterable[Completion]
def _deduplicate_completions(text: str, completions: _IC)-> _IC:
"""
Deduplicate a set of completions.
.. warning:: Unstable
This function is unstable, API may change without warning.
Parameters
----------
text: str
text that should be completed.
completions: Iterator[Completion]
iterator over the completions to deduplicate
Yields
------
`Completions` objects
Completions coming from multiple sources, may be different but end up having
the same effect when applied to ``text``. If this is the case, this will
consider completions as equal and only emit the first encountered.
Not folded in `completions()` yet for debugging purpose, and to detect when
the IPython completer does return things that Jedi does not, but should be
at some point.
"""
completions = list(completions)
if not completions:
return
new_start = min(c.start for c in completions)
new_end = max(c.end for c in completions)
seen = set()
for c in completions:
new_text = text[new_start:c.start] + c.text + text[c.end:new_end]
if new_text not in seen:
yield c
seen.add(new_text)
def rectify_completions(text: str, completions: _IC, *, _debug=False)->_IC:
"""
Rectify a set of completions to all have the same ``start`` and ``end``
.. warning:: Unstable
This function is unstable, API may change without warning.
It will also raise unless use in proper context manager.
Parameters
----------
text: str
text that should be completed.
completions: Iterator[Completion]
iterator over the completions to rectify
:any:`jedi.api.classes.Completion` s returned by Jedi may not have the same start and end, though
the Jupyter Protocol requires them to behave like so. This will readjust
the completion to have the same ``start`` and ``end`` by padding both
extremities with surrounding text.
During stabilisation should support a ``_debug`` option to log which
completion are return by the IPython completer and not found in Jedi in
order to make upstream bug report.
"""
warnings.warn("`rectify_completions` is a provisional API (as of IPython 6.0). "
"It may change without warnings. "
"Use in corresponding context manager.",
category=ProvisionalCompleterWarning, stacklevel=2)
completions = list(completions)
if not completions:
return
starts = (c.start for c in completions)
ends = (c.end for c in completions)
new_start = min(starts)
new_end = max(ends)
seen_jedi = set()
seen_python_matches = set()
for c in completions:
new_text = text[new_start:c.start] + c.text + text[c.end:new_end]
if c._origin == 'jedi':
seen_jedi.add(new_text)
elif c._origin == 'IPCompleter.python_matches':
seen_python_matches.add(new_text)
yield Completion(new_start, new_end, new_text, type=c.type, _origin=c._origin, signature=c.signature)
diff = seen_python_matches.difference(seen_jedi)
if diff and _debug:
print('IPython.python matches have extras:', diff)
if sys.platform == 'win32':
DELIMS = ' \t\n`!@#$^&*()=+[{]}|;\'",<>?'
else:
DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
GREEDY_DELIMS = ' =\r\n'
class CompletionSplitter(object):
"""An object to split an input line in a manner similar to readline.
By having our own implementation, we can expose readline-like completion in
a uniform manner to all frontends. This object only needs to be given the
line of text to be split and the cursor position on said line, and it
returns the 'word' to be completed on at the cursor after splitting the
entire line.
What characters are used as splitting delimiters can be controlled by
setting the ``delims`` attribute (this is a property that internally
automatically builds the necessary regular expression)"""
# Private interface
# A string of delimiter characters. The default value makes sense for
# IPython's most typical usage patterns.
_delims = DELIMS
# The expression (a normal string) to be compiled into a regular expression
# for actual splitting. We store it as an attribute mostly for ease of
# debugging, since this type of code can be so tricky to debug.
_delim_expr = None
# The regular expression that does the actual splitting
_delim_re = None
def __init__(self, delims=None):
delims = CompletionSplitter._delims if delims is None else delims
self.delims = delims
@property
def delims(self):
"""Return the string of delimiter characters."""
return self._delims
@delims.setter
def delims(self, delims):
"""Set the delimiters for line splitting."""
expr = '[' + ''.join('\\'+ c for c in delims) + ']'
self._delim_re = re.compile(expr)
self._delims = delims
self._delim_expr = expr
def split_line(self, line, cursor_pos=None):
"""Split a line of text with a cursor at the given position.
"""
l = line if cursor_pos is None else line[:cursor_pos]
return self._delim_re.split(l)[-1]
class Completer(Configurable):
greedy = Bool(False,
help="""Activate greedy completion
PENDING DEPRECTION. this is now mostly taken care of with Jedi.
This will enable completion on elements of lists, results of function calls, etc.,
but can be unsafe because the code is actually evaluated on TAB.
"""
).tag(config=True)
use_jedi = Bool(default_value=JEDI_INSTALLED,
help="Experimental: Use Jedi to generate autocompletions. "
"Default to True if jedi is installed.").tag(config=True)
jedi_compute_type_timeout = Int(default_value=400,
help="""Experimental: restrict time (in milliseconds) during which Jedi can compute types.
Set to 0 to stop computing types. Non-zero value lower than 100ms may hurt
performance by preventing jedi to build its cache.
""").tag(config=True)
debug = Bool(default_value=False,
help='Enable debug for the Completer. Mostly print extra '
'information for experimental jedi integration.')\
.tag(config=True)
backslash_combining_completions = Bool(True,
help="Enable unicode completions, e.g. \\alpha<tab> . "
"Includes completion of latex commands, unicode names, and expanding "
"unicode characters back to latex commands.").tag(config=True)
def __init__(self, namespace=None, global_namespace=None, **kwargs):
"""Create a new completer for the command line.
Completer(namespace=ns, global_namespace=ns2) -> completer instance.
If unspecified, the default namespace where completions are performed
is __main__ (technically, __main__.__dict__). Namespaces should be
given as dictionaries.
An optional second namespace can be given. This allows the completer
to handle cases where both the local and global scopes need to be
distinguished.
"""
# Don't bind to namespace quite yet, but flag whether the user wants a
# specific namespace or to use __main__.__dict__. This will allow us
# to bind to __main__.__dict__ at completion time, not now.
if namespace is None:
self.use_main_ns = True
else:
self.use_main_ns = False
self.namespace = namespace
# The global namespace, if given, can be bound directly
if global_namespace is None:
self.global_namespace = {}
else:
self.global_namespace = global_namespace
self.custom_matchers = []
super(Completer, self).__init__(**kwargs)
def complete(self, text, state):
"""Return the next possible completion for 'text'.
This is called successively with state == 0, 1, 2, ... until it
returns None. The completion should begin with 'text'.
"""
if self.use_main_ns:
self.namespace = __main__.__dict__
if state == 0:
if "." in text:
self.matches = self.attr_matches(text)
else:
self.matches = self.global_matches(text)
try:
return self.matches[state]
except IndexError:
return None
def global_matches(self, text):
"""Compute matches when text is a simple name.
Return a list of all keywords, built-in functions and names currently
defined in self.namespace or self.global_namespace that match.
"""
matches = []
match_append = matches.append
n = len(text)
for lst in [keyword.kwlist,
builtin_mod.__dict__.keys(),
self.namespace.keys(),
self.global_namespace.keys()]:
for word in lst:
if word[:n] == text and word != "__builtins__":
match_append(word)
snake_case_re = re.compile(r"[^_]+(_[^_]+)+?\Z")
for lst in [self.namespace.keys(),
self.global_namespace.keys()]:
shortened = {"_".join([sub[0] for sub in word.split('_')]) : word
for word in lst if snake_case_re.match(word)}
for word in shortened.keys():
if word[:n] == text and word != "__builtins__":
match_append(shortened[word])
return matches
def attr_matches(self, text):
"""Compute matches when text contains a dot.
Assuming the text is of the form NAME.NAME....[NAME], and is
evaluatable in self.namespace or self.global_namespace, it will be
evaluated and its attributes (as revealed by dir()) are used as
possible completions. (For class instances, class members are
also considered.)
WARNING: this can still invoke arbitrary C code, if an object
with a __getattr__ hook is evaluated.
"""
# Another option, seems to work great. Catches things like ''.<tab>
m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
if m:
expr, attr = m.group(1, 3)
elif self.greedy:
m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
if not m2:
return []
expr, attr = m2.group(1,2)
else:
return []
try:
obj = eval(expr, self.namespace)
except:
try:
obj = eval(expr, self.global_namespace)
except:
return []
if self.limit_to__all__ and hasattr(obj, '__all__'):
words = get__all__entries(obj)
else:
words = dir2(obj)
try:
words = generics.complete_object(obj, words)
except TryNext:
pass
except AssertionError:
raise
except Exception:
# Silence errors from completion function
#raise # dbg
pass
# Build match list to return
n = len(attr)
return [u"%s.%s" % (expr, w) for w in words if w[:n] == attr ]
def get__all__entries(obj):
"""returns the strings in the __all__ attribute"""
try:
words = getattr(obj, '__all__')
except:
return []
return [w for w in words if isinstance(w, str)]
def match_dict_keys(keys: List[str], prefix: str, delims: str):
"""Used by dict_key_matches, matching the prefix to a list of keys
Parameters
==========
keys:
list of keys in dictionary currently being completed.
prefix:
Part of the text already typed by the user. e.g. `mydict[b'fo`
delims:
String of delimiters to consider when finding the current key.
Returns
=======
A tuple of three elements: ``quote``, ``token_start``, ``matched``, with
``quote`` being the quote that need to be used to close current string.
``token_start`` the position where the replacement should start occurring,
``matches`` a list of replacement/completion
"""
if not prefix:
return None, 0, [repr(k) for k in keys
if isinstance(k, (str, bytes))]
quote_match = re.search('["\']', prefix)
quote = quote_match.group()
try:
prefix_str = eval(prefix + quote, {})
except Exception:
return None, 0, []
pattern = '[^' + ''.join('\\' + c for c in delims) + ']*$'
token_match = re.search(pattern, prefix, re.UNICODE)
token_start = token_match.start()
token_prefix = token_match.group()
matched = []
for key in keys:
try:
if not key.startswith(prefix_str):
continue
except (AttributeError, TypeError, UnicodeError):
# Python 3+ TypeError on b'a'.startswith('a') or vice-versa
continue
# reformat remainder of key to begin with prefix
rem = key[len(prefix_str):]
# force repr wrapped in '
rem_repr = repr(rem + '"') if isinstance(rem, str) else repr(rem + b'"')
if rem_repr.startswith('u') and prefix[0] not in 'uU':
# Found key is unicode, but prefix is Py2 string.
# Therefore attempt to interpret key as string.
try:
rem_repr = repr(rem.encode('ascii') + '"')
except UnicodeEncodeError:
continue
rem_repr = rem_repr[1 + rem_repr.index("'"):-2]
if quote == '"':
# The entered prefix is quoted with ",
# but the match is quoted with '.
# A contained " hence needs escaping for comparison:
rem_repr = rem_repr.replace('"', '\\"')
# then reinsert prefix from start of token
matched.append('%s%s' % (token_prefix, rem_repr))
return quote, token_start, matched
def cursor_to_position(text:str, line:int, column:int)->int:
"""
Convert the (line,column) position of the cursor in text to an offset in a
string.
Parameters
----------
text : str
The text in which to calculate the cursor offset
line : int
Line of the cursor; 0-indexed
column : int
Column of the cursor 0-indexed
Return
------
Position of the cursor in ``text``, 0-indexed.
See Also
--------
position_to_cursor: reciprocal of this function
"""
lines = text.split('\n')
assert line <= len(lines), '{} <= {}'.format(str(line), str(len(lines)))
return sum(len(l) + 1 for l in lines[:line]) + column
def position_to_cursor(text:str, offset:int)->Tuple[int, int]:
"""
Convert the position of the cursor in text (0 indexed) to a line
number(0-indexed) and a column number (0-indexed) pair
Position should be a valid position in ``text``.
Parameters
----------
text : str
The text in which to calculate the cursor offset
offset : int
Position of the cursor in ``text``, 0-indexed.
Return
------
(line, column) : (int, int)
Line of the cursor; 0-indexed, column of the cursor 0-indexed
See Also
--------
cursor_to_position : reciprocal of this function
"""
assert 0 <= offset <= len(text) , "0 <= %s <= %s" % (offset , len(text))
before = text[:offset]
blines = before.split('\n') # ! splitnes trim trailing \n
line = before.count('\n')
col = len(blines[-1])
return line, col
def _safe_isinstance(obj, module, class_name):
"""Checks if obj is an instance of module.class_name if loaded
"""
return (module in sys.modules and
isinstance(obj, getattr(import_module(module), class_name)))
def back_unicode_name_matches(text):
u"""Match unicode characters back to unicode name
This does ``☃`` -> ``\\snowman``
Note that snowman is not a valid python3 combining character but will be expanded.
Though it will not recombine back to the snowman character by the completion machinery.
This will not either back-complete standard sequences like \\n, \\b ...
Used on Python 3 only.
"""
if len(text)<2:
return u'', ()
maybe_slash = text[-2]
if maybe_slash != '\\':
return u'', ()
char = text[-1]
# no expand on quote for completion in strings.
# nor backcomplete standard ascii keys
if char in string.ascii_letters or char in ['"',"'"]:
return u'', ()
try :
unic = unicodedata.name(char)
return '\\'+char,['\\'+unic]
except KeyError:
pass
return u'', ()
def back_latex_name_matches(text:str):
"""Match latex characters back to unicode name
This does ``\\ℵ`` -> ``\\aleph``
Used on Python 3 only.
"""
if len(text)<2:
return u'', ()
maybe_slash = text[-2]
if maybe_slash != '\\':
return u'', ()
char = text[-1]
# no expand on quote for completion in strings.
# nor backcomplete standard ascii keys
if char in string.ascii_letters or char in ['"',"'"]:
return u'', ()
try :
latex = reverse_latex_symbol[char]
# '\\' replace the \ as well
return '\\'+char,[latex]
except KeyError:
pass
return u'', ()
def _formatparamchildren(parameter) -> str:
"""
Get parameter name and value from Jedi Private API
Jedi does not expose a simple way to get `param=value` from its API.
Parameter
=========
parameter:
Jedi's function `Param`
Returns
=======
A string like 'a', 'b=1', '*args', '**kwargs'
"""
description = parameter.description
if not description.startswith('param '):
raise ValueError('Jedi function parameter description have change format.'
'Expected "param ...", found %r".' % description)
return description[6:]
def _make_signature(completion)-> str:
"""
Make the signature from a jedi completion
Parameter
=========
completion: jedi.Completion
object does not complete a function type
Returns
=======
a string consisting of the function signature, with the parenthesis but
without the function name. example:
`(a, *args, b=1, **kwargs)`
"""
return '(%s)'% ', '.join([f for f in (_formatparamchildren(p) for signature in completion.get_signatures()
for p in signature.defined_names()) if f])
class IPCompleter(Completer):
"""Extension of the completer class with IPython-specific features"""
_names = None
@observe('greedy')
def _greedy_changed(self, change):