Completed
Pull Request — master (#1147)
by Lasse
02:15 queued 28s
created

bears.c_languages.IndentBear.run()   B

Complexity

Conditions 3

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 29
rs 8.8571
1
import platform
2
3
from coalib.bearlib.abstractions.CorrectionBasedBear import CorrectionBasedBear
4
from coalib.bearlib.spacing.SpacingHelper import SpacingHelper
5
6
7
class IndentBear(CorrectionBasedBear):
8
    BINARY = "indent" if platform.system() != "Darwin" else "gindent"
9
    RESULT_MESSAGE = "Indentation can be improved."
10
11
    def run(self,
12
            filename,
13
            file,
14
            max_line_length: int,
15
            use_spaces: bool,
16
            tab_width: int=SpacingHelper.DEFAULT_TAB_WIDTH,
17
            indent_cli_options: str=''):
18
        """
19
        This bear checks and corrects spacing and indentation via the well
20
        known Indent utility. It is designed to work with the C programming
21
        language but may work reasonably with syntactically similar languages.
22
23
        :param max_line_length:    Maximum number of characters for a line.
24
        :param use_spaces:         True if spaces are to be used, else tabs.
25
        :param tab_width:          Number of spaces per indent level.
26
        :param indent_cli_options: Any command line options the indent binary
27
                                   understands. They will be simply passed
28
                                   through.
29
        """
30
        options = "--no-tabs " if use_spaces else "--use-tabs "
31
        options += "--line-length {0} --indent-level {1} --tab-size {1} " \
32
                   "{2}".format(
33
                max_line_length,
34
                tab_width,
35
                indent_cli_options)
36
        for result in self.retrieve_results(filename,
37
                                            file,
38
                                            cli_options=options):
39
            yield result
40