-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIndentTips.py
More file actions
47 lines (39 loc) · 1.51 KB
/
IndentTips.py
File metadata and controls
47 lines (39 loc) · 1.51 KB
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
import sublime, sublime_plugin
import re
class IndentTipsCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.showTips(self.view, True)
def showTips(self, view, showInputPanel):
sel_a = view.sel()[0].a
sel_b = view.sel()[0].b
line_a = view.line(view.sel()[0]).a
line_b = view.line(view.sel()[0]).b
line_str = view.substr(view.line(view.sel()[0]))
# return if selection is not empty
if sel_a != sel_b :
sublime.status_message("")
return
# return if current line does not have indent
indent = view.find(r'^( | )+', line_a)
if indent.a > line_b or indent.a == -1 :
return
# handle vaild location
if sel_a >= indent.a and sel_a < indent.b :
offset = sel_a - indent.a
indent_str = line_str[0:offset]
indent_str_2 = line_str[0:offset+1]
thisLine = view.line(line_a)
while thisLine.a > 0 :
thisLine = view.line(thisLine.a-1)
# skip empty line
if view.find(r'[^ \n]', thisLine.a).a > thisLine.b :
continue
if indent_str == view.substr(sublime.Region(thisLine.a,thisLine.a+offset)) and indent_str_2 != view.substr(sublime.Region(thisLine.a,thisLine.a+offset+1)) :
result = re.sub(r'\s+', " ", view.substr(thisLine))
if showInputPanel :
sublime.Window.show_quick_panel(sublime.active_window(), [result] , sublime.status_message(result))
sublime.status_message(result)
return
class IndentTipsListener(sublime_plugin.EventListener):
def on_selection_modified(self, view):
IndentTipsCommand.showTips(IndentTipsCommand(self), view, False)