diff options
Diffstat (limited to 'lib/diff/lcs/hunk.rb')
-rw-r--r-- | lib/diff/lcs/hunk.rb | 99 |
1 files changed, 54 insertions, 45 deletions
diff --git a/lib/diff/lcs/hunk.rb b/lib/diff/lcs/hunk.rb index 05c3fb6..58a68db 100644 --- a/lib/diff/lcs/hunk.rb +++ b/lib/diff/lcs/hunk.rb @@ -1,4 +1,4 @@ -# -*- ruby encoding: utf-8 -*- +# frozen_string_literal: true require 'diff/lcs/block' @@ -10,9 +10,9 @@ class Diff::LCS::Hunk # the piece of data. def initialize(data_old, data_new, piece, flag_context, file_length_difference) # At first, a hunk will have just one Block in it - @blocks = [ Diff::LCS::Block.new(piece) ] + @blocks = [Diff::LCS::Block.new(piece)] if String.method_defined?(:encoding) - @preferred_data_encoding = data_old.fetch(0, data_new.fetch(0,'') ).encoding + @preferred_data_encoding = data_old.fetch(0, data_new.fetch(0, '')).encoding end @data_old = data_old @data_new = data_new @@ -55,19 +55,21 @@ class Diff::LCS::Hunk # Change the "start" and "end" fields to note that context should be added # to this hunk. attr_accessor :flag_context - undef :flag_context=; - def flag_context=(context) #:nodoc: + undef :flag_context= + def flag_context=(context) #:nodoc: # rubocop:disable Lint/DuplicateMethods return if context.nil? or context.zero? - add_start = (context > @start_old) ? @start_old : context + add_start = context > @start_old ? @start_old : context @start_old -= add_start @start_new -= add_start - if (@end_old + context) > @data_old.size - add_end = @data_old.size - @end_old - else - add_end = context - end + add_end = + if (@end_old + context) > @data_old.size + @data_old.size - @end_old + else + context + end + @end_old += add_end @end_new += add_end end @@ -76,15 +78,13 @@ class Diff::LCS::Hunk # a truthy value so that if there is no overlap, you can know the merge # was skipped. def merge(hunk) - if overlaps?(hunk) - @start_old = hunk.start_old - @start_new = hunk.start_new - blocks.unshift(*hunk.blocks) - else - nil - end + return unless overlaps?(hunk) + + @start_old = hunk.start_old + @start_new = hunk.start_new + blocks.unshift(*hunk.blocks) end - alias_method :unshift, :merge + alias unshift merge # Determines whether there is an overlap between this hunk and the # provided hunk. This will be true if the difference between the two hunks @@ -108,15 +108,15 @@ class Diff::LCS::Hunk when :reverse_ed, :ed_finish ed_diff(format) else - raise "Unknown diff format #{format}." + fail "Unknown diff format #{format}." end end # Note that an old diff can't have any context. Therefore, we know that # there's only one block in the hunk. def old_diff - warn "Expecting only one block in an old diff hunk!" if @blocks.size > 1 - op_act = { "+" => 'a', "-" => 'd', "!" => "c" } + warn 'Expecting only one block in an old diff hunk!' if @blocks.size > 1 + op_act = { '+' => 'a', '-' => 'd', '!' => 'c' } block = @blocks[0] @@ -126,9 +126,16 @@ class Diff::LCS::Hunk s = encode("#{context_range(:old)}#{op_act[block.op]}#{context_range(:new)}\n") # If removing anything, just print out all the remove lines in the hunk # which is just all the remove lines in the block. - @data_old[@start_old .. @end_old].each { |e| s << encode("< ") + e + encode("\n") } unless block.remove.empty? - s << encode("---\n") if block.op == "!" - @data_new[@start_new .. @end_new].each { |e| s << encode("> ") + e + encode("\n") } unless block.insert.empty? + unless block.remove.empty? + @data_old[@start_old..@end_old].each { |e| s << encode('< ') + e + encode("\n") } + end + + s << encode("---\n") if block.op == '!' + + unless block.insert.empty? + @data_new[@start_new..@end_new].each { |e| s << encode('> ') + e + encode("\n") } + end + s end private :old_diff @@ -148,7 +155,7 @@ class Diff::LCS::Hunk # file -- don't take removed items into account. lo, hi, num_added, num_removed = @start_old, @end_old, 0, 0 - outlist = @data_old[lo .. hi].map { |e| e.insert(0, encode(' ')) } + outlist = @data_old[lo..hi].map { |e| e.insert(0, encode(' ')) } @blocks.each do |block| block.remove.each do |item| @@ -177,9 +184,9 @@ class Diff::LCS::Hunk # Print out file 1 part for each block in context diff format if there # are any blocks that remove items lo, hi = @start_old, @end_old - removes = @blocks.select { |e| not e.remove.empty? } + removes = @blocks.reject { |e| e.remove.empty? } if removes - outlist = @data_old[lo .. hi].map { |e| e.insert(0, encode(' ')) } + outlist = @data_old[lo..hi].map { |e| e.insert(0, encode(' ')) } removes.each do |block| block.remove.each do |item| @@ -191,9 +198,9 @@ class Diff::LCS::Hunk s << encode("\n--- #{r} ----\n") lo, hi = @start_new, @end_new - inserts = @blocks.select { |e| not e.insert.empty? } + inserts = @blocks.reject { |e| e.insert.empty? } if inserts - outlist = @data_new[lo .. hi].collect { |e| e.insert(0, encode(' ')) } + outlist = @data_new[lo..hi].collect { |e| e.insert(0, encode(' ')) } inserts.each do |block| block.insert.each do |item| outlist[item.position - lo][0, 1] = encode(block.op) # + or ! @@ -206,17 +213,18 @@ class Diff::LCS::Hunk private :context_diff def ed_diff(format) - op_act = { "+" => 'a', "-" => 'd', "!" => "c" } - warn "Expecting only one block in an old diff hunk!" if @blocks.size > 1 - - if format == :reverse_ed - s = encode("#{op_act[@blocks[0].op]}#{context_range(:old)}\n") - else - s = encode("#{context_range(:old, ' ')}#{op_act[@blocks[0].op]}\n") - end + op_act = { '+' => 'a', '-' => 'd', '!' => 'c' } + warn 'Expecting only one block in an old diff hunk!' if @blocks.size > 1 + + s = + if format == :reverse_ed + encode("#{op_act[@blocks[0].op]}#{context_range(:old)}\n") + else + encode("#{context_range(:old, ' ')}#{op_act[@blocks[0].op]}\n") + end unless @blocks[0].insert.empty? - @data_new[@start_new .. @end_new].each { |e| s << e + encode("\n") } + @data_new[@start_new..@end_new].each do |e| s << e + encode("\n") end s << encode(".\n") end s @@ -225,7 +233,7 @@ class Diff::LCS::Hunk # Generate a range of item numbers to print. Only print 1 number if the # range has only one item in it. Otherwise, it's 'start,end' - def context_range(mode, op = ',') + def context_range(mode, op = ',') # rubocop:disable Naming/UncommunicativeMethodParamName case mode when :old s, e = (@start_old + 1), (@end_old + 1) @@ -233,7 +241,7 @@ class Diff::LCS::Hunk s, e = (@start_new + 1), (@end_new + 1) end - (s < e) ? "#{s}#{op}#{e}" : "#{e}" + s < e ? "#{s}#{op}#{e}" : e.to_s end private :context_range @@ -249,8 +257,8 @@ class Diff::LCS::Hunk end length = e - s + 1 - first = (length < 2) ? e : s # "strange, but correct" - (length == 1) ? "#{first}" : "#{first},#{length}" + first = length < 2 ? e : s # "strange, but correct" + length == 1 ? first.to_s : "#{first},#{length}" end private :unified_range @@ -263,10 +271,11 @@ class Diff::LCS::Hunk args.map { |arg| arg.encode(string.encoding) } end else - def encode(literal, target_encoding = nil) + def encode(literal, _target_encoding = nil) literal end - def encode_as(string, *args) + + def encode_as(_string, *args) args end end |