Skip to content

Commit 4e11626

Browse files
committed
Show and return words in the proper case in interactive mode.
1 parent b563279 commit 4e11626

1 file changed

Lines changed: 16 additions & 14 deletions

File tree

codespell.py

Lines changed: 16 additions & 14 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
@@ -358,15 +368,7 @@ def parse_file(filename, colors, summary):
358368
lword = word.lower()
359369
if lword in misspellings:
360370
fix = misspellings[lword].fix
361-
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
371+
fixword = fix_case(word, misspellings[lword].data)
370372

371373
if options.interactive and not lword in fixed_words:
372374
fix, fixword = ask_for_word_fix(lines[i - 1], word,

0 commit comments

Comments
 (0)