GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

IndentType   A
last analyzed

Complexity

Total Complexity 0

Size/Duplication

Total Lines 5
Duplicated Lines 0 %

Importance

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