-
Notifications
You must be signed in to change notification settings - Fork 148
/
autodelimiter.lua
26 lines (23 loc) · 941 Bytes
/
autodelimiter.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
local pairs = {
['('] = ')', ['['] = ']', ['{'] = '}', ['"'] = '"', ["'"] = "'"}
local closing = [[)}]'"]]
return {
name = "Auto-insertion of delimiters",
description = [[Adds auto-insertion of delimiters (), {}, [], '', and "".]],
author = "Paul Kulchenko",
version = 0.2,
onEditorCharAdded = function(self, editor, event)
local keycode = event:GetKey()
if keycode > 255 then return end -- special or unicode characters can be skipped here
local char = string.char(keycode)
local curpos = editor:GetCurrentPos()
if closing:find(char, 1, true) and editor:GetCharAt(curpos) == keycode then
-- if the entered text matches the closing one
-- and the current symbol is the same, then "eat" the character
editor:DeleteRange(curpos, 1)
elseif pairs[char] then
-- if the entered matches opening delimiter, then insert the pair
editor:InsertText(-1, pairs[char])
end
end,
}