问题描述
因为我想使用的高亮是为了获取上一行变量中store的先前状态,我可以通过将其存储在setCurrentBlockState中的方式在QSyntaxHighlighter中使用它。
例如:
from PyQt5.Qsci import QsciScintilla
class QsciLexerCustom1(QsciLexerCustom):
def styleText(self,start,end):
editor = self.editor()
SCI = editor.SendScintilla
interline_status = 0
for line in source:
#(tokenizing the line)
for token in tokenized_line:
if token == "string1":
interline_status = 1
if token == "string2":
interline_status = 2
但是,在处理到下一行时,interline_status
将被重置为 0。我发现变量 QsciScintilla.SCI_GETSTYLEAT
与我想要的类似,如下所示:
pos = SCI(QsciScintilla.SCI_GETLINEENDPOSITION,index - 1)
interline_state = SCI(QsciScintilla.SCI_GETSTYLEAT,pos)
然而,结果却出人意料。可能我要使用的状态值不在变量的末尾。
在 QsciScintilla(在 PyQt5 中)中是否有任何等效的 setCurrentBlockState
?
解决方法
我找到了答案:使用 QsciScintilla.SCI_SETLINESTATE
和 QsciScintilla.SCI_GETLINESTATE
:
from PyQt5.Qsci import QsciScintilla
class QsciLexerCustom1(QsciLexerCustom):
def styleText(self,start,end):
editor = self.editor()
SCI = editor.SendScintilla
index = 0 # current line indicator
interline_status = 0
# get the status of the prev. line
if index > 0:
interline_status = SCI(QsciScintilla.SCI_GETLINESTATE,index - 1)
for line in source:
#(tokenizing the line)
for token in tokenized_line:
if token == "string1":
interline_status = 1
if token == "string2":
interline_status = 2
# store the status of current line
SCI(QsciScintilla.SCI_SETLINESTATE,index,interline_status)