Class: Gitlab::Diff::Line
- Inherits:
-
Object
- Object
- Gitlab::Diff::Line
- Defined in:
- lib/gitlab/diff/line.rb
Constant Summary collapse
- SERIALIZE_KEYS =
When SERIALIZE_KEYS is updated, to reset the redis cache entries you’ll
need to bump the VERSION constant on Gitlab::Diff::HighlightCache
%i[line_code rich_text text type index old_pos new_pos].freeze
Instance Attribute Summary collapse
-
#embedded_image ⇒ Object
Returns the value of attribute embedded_image.
-
#index ⇒ Object
Returns the value of attribute index.
-
#line_code ⇒ Object
Returns the value of attribute line_code.
-
#marker_ranges ⇒ Object
readonly
Returns the value of attribute marker_ranges.
-
#new_pos ⇒ Object
Returns the value of attribute new_pos.
-
#old_pos ⇒ Object
Returns the value of attribute old_pos.
- #rich_text ⇒ Object
- #text(prefix: true) ⇒ Object
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #added? ⇒ Boolean
-
#as_json(opts = nil) ⇒ Object
We have to keep this here since it is still used for conflict resolution Conflict::File#as_json renders json diff lines in sections.
- #discussable? ⇒ Boolean
- #has_mapping_in_raw? ⇒ Boolean
-
#initialize(text, type, index, old_pos, new_pos, parent_file: nil, line_code: nil, rich_text: nil) ⇒ Line
constructor
A new instance of Line.
- #line ⇒ Object
- #match? ⇒ Boolean
- #meta? ⇒ Boolean
- #meta_positions ⇒ Object
- #new_line ⇒ Object
- #old_line ⇒ Object
- #removed? ⇒ Boolean
- #set_marker_ranges(marker_ranges) ⇒ Object
- #suggestible? ⇒ Boolean
- #to_hash ⇒ Object
- #unchanged? ⇒ Boolean
Constructor Details
#initialize(text, type, index, old_pos, new_pos, parent_file: nil, line_code: nil, rich_text: nil) ⇒ Line
Returns a new instance of Line.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/gitlab/diff/line.rb', line 15 def initialize(text, type, index, old_pos, new_pos, parent_file: nil, line_code: nil, rich_text: nil) @text = text @type = type @index = index @old_pos = old_pos @new_pos = new_pos @parent_file = parent_file @rich_text = rich_text # When line code is not provided from cache store we build it # using the parent_file(Diff::File or Conflict::File). @line_code = line_code || calculate_line_code @marker_ranges = [] end |
Instance Attribute Details
#embedded_image ⇒ Object
Returns the value of attribute embedded_image.
13 14 15 |
# File 'lib/gitlab/diff/line.rb', line 13 def @embedded_image end |
#index ⇒ Object
Returns the value of attribute index.
13 14 15 |
# File 'lib/gitlab/diff/line.rb', line 13 def index @index end |
#line_code ⇒ Object
Returns the value of attribute line_code.
13 14 15 |
# File 'lib/gitlab/diff/line.rb', line 13 def line_code @line_code end |
#marker_ranges ⇒ Object (readonly)
Returns the value of attribute marker_ranges.
11 12 13 |
# File 'lib/gitlab/diff/line.rb', line 11 def marker_ranges @marker_ranges end |
#new_pos ⇒ Object
Returns the value of attribute new_pos.
13 14 15 |
# File 'lib/gitlab/diff/line.rb', line 13 def new_pos @new_pos end |
#old_pos ⇒ Object
Returns the value of attribute old_pos.
13 14 15 |
# File 'lib/gitlab/diff/line.rb', line 13 def old_pos @old_pos end |
#rich_text ⇒ Object
110 111 112 113 114 |
# File 'lib/gitlab/diff/line.rb', line 110 def rich_text @parent_file.try(:highlight_lines!) if @parent_file && !@rich_text @rich_text end |
#text(prefix: true) ⇒ Object
59 60 61 62 63 |
# File 'lib/gitlab/diff/line.rb', line 59 def text(prefix: true) return @text if prefix @text&.slice(1..).to_s end |
#type ⇒ Object
Returns the value of attribute type.
13 14 15 |
# File 'lib/gitlab/diff/line.rb', line 13 def type @type end |
Class Method Details
.init_from_hash(hash) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/gitlab/diff/line.rb', line 30 def self.init_from_hash(hash) new(hash[:text], hash[:type], hash[:index], hash[:old_pos], hash[:new_pos], parent_file: hash[:parent_file], line_code: hash[:line_code], rich_text: hash[:rich_text]) end |
.safe_init_from_hash(hash) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/gitlab/diff/line.rb', line 41 def self.safe_init_from_hash(hash) line = hash.with_indifferent_access rich_text = line[:rich_text] line[:rich_text] = rich_text&.html_safe init_from_hash(line) end |
Instance Method Details
#added? ⇒ Boolean
81 82 83 |
# File 'lib/gitlab/diff/line.rb', line 81 def added? %w[new new-nonewline new-nomappinginraw].include?(type) end |
#as_json(opts = nil) ⇒ Object
We have to keep this here since it is still used for conflict resolution Conflict::File#as_json renders json diff lines in sections
127 128 129 |
# File 'lib/gitlab/diff/line.rb', line 127 def as_json(opts = nil) DiffLineSerializer.new.represent(self) end |
#discussable? ⇒ Boolean
102 103 104 |
# File 'lib/gitlab/diff/line.rb', line 102 def discussable? has_mapping_in_raw? && ! end |
#has_mapping_in_raw? ⇒ Boolean
93 94 95 96 |
# File 'lib/gitlab/diff/line.rb', line 93 def has_mapping_in_raw? # Used for rendered diff, when the displayed line doesn't have a matching line in the raw diff !type&.ends_with?('nomappinginraw') end |
#line ⇒ Object
73 74 75 |
# File 'lib/gitlab/diff/line.rb', line 73 def line new_line || old_line end |
#match? ⇒ Boolean
98 99 100 |
# File 'lib/gitlab/diff/line.rb', line 98 def match? type == :match end |
#meta? ⇒ Boolean
89 90 91 |
# File 'lib/gitlab/diff/line.rb', line 89 def %w[match new-nonewline old-nonewline].include?(type) end |
#meta_positions ⇒ Object
116 117 118 119 120 121 122 123 |
# File 'lib/gitlab/diff/line.rb', line 116 def return unless { old_pos: old_pos, new_pos: new_pos } end |
#new_line ⇒ Object
69 70 71 |
# File 'lib/gitlab/diff/line.rb', line 69 def new_line new_pos unless removed? || end |
#old_line ⇒ Object
65 66 67 |
# File 'lib/gitlab/diff/line.rb', line 65 def old_line old_pos unless added? || end |
#removed? ⇒ Boolean
85 86 87 |
# File 'lib/gitlab/diff/line.rb', line 85 def removed? %w[old old-nonewline old-nomappinginraw].include?(type) end |
#set_marker_ranges(marker_ranges) ⇒ Object
55 56 57 |
# File 'lib/gitlab/diff/line.rb', line 55 def set_marker_ranges(marker_ranges) @marker_ranges = marker_ranges end |
#suggestible? ⇒ Boolean
106 107 108 |
# File 'lib/gitlab/diff/line.rb', line 106 def suggestible? !removed? end |
#to_hash ⇒ Object
49 50 51 52 53 |
# File 'lib/gitlab/diff/line.rb', line 49 def to_hash hash = {} SERIALIZE_KEYS.each { |key| hash[key] = send(key) } # rubocop:disable GitlabSecurity/PublicSend hash end |
#unchanged? ⇒ Boolean
77 78 79 |
# File 'lib/gitlab/diff/line.rb', line 77 def unchanged? type.nil? end |