21
21
from typing import (
22
22
Container ,
23
23
Dict ,
24
+ FrozenSet ,
24
25
Generic ,
25
26
Iterable ,
26
27
Optional ,
108
109
109
110
_builtin_default_as_tuple = tuple (_builtin_default .split ("," ))
110
111
112
+ _inline_ignore_regex = re .compile (r"[^\w\s]\s?codespell:ignore\b(\s+(?P<words>[\w,]*))?" )
113
+
111
114
112
115
class UnknownBuiltinDictionaryError (ValueError ):
113
116
def __init__ (self , name : str ) -> None :
@@ -173,12 +176,21 @@ def __init__(self) -> None:
173
176
self ._misspellings : Dict [str , Misspelling ] = {}
174
177
self .ignore_words_cased : Container [str ] = frozenset ()
175
178
179
+ def _parse_inline_ignore (self , line : str ) -> Optional [FrozenSet [str ]]:
180
+ inline_ignore_match = _inline_ignore_regex .search (line )
181
+ if inline_ignore_match :
182
+ words = frozenset (
183
+ filter (None , (inline_ignore_match .group ("words" ) or "" ).split ("," ))
184
+ )
185
+ return words if words else None
186
+ return frozenset ()
187
+
176
188
def spellcheck_line (
177
189
self ,
178
190
line : str ,
179
191
tokenizer : LineTokenizer [T_co ],
180
192
* ,
181
- extra_words_to_ignore : Container [ str ] = frozenset ()
193
+ respect_inline_ignore : bool = True ,
182
194
) -> Iterable [DetectedMisspelling [T_co ]]:
183
195
"""Tokenize and spellcheck a line
184
196
@@ -187,12 +199,19 @@ def spellcheck_line(
187
199
188
200
:param line: The line to spellcheck.
189
201
:param tokenizer: A callable that will tokenize the line
190
- :param extra_words_to_ignore: Extra words to ignore for this particular line
191
- (such as content from a `codespell:ignore` comment)
202
+ :param respect_inline_ignore: Whether to check the line for
203
+ `codespell:ignore` instructions
204
+ :returns: An iterable of discovered typos.
192
205
"""
193
206
misspellings = self ._misspellings
194
207
ignore_words_cased = self .ignore_words_cased
195
208
209
+ extra_words_to_ignore = (
210
+ self ._parse_inline_ignore (line ) if respect_inline_ignore else frozenset ()
211
+ )
212
+ if extra_words_to_ignore is None :
213
+ return
214
+
196
215
for token in tokenizer (line ):
197
216
word = token .group ()
198
217
if word in ignore_words_cased :
0 commit comments