|
| 1 | +# MIT License |
| 2 | +# |
| 3 | +# Copyright (c) 2022 CustomEntity |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | +require "../coding_style" |
| 23 | +require "../../file/file_manager" |
| 24 | + |
| 25 | +# TO IMPROVE (More precision on functions with level) |
| 26 | + |
| 27 | +class Indent < CodingStyle |
| 28 | + def initialize(@type : CodingStyleType, @file_target : Int32, @level : CodingStyleLevel, @name : String, @desc : String) |
| 29 | + super(@type, @file_target, @level, @name, @desc) |
| 30 | + end |
| 31 | + |
| 32 | + def handle(file_path : String, content : String, options : Hash(String, String)) : Set(CodingStyleErrorInfo) |
| 33 | + errors : Set(CodingStyleErrorInfo) = Set(CodingStyleErrorInfo).new |
| 34 | + curr_line = 1 |
| 35 | + bad_indent_name : String |
| 36 | + good_indent_char : Char |
| 37 | + bad_indent_regex : Regex |
| 38 | + indent_incr : Int32 |
| 39 | + |
| 40 | + if get_file_type(file_path) == FileType::Makefile |
| 41 | + bad_indent_name = "Spaces" |
| 42 | + good_indent_char = '\t' |
| 43 | + indent_incr = 4 |
| 44 | + bad_indent_regex = /^ +.*$/ |
| 45 | + else |
| 46 | + bad_indent_name = "Tabulations" |
| 47 | + good_indent_char = ' ' |
| 48 | + bad_indent_regex = /^\t+.*$/ |
| 49 | + indent_incr = 1 |
| 50 | + end |
| 51 | + content.each_line { |line| |
| 52 | + curr_indent = 0 |
| 53 | + while curr_indent < line.size && line[curr_indent] == good_indent_char |
| 54 | + curr_indent += indent_incr |
| 55 | + end |
| 56 | + if line =~ bad_indent_regex |
| 57 | + errors.add(CodingStyleErrorInfo.new(self, file_path, curr_line, -1, " (#{bad_indent_name} are not allowed)".magenta)) |
| 58 | + elsif curr_indent % 4 != 0 |
| 59 | + errors.add(CodingStyleErrorInfo.new(self, file_path, curr_line, -1, " (Bad indentation)".magenta)) |
| 60 | + end |
| 61 | + curr_line += 1 |
| 62 | + } |
| 63 | + errors |
| 64 | + end |
| 65 | +end |
0 commit comments