11# frozen_string_literal: true
22
3+ require 'differ'
4+
35# Contains methods to write out html for the violations.
46# This mixin is used by DiffReportBuilder.
57module DiffReportBuilderViolations
@@ -25,7 +27,7 @@ def build_violation_table(doc, key, value)
2527 end
2628
2729 def build_violation_table_head ( doc )
28- build_table_head ( doc , '' , 'Priority' , ' Rule', 'Message' , 'Line' )
30+ build_table_head ( doc , '' , 'Rule' , 'Message' , 'Line' )
2931 end
3032
3133 def build_violation_table_body ( doc , key , value )
@@ -36,42 +38,61 @@ def build_violation_table_body(doc, key, value)
3638 end
3739 end
3840
39- def build_violation_table_row ( doc , key , pmd_violation )
40- doc . tr ( class : pmd_violation . branch == PmdTester :: BASE ? 'b' : 'a' ) do
41+ def build_violation_table_row ( doc , key , violation )
42+ doc . tr ( class : get_css_class ( violation ) ) do
4143 build_table_anchor_column ( doc , 'A' , increment_violation_index )
4244
43- violation = pmd_violation . attrs
44-
45- # The priority of the rule
46- doc . td violation [ 'priority' ]
47-
4845 # The rule that trigger the violation
4946 doc . td do
50- doc . a ( href : ( violation [ 'externalInfoUrl' ] ) . to_s ) { doc . text violation [ 'rule' ] }
47+ doc . a ( href : violation . info_url . to_s ) { doc . text violation . rule_name }
5148 end
5249
5350 # The violation message
54- doc . td "\n " + pmd_violation . text + "\n "
55-
56- # The begin line of the violation
57- line = violation [ 'beginline' ]
51+ if violation . changed && violation . message != violation . old_message
52+ doc . td { diff_fragments ( doc , violation ) }
53+ else
54+ doc . td violation . text
55+ end
5856
5957 # The link to the source file
6058 doc . td do
6159 link = get_link_to_source ( violation , key )
62- doc . a ( href : link . to_s ) { doc . text line }
60+ doc . a ( href : link . to_s ) { doc . text display_line ( violation ) }
6361 end
6462 end
6563 end
6664
65+ def diff_fragments ( doc , violation )
66+ diff = Differ . diff_by_word ( violation . old_message , violation . message )
67+ doc << diff . format_as ( :html )
68+ end
69+
70+ def display_line ( violation )
71+ if violation . changed && violation . old_line && violation . old_line != violation . line
72+ "#{ violation . old_line } => #{ violation . line } "
73+ else
74+ violation . line
75+ end
76+ end
77+
6778 def get_link_to_source ( violation , key )
6879 l_str = @project . type == 'git' ? 'L' : 'l'
69- line_str = "##{ l_str } #{ violation [ 'beginline' ] } "
80+ line_str = "##{ l_str } #{ violation . line } "
7081 @project . get_webview_url ( key ) + line_str
7182 end
7283
7384 def increment_violation_index
7485 @violation_index ||= 0 # init with 0
7586 @violation_index += 1
7687 end
88+
89+ def get_css_class ( pmd_violation )
90+ if pmd_violation . changed
91+ 'd'
92+ elsif pmd_violation . branch == PmdTester ::BASE
93+ 'b'
94+ else
95+ 'a'
96+ end
97+ end
7798end
0 commit comments