53 SpellCheckMixin.DefaultLanguage, |
53 SpellCheckMixin.DefaultLanguage, |
54 SpellCheckMixin.DefaultUserWordList, |
54 SpellCheckMixin.DefaultUserWordList, |
55 SpellCheckMixin.DefaultUserExceptionList |
55 SpellCheckMixin.DefaultUserExceptionList |
56 ) |
56 ) |
57 except DictNotFoundError: |
57 except DictNotFoundError: |
58 # Use English dictionary if no locale dictionary is available |
58 try: |
59 # or the default one could not be found. |
59 # Use English dictionary if no locale dictionary is |
60 spellDict = enchant.DictWithPWL( |
60 # available or the default one could not be found. |
61 "en", |
61 spellDict = enchant.DictWithPWL( |
62 SpellCheckMixin.DefaultUserWordList, |
62 "en", |
63 SpellCheckMixin.DefaultUserExceptionList |
63 SpellCheckMixin.DefaultUserWordList, |
64 ) |
64 SpellCheckMixin.DefaultUserExceptionList |
|
65 ) |
|
66 except DictNotFoundError: |
|
67 # Still no dictionary could be found. Forget about spell |
|
68 # checking. |
|
69 spellDict = None |
|
70 |
65 self.__highlighter.setDict(spellDict) |
71 self.__highlighter.setDict(spellDict) |
66 |
72 |
67 def contextMenuEvent(self, evt): |
73 def contextMenuEvent(self, evt): |
68 """ |
74 """ |
69 Protected method to handle context menu events to add a spelling |
75 Protected method to handle context menu events to add a spelling |
487 Public method to set the spelling dictionary to be used. |
494 Public method to set the spelling dictionary to be used. |
488 |
495 |
489 @param spellDict spelling dictionary |
496 @param spellDict spelling dictionary |
490 @type enchant.Dict |
497 @type enchant.Dict |
491 """ |
498 """ |
492 try: |
499 if spellDict: |
493 self.__tokenizer = enchant.tokenize.get_tokenizer( |
500 try: |
494 spellDict.tag, |
501 self.__tokenizer = enchant.tokenize.get_tokenizer( |
495 chunkers=self.__chunkers, |
502 spellDict.tag, |
496 filters=EnchantHighlighter.TokenFilters) |
503 chunkers=self.__chunkers, |
497 except TokenizerNotFoundError: |
504 filters=EnchantHighlighter.TokenFilters) |
498 # Fall back to the "good for most euro languages" |
505 except TokenizerNotFoundError: |
499 # English tokenizer |
506 # Fall back to the "good for most euro languages" |
500 self.__tokenizer = enchant.tokenize.get_tokenizer( |
507 # English tokenizer |
501 chunkers=self.__chunkers, |
508 self.__tokenizer = enchant.tokenize.get_tokenizer( |
502 filters=EnchantHighlighter.TokenFilters) |
509 chunkers=self.__chunkers, |
|
510 filters=EnchantHighlighter.TokenFilters) |
|
511 else: |
|
512 self.__tokenizer = None |
|
513 |
503 self.__spellDict = spellDict |
514 self.__spellDict = spellDict |
504 |
515 |
505 self.rehighlight() |
516 self.rehighlight() |
506 |
517 |
507 def highlightBlock(self, text): |
518 def highlightBlock(self, text): |
510 |
521 |
511 @param text text to be spell-checked |
522 @param text text to be spell-checked |
512 @type str |
523 @type str |
513 """ |
524 """ |
514 """Overridden QSyntaxHighlighter method to apply the highlight""" |
525 """Overridden QSyntaxHighlighter method to apply the highlight""" |
515 if not self.__spellDict: |
526 if self.__spellDict is None or self.__tokenizer is None: |
516 return |
527 return |
517 |
528 |
518 # Build a list of all misspelled words and highlight them |
529 # Build a list of all misspelled words and highlight them |
519 misspellings = [] |
530 misspellings = [] |
520 for (word, pos) in self.__tokenizer(text): |
531 for (word, pos) in self.__tokenizer(text): |