Class: Insta::Inline::FilePatcher
- Inherits:
-
Object
- Object
- Insta::Inline::FilePatcher
- Defined in:
- lib/insta/inline/file_patcher.rb,
sig/insta/inline/file_patcher.rbs
Class Method Summary collapse
-
.apply_edits(source, edits) ⇒ String
: (String, Array) -> String.
-
.atomic_write(file_path, content) ⇒ void
: (String, String) -> void.
-
.patch(file_path, pending_entries) ⇒ String
: (String, Array) -> String.
Class Method Details
.apply_edits(source, edits) ⇒ String
: (String, Array) -> String
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/insta/inline/file_patcher.rb', line 34 def self.apply_edits(source, edits) sorted = edits.sort_by { |e| -e.start_offset } result = source.dup sorted.each do |edit| before = result.byteslice(0, edit.start_offset) || "" after = result.byteslice(edit.end_offset..) || "" result = before + edit.replacement + after end result end |
.atomic_write(file_path, content) ⇒ void
This method returns an undefined value.
: (String, String) -> void
247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/insta/inline/file_patcher.rb', line 247 def self.atomic_write(file_path, content) directory = File.dirname(file_path) temp = Tempfile.new("insta", directory) temp.write(content) temp.close File.rename(temp.path.to_s, file_path) rescue StandardError temp&.unlink raise end |
.patch(file_path, pending_entries) ⇒ String
: (String, Array) -> String
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/insta/inline/file_patcher.rb', line 10 def self.patch(file_path, pending_entries) source = File.read(file_path) result = Prism.parse(source) edits = [] #: Array[Edit] pending_entries.each do |entry| line = entry[:line] content = entry[:content] type = entry[:type] finder = CallFinder.new(line) finder.visit(result.value) call = finder.found_call next unless call edit = build_edit(source, call, content, type) edits << edit if edit end apply_edits(source, edits) end |