Class: Gitlab::Diff::Position
- Inherits:
-
Object
- Object
- Gitlab::Diff::Position
- Defined in:
- lib/gitlab/diff/position.rb
Instance Attribute Summary collapse
-
#formatter ⇒ Object
Returns the value of attribute formatter.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #added? ⇒ Boolean
- #as_json(opts = nil) ⇒ Object
- #complete? ⇒ Boolean
- #diff_file(repository) ⇒ Object
- #diff_line(repository) ⇒ Object
- #diff_options ⇒ Object
- #diff_refs ⇒ Object
- #encode_with(coder) ⇒ Object
- #file_hash ⇒ Object
- #file_path ⇒ Object
- #find_diff_file_from(diffable) ⇒ Object
-
#init_with(coder) ⇒ Object
‘Gitlab::Diff::Position` objects are stored as serialized attributes in `DiffNote`, which use YAML to encode and decode objects.
-
#initialize(attrs = {}) ⇒ Position
constructor
A position can belong to a text line or to an image coordinate it depends of the position_type argument.
- #inspect ⇒ Object
- #key ⇒ Object
- #line_code(repository) ⇒ Object
- #multiline? ⇒ Boolean
- #on_file? ⇒ Boolean
- #on_image? ⇒ Boolean
- #on_text? ⇒ Boolean
- #paths ⇒ Object
- #removed? ⇒ Boolean
- #to_h ⇒ Object
- #to_json(opts = nil) ⇒ Object
- #type ⇒ Object
- #unchanged? ⇒ Boolean
- #unfoldable? ⇒ Boolean
- #unfolded_diff?(repository) ⇒ Boolean
Constructor Details
#initialize(attrs = {}) ⇒ Position
A position can belong to a text line or to an image coordinate it depends of the position_type argument. Text position will have: new_line and old_line Image position will have: width, height, x, y
29 30 31 |
# File 'lib/gitlab/diff/position.rb', line 29 def initialize(attrs = {}) @formatter = get_formatter_class(attrs[:position_type]).new(attrs) end |
Instance Attribute Details
#formatter ⇒ Object
Returns the value of attribute formatter.
8 9 10 |
# File 'lib/gitlab/diff/position.rb', line 8 def formatter @formatter end |
Instance Method Details
#==(other) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/gitlab/diff/position.rb', line 52 def ==(other) other.is_a?(self.class) && other.diff_refs == diff_refs && other.old_path == old_path && other.new_path == new_path && other.formatter == formatter end |
#added? ⇒ Boolean
92 93 94 |
# File 'lib/gitlab/diff/position.rb', line 92 def added? type == 'new' end |
#as_json(opts = nil) ⇒ Object
76 77 78 |
# File 'lib/gitlab/diff/position.rb', line 76 def as_json(opts = nil) to_h.except(:ignore_whitespace_change).as_json(opts) end |
#complete? ⇒ Boolean
68 69 70 |
# File 'lib/gitlab/diff/position.rb', line 68 def complete? file_path.present? && formatter.complete? && diff_refs.complete? end |
#diff_file(repository) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/gitlab/diff/position.rb', line 116 def diff_file(repository) return @diff_file if defined?(@diff_file) @diff_file = begin key = { project_id: repository.project.id, start_sha: start_sha, head_sha: head_sha, path: file_path } # Takes action when creating diff notes (multiple calls are # submitted to this method). Gitlab::SafeRequestStore.fetch(key) { find_diff_file(repository) } end # We need to unfold diff lines according to the position in order # to correctly calculate the line code and trace position changes. @diff_file&.tap { |file| file.unfold_diff_lines(self) } end |
#diff_line(repository) ⇒ Object
141 142 143 |
# File 'lib/gitlab/diff/position.rb', line 141 def diff_line(repository) @diff_line ||= diff_file(repository)&.line_for_position(self) end |
#diff_options ⇒ Object
137 138 139 |
# File 'lib/gitlab/diff/position.rb', line 137 def { paths: paths, expanded: true, include_stats: false, ignore_whitespace_change: ignore_whitespace_change } end |
#diff_refs ⇒ Object
108 109 110 |
# File 'lib/gitlab/diff/position.rb', line 108 def diff_refs @diff_refs ||= DiffRefs.new(base_sha: base_sha, start_sha: start_sha, head_sha: head_sha) end |
#encode_with(coder) ⇒ Object
44 45 46 |
# File 'lib/gitlab/diff/position.rb', line 44 def encode_with(coder) coder['attributes'] = formatter.to_h end |
#file_hash ⇒ Object
149 150 151 |
# File 'lib/gitlab/diff/position.rb', line 149 def file_hash @file_hash ||= Digest::SHA1.hexdigest(file_path) end |
#file_path ⇒ Object
104 105 106 |
# File 'lib/gitlab/diff/position.rb', line 104 def file_path new_path.presence || old_path end |
#find_diff_file_from(diffable) ⇒ Object
165 166 167 168 169 |
# File 'lib/gitlab/diff/position.rb', line 165 def find_diff_file_from(diffable) diff_files = diffable.diffs().diff_files diff_files.first end |
#init_with(coder) ⇒ Object
‘Gitlab::Diff::Position` objects are stored as serialized attributes in `DiffNote`, which use YAML to encode and decode objects. `#init_with` and `#encode_with` can be used to customize the en/decoding behavior. In this case, we override these to prevent memoized instance variables like `@diff_file` and `@diff_line` from being serialized.
38 39 40 41 42 |
# File 'lib/gitlab/diff/position.rb', line 38 def init_with(coder) initialize(coder['attributes']) self end |
#inspect ⇒ Object
64 65 66 |
# File 'lib/gitlab/diff/position.rb', line 64 def inspect %(#<#{self.class}:#{object_id} #{to_h}>) end |
#key ⇒ Object
48 49 50 |
# File 'lib/gitlab/diff/position.rb', line 48 def key formatter.key end |
#line_code(repository) ⇒ Object
145 146 147 |
# File 'lib/gitlab/diff/position.rb', line 145 def line_code(repository) @line_code ||= diff_file(repository)&.line_code_for_position(self) end |
#multiline? ⇒ Boolean
171 172 173 174 175 |
# File 'lib/gitlab/diff/position.rb', line 171 def multiline? return unless on_text? && line_range line_range['start'] != line_range['end'] end |
#on_file? ⇒ Boolean
153 154 155 |
# File 'lib/gitlab/diff/position.rb', line 153 def on_file? position_type == 'file' end |
#on_image? ⇒ Boolean
157 158 159 |
# File 'lib/gitlab/diff/position.rb', line 157 def on_image? position_type == 'image' end |
#on_text? ⇒ Boolean
161 162 163 |
# File 'lib/gitlab/diff/position.rb', line 161 def on_text? position_type == 'text' end |
#paths ⇒ Object
100 101 102 |
# File 'lib/gitlab/diff/position.rb', line 100 def paths [old_path, new_path].compact.uniq end |
#removed? ⇒ Boolean
96 97 98 |
# File 'lib/gitlab/diff/position.rb', line 96 def removed? type == 'old' end |
#to_h ⇒ Object
60 61 62 |
# File 'lib/gitlab/diff/position.rb', line 60 def to_h formatter.to_h end |
#to_json(opts = nil) ⇒ Object
72 73 74 |
# File 'lib/gitlab/diff/position.rb', line 72 def to_json(opts = nil) Gitlab::Json.generate(to_h.except(:ignore_whitespace_change), opts) end |
#type ⇒ Object
80 81 82 |
# File 'lib/gitlab/diff/position.rb', line 80 def type formatter.line_age end |
#unchanged? ⇒ Boolean
88 89 90 |
# File 'lib/gitlab/diff/position.rb', line 88 def unchanged? type.nil? end |
#unfoldable? ⇒ Boolean
84 85 86 |
# File 'lib/gitlab/diff/position.rb', line 84 def unfoldable? on_text? && unchanged? end |
#unfolded_diff?(repository) ⇒ Boolean
112 113 114 |
# File 'lib/gitlab/diff/position.rb', line 112 def unfolded_diff?(repository) diff_file(repository)&.unfolded? end |