forked from rgieseke/ta-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bracematching.lua
60 lines (54 loc) · 1.7 KB
/
bracematching.lua
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
-- Highlight and match braces before and after a brace.
-- Modified from
-- [Textadept's editing.lua](http://code.google.com/p/textadept/source/browse/modules/textadept/editing.lua).
-- Textadept's default is to only match the brace to the right which can be
-- preferable if a block caret is used.
module('_m.common.bracematching', package.seeall)
-- ## Setup
-- Disable default highlighting of matching braces.
_m.textadept.editing.HIGHLIGHT_BRACES = false
-- Table with char codes as indices.
braces = { -- () [] {}
[40] = 1, [91] = 1, [123] = 1,
[41] = 1, [93] = 1, [125] = 1,
}
-- ## Commands
-- Highlights matching braces, before and after a brace.
-- Between two braces preference is to the left.
events.connect('update_ui', function()
local buffer = buffer
local pos = buffer.current_pos
if braces[buffer.char_at[pos - 1]] then pos = pos - 1 end
if braces[buffer.char_at[pos]] then
local match_pos = buffer:brace_match(pos)
if match_pos ~= -1 then
buffer:brace_highlight(pos, match_pos)
else
buffer:brace_bad_light(pos)
end
else
buffer:brace_bad_light(-1)
end
end)
-- Goes to a matching brace position, selecting the text inside if specified.<br>
-- Parameter:<br>
-- _select_: If true, selects the text between matching braces.
function match_brace(select)
local buffer = buffer
local caret = buffer.current_pos
if braces[buffer.char_at[caret - 1]] then
caret = caret - 1
end
local match_pos = buffer:brace_match(caret)
if match_pos ~= -1 then
if select then
if match_pos > caret then
buffer:set_sel(caret, match_pos + 1)
else
buffer:set_sel(caret + 1, match_pos)
end
else
buffer:goto_pos(match_pos)
end
end
end