Sha256: 56635ff6fd5e8019d8c62ea1658eb868f783ca30e99054a7802ad8d4fa3796bc

Contents?: true

Size: 1.34 KB

Versions: 47

Compression:

Stored size: 1.34 KB

Contents

require 'diff/lcs'
require 'diff/lcs/hunk'
module Compass
  module Diff
    #stole this from rspec who stole this from the gem
    def diff_as_string(data_new, data_old)
      data_old = data_old.split(/\n/).map! { |e| e.chomp }
      data_new = data_new.split(/\n/).map! { |e| e.chomp }
      output = ""
      diffs = ::Diff::LCS.diff(data_old, data_new)
      return output if diffs.empty?
      oldhunk = hunk = nil  
      file_length_difference = 0
      diffs.each do |piece|
        begin
          hunk = ::Diff::LCS::Hunk.new(
            data_old, data_new, piece, context_lines, file_length_difference
          )
          file_length_difference = hunk.file_length_difference      
          next unless oldhunk      
          # Hunks may overlap, which is why we need to be careful when our
          # diff includes lines of context. Otherwise, we might print
          # redundant lines.
          if (context_lines > 0) and hunk.overlaps?(oldhunk)
            hunk.unshift(oldhunk)
          else
            output << oldhunk.diff(format)
          end
        ensure
          oldhunk = hunk
          output << "\n"
        end
      end  
      #Handle the last remaining hunk
      output << oldhunk.diff(format) << "\n"
    end
  
    protected

      def format
        :unified
      end

      def context_lines
        3
      end
  
  end
end

Version data entries

47 entries across 46 versions & 6 rubygems

Version Path
compass-0.12.7 test/helpers/diff.rb
archetype-0.0.1.pre.13 test/helpers/diff.rb
archetype-0.0.1.pre.12 test/helpers/diff.rb
archetype-0.0.1.pre.11 test/helpers/diff.rb
archetype-0.0.1.pre.10 test/helpers/diff.rb
archetype-0.0.1.pre.9 test/helpers/diff.rb
archetype-0.0.1.pre.8 test/helpers/diff.rb
sadui-0.0.4 vendor/bundle/ruby/2.0.0/gems/compass-0.12.3/test/helpers/diff.rb
sadui-0.0.4 vendor/bundle/ruby/2.1.0/gems/compass-0.12.3/test/helpers/diff.rb
archetype-0.0.1.pre.7 test/helpers/diff.rb
compass-0.12.6 test/helpers/diff.rb
archetype-0.0.1.pre.6 test/helpers/diff.rb
archetype-0.0.1.pre.5 test/helpers/diff.rb
archetype-0.0.1.pre.4 test/helpers/diff.rb
compass-0.12.5 test/helpers/diff.rb
compass-0.12.4 test/helpers/diff.rb
compass-0.12.3 test/helpers/diff.rb
archetype-0.0.1.pre.3.90263a7 test/helpers/diff.rb
archetype-0.0.1.pre.3.6ed259b test/helpers/diff.rb
archetype-0.0.1.pre.3.f9dde24 test/helpers/diff.rb