Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit cface88

Browse files
authored
Merge pull request #318 from atom/project-level-find-replace-regex-bug
Fix project-level find-and-replace regex bug
2 parents 17c5ea4 + 31f5b8c commit cface88

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

spec/text-buffer-spec.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,31 @@ const path = require('path')
22
const TextBuffer = require('../src/text-buffer')
33

44
describe('when a buffer is already open', () => {
5+
const filePath = path.join(__dirname, 'fixtures', 'sample.js')
6+
const buffer = new TextBuffer()
7+
58
it('replaces foo( with bar( using /\bfoo\\(\b/gim', () => {
6-
const filePath = path.join(__dirname, 'fixtures', 'sample.js')
7-
const buffer = new TextBuffer()
89
buffer.setPath(filePath)
910
buffer.setText('foo(x)')
1011
buffer.replace(/\bfoo\(\b/gim, 'bar(')
1112

1213
expect(buffer.getText()).toBe('bar(x)')
1314
})
15+
16+
describe('Texts should be replaced properly with strings containing literals when using the regex option', () => {
17+
it('replaces tstat_fvars()->curr_setpoint[HEAT_EN] with tstat_set_curr_setpoint($1, $2);', () => {
18+
buffer.setPath(filePath)
19+
buffer.setText('tstat_fvars()->curr_setpoint[HEAT_EN] = new_tptr->heat_limit;')
20+
buffer.replace(/tstat_fvars\(\)->curr_setpoint\[(.+?)\] = (.+?);/, 'tstat_set_curr_setpoint($1, $2);')
21+
22+
expect(buffer.getText()).toBe('tstat_set_curr_setpoint(HEAT_EN, new_tptr->heat_limit);')
23+
})
24+
25+
it('replaces atom/flight-manualatomio with $1', () => {
26+
buffer.setText('atom/flight-manualatomio')
27+
buffer.replace(/\.(atom)\./, '$1')
28+
29+
expect(buffer.getText()).toBe('atom/flight-manualatomio')
30+
})
31+
})
1432
})

src/text-buffer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1608,7 +1608,9 @@ class TextBuffer {
16081608
let replacements = 0
16091609

16101610
this.transact(() => {
1611-
return this.scan(regex, function ({replace}) {
1611+
return this.scan(regex, function ({matchText, replace}) {
1612+
const text = matchText.replace(regex, replacementText)
1613+
replacementText = text === matchText ? replacementText : text
16121614
replace(replacementText)
16131615
return replacements++
16141616
})

0 commit comments

Comments
 (0)