-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbracket-matching.py
93 lines (67 loc) · 2.9 KB
/
bracket-matching.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
# -*- coding: utf-8 -*-
from gi.repository import GObject, Gedit, Gio
actions = [
("bracket_match", "<Control>M", "GoTo Bracket Match")
]
brackets = '({[]})'
class BracketMatchingPyPluginAppActivatable( GObject.Object, Gedit.AppActivatable ):
app = GObject.property(type=Gedit.App)
def do_activate( self ):
self.menu_ext = self.extend_menu("tools-section")
for action_name, key, menu_name in actions:
fullname = "win." + action_name
self.app.add_accelerator(key, fullname, None)
item = Gio.MenuItem.new(_(menu_name), fullname)
self.menu_ext.append_menu_item(item)
def do_deactivate( self ):
for action_name, key, menu_name in actions:
self.app.remove_accelerator("win." + action_name, None)
self.menu_ext = None
class BracketMatchingPyPlugin( GObject.Object, Gedit.WindowActivatable ):
__gtype_name__ = 'BracketMatchingPyPlugin'
window = GObject.property(type=Gedit.Window)
def __init__( self ):
GObject.Object.__init__(self)
def do_activate( self ):
self.do_update_state()
for action_name, key, menu_name in actions:
action = Gio.SimpleAction(name=action_name)
action.connect('activate', getattr(self, action_name))
self.window.add_action(action)
def do_update_state( self ):
self.doc = self.window.get_active_document()
self.view = self.window.get_active_view()
def bracket_match( self, action=None, data=None ):
pos = self.doc.get_iter_at_mark(self.doc.get_insert())
bracket = pos.get_char()
if not bracket or bracket not in brackets:
# maybe the bracket is in front of the cursor?
pos.backward_char()
bracket = pos.get_char()
if not bracket or bracket not in brackets:
# print('Not a bracket')
return
i = brackets.index(bracket)
match = brackets[i*-1 - 1]
def find_match(pos, move_pos):
nest_count = 0
while move_pos():
check_char = pos.get_char()
if check_char == bracket:
nest_count += 1
elif check_char == match and nest_count > 0:
nest_count -= 1
elif check_char == match and nest_count == 0:
# found it!
self.doc.place_cursor(pos)
self.view.scroll_to_iter(pos, 0, False, 0, 0)
break
if i < len(brackets)/2:
# opening bracket, so search forward
# lame... the _find_char functions don't work :(
# if pos.forward_find_char(check_char, data, None):
# self.doc.place_cursor(pos)
find_match(pos, pos.forward_char)
else:
# closing bracket, so search backward
find_match(pos, pos.backward_char)