-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSmartDelete.py
43 lines (34 loc) · 1.52 KB
/
SmartDelete.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
import sublime
import sublime_plugin
import re
class SmartDeleteCommand(sublime_plugin.TextCommand):
def run(self, edit):
if len(self.view.sel()) is not 1:
# Default Delete if there are multiple selections.
self.view.run_command('right_delete')
return
# Has only 1 sel
for s in self.view.sel():
if s.empty():
line = self.view.line(s)
after = self.view.substr(line)[s.end() - line.end():]
next_line = self.view.line(sublime.Region(line.end() + 1, line.end() + 1))
next_line_is_not_empty = re.match(r'^\s*$', self.view.substr(next_line)) is None
if(len(line) > 0 and next_line_is_not_empty and (after.isspace() or line.end() == s.end())):
b = s.begin()
e = s.end()
spaces_before = 0
while(self.view.substr(b - 1).isspace()):
spaces_before = spaces_before + 1
b = b - 1
# If there is only one space before - leave it
if(spaces_before == 1):
b = b + 1
while(self.view.substr(e).isspace()):
e = e + 1
self.view.sel().clear()
self.view.sel().add(sublime.Region(b, e))
else:
self.view.run_command('right_delete')
for s in self.view.sel():
self.view.erase(edit, s)