Class: Pronto::ClangFormatRunner

Inherits:
Runner
  • Object
show all
Defined in:
lib/pronto/clang_format_runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(_, __ = nil) ⇒ ClangFormatRunner

Returns a new instance of ClangFormatRunner.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/pronto/clang_format_runner.rb', line 6

def initialize(_, __ = nil)
  super
  @inspector = ::Pronto::ClangFormat::Wrapper.new
  comma_separated_exts = ENV['PRONTO_CLANG_FORMAT_FILE_EXTS']
  if comma_separated_exts.nil? # load default cpp files extensions
    @cpp_extensions = %w[c h cpp cc cxx c++ hh hxx hpp h++ icc inl tcc tpp
                         ipp]
  else # load desired extensions from environment variable
    @cpp_extensions = comma_separated_exts.split(',').map(&:strip)
  end
end

Instance Method Details

#cpp_file?(file_path) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/pronto/clang_format_runner.rb', line 32

def cpp_file?(file_path)
  @cpp_extensions.include? file_path.extname[1..-1]
end

#inspect(patch) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/pronto/clang_format_runner.rb', line 36

def inspect(patch)
  file_path = patch.new_file_full_path
  offences = @inspector.run(file_path)
  offences.map do |offence|
    line = patch.added_lines.find do |added_line|
      offence.affected_lines_range.cover? added_line.new_lineno
    end
    new_message(offence, line) unless line.nil?
  end
end

#new_message(offence, line) ⇒ Object



47
48
49
50
# File 'lib/pronto/clang_format_runner.rb', line 47

def new_message(offence, line)
  path = line.patch.delta.new_file[:path]
  Message.new(path, line, :warning, offence.msg, nil, self.class)
end

#runObject



18
19
20
21
22
23
24
25
# File 'lib/pronto/clang_format_runner.rb', line 18

def run
  return [] if !@patches || @patches.count.zero?
  @patches
    .select { |p| valid_patch?(p) }
    .map { |p| inspect(p) }
    .flatten.compact
    .uniq { |message| message.line } # generate only one message per line
end

#valid_patch?(patch) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/pronto/clang_format_runner.rb', line 27

def valid_patch?(patch)
  return false if patch.additions < 1
  cpp_file?(patch.new_file_full_path)
end