-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wraptor.py
239 lines (187 loc) · 6.95 KB
/
wraptor.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
import json
import os
from os import path
from gi.repository import Gedit
from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import GtkSource
from gi.repository import PeasGtk
from xdg import BaseDirectory
#: Global whitelist which may be used by both the Wraptor as the ConfigMenu
whitelist = []
#: Global whitelist which may be used by both the Wraptor as the ConfigMenu
blacklist = []
#: The path where to store the configuration.
config_dir = path.join(BaseDirectory.xdg_config_home, 'gedit-wraptor')
def mkdirp(full):
"""
Recursively constructs a full directory path.
Similar to ``mkdir -p``.
"""
assembled = ''
for p in path.split(full):
assembled = path.join(assembled, p)
if not path.exists(assembled):
os.mkdir(assembled)
class ListWidget(Gtk.ScrolledWindow):
"""
Represents a fully preconfigured scrolled Gtk.TreeView representing a list.
"""
def __init__(self, items, label='', location=None):
"""
Args:
items (list): The list of items to represent.
label (str): The label to add to the top of the view.
"""
super().__init__()
self.items = items
self.location = path.join(config_dir, location) if location else None
self.store = Gtk.ListStore(str)
for item in items:
self.store.append([item])
combo_cell_text = Gtk.CellRendererText()
column_text = Gtk.TreeViewColumn(label, combo_cell_text, text=0)
self.tree = Gtk.TreeView(self.store)
self.tree.get_selection().set_mode(Gtk.SelectionMode.MULTIPLE)
self.tree.append_column(column_text)
self.set_hexpand(True)
self.set_vexpand(True)
self.add(self.tree)
def append(self, item):
"""
Appends an item to the list and the view.
item (str): The item to append.
"""
self.store.append([item])
self.items.append(item)
self._save()
def remove(self, row):
"""
Removed a tree row value from the list and its iter from view.
Args:
row: (gi.repository.Gtk.TreeModelRow): The row to remove.
"""
# We can't safely rely on the index after removing it from the store.
item = row[0]
self.store.remove(row.iter)
self.items.remove(item)
self._save()
def _save(self):
if self.location:
with open(self.location, 'w') as f:
json.dump(self.items, f)
def tab_added(tab):
"""
Sets the wrap mode for whitelisted languages.
Args:
tab: The tab of which view to set the wrap mode.
"""
view = tab.get_view()
document = tab.get_document()
language = document.get_language()
if not language:
return
if language.get_name().lower() in whitelist:
view.set_wrap_mode(Gtk.WrapMode.WORD)
elif language.get_name().lower() in blacklist:
view.set_wrap_mode(Gtk.WrapMode.NONE)
class WraptorInit(GObject.Object, Gedit.AppActivatable):
"""
Load the global plugin configuration.
"""
window = GObject.property(type=Gedit.Window)
def do_activate(self):
"""
Load the plugin configuration.
"""
mkdirp(config_dir)
try:
with open(path.join(config_dir, 'whitelist.json')) as f:
whitelist.extend(json.load(f))
except FileNotFoundError:
pass
try:
with open(path.join(config_dir, 'blacklist.json')) as f:
blacklist.extend(json.load(f))
except FileNotFoundError:
pass
class Wraptor(GObject.Object, Gedit.WindowActivatable):
"""
The main plugin activatable.
"""
__gtype_name__ = 'Wraptor'
window = GObject.property(type=Gedit.Window)
def do_activate(self):
"""
Binds tab changing events.
"""
self.window.connect('tab-added', lambda _, tab: tab_added(tab))
self.window.connect('active-tab-state-changed',
lambda window: tab_added(window.get_active_tab()))
class ConfigMenu(GObject.Object, Gedit.WindowActivatable,
PeasGtk.Configurable):
"""
Represents the configuration screen for the plugin.
"""
window = GObject.property(type=Gedit.Window)
def do_create_configure_widget(self):
box = Gtk.Box()
language_manager = GtkSource.LanguageManager()
self.undecided = []
for id in language_manager.get_language_ids():
if id not in whitelist and id not in blacklist:
self.undecided.append(id)
self.whitelist_widget = ListWidget(whitelist,
'Always wrap', 'whitelist.json')
self.blacklist_widget = ListWidget(blacklist,
'Never wrap', 'blacklist.json')
self.undecided_widget = ListWidget(self.undecided)
self.button_add_whitelist = Gtk.Button('<')
self.button_remove_whitelist = Gtk.Button('>')
self.button_add_blacklist = Gtk.Button('>')
self.button_remove_blacklist = Gtk.Button('<')
self.button_add_whitelist.connect('clicked', self.whitelist_add)
self.button_remove_whitelist.connect('clicked', self.whitelist_remove)
self.button_add_blacklist.connect('clicked', self.blacklist_add)
self.button_remove_blacklist.connect('clicked', self.blacklist_remove)
box.add(self.whitelist_widget)
box.add(self.button_add_whitelist)
box.add(self.button_remove_whitelist)
box.add(self.undecided_widget)
box.add(self.button_remove_blacklist)
box.add(self.button_add_blacklist)
box.add(self.blacklist_widget)
box.set_size_request(500, 700)
return box
def whitelist_add(self, _):
"""
Adds the undecided selection to the whitelist.
"""
self.move(self.undecided_widget, self.whitelist_widget)
def whitelist_remove(self, _):
"""
Removes the whitelist selection from the whitelist.
"""
self.move(self.whitelist_widget, self.undecided_widget)
def blacklist_add(self, _):
"""
Adds the undecided selection to the blacklist.
"""
self.move(self.undecided_widget, self.blacklist_widget)
def blacklist_remove(self, _):
"""
Removes the blacklist selection from the whitelist.
"""
self.move(self.blacklist_widget, self.undecided_widget)
def move(self, from_, to):
"""
Move a selection from one list widget to another.
from_ (ListWidget): The list widget from which to remove the selection.
to (ListWidget): The list widget to which to append the selection.
"""
selection = from_.tree.get_selection()
model, rows = selection.get_selected_rows()
iters = [model[r] for r in rows]
for row in iters:
to.append(row[0])
from_.remove(row)