QScintilla/SpellChecker.py

changeset 3011
18292228c724
parent 2965
d133c7edd88a
child 3057
10516539f238
child 3160
209a07d7e401
equal deleted inserted replaced
3010:befeff46ec0f 3011:18292228c724
28 """ 28 """
29 # class attributes to be used as defaults 29 # class attributes to be used as defaults
30 _spelling_lang = None 30 _spelling_lang = None
31 _spelling_dict = None 31 _spelling_dict = None
32 32
33 def __init__(self, editor, indicator, defaultLanguage=None, checkRegion=None): 33 def __init__(self, editor, indicator, defaultLanguage=None,
34 checkRegion=None):
34 """ 35 """
35 Constructor 36 Constructor
36 37
37 @param editor reference to the editor object (QScintilla.Editor) 38 @param editor reference to the editor object (QScintilla.Editor)
38 @param indicator spell checking indicator 39 @param indicator spell checking indicator
39 @keyparam defaultLanguage the language to be used as the default (string). 40 @keyparam defaultLanguage the language to be used as the default
40 The string should be in language locale format (e.g. en_US, de). 41 (string). The string should be in language locale format
41 @keyparam checkRegion reference to a function to check for a valid region 42 (e.g. en_US, de).
43 @keyparam checkRegion reference to a function to check for a valid
44 region
42 """ 45 """
43 super().__init__(editor) 46 super().__init__(editor)
44 47
45 self.editor = editor 48 self.editor = editor
46 self.indicator = indicator 49 self.indicator = indicator
92 exception dictionary (boolean) 95 exception dictionary (boolean)
93 @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
94 exception dictionary (string) 97 exception dictionary (string)
95 """ 98 """
96 if isException: 99 if isException:
97 return os.path.join(Utilities.getConfigDir(), "spelling", "pel.dic") 100 return os.path.join(
101 Utilities.getConfigDir(), "spelling", "pel.dic")
98 else: 102 else:
99 return os.path.join(Utilities.getConfigDir(), "spelling", "pwl.dic") 103 return os.path.join(
104 Utilities.getConfigDir(), "spelling", "pwl.dic")
100 105
101 @classmethod 106 @classmethod
102 def getUserDictionaryPath(cls, isException=False): 107 def getUserDictionaryPath(cls, isException=False):
103 """ 108 """
104 Class method to get the path name of a user dictionary file. 109 Class method to get the path name of a user dictionary file.
189 """ 194 """
190 self.minimumWordSize = size 195 self.minimumWordSize = size
191 196
192 def __getNextWord(self, pos, endPosition): 197 def __getNextWord(self, pos, endPosition):
193 """ 198 """
194 Private method to get the next word in the text after the given position. 199 Private method to get the next word in the text after the given
200 position.
195 201
196 @param pos position to start word extraction (integer) 202 @param pos position to start word extraction (integer)
197 @param endPosition position to stop word extraction (integer) 203 @param endPosition position to stop word extraction (integer)
198 @return tuple of three values (the extracted word (string), 204 @return tuple of three values (the extracted word (string),
199 start position (integer), end position (integer)) 205 start position (integer), end position (integer))
238 def getError(self): 244 def getError(self):
239 """ 245 """
240 Public method to get information about the last error found. 246 Public method to get information about the last error found.
241 247
242 @return tuple of last faulty word (string), starting position of the 248 @return tuple of last faulty word (string), starting position of the
243 faulty word (integer) and ending position of the faulty word (integer) 249 faulty word (integer) and ending position of the faulty word
250 (integer)
244 """ 251 """
245 return (self.word, self.wordStart, self.wordEnd) 252 return (self.word, self.wordStart, self.wordEnd)
246 253
247 def initCheck(self, startPos, endPos): 254 def initCheck(self, startPos, endPos):
248 """ 255 """
257 264
258 spell = self._spelling_dict 265 spell = self._spelling_dict
259 if spell is None: 266 if spell is None:
260 return False 267 return False
261 268
262 self.editor.clearIndicatorRange(self.indicator, startPos, endPos - startPos) 269 self.editor.clearIndicatorRange(
270 self.indicator, startPos, endPos - startPos)
263 271
264 self.pos = startPos 272 self.pos = startPos
265 self.endPos = endPos 273 self.endPos = endPos
266 self.word = "" 274 self.word = ""
267 self.wordStart = -1 275 self.wordStart = -1
292 """ 300 """
293 if self.lastCheckedLine < 0: 301 if self.lastCheckedLine < 0:
294 return 302 return
295 303
296 linesChunk = Preferences.getEditor("AutoSpellCheckChunkSize") 304 linesChunk = Preferences.getEditor("AutoSpellCheckChunkSize")
297 self.checkLines(self.lastCheckedLine, self.lastCheckedLine + linesChunk) 305 self.checkLines(self.lastCheckedLine,
306 self.lastCheckedLine + linesChunk)
298 self.lastCheckedLine = self.lastCheckedLine + linesChunk + 1 307 self.lastCheckedLine = self.lastCheckedLine + linesChunk + 1
299 if self.lastCheckedLine >= self.editor.lines(): 308 if self.lastCheckedLine >= self.editor.lines():
300 self.lastCheckedLine = -1 309 self.lastCheckedLine = -1
301 else: 310 else:
302 QTimer.singleShot(0, self.__incrementalCheck) 311 QTimer.singleShot(0, self.__incrementalCheck)
319 if pos >= 0 and self.__checkRegion(pos): 328 if pos >= 0 and self.__checkRegion(pos):
320 pos0 = pos 329 pos0 = pos
321 pos1 = -1 330 pos1 = -1
322 if not self.editor.charAt(pos).isalnum(): 331 if not self.editor.charAt(pos).isalnum():
323 line, index = self.editor.lineIndexFromPosition(pos) 332 line, index = self.editor.lineIndexFromPosition(pos)
324 self.editor.clearIndicator(self.indicator, line, index, line, index + 1) 333 self.editor.clearIndicator(
334 self.indicator, line, index, line, index + 1)
325 pos1 = self.editor.positionAfter(pos) 335 pos1 = self.editor.positionAfter(pos)
326 pos0 = self.editor.positionBefore(pos) 336 pos0 = self.editor.positionBefore(pos)
327 337
328 for pos in [pos0, pos1]: 338 for pos in [pos0, pos1]:
329 if self.editor.charAt(pos).isalnum(): 339 if self.editor.charAt(pos).isalnum():
331 word = self.editor.getWord(line, index, useWordChars=False) 341 word = self.editor.getWord(line, index, useWordChars=False)
332 if len(word) >= self.minimumWordSize: 342 if len(word) >= self.minimumWordSize:
333 ok = spell.check(word) 343 ok = spell.check(word)
334 else: 344 else:
335 ok = True 345 ok = True
336 start, end = \ 346 start, end = self.editor.getWordBoundaries(
337 self.editor.getWordBoundaries(line, index, useWordChars=False) 347 line, index, useWordChars=False)
338 if ok: 348 if ok:
339 self.editor.clearIndicator(self.indicator, line, start, line, end) 349 self.editor.clearIndicator(
350 self.indicator, line, start, line, end)
340 else: 351 else:
341 # spell check indicated an error 352 # spell check indicated an error
342 self.editor.setIndicator(self.indicator, line, start, line, end) 353 self.editor.setIndicator(
354 self.indicator, line, start, line, end)
343 355
344 def checkLines(self, firstLine, lastLine): 356 def checkLines(self, firstLine, lastLine):
345 """ 357 """
346 Public method to check some lines of text. 358 Public method to check some lines of text.
347 359
401 413
402 def clearAll(self): 414 def clearAll(self):
403 """ 415 """
404 Public method to clear all spelling markers. 416 Public method to clear all spelling markers.
405 """ 417 """
406 self.editor.clearIndicatorRange(self.indicator, 0, self.editor.length()) 418 self.editor.clearIndicatorRange(
419 self.indicator, 0, self.editor.length())
407 420
408 def getSuggestions(self, word): 421 def getSuggestions(self, word):
409 """ 422 """
410 Public method to get suggestions for the given word. 423 Public method to get suggestions for the given word.
411 424
502 @exception StopIteration raised to indicate the end of the iteration 515 @exception StopIteration raised to indicate the end of the iteration
503 """ 516 """
504 spell = self._spelling_dict 517 spell = self._spelling_dict
505 if spell: 518 if spell:
506 while self.pos < self.endPos and self.pos >= 0: 519 while self.pos < self.endPos and self.pos >= 0:
507 word, wordStart, wordEnd = self.__getNextWord(self.pos, self.endPos) 520 word, wordStart, wordEnd = \
521 self.__getNextWord(self.pos, self.endPos)
508 self.pos = wordEnd 522 self.pos = wordEnd
509 if (wordEnd - wordStart) >= self.minimumWordSize and \ 523 if (wordEnd - wordStart) >= self.minimumWordSize and \
510 self.__checkRegion(wordStart): 524 self.__checkRegion(wordStart):
511 if spell.check(word): 525 if spell.check(word):
512 continue 526 continue

eric ide

mercurial