31 |
31 |
32 def __init__(self, editor, indicator, defaultLanguage=None, checkRegion=None): |
32 def __init__(self, editor, indicator, defaultLanguage=None, checkRegion=None): |
33 """ |
33 """ |
34 Constructor |
34 Constructor |
35 |
35 |
36 @param editor reference to the editor object (QScintilla.Editor) |
36 @param editor reference to the editor object |
|
37 @type QScintilla.Editor |
37 @param indicator spell checking indicator |
38 @param indicator spell checking indicator |
38 @param defaultLanguage the language to be used as the default |
39 @type int |
39 (string). The string should be in language locale format |
40 @param defaultLanguage the language to be used as the default. The string |
40 (e.g. en_US, de). |
41 should be in language locale format (e.g. en_US, de). |
|
42 @type str |
41 @param checkRegion reference to a function to check for a valid |
43 @param checkRegion reference to a function to check for a valid |
42 region |
44 region |
|
45 @type function |
43 """ |
46 """ |
44 super().__init__(editor) |
47 super().__init__(editor) |
45 |
48 |
46 self.editor = editor |
49 self.editor = editor |
47 self.indicator = indicator |
50 self.indicator = indicator |
60 @classmethod |
63 @classmethod |
61 def getAvailableLanguages(cls): |
64 def getAvailableLanguages(cls): |
62 """ |
65 """ |
63 Class method to get all available languages. |
66 Class method to get all available languages. |
64 |
67 |
65 @return list of available languages (list of strings) |
68 @return list of available languages |
|
69 @rtype list of str |
66 """ |
70 """ |
67 with contextlib.suppress(NameError): |
71 with contextlib.suppress(NameError): |
68 return enchant.list_languages() |
72 return enchant.list_languages() |
69 return [] |
73 return [] |
70 |
74 |
71 @classmethod |
75 @classmethod |
72 def isAvailable(cls): |
76 def isAvailable(cls): |
73 """ |
77 """ |
74 Class method to check, if spellchecking is available. |
78 Class method to check, if spellchecking is available. |
75 |
79 |
76 @return flag indicating availability (boolean) |
80 @return flag indicating availability |
|
81 @rtype bool |
77 """ |
82 """ |
78 if Preferences.getEditor("SpellCheckingEnabled"): |
83 if Preferences.getEditor("SpellCheckingEnabled"): |
79 with contextlib.suppress(NameError, AttributeError): |
84 with contextlib.suppress(NameError, AttributeError): |
80 return len(enchant.list_languages()) > 0 |
85 return len(enchant.list_languages()) > 0 |
81 return False |
86 return False |
84 def getDefaultPath(cls, isException=False): |
89 def getDefaultPath(cls, isException=False): |
85 """ |
90 """ |
86 Class method to get the default path names of the user dictionaries. |
91 Class method to get the default path names of the user dictionaries. |
87 |
92 |
88 @param isException flag indicating to return the name of the default |
93 @param isException flag indicating to return the name of the default |
89 exception dictionary (boolean) |
94 exception dictionary |
|
95 @type bool |
90 @return file name of the default user dictionary or the default user |
96 @return file name of the default user dictionary or the default user |
91 exception dictionary (string) |
97 exception dictionary |
|
98 @rtype str |
92 """ |
99 """ |
93 if isException: |
100 if isException: |
94 return os.path.join(Globals.getConfigDir(), "spelling", "pel.dic") |
101 return os.path.join(Globals.getConfigDir(), "spelling", "pel.dic") |
95 else: |
102 else: |
96 return os.path.join(Globals.getConfigDir(), "spelling", "pwl.dic") |
103 return os.path.join(Globals.getConfigDir(), "spelling", "pwl.dic") |
99 def getUserDictionaryPath(cls, isException=False): |
106 def getUserDictionaryPath(cls, isException=False): |
100 """ |
107 """ |
101 Class method to get the path name of a user dictionary file. |
108 Class method to get the path name of a user dictionary file. |
102 |
109 |
103 @param isException flag indicating to return the name of the user |
110 @param isException flag indicating to return the name of the user |
104 exception dictionary (boolean) |
111 exception dictionary |
|
112 @type bool |
105 @return file name of the user dictionary or the user exception |
113 @return file name of the user dictionary or the user exception |
106 dictionary (string) |
114 dictionary |
|
115 @rtype str |
107 """ |
116 """ |
108 if isException: |
117 if isException: |
109 dicFile = Preferences.getEditor("SpellCheckingPersonalExcludeList") |
118 dicFile = Preferences.getEditor("SpellCheckingPersonalExcludeList") |
110 if not dicFile: |
119 if not dicFile: |
111 dicFile = SpellChecker.getDefaultPath(True) |
120 dicFile = SpellChecker.getDefaultPath(True) |
118 @classmethod |
127 @classmethod |
119 def _getDict(cls, lang, pwl="", pel=""): |
128 def _getDict(cls, lang, pwl="", pel=""): |
120 """ |
129 """ |
121 Protected class method to get a new dictionary. |
130 Protected class method to get a new dictionary. |
122 |
131 |
123 @param lang the language to be used as the default (string). |
132 @param lang the language to be used as the default. The string should |
124 The string should be in language locale format (e.g. en_US, de). |
133 be in language locale format (e.g. en_US, de). |
125 @param pwl name of the personal/project word list (string) |
134 @type str |
126 @param pel name of the personal/project exclude list (string) |
135 @param pwl name of the personal/project word list |
127 @return reference to the dictionary (enchant.Dict) |
136 @type str |
|
137 @param pel name of the personal/project exclude list |
|
138 @type str |
|
139 @return reference to the dictionary |
|
140 @rtype enchant.Dict |
128 """ |
141 """ |
129 if not pwl: |
142 if not pwl: |
130 pwl = SpellChecker.getUserDictionaryPath() |
143 pwl = SpellChecker.getUserDictionaryPath() |
131 d = os.path.dirname(pwl) |
144 d = os.path.dirname(pwl) |
132 if not os.path.exists(d): |
145 if not os.path.exists(d): |
149 @classmethod |
162 @classmethod |
150 def setDefaultLanguage(cls, language): |
163 def setDefaultLanguage(cls, language): |
151 """ |
164 """ |
152 Class method to set the default language. |
165 Class method to set the default language. |
153 |
166 |
154 @param language the language to be used as the default (string). |
167 @param language the language to be used as the default. The string should |
155 The string should be in language locale format (e.g. en_US, de). |
168 be in language locale format (e.g. en_US, de). |
|
169 @type str |
156 """ |
170 """ |
157 cls._spelling_lang = language |
171 cls._spelling_lang = language |
158 cls._spelling_dict = cls._getDict(language) |
172 cls._spelling_dict = cls._getDict(language) |
159 |
173 |
160 def setLanguage(self, language, pwl="", pel=""): |
174 def setLanguage(self, language, pwl="", pel=""): |
161 """ |
175 """ |
162 Public method to set the current language. |
176 Public method to set the current language. |
163 |
177 |
164 @param language the language to be used as the default (string). |
178 @param language the language to be used as the default. The string should |
165 The string should be in language locale format (e.g. en_US, de). |
179 be in language locale format (e.g. en_US, de). |
166 @param pwl name of the personal/project word list (string) |
180 @type str |
167 @param pel name of the personal/project exclude list (string) |
181 @param pwl name of the personal/project word list |
|
182 @type str |
|
183 @param pel name of the personal/project exclude list |
|
184 @type str |
168 """ |
185 """ |
169 self._spelling_lang = language |
186 self._spelling_lang = language |
170 self._spelling_dict = self._getDict(language, pwl=pwl, pel=pel) |
187 self._spelling_dict = self._getDict(language, pwl=pwl, pel=pel) |
171 |
188 |
172 def getLanguage(self): |
189 def getLanguage(self): |
173 """ |
190 """ |
174 Public method to get the current language. |
191 Public method to get the current language. |
175 |
192 |
176 @return current language in language locale format (string) |
193 @return current language in language locale format |
|
194 @rtype str |
177 """ |
195 """ |
178 return self._spelling_lang |
196 return self._spelling_lang |
179 |
197 |
180 def setMinimumWordSize(self, size): |
198 def setMinimumWordSize(self, size): |
181 """ |
199 """ |
182 Public method to set the minimum word size. |
200 Public method to set the minimum word size. |
183 |
201 |
184 @param size minimum word size (integer) |
202 @param size minimum word size |
|
203 @type int |
185 """ |
204 """ |
186 if size > 0: |
205 if size > 0: |
187 self.minimumWordSize = size |
206 self.minimumWordSize = size |
188 |
207 |
189 def __getNextWord(self, pos, endPosition): |
208 def __getNextWord(self, pos, endPosition): |
190 """ |
209 """ |
191 Private method to get the next word in the text after the given |
210 Private method to get the next word in the text after the given |
192 position. |
211 position. |
193 |
212 |
194 @param pos position to start word extraction (integer) |
213 @param pos position to start word extraction |
195 @param endPosition position to stop word extraction (integer) |
214 @type int |
196 @return tuple of three values (the extracted word (string), |
215 @param endPosition position to stop word extraction |
197 start position (integer), end position (integer)) |
216 @type int |
|
217 @return tuple of three values (the extracted word, start position, end position) |
|
218 @rtype tuple of (str, int, int) |
198 """ |
219 """ |
199 if pos < 0 or pos >= endPosition: |
220 if pos < 0 or pos >= endPosition: |
200 return "", -1, -1 |
221 return "", -1, -1 |
201 |
222 |
202 ch = self.editor.charAt(pos) |
223 ch = self.editor.charAt(pos) |
222 |
243 |
223 def getContext(self, wordStart, wordEnd): |
244 def getContext(self, wordStart, wordEnd): |
224 """ |
245 """ |
225 Public method to get the context of a faulty word. |
246 Public method to get the context of a faulty word. |
226 |
247 |
227 @param wordStart the starting position of the word (integer) |
248 @param wordStart the starting position of the word |
228 @param wordEnd the ending position of the word (integer) |
249 @type int |
229 @return tuple of the leading and trailing context (string, string) |
250 @param wordEnd the ending position of the word |
|
251 @type int |
|
252 @return tuple of the leading and trailing context |
|
253 @rtype tuple of (str, str) |
230 """ |
254 """ |
231 sline, sindex = self.editor.lineIndexFromPosition(wordStart) |
255 sline, sindex = self.editor.lineIndexFromPosition(wordStart) |
232 eline, eindex = self.editor.lineIndexFromPosition(wordEnd) |
256 eline, eindex = self.editor.lineIndexFromPosition(wordEnd) |
233 text = self.editor.text(sline) |
257 text = self.editor.text(sline) |
234 return (text[:sindex], text[eindex:]) |
258 return (text[:sindex], text[eindex:]) |
235 |
259 |
236 def getError(self): |
260 def getError(self): |
237 """ |
261 """ |
238 Public method to get information about the last error found. |
262 Public method to get information about the last error found. |
239 |
263 |
240 @return tuple of last faulty word (string), starting position of the |
264 @return tuple of last faulty word, starting position of the |
241 faulty word (integer) and ending position of the faulty word |
265 faulty word and ending position of the faulty word |
242 (integer) |
266 @rtype tuple of (str, int, int) |
243 """ |
267 """ |
244 return (self.word, self.wordStart, self.wordEnd) |
268 return (self.word, self.wordStart, self.wordEnd) |
245 |
269 |
246 def initCheck(self, startPos, endPos): |
270 def initCheck(self, startPos, endPos): |
247 """ |
271 """ |
248 Public method to initialize a spell check. |
272 Public method to initialize a spell check. |
249 |
273 |
250 @param startPos position to start at (integer) |
274 @param startPos position to start at |
251 @param endPos position to end at (integer) |
275 @type int |
252 @return flag indicating successful initialization (boolean) |
276 @param endPos position to end at |
|
277 @type int |
|
278 @return flag indicating successful initialization |
|
279 @rtype bool |
253 """ |
280 """ |
254 if startPos == endPos: |
281 if startPos == endPos: |
255 return False |
282 return False |
256 |
283 |
257 spell = self._spelling_dict |
284 spell = self._spelling_dict |
351 |
381 |
352 def checkLines(self, firstLine, lastLine): |
382 def checkLines(self, firstLine, lastLine): |
353 """ |
383 """ |
354 Public method to check some lines of text. |
384 Public method to check some lines of text. |
355 |
385 |
356 @param firstLine line number of first line to check (integer) |
386 @param firstLine line number of first line to check |
357 @param lastLine line number of last line to check (integer) |
387 @type int |
|
388 @param lastLine line number of last line to check |
|
389 @type int |
358 """ |
390 """ |
359 startPos = self.editor.positionFromLineIndex(firstLine, 0) |
391 startPos = self.editor.positionFromLineIndex(firstLine, 0) |
360 |
392 |
361 if lastLine >= self.editor.lines(): |
393 if lastLine >= self.editor.lines(): |
362 lastLine = self.editor.lines() - 1 |
394 lastLine = self.editor.lines() - 1 |
419 |
451 |
420 def getSuggestions(self, word): |
452 def getSuggestions(self, word): |
421 """ |
453 """ |
422 Public method to get suggestions for the given word. |
454 Public method to get suggestions for the given word. |
423 |
455 |
424 @param word word to get suggestions for (string) |
456 @param word word to get suggestions for |
425 @return list of suggestions (list of strings) |
457 @type str |
|
458 @return list of suggestions |
|
459 @rtype list of str |
426 """ |
460 """ |
427 suggestions = [] |
461 suggestions = [] |
428 spell = self._spelling_dict |
462 spell = self._spelling_dict |
429 if spell and len(word) >= self.minimumWordSize: |
463 if spell and len(word) >= self.minimumWordSize: |
430 with contextlib.suppress(enchant.errors.Error): |
464 with contextlib.suppress(enchant.errors.Error): |
445 |
480 |
446 def remove(self, word): |
481 def remove(self, word): |
447 """ |
482 """ |
448 Public method to add a word to the personal exclude list. |
483 Public method to add a word to the personal exclude list. |
449 |
484 |
450 @param word word to add (string) |
485 @param word word to add |
|
486 @type str |
451 """ |
487 """ |
452 spell = self._spelling_dict |
488 spell = self._spelling_dict |
453 if spell: |
489 if spell: |
454 spell.remove(word) |
490 spell.remove(word) |
455 |
491 |
456 def ignoreAlways(self, word=None): |
492 def ignoreAlways(self, word=None): |
457 """ |
493 """ |
458 Public method to tell the checker, to always ignore the given word |
494 Public method to tell the checker, to always ignore the given word |
459 or the current word. |
495 or the current word. |
460 |
496 |
461 @param word word to be ignored (string) |
497 @param word word to be ignored |
|
498 @type str |
462 """ |
499 """ |
463 if word is None: |
500 if word is None: |
464 word = self.word |
501 word = self.word |
465 if word not in self.__ignoreWords: |
502 if word not in self.__ignoreWords: |
466 self.__ignoreWords.append(word) |
503 self.__ignoreWords.append(word) |
468 def replace(self, replacement): |
505 def replace(self, replacement): |
469 """ |
506 """ |
470 Public method to tell the checker to replace the current word with |
507 Public method to tell the checker to replace the current word with |
471 the replacement string. |
508 the replacement string. |
472 |
509 |
473 @param replacement replacement string (string) |
510 @param replacement replacement string |
|
511 @type str |
474 """ |
512 """ |
475 sline, sindex = self.editor.lineIndexFromPosition(self.wordStart) |
513 sline, sindex = self.editor.lineIndexFromPosition(self.wordStart) |
476 eline, eindex = self.editor.lineIndexFromPosition(self.wordEnd) |
514 eline, eindex = self.editor.lineIndexFromPosition(self.wordEnd) |
477 self.editor.setSelection(sline, sindex, eline, eindex) |
515 self.editor.setSelection(sline, sindex, eline, eindex) |
478 self.editor.beginUndoAction() |
516 self.editor.beginUndoAction() |
484 def replaceAlways(self, replacement): |
522 def replaceAlways(self, replacement): |
485 """ |
523 """ |
486 Public method to tell the checker to always replace the current word |
524 Public method to tell the checker to always replace the current word |
487 with the replacement string. |
525 with the replacement string. |
488 |
526 |
489 @param replacement replacement string (string) |
527 @param replacement replacement string |
|
528 @type str |
490 """ |
529 """ |
491 self.__replaceWords[self.word] = replacement |
530 self.__replaceWords[self.word] = replacement |
492 self.replace(replacement) |
531 self.replace(replacement) |
493 |
532 |
494 ################################################################## |
533 ################################################################## |