Skip to content

Commit e134ef6

Browse files
committed
Merge pull request #1 from tobiasbrunner/fix_case
Fix case in interactive mode
2 parents b563279 + b22e8d5 commit e134ef6

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

codespell.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,24 @@ def istextfile(filename):
273273

274274
return True
275275

276+
def fix_case(word, fixword):
277+
if word == word.capitalize():
278+
return fixword.capitalize()
279+
elif word == word.upper():
280+
return fixword.upper()
281+
# they are both lower case
282+
# or we don't have any idea
283+
return fixword
284+
276285
def ask_for_word_fix(line, wrongword, misspelling, interactivity):
277286
if interactivity <= 0:
278-
return misspelling.fix, misspelling.data
287+
return misspelling.fix, fix_case(wrongword, misspelling.data)
279288

280289
if misspelling.fix and interactivity & 1:
281290
r = ''
291+
fixword = fix_case(wrongword, misspelling.data)
282292
while not r:
283-
print("%s\t%s ==> %s (Y/n) " % (line, wrongword,
284-
misspelling.data), end='')
293+
print("%s\t%s ==> %s (Y/n) " % (line, wrongword, fixword), end='')
285294
r = sys.stdin.readline().strip().upper()
286295
if not r: r = 'Y'
287296
if r != 'Y' and r != 'N':
@@ -301,7 +310,8 @@ def ask_for_word_fix(line, wrongword, misspelling, interactivity):
301310
while not r:
302311
print("%s Choose an option (blank for none): " % line, end='')
303312
for i in range(len(opt)):
304-
print(" %d) %s" % (i, opt[i]), end='')
313+
fixword = fix_case(wrongword, opt[i])
314+
print(" %d) %s" % (i, fixword), end='')
305315
print(": ", end='')
306316
sys.stdout.flush()
307317

@@ -319,7 +329,7 @@ def ask_for_word_fix(line, wrongword, misspelling, interactivity):
319329
misspelling.fix = True
320330
misspelling.data = r
321331

322-
return misspelling.fix, misspelling.data
332+
return misspelling.fix, fix_case(wrongword, misspelling.data)
323333

324334
def parse_file(filename, colors, summary):
325335
lines = None
@@ -353,36 +363,30 @@ def parse_file(filename, colors, summary):
353363
continue
354364

355365
fixed_words = set()
366+
asked_for = set()
356367

357368
for word in rx.findall(line):
358369
lword = word.lower()
359370
if lword in misspellings:
360371
fix = misspellings[lword].fix
372+
fixword = fix_case(word, misspellings[lword].data)
361373

362-
if word == word.capitalize():
363-
fixword = misspellings[lword].data.capitalize()
364-
elif word == word.upper():
365-
fixword = misspellings[lword].data.upper()
366-
else:
367-
# even they are the same lower case or
368-
# or we don't have any idea
369-
fixword = misspellings[lword].data
370-
371-
if options.interactive and not lword in fixed_words:
374+
if options.interactive and not lword in asked_for:
372375
fix, fixword = ask_for_word_fix(lines[i - 1], word,
373376
misspellings[lword],
374377
options.interactive)
378+
asked_for.add(lword)
375379

376380
if summary and fix:
377381
summary.update(lword)
378382

379-
if lword in fixed_words:
383+
if word in fixed_words:
380384
continue
381385

382386
if options.write_changes and fix:
383387
changed = True
384388
lines[i - 1] = re.sub(r'\b%s\b' % word, fixword, lines[i - 1])
385-
fixed_words.add(lword)
389+
fixed_words.add(word)
386390
continue
387391

388392
# otherwise warning was explicitly set by interactive mode

0 commit comments

Comments
 (0)