2015-06-25 01:43:35 +12:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf8 -*-
|
|
|
|
|
|
|
|
from qt import *
|
|
|
|
import re
|
|
|
|
|
|
|
|
class basicHighlighter(QSyntaxHighlighter):
|
|
|
|
|
|
|
|
def __init__(self, editor):
|
|
|
|
QSyntaxHighlighter.__init__(self, editor.document())
|
|
|
|
|
|
|
|
self.editor = editor
|
2015-06-25 04:39:38 +12:00
|
|
|
self._misspelledColor = Qt.red
|
2015-06-25 01:43:35 +12:00
|
|
|
|
|
|
|
def setDefaultBlockFormat(self, bf):
|
|
|
|
self._defaultBlockFormat = bf
|
|
|
|
self.rehighlight()
|
|
|
|
|
|
|
|
def setMisspelledColor(self, color):
|
|
|
|
self._misspelledColor = color
|
2015-06-25 04:39:38 +12:00
|
|
|
|
|
|
|
def setStyle(self):
|
|
|
|
"""t2tHighlighter needs to reupdates styles on some occasions (see themes.py).
|
|
|
|
This lazy function allow to update without checking the type of highlighter.
|
|
|
|
"""
|
|
|
|
pass
|
2015-06-25 01:43:35 +12:00
|
|
|
|
|
|
|
def highlightBlock(self, text):
|
|
|
|
"""Apply syntax highlighting to the given block of text.
|
|
|
|
"""
|
|
|
|
|
|
|
|
QTextCursor(self.currentBlock()).setBlockFormat(self._defaultBlockFormat)
|
|
|
|
|
|
|
|
# Spell checking
|
|
|
|
# Based on http://john.nachtimwald.com/2009/08/22/qplaintextedit-with-in-line-spell-check/
|
|
|
|
WORDS = '(?iu)[\w\']+'
|
|
|
|
if self.editor.spellcheck:
|
|
|
|
for word_object in re.finditer(WORDS, text):
|
|
|
|
if self.editor._dict and not self.editor._dict.check(word_object.group()):
|
|
|
|
format = self.format(word_object.start())
|
|
|
|
format.setUnderlineColor(self._misspelledColor)
|
|
|
|
format.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline)
|
|
|
|
self.setFormat(word_object.start(),
|
|
|
|
word_object.end() - word_object.start(), format)
|