MCPcopy Create free account
hub / github.com/codespell-project/codespell / ask_for_word_fix

Function ask_for_word_fix

codespell_lib/_codespell.py:792–851  ·  view source on GitHub ↗
(
    line: str,
    match: Match[str],
    misspelling: Misspelling,
    interactivity: int,
    colors: TermColors,
)

Source from the content-addressed store, hash-verified

790
791
792def ask_for_word_fix(
793 line: str,
794 match: Match[str],
795 misspelling: Misspelling,
796 interactivity: int,
797 colors: TermColors,
798) -> tuple[bool, str]:
799 wrongword = match.group()
800 if interactivity <= 0:
801 return misspelling.fix, fix_case(wrongword, misspelling.data)
802
803 line_ui = (
804 f"{line[: match.start()]}"
805 f"{colors.WWORD}{wrongword}{colors.DISABLE}"
806 f"{line[match.end() :]}"
807 )
808
809 if misspelling.fix and interactivity & 1:
810 r = ""
811 fixword = fix_case(wrongword, misspelling.data)
812 while not r:
813 print(f"{line_ui}\t{wrongword} ==> {fixword} (Y/n) ", end="", flush=True)
814 r = sys.stdin.readline().strip().upper()
815 if not r:
816 r = "Y"
817 if r not in ("Y", "N"):
818 print("Say 'y' or 'n'")
819 r = ""
820
821 if r == "N":
822 misspelling.fix = False
823
824 elif (interactivity & 2) and not misspelling.reason:
825 # if it is not disabled, i.e. it just has more than one possible fix,
826 # we ask the user which word to use
827
828 r = ""
829 opt = [w.strip() for w in misspelling.data.split(",")]
830 while not r:
831 print(f"{line_ui} Choose an option (blank for none): ", end="")
832 for i, o in enumerate(opt):
833 fixword = fix_case(wrongword, o)
834 print(f" {i}) {fixword}", end="")
835 print(": ", end="", flush=True)
836
837 n = sys.stdin.readline().strip()
838 if not n:
839 break
840
841 try:
842 i = int(n)
843 r = opt[i]
844 except (ValueError, IndexError):
845 print("Not a valid option\n")
846
847 if r:
848 misspelling.fix = True
849 misspelling.data = r

Callers 1

parse_linesFunction · 0.85

Calls 1

fix_caseFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…