Class: Unparser::CLI::Differ
- Inherits:
-
Object
- Object
- Unparser::CLI::Differ
- 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
-
.build(old, new) ⇒ Differ
private
Return new object.
-
.colorize_line(line) ⇒ String
private
Return colorized diff line.
Instance Method Summary collapse
-
#collapsed_hunks ⇒ Enumerable<Diff::LCS::Hunk>
private
Return collapsed hunks.
-
#colorized_diff ⇒ String?
private
Return colorized source diff.
-
#diff ⇒ String?
private
Return source diff.
-
#hunks ⇒ Array<Diff::LCS::Hunk>
private
Return hunks.
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
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
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_hunks ⇒ Enumerable<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
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_diff ⇒ 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 source diff
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 |
#diff ⇒ 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 source diff
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 |
#hunks ⇒ Array<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
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 |