Class: RuboCop::UnifiedDiff Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/unified_diff.rb

Overview

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

Renders a unified diff between two versions of a file's source.

RuboCop has no diffing dependency and this output is meant to be read by people (and occasionally piped into git apply), so the goal is a diff that is correct and readable, not one that matches diff -u byte for byte.

Constant Summary collapse

CONTEXT_LINES =

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

3
NO_NEWLINE_MARKER =

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

"\\ No newline at end of file\n"
MAX_EDIT_DISTANCE =

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

Finding a minimal diff costs time and memory proportional to the square of the number of edits. Past this many edits the file was rewritten rather than corrected, so the changed region is reported as a single replacement instead.

400

Instance Method Summary collapse

Constructor Details

#initialize(path, old_source, new_source) ⇒ UnifiedDiff

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.

Returns a new instance of UnifiedDiff.



21
22
23
24
25
# File 'lib/rubocop/unified_diff.rb', line 21

def initialize(path, old_source, new_source)
  @path = path
  @old_lines = old_source.lines
  @new_lines = new_source.lines
end

Instance Method Details

#to_sString

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.

Returns the diff, or an empty string when the sources are identical.

Returns:

  • (String)

    the diff, or an empty string when the sources are identical



28
29
30
31
32
33
34
35
# File 'lib/rubocop/unified_diff.rb', line 28

def to_s
  return '' if @old_lines == @new_lines

  hunks = hunks_for(edit_script)
  return '' if hunks.empty?

  ["--- a/#{@path}\n", "+++ b/#{@path}\n", *hunks].join
end