|
1
|
|
|
""" |
|
2
|
|
|
This module handles the indention of the rainmeter files. |
|
3
|
|
|
|
|
4
|
|
|
Default indention is required to allow collapsing of code blocks. |
|
5
|
|
|
""" |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
import re |
|
9
|
|
|
|
|
10
|
|
|
import sublime |
|
11
|
|
|
import sublime_plugin |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
class RainmeterIndentCommand(sublime_plugin.TextCommand): |
|
15
|
|
|
""" |
|
16
|
|
|
Indent a Rainmeter file so code folding is possible in a sensible way. |
|
17
|
|
|
|
|
18
|
|
|
Double semicolons at the start of a line indent everything until the next |
|
19
|
|
|
double semicolon so you can create custom fold markers. If nothing is |
|
20
|
|
|
selected, the whole file will be indented. If one or more regions are |
|
21
|
|
|
selected, only these lines will be indented without paying attention to |
|
22
|
|
|
the surroundings |
|
23
|
|
|
""" |
|
24
|
|
|
|
|
25
|
|
|
# Compile regexs to be used later |
|
26
|
|
|
rwhitespace_line = re.compile("^([ \\t]*)(.*)$") |
|
27
|
|
|
rfold_comment = re.compile("^([ \\t]*)(;;.*)") |
|
28
|
|
|
rsection_head = re.compile("^([ \\t]*)(\\[.*)$") |
|
29
|
|
|
|
|
30
|
|
|
def __get_selected_region(self): |
|
31
|
|
|
# If nothing is selected, apply to whole buffer |
|
32
|
|
|
if self.view.sel()[0].a == self.view.sel()[-1].b: |
|
33
|
|
|
regions = [sublime.Region(0, self.view.size())] |
|
34
|
|
|
# If something is selected, apply only to selected regions |
|
35
|
|
|
else: |
|
36
|
|
|
regions = self.view.sel() |
|
37
|
|
|
|
|
38
|
|
|
return regions |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
def run(self, edit): #pylint: disable=R0201; sublime text API, no need for class reference |
|
42
|
|
|
"""Called when the command is run.""" |
|
43
|
|
|
regions = self.__get_selected_region() |
|
44
|
|
|
|
|
45
|
|
|
for region in regions: |
|
46
|
|
|
# Get numbers of regions' lines |
|
47
|
|
|
reg_lines = self.view.lines(region) |
|
48
|
|
|
line_nums = map(lambda reg: self.view.rowcol(reg.a)[0], reg_lines) |
|
49
|
|
|
|
|
50
|
|
|
# Traverse selected lines |
|
51
|
|
|
current_indent = -1 |
|
52
|
|
|
adjustment = -1 |
|
53
|
|
|
|
|
54
|
|
|
lines = self.view.lines(sublime.Region(0, self.view.size())) |
|
55
|
|
|
for i in line_nums: |
|
56
|
|
|
line = lines[i] |
|
57
|
|
|
line_content = self.view.substr(line) |
|
58
|
|
|
|
|
59
|
|
|
mfc = self.rfold_comment.search(line_content) |
|
60
|
|
|
# If current line is fold comment, extract indentation |
|
61
|
|
|
if mfc: |
|
62
|
|
|
current_indent = len(mfc.group(1)) |
|
63
|
|
|
adjustment = -1 |
|
64
|
|
|
# Else indent current line according to last fold comment |
|
65
|
|
|
else: |
|
66
|
|
|
# Strip leading whitespace |
|
67
|
|
|
mwl = self.rwhitespace_line.search(line_content) |
|
68
|
|
|
stripped_line = "" |
|
69
|
|
|
if mwl: |
|
70
|
|
|
stripped_line = mwl.group(2) |
|
71
|
|
|
# Indent section heads by one more |
|
72
|
|
|
mse = self.rsection_head.search(stripped_line) |
|
73
|
|
|
if mse: |
|
74
|
|
|
adjustment = 0 |
|
75
|
|
|
self.view.replace( |
|
76
|
|
|
edit, |
|
77
|
|
|
line, |
|
78
|
|
|
"\t" * (current_indent + 1) + stripped_line) |
|
79
|
|
|
# Indent key = value line two more |
|
80
|
|
|
else: |
|
81
|
|
|
self.view.replace( |
|
82
|
|
|
edit, |
|
83
|
|
|
line, |
|
84
|
|
|
"\t" * (current_indent + 2 + adjustment) + |
|
85
|
|
|
stripped_line) |
|
86
|
|
|
|
|
87
|
|
|
def is_enabled(self): #pylint: disable=R0201; sublime text API, no need for class reference |
|
88
|
|
|
""" |
|
89
|
|
|
Return True if the command is able to be run at this time. |
|
90
|
|
|
|
|
91
|
|
|
The default implementation simply always returns True. |
|
92
|
|
|
""" |
|
93
|
|
|
# Check if current syntax is rainmeter |
|
94
|
|
|
israinmeter = self.view.score_selector(self.view.sel()[0].a, |
|
95
|
|
|
"source.rainmeter") |
|
96
|
|
|
|
|
97
|
|
|
return israinmeter > 0 |
|
98
|
|
|
|
|
99
|
|
|
def description(self): #pylint: disable=R0201; sublime text API, no need for class reference |
|
100
|
|
|
""" |
|
101
|
|
|
Return a description of the command with the given arguments. |
|
102
|
|
|
|
|
103
|
|
|
Used in the menus, and for Undo/Redo descriptions. |
|
104
|
|
|
|
|
105
|
|
|
Return None to get the default description. |
|
106
|
|
|
""" |
|
107
|
|
|
return "Indent Ini for Code Folding" |
|
108
|
|
|
|