4343from ._version import ( # type: ignore[import-not-found]
4444 __version__ as VERSION , # noqa: N812
4545)
46+ from .spellchecker import Misspelling , build_dict
47+ from ._text_util import fix_case
4648
4749word_regex_def = r"[\w\-'’]+" # noqa: RUF001
4850# While we want to treat characters like ( or " as okay for a starting break,
5254 "(\\ b(?:https?|[ts]?ftp|file|git|smb)://[^\\ s]+(?=$|\\ s)|"
5355 "\\ b[\\ w.%+-]+@[\\ w.-]+\\ b)"
5456)
55- # Pass all misspellings through this translation table to generate
56- # alternative misspellings and fixes.
57- alt_chars = (("'" , "’" ),) # noqa: RUF001
5857inline_ignore_regex = re .compile (r"[^\w\s]\s?codespell:ignore\b(\s+(?P<words>[\w,]*))?" )
5958USAGE = """
6059\t %prog [OPTIONS] [file1 file2 ... fileN]
@@ -167,13 +166,6 @@ def match(self, filename: str) -> bool:
167166 return any (fnmatch .fnmatch (filename , p ) for p in self .pattern_list )
168167
169168
170- class Misspelling :
171- def __init__ (self , data : str , fix : bool , reason : str ) -> None :
172- self .data = data
173- self .fix = fix
174- self .reason = reason
175-
176-
177169class TermColors :
178170 def __init__ (self ) -> None :
179171 self .FILE = "\033 [33m"
@@ -703,48 +695,6 @@ def build_ignore_words(
703695 )
704696
705697
706- def add_misspelling (
707- key : str ,
708- data : str ,
709- misspellings : Dict [str , Misspelling ],
710- ) -> None :
711- data = data .strip ()
712-
713- if "," in data :
714- fix = False
715- data , reason = data .rsplit ("," , 1 )
716- reason = reason .lstrip ()
717- else :
718- fix = True
719- reason = ""
720-
721- misspellings [key ] = Misspelling (data , fix , reason )
722-
723-
724- def build_dict (
725- filename : str ,
726- misspellings : Dict [str , Misspelling ],
727- ignore_words : Set [str ],
728- ) -> None :
729- with open (filename , encoding = "utf-8" ) as f :
730- translate_tables = [(x , str .maketrans (x , y )) for x , y in alt_chars ]
731- for line in f :
732- [key , data ] = line .split ("->" )
733- # TODO: For now, convert both to lower.
734- # Someday we can maybe add support for fixing caps.
735- key = key .lower ()
736- data = data .lower ()
737- if key not in ignore_words :
738- add_misspelling (key , data , misspellings )
739- # generate alternative misspellings/fixes
740- for x , table in translate_tables :
741- if x in key :
742- alt_key = key .translate (table )
743- alt_data = data .translate (table )
744- if alt_key not in ignore_words :
745- add_misspelling (alt_key , alt_data , misspellings )
746-
747-
748698def is_hidden (filename : str , check_hidden : bool ) -> bool :
749699 bfilename = os .path .basename (filename )
750700
@@ -759,16 +709,6 @@ def is_text_file(filename: str) -> bool:
759709 return b"\x00 " not in s
760710
761711
762- def fix_case (word : str , fixword : str ) -> str :
763- if word == word .capitalize ():
764- return ", " .join (w .strip ().capitalize () for w in fixword .split ("," ))
765- if word == word .upper ():
766- return fixword .upper ()
767- # they are both lower case
768- # or we don't have any idea
769- return fixword
770-
771-
772712def ask_for_word_fix (
773713 line : str ,
774714 match : Match [str ],
0 commit comments