eric6/QScintilla/SpellChecker.py

branch
maintenance
changeset 8273
698ae46f40a4
parent 8043
0acf98cd089a
parent 8243
cc717c2ae956
equal deleted inserted replaced
8190:fb0ef164f536 8273:698ae46f40a4
8 8
9 The spell checker is based on pyenchant. 9 The spell checker is based on pyenchant.
10 """ 10 """
11 11
12 import os 12 import os
13 import contextlib
13 14
14 from PyQt5.QtCore import QTimer, QObject 15 from PyQt5.QtCore import QTimer, QObject
15 16
16 import Preferences 17 import Preferences
17 import Utilities 18 import Utilities
18 19
19 try: 20 with contextlib.suppress(ImportError, AttributeError, OSError):
20 import enchant 21 import enchant
21 except (ImportError, AttributeError, OSError):
22 pass
23 22
24 23
25 class SpellChecker(QObject): 24 class SpellChecker(QObject):
26 """ 25 """
27 Class implementing a pyenchant based spell checker. 26 Class implementing a pyenchant based spell checker.
41 (string). The string should be in language locale format 40 (string). The string should be in language locale format
42 (e.g. en_US, de). 41 (e.g. en_US, de).
43 @param checkRegion reference to a function to check for a valid 42 @param checkRegion reference to a function to check for a valid
44 region 43 region
45 """ 44 """
46 super(SpellChecker, self).__init__(editor) 45 super().__init__(editor)
47 46
48 self.editor = editor 47 self.editor = editor
49 self.indicator = indicator 48 self.indicator = indicator
50 if defaultLanguage is not None: 49 if defaultLanguage is not None:
51 self.setDefaultLanguage(defaultLanguage) 50 self.setDefaultLanguage(defaultLanguage)
64 """ 63 """
65 Class method to get all available languages. 64 Class method to get all available languages.
66 65
67 @return list of available languages (list of strings) 66 @return list of available languages (list of strings)
68 """ 67 """
69 try: 68 with contextlib.suppress(NameError):
70 return enchant.list_languages() 69 return enchant.list_languages()
71 except NameError:
72 pass
73 return [] 70 return []
74 71
75 @classmethod 72 @classmethod
76 def isAvailable(cls): 73 def isAvailable(cls):
77 """ 74 """
78 Class method to check, if spellchecking is available. 75 Class method to check, if spellchecking is available.
79 76
80 @return flag indicating availability (boolean) 77 @return flag indicating availability (boolean)
81 """ 78 """
82 if Preferences.getEditor("SpellCheckingEnabled"): 79 if Preferences.getEditor("SpellCheckingEnabled"):
83 try: 80 with contextlib.suppress(NameError, AttributeError):
84 return len(enchant.list_languages()) > 0 81 return len(enchant.list_languages()) > 0
85 except (NameError, AttributeError):
86 pass
87 return False 82 return False
88 83
89 @classmethod 84 @classmethod
90 def getDefaultPath(cls, isException=False): 85 def getDefaultPath(cls, isException=False):
91 """ 86 """
431 @return list of suggestions (list of strings) 426 @return list of suggestions (list of strings)
432 """ 427 """
433 suggestions = [] 428 suggestions = []
434 spell = self._spelling_dict 429 spell = self._spelling_dict
435 if spell and len(word) >= self.minimumWordSize: 430 if spell and len(word) >= self.minimumWordSize:
436 try: 431 with contextlib.suppress(enchant.errors.Error):
437 suggestions = spell.suggest(word) 432 suggestions = spell.suggest(word)
438 except enchant.errors.Error:
439 # ignore these
440 pass
441 return suggestions 433 return suggestions
442 434
443 def add(self, word=None): 435 def add(self, word=None):
444 """ 436 """
445 Public method to add a word to the personal word list. 437 Public method to add a word to the personal word list.
527 self.pos = wordEnd 519 self.pos = wordEnd
528 if ( 520 if (
529 (wordEnd - wordStart) >= self.minimumWordSize and 521 (wordEnd - wordStart) >= self.minimumWordSize and
530 self.__checkRegion(wordStart) 522 self.__checkRegion(wordStart)
531 ): 523 ):
532 try: 524 with contextlib.suppress(enchant.errors.Error):
533 if spell.check(word): 525 if spell.check(word):
534 continue 526 continue
535 except enchant.errors.Error:
536 # ignore these
537 pass
538 if word in self.__ignoreWords: 527 if word in self.__ignoreWords:
539 continue 528 continue
540 self.word = word 529 self.word = word
541 self.wordStart = wordStart 530 self.wordStart = wordStart
542 self.wordEnd = wordEnd 531 self.wordEnd = wordEnd

eric ide

mercurial