Skip to content

Commit 7efcead

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 were not updated according 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 20d61df commit 7efcead

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

codespell.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,13 @@ def parse_file(filename, colors):
164164

165165
i = 1
166166
for line in lines:
167+
fixed_words = set()
167168
for word in re.findall('\w+', line):
168169
lword = word.lower()
170+
171+
if lword in fixed_words:
172+
continue
173+
169174
if lword in misspellings:
170175
if word == word.capitalize():
171176
fixword = misspellings[lword].data.capitalize()
@@ -178,7 +183,8 @@ def parse_file(filename, colors):
178183

179184
if options.write_changes and misspellings[lword].fix:
180185
changed = True
181-
lines[i - 1] = lines[i - 1].replace(word, fixword, 1)
186+
lines[i - 1] = re.sub(r'\b%s\b' % word, fixword, lines[i - 1])
187+
fixed_words.add(lword)
182188
continue
183189

184190
cfilename = "%s%s%s" % (colors.FILE, filename, colors.DISABLE)

0 commit comments

Comments
 (0)