Skip to content

Commit 56c8006

Browse files
committed
Implement Splice Sexp Paredit command #9
1 parent 45c48b7 commit 56c8006

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

paredit.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,20 @@ def raise_sexp(view, edit):
299299
element = find_next_element(view, point)
300300
view.replace(edit, innermost, view.substr(element))
301301
view.run_command('tutkain_indent_region', {'prune': True})
302+
303+
304+
def splice_sexp(view, edit):
305+
for region, _ in iterate(view):
306+
point = region.begin()
307+
308+
innermost = sexp.innermost(view, point, absorb=True, edge=False)
309+
310+
open_point = sexp.find_point(
311+
view, innermost.begin(), lambda p: view.substr(p) in sexp.OPEN or view.substr(p) in '"'
312+
) + 1
313+
314+
# Erase the close character
315+
view.erase(edit, sublime.Region(innermost.end(), innermost.end() - 1))
316+
# Erase one or more open characters
317+
view.erase(edit, sublime.Region(innermost.begin(), open_point))
318+
view.run_command('tutkain_indent_region', {'prune': True})

tests/test_paredit.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,22 @@ def test_raise_sexp(self):
427427
self.set_selections((1, 1))
428428
self.view.run_command('tutkain_paredit_raise_sexp')
429429
self.assertEquals('"b"', self.view_content())
430+
431+
def test_splice_sexp(self):
432+
self.set_view_content('(a (b c) d) (e (f g) h)')
433+
self.set_selections((5, 5), (17, 17))
434+
self.view.run_command('tutkain_paredit_splice_sexp')
435+
self.assertEquals('(a b c d) (e f g h)', self.view_content())
436+
self.assertEquals([(4, 4), (14, 14)], self.selections())
437+
438+
self.set_view_content('#{1 2 3}')
439+
self.set_selections((2, 2))
440+
self.view.run_command('tutkain_paredit_splice_sexp')
441+
self.assertEquals('1 2 3', self.view_content())
442+
self.assertEquals([(0, 0)], self.selections())
443+
444+
self.set_view_content('"a b"')
445+
self.set_selections((1, 1))
446+
self.view.run_command('tutkain_paredit_splice_sexp')
447+
self.assertEquals('a b', self.view_content())
448+
self.assertEquals([(0, 0)], self.selections())

tutkain.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,11 @@ def run(self, edit):
587587
paredit.raise_sexp(self.view, edit)
588588

589589

590+
class TutkainPareditSpliceSexpCommand(sublime_plugin.TextCommand):
591+
def run(self, edit):
592+
paredit.splice_sexp(self.view, edit)
593+
594+
590595
class TutkainCycleCollectionTypeCommand(sublime_plugin.TextCommand):
591596
def run(self, edit):
592597
sexp.cycle_collection_type(self.view, edit)

0 commit comments

Comments
 (0)