forked from odyssomay/paredit
/
depth.py
78 lines (58 loc) · 1.92 KB
/
depth.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
import sublime, sublime_plugin
try:
from paredit import shared
except:
import shared
def insert_brackets(view, edit, lbracket, rbracket, region):
view.insert(edit, region.begin(), lbracket)
view.insert(edit, region.end() + 1, rbracket)
return region.begin() + 1
def paredit_wrap(view, edit, lbracket, rbracket):
def f(region):
if not region.a == region.b:
return insert_brackets(view, edit, lbracket, rbracket, region)
point = region.a
(lb, rb) = shared.get_next_expression(view, point)
if lb and rb:
return insert_brackets(view, edit, lbracket, rbracket,
sublime.Region(lb, rb))
return point
shared.edit_selections(view, f)
def paredit_wrap_round(view, edit):
paredit_wrap(view, edit, "(", ")")
def paredit_wrap_square(view, edit):
paredit_wrap(view, edit, "[", "]")
def paredit_wrap_curly(view, edit):
paredit_wrap(view, edit, "{", "}")
def paredit_meta_doublequote(view, edit):
paredit_wrap(view, edit, "\"", "\"")
def paredit_splice_sexp(view, edit):
def f(region):
if not region.a == region.b:
return region
point = region.a
(lb, rb) = shared.get_expression(view, point)
if lb and rb:
view.erase(edit, sublime.Region(rb - 1, rb))
view.erase(edit, sublime.Region(lb, lb + 1))
return point - 1
else:
return point
shared.edit_selections(view, f)
####
#### Commands
class Paredit_wrap_roundCommand(sublime_plugin.TextCommand):
def run(self, edit):
paredit_wrap_round(self.view, edit)
class Paredit_wrap_squareCommand(sublime_plugin.TextCommand):
def run(self, edit):
paredit_wrap_square(self.view, edit)
class Paredit_wrap_curlyCommand(sublime_plugin.TextCommand):
def run(self, edit):
paredit_wrap_curly(self.view, edit)
class Paredit_meta_doublequoteCommand(sublime_plugin.TextCommand):
def run(self, edit):
paredit_meta_doublequote(self.view, edit)
class Paredit_splice_sexpCommand(sublime_plugin.TextCommand):
def run(self, edit):
paredit_splice_sexp(self.view, edit)