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
|
|
|
FOLD_MARKER_EXP = re.compile("^([ \\t]*)(;;.*)$") |
15
|
|
|
SECTION_EXP = re.compile("^([ \\t]*)(\\[.*)$") |
16
|
|
|
EMPTY_LINE_EXP = re.compile("^\\s*$") |
17
|
|
|
COMMENT_EXP = re.compile("^\\s*(;.*)") |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
class IndentType: # pylint: disable=R0903; enum |
21
|
|
|
"""Enum to store the several types of area blocks you can have in a rainmeter skin.""" |
22
|
|
|
|
23
|
|
|
# lvl 0, lvl 1, lvl 1 or 2 |
24
|
|
|
Initial, FoldMarker, Section = range(1, 4) |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
def calc_line_indention_depth(line, context, context_depth): |
28
|
|
|
""".""" |
29
|
|
|
empty_line_match = EMPTY_LINE_EXP.match(line) |
30
|
|
|
if empty_line_match: |
31
|
|
|
return (0, context, context_depth) |
32
|
|
|
|
33
|
|
|
folder_marker_match = FOLD_MARKER_EXP.match(line) |
34
|
|
|
if folder_marker_match: |
35
|
|
|
return (0, IndentType.FoldMarker, 1) |
36
|
|
|
|
37
|
|
|
comment_match = COMMENT_EXP.match(line) |
38
|
|
|
if comment_match: |
39
|
|
|
return (context_depth, context, context_depth) |
40
|
|
|
|
41
|
|
|
section_match = SECTION_EXP.match(line) |
42
|
|
|
if section_match: |
43
|
|
|
if context == IndentType.Section: |
44
|
|
|
return (context_depth - 1, IndentType.Section, context_depth) |
45
|
|
|
else: |
46
|
|
|
return (context_depth, IndentType.Section, context_depth + 1) |
47
|
|
|
|
48
|
|
|
# key value case |
49
|
|
|
return (context_depth, context, context_depth) |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
def get_line_replacement(line, context_depth): |
53
|
|
|
"""Replace the current line with the given indention level.""" |
54
|
|
|
stripped = line.lstrip() |
55
|
|
|
replacement = "\t" * context_depth + stripped |
56
|
|
|
|
57
|
|
|
return replacement |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
def indent_text_by_tab_size(text): |
61
|
|
|
"""Main entry point for indenting text.""" |
62
|
|
|
lines = text.split("\n") |
63
|
|
|
context = IndentType.Initial |
64
|
|
|
context_depth = 0 |
65
|
|
|
|
66
|
|
|
result = [] |
67
|
|
|
|
68
|
|
|
for line in lines: |
69
|
|
|
depth, context, context_depth = calc_line_indention_depth(line, context, context_depth) |
70
|
|
|
replacement = get_line_replacement(line, depth) |
71
|
|
|
result.append(replacement) |
72
|
|
|
|
73
|
|
|
return "\n".join(result) |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
class RainmeterIndentCommand(sublime_plugin.TextCommand): |
77
|
|
|
""" |
78
|
|
|
Indent a Rainmeter file so code folding is possible in a sensible way. |
79
|
|
|
|
80
|
|
|
Double semicolons at the start of a line indent everything until the next |
81
|
|
|
double semicolon so you can create custom fold markers. If nothing is |
82
|
|
|
selected, the whole file will be indented. If one or more regions are |
83
|
|
|
selected, only these lines will be indented without paying attention to |
84
|
|
|
the surroundings |
85
|
|
|
""" |
86
|
|
|
|
87
|
|
|
def __get_selected_region(self): |
88
|
|
|
# If nothing is selected, apply to whole buffer |
89
|
|
|
if self.view.sel()[0].a == self.view.sel()[-1].b: |
90
|
|
|
regions = [sublime.Region(0, self.view.size())] |
91
|
|
|
# If something is selected, apply only to selected regions |
92
|
|
|
else: |
93
|
|
|
regions = self.view.sel() |
94
|
|
|
|
95
|
|
|
return regions |
96
|
|
|
|
97
|
|
|
def run(self, edit): # pylint: disable=R0201; sublime text API, no need for class reference |
98
|
|
|
"""Called when the command is run.""" |
99
|
|
|
regions = self.__get_selected_region() |
100
|
|
|
|
101
|
|
|
for region in regions: |
102
|
|
|
text = self.view.substr(region) |
103
|
|
|
indented_text = indent_text_by_tab_size(text) |
104
|
|
|
self.view.replace(edit, region, indented_text) |
105
|
|
|
|
106
|
|
|
def is_enabled(self): # pylint: disable=R0201; sublime text API, no need for class reference |
107
|
|
|
""" |
108
|
|
|
Return True if the command is able to be run at this time. |
109
|
|
|
|
110
|
|
|
The default implementation simply always returns True. |
111
|
|
|
""" |
112
|
|
|
# Check if current syntax is rainmeter |
113
|
|
|
israinmeter = self.view.score_selector(self.view.sel()[0].a, "source.rainmeter") |
114
|
|
|
|
115
|
|
|
return israinmeter > 0 |
116
|
|
|
|
117
|
|
|
def description(self): # pylint: disable=R0201; sublime text API, no need for class reference |
118
|
|
|
""" |
119
|
|
|
Return a description of the command with the given arguments. |
120
|
|
|
|
121
|
|
|
Used in the menus, and for Undo/Redo descriptions. |
122
|
|
|
|
123
|
|
|
Return None to get the default description. |
124
|
|
|
""" |
125
|
|
|
return "Indent Ini for Code Folding" |
126
|
|
|
|