Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit dbca5b0

Browse files
committed
FIX: need to be able to search replace within lines
(this is needed for very simple diffs and HTML)
1 parent a7d032f commit dbca5b0

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

lib/utils/diff_utils/simple_diff.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ def apply(content, search, replace)
1919
raise ArgumentError, "content cannot be nil" if content.nil?
2020
raise ArgumentError, "search cannot be nil" if search.nil?
2121
raise ArgumentError, "replace cannot be nil" if replace.nil?
22+
raise ArgumentError, "search cannot be empty" if search.empty?
23+
24+
return content.gsub(search, replace) if content.include?(search)
2225

2326
lines = content.split("\n")
2427
search_lines = search.split("\n")

spec/lib/utils/diff_utils/simple_diff_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@
121121
expect(subject.apply(content, search, replace)).to eq(expected.strip)
122122
end
123123

124+
it "handles partial line matches" do
125+
content = "abc hello efg\nabc hello efg"
126+
search = "hello"
127+
replace = "bob"
128+
expect(subject.apply(content, search, replace)).to eq("abc bob efg\nabc bob efg")
129+
end
130+
124131
it "handles JavaScript blocks in different orders" do
125132
content = <<~JS
126133
function first() {

0 commit comments

Comments
 (0)