forked from jsiwek/WhitesmithsIndentStyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
167 lines (127 loc) · 6.17 KB
/
plugin.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import sublime, sublime_plugin
class State:
reindents = {}
def get_cursor(view):
return view.sel()[0].begin()
def scope_matches(view, position, selectors):
for selector in selectors:
if view.match_selector(position, selector):
return True
return False
def get_start_of_line_text(view, until):
line_region = view.line(until)
line_region.b = until
return view.substr(line_region)
def text_allows_attached_brace(text):
for keyword in ("class", "struct"):
if keyword in text:
return True
return False
class WhitesmithsEnterBlockCommand(sublime_plugin.TextCommand):
def run(self, edit):
cursor_position = get_cursor(self.view)
next_char = self.view.substr(cursor_position)
if next_char == '}':
self.view.run_command('insert', {'characters': '\n\n'})
self.view.run_command('move', {'by': 'lines', 'forward': False})
self.view.run_command('move_to', {'to': 'hardeol', 'extend': False})
self.view.run_command('reindent', {'single_line': True})
self.view.run_command('unindent')
else:
self.view.run_command('insert', {'characters': '\n'})
self.view.run_command('reindent', {'single_line': True})
self.view.run_command('unindent')
class WhitesmithsExitBlockCommand(sublime_plugin.TextCommand):
def run(self, edit):
close_brace_position = get_cursor(self.view)
current_viewport = self.view.viewport_position()
self.view.run_command('move_to', {'to': 'brackets'})
open_brace_position = get_cursor(self.view)
if close_brace_position == open_brace_position:
return
indent_text = get_start_of_line_text(self.view, open_brace_position)
if indent_text.isspace():
prev_line_position = self.view.line(open_brace_position).a - 1
prev_indent_level = self.view.indentation_level(prev_line_position)
else:
prev_indent_level = self.view.indentation_level(open_brace_position)
self.view.sel().clear()
self.view.sel().add(sublime.Region(close_brace_position))
self.view.set_viewport_position(current_viewport)
newline = '\n'
for i in range(prev_indent_level):
newline += '\t'
self.view.insert(edit, close_brace_position, newline)
State.reindents[self.view.buffer_id()] = get_cursor(self.view)
class WhitesmithsEnterCommand(sublime_plugin.TextCommand):
def run(self, edit):
cursor_position = get_cursor(self.view)
last_reindent = State.reindents.pop(self.view.buffer_id(), None)
self.view.run_command('insert', {'characters': '\n'})
if cursor_position == last_reindent:
self.view.run_command('whitesmiths_erase_orphan_reindent',
{'position': last_reindent,
'line_before_cursor': True})
class WhitesmithsEraseOrphanReindentCommand(sublime_plugin.TextCommand):
def run(self, edit, position, line_before_cursor = False):
if self.view.settings().get('trim_automatic_white_space'):
if line_before_cursor:
# Sublime's builtin auto-indent may have removed it's own
# automatic whitespace and invalidated our own reindent
# tracking position.
row = self.view.rowcol(get_cursor(self.view))[0] - 1
indent_region = self.view.line(self.view.text_point(row, 0))
else:
indent_region = self.view.line(position)
indent_text = self.view.substr(indent_region)
if indent_text.isspace():
self.view.erase(edit, indent_region)
class WhitesmithsReindentCommand(sublime_plugin.TextCommand):
def run(self, edit):
cursor_position = get_cursor(self.view)
scopes = self.view.settings().get('whitesmiths_reindent_scopes')
if not scope_matches(self.view, cursor_position, scopes):
self.view.run_command('reindent', {'single_line': True})
return
current_viewport = self.view.viewport_position()
self.view.run_command('move_to', {'to': 'brackets'})
brace_position = get_cursor(self.view)
if brace_position == cursor_position:
self.view.run_command('reindent', {'single_line': True})
return
close_indent_text = get_start_of_line_text(self.view, brace_position)
self.view.run_command('move_to', {'to': 'brackets'})
open_brace_position = get_cursor(self.view)
open_line_text = get_start_of_line_text(self.view, open_brace_position)
self.view.sel().clear()
self.view.sel().add(sublime.Region(cursor_position))
self.view.set_viewport_position(current_viewport)
if text_allows_attached_brace(open_line_text):
open_indent_text = ""
for char in open_line_text:
if not char.isspace():
break;
open_indent_text += char
if open_indent_text:
open_indent_text += '\t'
self.view.insert(edit, cursor_position, open_indent_text)
State.reindents[self.view.buffer_id()] = get_cursor(self.view)
return
if not close_indent_text:
self.view.insert(edit, cursor_position, '\t')
State.reindents[self.view.buffer_id()] = get_cursor(self.view)
return
if not close_indent_text.isspace():
self.view.run_command('reindent', {'single_line': True})
return
self.view.insert(edit, cursor_position, close_indent_text)
State.reindents[self.view.buffer_id()] = get_cursor(self.view)
class WhitesmithsListener(sublime_plugin.EventListener):
def on_selection_modified(self, view):
last_reindent = State.reindents.get(view.buffer_id(), None)
if last_reindent is not None:
cursor_position = get_cursor(view)
if last_reindent != cursor_position:
State.reindents.pop(view.buffer_id(), None)
view.run_command('whitesmiths_erase_orphan_reindent',
{'position': last_reindent})