forked from isamert/gedi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gedi.py
122 lines (95 loc) · 3.98 KB
/
gedi.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
import os
import tempfile
import subprocess
import jedi
from gi.repository import GObject, Gedit, Gtk, GtkSource
#FIXME: find real icon names
icon_names = {'import': '',
'module': '',
'class': '',
'function': '',
'statement': '',
'param': ''}
class Jedi:
def get_script(document):
doc_text = document.get_text(document.get_start_iter(), document.get_end_iter(), False)
iter_cursor = document.get_iter_at_mark(document.get_insert())
linenum = iter_cursor.get_line() + 1
charnum = iter_cursor.get_line_index()
return jedi.api.Script(doc_text, linenum, charnum, document.get_uri_for_display())
class GediPlugin(GObject.Object, Gedit.ViewActivatable):
__gtype_name__ = "GediPlugin"
py_extension = ".py"
view = GObject.property(type=Gedit.View)
def __init__(self):
GObject.Object.__init__(self)
self.completion_provider = None
def do_activate(self):
print("Gedi is activated.")
document = self.view.get_buffer()
document.connect("loaded", self.on_document_load)
if document.get_uri_for_display().endswith(self.py_extension):
self.completion_provider = GediCompletionProvider()
self.view.get_completion().add_provider(self.completion_provider)
def do_deactivate(self):
print("Gedi is deactivated.")
def on_document_load(self, document, p3=None, p4=None, p5=0, p6=0):
if document.get_uri_for_display().endswith(self.py_extension):
if self.completion_provider is None:
self.completion_provider = GediCompletionProvider()
self.view.get_completion().add_provider(self.completion_provider)
else:
if self.completion_provider is not None:
self.view.get_completion().remove_provider(self.completion_provider)
self.completion_provider = None
class GediCompletionProvider(GObject.Object, GtkSource.CompletionProvider):
__gtype_name__ = 'GediProvider'
def __init__(self):
GObject.Object.__init__(self)
def do_get_name(self):
return _("Gedi Python Code Completion")
def get_iter_correctly(self, context):
if isinstance(context.get_iter(), tuple):
return context.get_iter()[1];
else:
return context.get_iter()
def do_match(self, context):
iter = self.get_iter_correctly(context)
iter.backward_char()
buffer=iter.get_buffer()
if buffer.get_context_classes_at_iter(iter) != ['no-spell-check']:
return False
ch = iter.get_char()
if not (ch in ('_', '.') or ch.isalnum()):
return False
return True
def do_get_priority(self):
return 1
def do_get_activation(self):
return GtkSource.CompletionActivation.INTERACTIVE
def do_populate(self, context):
#TODO: do async maybe?
it = self.get_iter_correctly(context)
document = it.get_buffer()
proposals = []
for completion in Jedi.get_script(document).completions():
complete = completion.name
if jedi.__version__ <= (0,7,0):
doc=completion.doc
else:
doc=completion.docstring()
proposals.append(GtkSource.CompletionItem.new(completion.name,
completion.name,
self.get_icon_for_type(completion.type),
doc))
context.add_proposals(self, proposals, True)
def get_icon_for_type(self, _type):
theme = Gtk.IconTheme.get_default()
try:
return theme.load_icon(icon_names[_type.lower()], 16, 0)
except:
try:
return theme.load_icon(Gtk.STOCK_ADD, 16, 0)
except:
return None
GObject.type_register(GediCompletionProvider)