Class: Unparser::CLI::Differ

Inherits:
Object
  • Object
show all
Includes:
Adamantium::Flat
Defined in:
lib/unparser/cli/differ.rb

Overview

Class to create diffs from source code

Constant Summary collapse

CONTEXT_LINES =
5

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(old, new) ⇒ Differ

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return new object

Parameters:

  • old (String)
  • new (String)

Returns:



20
21
22
# File 'lib/unparser/cli/differ.rb', line 20

def self.build(old, new)
  new(lines(old), lines(new))
end

.colorize_line(line) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return colorized diff line

Parameters:

  • line (String)

Returns:

  • (String)


32
33
34
35
36
37
38
39
40
41
# File 'lib/unparser/cli/differ.rb', line 32

def self.colorize_line(line)
  case line[0]
  when '+'
    Color::GREEN
  when '-'
    Color::RED
  else
    Color::NONE
  end.format(line)
end

Instance Method Details

#collapsed_hunksEnumerable<Diff::LCS::Hunk>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return collapsed hunks

Returns:

  • (Enumerable<Diff::LCS::Hunk>)


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/unparser/cli/differ.rb', line 77

def collapsed_hunks
  hunks.each_with_object([]) do |hunk, output|
    last = output.last

    if last && hunk.merge(last)
      output.pop
    end

    output << hunk
  end
end

#colorized_diffString?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return colorized source diff

Returns:

  • (String)

    if there is a diff

  • (nil)

    otherwise



120
121
122
123
124
# File 'lib/unparser/cli/differ.rb', line 120

def colorized_diff
  diff.lines.map do |line|
    self.class.colorize_line(line)
  end.join
end

#diffString?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return source diff

Returns:

  • (String)

    if there is a diff

  • (nil)

    otherwise



99
100
101
102
103
104
105
106
107
# File 'lib/unparser/cli/differ.rb', line 99

def diff
  output = +''

  collapsed_hunks.each do |hunk|
    output << hunk.diff(:unified) << "\n"
  end

  output
end

#hunksArray<Diff::LCS::Hunk>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return hunks

Returns:

  • (Array<Diff::LCS::Hunk>)


62
63
64
65
66
67
68
69
# File 'lib/unparser/cli/differ.rb', line 62

def hunks
  file_length_difference = new.length - old.length
  diffs.map do |piece|
    hunk = Diff::LCS::Hunk.new(old, new, piece, CONTEXT_LINES, file_length_difference)
    file_length_difference = hunk.file_length_difference
    hunk
  end
end