16
16
Copyright (C) 2011 ProFUSION embedded systems
17
17
"""
18
18
19
- import re
20
19
import os
20
+ import re
21
21
from typing import (
22
- Dict ,
23
- Sequence ,
24
22
Container ,
25
- Optional ,
23
+ Dict ,
24
+ FrozenSet ,
25
+ Generic ,
26
26
Iterable ,
27
+ Optional ,
27
28
Protocol ,
28
- Generic ,
29
+ Sequence ,
29
30
TypeVar ,
30
31
)
31
32
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 :
@@ -206,12 +209,21 @@ def __init__(
206
209
if builtin_dictionaries :
207
210
self .load_builtin_dictionaries (builtin_dictionaries )
208
211
212
+ def _parse_inline_ignore (self , line : str ) -> Optional [FrozenSet [str ]]:
213
+ inline_ignore_match = _inline_ignore_regex .search (line )
214
+ if inline_ignore_match :
215
+ words = frozenset (
216
+ filter (None , (inline_ignore_match .group ("words" ) or "" ).split ("," ))
217
+ )
218
+ return words if words else None
219
+ return frozenset ()
220
+
209
221
def spellcheck_line (
210
222
self ,
211
223
line : str ,
212
224
tokenizer : LineTokenizer [T_co ],
213
225
* ,
214
- extra_words_to_ignore : Container [ str ] = frozenset ()
226
+ respect_inline_ignore : bool = True ,
215
227
) -> Iterable [DetectedMisspelling [T_co ]]:
216
228
"""Tokenize and spellcheck a line
217
229
@@ -220,12 +232,19 @@ def spellcheck_line(
220
232
221
233
:param line: The line to spellcheck.
222
234
:param tokenizer: A callable that will tokenize the line
223
- :param extra_words_to_ignore: Extra words to ignore for this particular line
224
- (such as content from a `codespell:ignore` comment)
235
+ :param respect_inline_ignore: Whether to check the line for
236
+ `codespell:ignore` instructions
237
+ :returns: An iterable of discovered typos.
225
238
"""
226
239
misspellings = self ._misspellings
227
240
ignore_words_cased = self .ignore_words_cased
228
241
242
+ extra_words_to_ignore = (
243
+ self ._parse_inline_ignore (line ) if respect_inline_ignore else frozenset ()
244
+ )
245
+ if extra_words_to_ignore is None :
246
+ return
247
+
229
248
for token in tokenizer (line ):
230
249
word = token .group ()
231
250
if word in ignore_words_cased :
0 commit comments