Skip to content

Commit 3ef81cc

Browse files
author
Lucas De Marchi
committed
Fix replacement of same word in one line
When one line had the same mispelled word, codespell was incorrectly fixing that line, even introducing new typos. This was because the list of misspelled words is not updated according to the fixes. Instead of always updating this list and making the loop more difficult, we do as following: - Cache the words that are fixed in a certain line - Fix all cases of a misspelled in each line (this means that interactive mode will fix all cases with the same suggestions... not awesome, but simplifies a lot the code) - Use a regex with re.sub() instead of the naive string.replace() function. This eliminates dumb cases of matching partial words and modifying them. Eg.: addres->address would modify addressable to addresssable. - Skip words that were already fixed by previous iteration. Thanks to Bruce Cran <bruce@cran.org.uk> for reporting this issue.
1 parent 749cff7 commit 3ef81cc

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

codespell.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ def parse_file(filename, colors, summary):
287287
i += 1
288288
continue
289289

290+
fixed_words = set()
291+
290292
for word in rx.findall(line):
291293
lword = word.lower()
292294
if lword in misspellings:
@@ -301,17 +303,21 @@ def parse_file(filename, colors, summary):
301303
# or we don't have any idea
302304
fixword = misspellings[lword].data
303305

304-
if options.interactive:
306+
if options.interactive and not lword in fixed_words:
305307
fix, fixword = ask_for_word_fix(lines[i - 1], word,
306308
misspellings[lword],
307309
options.interactive)
308310

309311
if summary and fix:
310312
summary.update(lword)
311313

314+
if lword in fixed_words:
315+
continue
316+
312317
if options.write_changes and fix:
313318
changed = True
314-
lines[i - 1] = lines[i - 1].replace(word, fixword, 1)
319+
lines[i - 1] = re.sub(r'\b%s\b' % word, fixword, lines[i - 1])
320+
fixed_words.add(lword)
315321
continue
316322

317323
# otherwise warning was explicitly set by interactive mode

0 commit comments

Comments
 (0)