-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdoc_indentation_checker.rb
76 lines (58 loc) · 2.29 KB
/
rdoc_indentation_checker.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# FIXME:
# class Comment ; end
# function names should be verbs: "calculate_comment_line_indentations", etc.
# consistency indentation is tricky -- what are rdoc's rules?
# FIXME: Re-implement with Enumerable#chunk.
require_relative 'array_select_ranges'
CALL_SEQ_REGEX = /\A +\*( +)call-seq:/
C_COMMENT_LINE_REGEX = /\A ?(\/)?\* ?(.*)/
# files.each do |file|
# call_sequence_comments(file).each do |comment|
# puts "Indentation warning: #{file}" unless comment_has_consistent_indentation? comment
# end
# end
def call_sequence_comments file
File.open filename do |file|
find_call_sequence_comments file
end
end
def find_call_sequence_comments file
# Could be done in one multiline regex. Fail.
c_comments = find_c_comments file
c_comments.select { |comment| is_call_sequence_comment? comment }
end
def find_c_comments file
data = file.readlines # Keep an array; otherwise, we must rewind after each access.
c_comment_ranges = data.select_ranges { |line| line =~ C_COMMENT_LINE_REGEX }
c_comment_ranges.map { |comment_range| data[comment_range] }
end
def is_call_sequence_comment? comment
comment.any? { |line| line =~ CALL_SEQ_REGEX }
end
def comment_has_consistent_indentation? comment
indentation_unit = call_sequence_line_indentation comment
indentations = comment_line_indentations comment
has_consistent_indentation = true
# To add line numbers: "indentations.each_with_index.map { |line_num, ind| ... }"
indentations.map do |line_number, indentation|
previous_indentation = indentations.fetch(line_number - 1, 0)
has_consistent_indentation = false unless
(indentation - previous_indentation).abs % indentation_unit
# FIXME: Check how rdoc handles " * call-seq: " single space, then double space on
# following lines.
end
has_consistent_indentation
end
def call_sequence_line_indentation comment
call_sequence_line_index = comment.find_index { |line| line =~ /\A +\* +call-seq:/ }
indentations = comment_line_indentations comment
indentations[call_sequence_line_index]
end
def comment_line_indentations comment
comment.each_with_index.inject({}) do |indentations, (line_data, line_number)|
indentations.merge({ line_number => line_indentation(line_data) })
end
end
def line_indentation line
line[/\A *\/?\*( *).*\z/, 1].length
end