forked from abo-abo/lispy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lispy-python.py
63 lines (53 loc) · 2.07 KB
/
lispy-python.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
# lispy-python.py --- lispy support for Python.
# Copyright (C) 2016 Oleh Krehel
# This file is not part of GNU Emacs
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# For a full copy of the GNU General Public License
# see <http://www.gnu.org/licenses/>.
import inspect
import jedi
def arglist_retrieve (sym):
try:
return inspect.getargspec (sym.__init__)
except TypeError:
return inspect.getargspec (sym)
def format_arg (arg_pair):
name, default_value = arg_pair
if default_value:
return name + " = " + default_value
else:
return name
def delete (element, lst):
return [x for x in lst if x != element]
def arglist (sym, filename = None, line = None, column = None):
try:
arg_info = arglist_retrieve (sym)
if "self" in arg_info.args:
arg_info.args.remove ("self")
if arg_info.defaults:
defaults = [None] * (len (arg_info.args) - len (arg_info.defaults)) + \
map (repr, arg_info.defaults)
args = map (format_arg, zip (arg_info.args, defaults))
else:
args = arg_info.args
if arg_info.varargs:
args += arg_info.varargs
if arg_info.keywords:
args.append ("**" + arg_info.keywords)
return args
except TypeError:
script = jedi.Script(None, line, column, filename)
defs = script.goto_definitions ()
if len (defs) == 0:
raise TypeError ("0 definitions found")
elif len (defs) > 1:
raise TypeError (">1 definitions found")
else:
return delete ('', map (lambda (x): x.description, defs[0].params))