Skip to content

Commit

Permalink
Merge pull request #1 from tobiasbrunner/fix_case
Browse files Browse the repository at this point in the history
Fix case in interactive mode
  • Loading branch information
lucasdemarchi committed Jul 25, 2011
2 parents b563279 + b22e8d5 commit e134ef6
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,24 @@ def istextfile(filename):

return True

def fix_case(word, fixword):
if word == word.capitalize():
return fixword.capitalize()
elif word == word.upper():
return fixword.upper()
# they are both lower case
# or we don't have any idea
return fixword

def ask_for_word_fix(line, wrongword, misspelling, interactivity):
if interactivity <= 0:
return misspelling.fix, misspelling.data
return misspelling.fix, fix_case(wrongword, misspelling.data)

if misspelling.fix and interactivity & 1:
r = ''
fixword = fix_case(wrongword, misspelling.data)
while not r:
print("%s\t%s ==> %s (Y/n) " % (line, wrongword,
misspelling.data), end='')
print("%s\t%s ==> %s (Y/n) " % (line, wrongword, fixword), end='')
r = sys.stdin.readline().strip().upper()
if not r: r = 'Y'
if r != 'Y' and r != 'N':
Expand All @@ -301,7 +310,8 @@ def ask_for_word_fix(line, wrongword, misspelling, interactivity):
while not r:
print("%s Choose an option (blank for none): " % line, end='')
for i in range(len(opt)):
print(" %d) %s" % (i, opt[i]), end='')
fixword = fix_case(wrongword, opt[i])
print(" %d) %s" % (i, fixword), end='')
print(": ", end='')
sys.stdout.flush()

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

return misspelling.fix, misspelling.data
return misspelling.fix, fix_case(wrongword, misspelling.data)

def parse_file(filename, colors, summary):
lines = None
Expand Down Expand Up @@ -353,36 +363,30 @@ def parse_file(filename, colors, summary):
continue

fixed_words = set()
asked_for = set()

for word in rx.findall(line):
lword = word.lower()
if lword in misspellings:
fix = misspellings[lword].fix
fixword = fix_case(word, misspellings[lword].data)

if word == word.capitalize():
fixword = misspellings[lword].data.capitalize()
elif word == word.upper():
fixword = misspellings[lword].data.upper()
else:
# even they are the same lower case or
# or we don't have any idea
fixword = misspellings[lword].data

if options.interactive and not lword in fixed_words:
if options.interactive and not lword in asked_for:
fix, fixword = ask_for_word_fix(lines[i - 1], word,
misspellings[lword],
options.interactive)
asked_for.add(lword)

if summary and fix:
summary.update(lword)

if lword in fixed_words:
if word in fixed_words:
continue

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

# otherwise warning was explicitly set by interactive mode
Expand Down

0 comments on commit e134ef6

Please sign in to comment.