为什么我的QsciLexerCustom子类在PyQt4中使用QsciScintilla时不起作用?
我的最终目标是让Erlang的语法在使用PyQt4和Python 2.6的QsciScintilla中高亮显示。我现在是在Windows 7上运行,但也需要支持Ubuntu。
PyQt4缺少Erlang词法分析器/高亮显示器所需的包装代码,而这个代码在“基础”scintilla中是有的。所以我想在QsciLexerCustom的基础上自己写一个轻量级的。这个过程有点麻烦,因为Qsci的包装器似乎更喜欢使用行号和索引,而不是从开始位置的偏移量来获取或设置文本的子范围。而词法分析器是以从开始位置的偏移量作为参数的。目前,我是先获取整个文本的副本,然后再根据需要进行拆分。
我有了以下的词法分析器,并通过setLexer()来应用它。每当我打开一个新文件时,它会得到所有合适的调用,并将其设置为词法分析器,还会根据它正在做的事情打印出一堆相关的行……但文档中没有任何样式。我尝试把所有定义的样式都设置为红色,但文档依然顽固地保持黑底白字,所以显然这些样式并没有真正“生效”。
我哪里做错了?如果这里没有人知道,那有什么合适的讨论论坛可以让人们真正了解这些问题?(这是Python、Qt和Scintilla之间一个有趣的交集,所以我想知道知道的人应该不多)
假设prefs.declare()只是设置一个字典,用于返回给定键的值(我已经验证过了——这不是问题)。假设scintilla在它的主窗口QWidget中合理地构建好了。具体来说,如果我应用一个捆绑的词法分析器(比如QsciLexerPython),它就会生效并显示样式化的文本。
prefs.declare('font.name.margin', "MS Dlg")
prefs.declare('font.size.margin', 8)
prefs.declare('font.name.code', "Courier New")
prefs.declare('font.size.code', 10)
prefs.declare('color.editline', "#d0e0ff")
class LexerErlang(Qsci.QsciLexerCustom):
def __init__(self, obj = None):
Qsci.QsciLexerCustom.__init__(self, obj)
self.sci = None
self.plainFont = QtGui.QFont()
self.plainFont.setPointSize(int(prefs.get('font.size.code')))
self.plainFont.setFamily(prefs.get('font.name.code'))
self.marginFont = QtGui.QFont()
self.marginFont.setPointSize(int(prefs.get('font.size.code')))
self.marginFont.setFamily(prefs.get('font.name.margin'))
self.boldFont = QtGui.QFont()
self.boldFont.setPointSize(int(prefs.get('font.size.code')))
self.boldFont.setFamily(prefs.get('font.name.code'))
self.boldFont.setBold(True)
self.styles = [
Qsci.QsciStyle(0, QtCore.QString("base"), QtGui.QColor("#000000"), QtGui.QColor("#ffffff"), self.plainFont, True),
Qsci.QsciStyle(1, QtCore.QString("comment"), QtGui.QColor("#008000"), QtGui.QColor("#eeffee"), self.marginFont, True),
Qsci.QsciStyle(2, QtCore.QString("keyword"), QtGui.QColor("#000080"), QtGui.QColor("#ffffff"), self.boldFont, True),
Qsci.QsciStyle(3, QtCore.QString("string"), QtGui.QColor("#800000"), QtGui.QColor("#ffffff"), self.marginFont, True),
Qsci.QsciStyle(4, QtCore.QString("atom"), QtGui.QColor("#008080"), QtGui.QColor("#ffffff"), self.plainFont, True),
Qsci.QsciStyle(5, QtCore.QString("macro"), QtGui.QColor("#808000"), QtGui.QColor("#ffffff"), self.boldFont, True),
Qsci.QsciStyle(6, QtCore.QString("error"), QtGui.QColor("#000000"), QtGui.QColor("#ffd0d0"), self.plainFont, True),
]
print("LexerErlang created")
def description(self, ix):
for i in self.styles:
if i.style() == ix:
return QtCore.QString(i.description())
return QtCore.QString("")
def setEditor(self, sci):
self.sci = sci
Qsci.QsciLexerCustom.setEditor(self, sci)
print("LexerErlang.setEditor()")
def styleText(self, start, end):
print("LexerErlang.styleText(%d,%d)" % (start, end))
lines = self.getText(start, end)
offset = start
self.startStyling(offset, 0)
print("startStyling()")
for i in lines:
if i == "":
self.setStyling(1, self.styles[0])
print("setStyling(1)")
offset += 1
continue
if i[0] == '%':
self.setStyling(len(i)+1, self.styles[1])
print("setStyling(%)")
offset += len(i)+1
continue
self.setStyling(len(i)+1, self.styles[0])
print("setStyling(n)")
offset += len(i)+1
def getText(self, start, end):
data = self.sci.text()
print("LexerErlang.getText(): " + str(len(data)) + " chars")
return data[start:end].split('\n')
应用到QsciScintilla小部件的方式如下:
_lexers = {
'erl': (Q.SCLEX_ERLANG, LexerErlang),
'hrl': (Q.SCLEX_ERLANG, LexerErlang),
'html': (Q.SCLEX_HTML, Qsci.QsciLexerHTML),
'css': (Q.SCLEX_CSS, Qsci.QsciLexerCSS),
'py': (Q.SCLEX_PYTHON, Qsci.QsciLexerPython),
'php': (Q.SCLEX_PHP, Qsci.QsciLexerHTML),
'inc': (Q.SCLEX_PHP, Qsci.QsciLexerHTML),
'js': (Q.SCLEX_CPP, Qsci.QsciLexerJavaScript),
'cpp': (Q.SCLEX_CPP, Qsci.QsciLexerCPP),
'h': (Q.SCLEX_CPP, Qsci.QsciLexerCPP),
'cxx': (Q.SCLEX_CPP, Qsci.QsciLexerCPP),
'hpp': (Q.SCLEX_CPP, Qsci.QsciLexerCPP),
'c': (Q.SCLEX_CPP, Qsci.QsciLexerCPP),
'hxx': (Q.SCLEX_CPP, Qsci.QsciLexerCPP),
'tpl': (Q.SCLEX_CPP, Qsci.QsciLexerCPP),
'xml': (Q.SCLEX_XML, Qsci.QsciLexerXML),
}
... 在我的文档窗口类中 ...
def addContentsDocument(self, contents, title):
handler = self.makeScintilla()
handler.title = title
sci = handler.sci
sci.append(contents)
self.tabWidget.addTab(sci, title)
self.tabWidget.setCurrentWidget(sci)
self.applyLexer(sci, title)
EventBus.bus.broadcast('command.done', {'text': 'Opened ' + title})
return handler
def applyLexer(self, sci, title):
(language, lexer) = language_and_lexer_from_title(title)
if lexer:
l = lexer()
print("making lexer: " + str(l))
sci.setLexer(l)
else:
print("setting lexer by id: " + str(language))
sci.SendScintilla(Qsci.QsciScintillaBase.SCI_SETLEXER, language)
linst = sci.lexer()
print("lexer: " + str(linst))
def makeScintilla(self):
sci = Qsci.QsciScintilla()
sci.setUtf8(True)
sci.setTabIndents(True)
sci.setIndentationsUseTabs(False)
sci.setIndentationWidth(4)
sci.setMarginsFont(self.smallFont)
sci.setMarginWidth(0, self.smallFontMetrics.width('00000'))
sci.setFont(self.monoFont)
sci.setAutoIndent(True)
sci.setBraceMatching(Qsci.QsciScintilla.StrictBraceMatch)
handler = SciHandler(sci)
self.handlers[sci] = handler
sci.setMarginLineNumbers(0, True)
sci.setCaretLineVisible(True)
sci.setCaretLineBackgroundColor(QtGui.QColor(prefs.get('color.editline')))
return handler
假设应用程序的其他部分也正常工作(因为确实是这样 :-)
1 个回答
这个回答说的是,关于QsciLexerCustom的说明文档让人误解。仅仅调用setStyling()并传入一个QsciStyle对象是不够的。实际上,只有这个对象里的数字索引才是重要的。此外,你的自定义词法分析器还需要重写一些函数,比如font()、color()等,这些函数会根据样式索引来获取样式,并返回你想要的样式。