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

Commit 43c56d7

Browse files
authored
FIX: need to be able to search replace within lines (#1110)
(this is needed for very simple diffs and HTML)
1 parent a7d032f commit 43c56d7

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
lin 1
2626
TEXT
2727

28-
expect(subject.apply(content, search, replace)).to eq(expected.strip)
28+
expect(subject.apply(content, search, replace).strip).to eq(expected.strip)
2929
end
3030

3131
it "raises error when no match is found" do
@@ -61,10 +61,10 @@
6161
end
6262

6363
it "is forgiving of whitespace differences" do
64-
content = "line1\n line2\nline3"
64+
content = "line1\n line2\nline3"
6565
search = "line2"
6666
replace = "new_line2"
67-
expect(subject.apply(content, search, replace)).to eq("line1\nnew_line2\nline3")
67+
expect(subject.apply(content, search, replace).strip).to eq("line1\n new_line2\nline3")
6868
end
6969

7070
it "is forgiving of small character differences" do
@@ -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)