FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
bpython/bpython/autocomplete.py at 0.13-bugfix · optionalg/bpython · GitHub
optionalg
/
bpython
Public
forked from
bpython/bpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
bpython
/
bpython
/
autocomplete.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
178 lines (158 loc) · 6.36 KB
Breadcrumbs
bpython
/
bpython
/
autocomplete.py
Copy path
File metadata and controls
178 lines (158 loc) · 6.36 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# The MIT License
#
# Copyright (c) 2009-2012 the bpython authors.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
from
__future__
import
with_statement
import
__builtin__
import
rlcompleter
import
re
from
bpython
import
inspection
# Needed for special handling of __abstractmethods__
# abc only exists since 2.6, so check both that it exists and that it's
# the one we're expecting
try
:
import
abc
abc
.
ABCMeta
has_abc
=
True
except
(
ImportError
,
AttributeError
):
has_abc
=
False
# Autocomplete modes
SIMPLE
=
'simple'
SUBSTRING
=
'substring'
FUZZY
=
'fuzzy'
class
Autocomplete
(
rlcompleter
.
Completer
):
"""
"""
def
__init__
(
self
,
namespace
=
None
,
config
=
None
):
rlcompleter
.
Completer
.
__init__
(
self
,
namespace
)
self
.
locals
=
namespace
if
hasattr
(
config
,
'autocomplete_mode'
):
self
.
autocomplete_mode
=
config
.
autocomplete_mode
else
:
self
.
autocomplete_mode
=
SUBSTRING
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__
dictpattern
=
re
.
compile
(
'[^\[\]]+\[$'
)
def
complete_dict
(
text
):
lastbracket_index
=
text
.
rindex
(
'['
)
dexpr
=
text
[:
lastbracket_index
].
lstrip
()
obj
=
eval
(
dexpr
,
self
.
locals
)
if
obj
and
isinstance
(
obj
,
type
({}))
and
obj
.
keys
():
self
.
matches
=
[
dexpr
+
"[{!r}]"
.
format
(
k
)
for
k
in
obj
.
keys
()]
else
:
# empty dictionary
self
.
matches
=
[]
if
state
==
0
:
if
"."
in
text
:
if
dictpattern
.
match
(
text
):
complete_dict
(
text
)
else
:
# Examples: 'foo.b' or 'foo[bar.'
for
i
in
range
(
1
,
len
(
text
)
+
1
):
if
text
[
-
i
]
==
'['
:
i
-=
1
break
methodtext
=
text
[
-
i
:]
self
.
matches
=
[
''
.
join
([
text
[:
-
i
],
m
])
for
m
in
self
.
attr_matches
(
methodtext
)]
elif
dictpattern
.
match
(
text
):
complete_dict
(
text
)
else
:
self
.
matches
=
self
.
global_matches
(
text
)
try
:
return
self
.
matches
[
state
]
except
IndexError
:
return
None
def
attr_matches
(
self
,
text
):
"""Taken from rlcompleter.py and bent to my will.
"""
# Gna, Py 2.6's rlcompleter searches for __call__ inside the
# instance instead of the type, so we monkeypatch to prevent
# side-effects (__getattr__/__getattribute__)
m
=
re
.
match
(
r"(\w+(\.\w+)*)\.(\w*)"
,
text
)
if
not
m
:
return
[]
expr
,
attr
=
m
.
group
(
1
,
3
)
if
expr
.
isdigit
():
# Special case: float literal, using attrs here will result in
# a SyntaxError
return
[]
obj
=
eval
(
expr
,
self
.
locals
)
with
inspection
.
AttrCleaner
(
obj
):
matches
=
self
.
attr_lookup
(
obj
,
expr
,
attr
)
return
matches
def
attr_lookup
(
self
,
obj
,
expr
,
attr
):
"""Second half of original attr_matches method factored out so it can
be wrapped in a safe try/finally block in case anything bad happens to
restore the original __getattribute__ method."""
words
=
dir
(
obj
)
if
hasattr
(
obj
,
'__class__'
):
words
.
append
(
'__class__'
)
words
=
words
+
rlcompleter
.
get_class_members
(
obj
.
__class__
)
if
has_abc
and
not
isinstance
(
obj
.
__class__
,
abc
.
ABCMeta
):
try
:
words
.
remove
(
'__abstractmethods__'
)
except
ValueError
:
pass
matches
=
[]
n
=
len
(
attr
)
for
word
in
words
:
if
self
.
method_match
(
word
,
n
,
attr
)
and
word
!=
"__builtins__"
:
matches
.
append
(
"%s.%s"
%
(
expr
,
word
))
return
matches
def
_callable_postfix
(
self
,
value
,
word
):
"""rlcompleter's _callable_postfix done right."""
with
inspection
.
AttrCleaner
(
value
):
if
inspection
.
is_callable
(
value
):
word
+=
'('
return
word
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 that match.
"""
hash
=
{}
n
=
len
(
text
)
import
keyword
for
word
in
keyword
.
kwlist
:
if
self
.
method_match
(
word
,
n
,
text
):
hash
[
word
]
=
1
for
nspace
in
[
__builtin__
.
__dict__
,
self
.
namespace
]:
for
word
,
val
in
nspace
.
items
():
if
self
.
method_match
(
word
,
len
(
text
),
text
)
and
word
!=
"__builtins__"
:
hash
[
self
.
_callable_postfix
(
val
,
word
)]
=
1
matches
=
hash
.
keys
()
matches
.
sort
()
return
matches
def
method_match
(
self
,
word
,
size
,
text
):
if
self
.
autocomplete_mode
==
SIMPLE
:
return
word
[:
size
]
==
text
elif
self
.
autocomplete_mode
==
SUBSTRING
:
s
=
r'.*%s.*'
%
text
return
re
.
search
(
s
,
word
)
else
:
s
=
r'.*%s.*'
%
'.*'
.
join
(
list
(
text
))
return
re
.
search
(
s
,
word
)
Back
|
FazBrowse Home
|
New Git URL