为什么我的 QsciLexerCustom 子类不能使用 QsciScintilla 在 PyQt4 中工作?

发布于 2024-08-30 03:02:00 字数 5807 浏览 6 评论 0原文

我的最终目标是使用 PyQt4 和 Python 2.6 在 QsciScintilla 中获得 Erlang 语法突出显示。我在 Windows 7 上运行,但还需要 Ubuntu 支持。

PyQt4 缺少“base”scintilla 所具有的 Erlang 词法分析器/荧光笔所需的包装器代码,因此我想我应该在 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

让我们假设应用程序的其余部分也可以工作(因为它确实有效:-)

My end goal is to get Erlang syntax highlighting in QsciScintilla using PyQt4 and Python 2.6. I'm running on Windows 7, but will also need Ubuntu support.

PyQt4 is missing the necessary wrapper code for the Erlang lexer/highlighter that "base" scintilla has, so I figured I'd write a lightweight one on top of QsciLexerCustom. It's a little bit problematic, because the Qsci wrapper seems to really want to talk about line+index rather than offset-from-start when getting/setting subranges of text. Meanwhile, the lexer gets arguments as offset-from-start. For now, I get a copy of the entire text, and split that up as appropriate.

I have the following lexer, and I apply it with setLexer(). It gets all the appropriate calls when I open a new file and sets this as the lexer, and prints a bunch of appropriate lines based on what it's doing... but there is no styling in the document. I tried making all the defined styles red, and the document is still stubbornly black-on-white, so apparently the styles don't really "take effect"

What am I doing wrong? If nobody here knows, what's the appropriate discussion forum where people might actually know these things? (It's an interesting intersection between Python, Qt and Scintilla, so I imagine the set of people who would know is small)

Let's assume prefs.declare() just sets up a dict that returns the value for the given key (I've verified this -- it's not the problem).
Let's assume scintilla is reasonably properly constructed into its host window QWidget. Specifically, if I apply a bundled lexer (such as QsciLexerPython), it takes effect and does show styled text.

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')

Applied to the QsciScintilla widget as follows:

_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),
}

... inside my document window class ...

  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

Let's assume the rest of the application works, too (because it does :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安人多梦 2024-09-06 03:02:00

答案是 QsciLexerCustom 的文档具有误导性。
使用 QsciStyle 对象调用 setStyling() 是不够的。实际上,只有该对象的数字索引似乎很重要。
此外,您的自定义词法分析器需要重写 font()、color() 和其他采用样式索引的样式获取函数,并返回您想要该索引具有的样式。

The answer was that the documentation for QsciLexerCustom is misleading.
It's not enough to call setStyling() with a QsciStyle object. Only the numeric index from that object actually seems to matter.
Additionally, your custom lexer needs to override the font(), color() and other style-getting functions that take a style index, and return the style you want to have for that index.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文