PyQt5 中基于 QScintilla 的文本编辑器,具有可点击的函数和变量

QScintilla based text editor in PyQt5 with clickable functions and variables

我正在尝试在 PyQt5 中制作一个简单的文本编辑器,它具有基本的语法突出显示、代码完成以及可点击的函数和变量。我最大的希望是使用 QScintilla 端口
对于 PyQt5.
我在 Eli Bendersky 网站上找到了以下基于 QScintilla 的文本编辑器示例(http://eli.thegreenplace.net/2011/04/01/sample-using-qscintilla-with-pyqt,Victor S. 已将其改编为 PyQt5)。我认为这个例子是一个很好的起点:

#-------------------------------------------------------------------------
# qsci_simple_pythoneditor.pyw
#
# QScintilla sample with PyQt
#
# Eli Bendersky (eliben@gmail.com)
# This code is in the public domain
#-------------------------------------------------------------------------
import sys

import sip
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.Qsci import QsciScintilla, QsciLexerPython


class SimplePythonEditor(QsciScintilla):
    ARROW_MARKER_NUM = 8

    def __init__(self, parent=None):
        super(SimplePythonEditor, self).__init__(parent)

        # Set the default font
        font = QFont()
        font.setFamily('Courier')
        font.setFixedPitch(True)
        font.setPointSize(10)
        self.setFont(font)
        self.setMarginsFont(font)

        # Margin 0 is used for line numbers
        fontmetrics = QFontMetrics(font)
        self.setMarginsFont(font)
        self.setMarginWidth(0, fontmetrics.width("00000") + 6)
        self.setMarginLineNumbers(0, True)
        self.setMarginsBackgroundColor(QColor("#cccccc"))

        # Clickable margin 1 for showing markers
        self.setMarginSensitivity(1, True)
#        self.connect(self,
#            SIGNAL('marginClicked(int, int, Qt::KeyboardModifiers)'),
#            self.on_margin_clicked)
        self.markerDefine(QsciScintilla.RightArrow,
            self.ARROW_MARKER_NUM)
        self.setMarkerBackgroundColor(QColor("#ee1111"),
            self.ARROW_MARKER_NUM)

        # Brace matching: enable for a brace immediately before or after
        # the current position
        #
        self.setBraceMatching(QsciScintilla.SloppyBraceMatch)

        # Current line visible with special background color
        self.setCaretLineVisible(True)
        self.setCaretLineBackgroundColor(QColor("#ffe4e4"))

        # Set Python lexer
        # Set style for Python comments (style number 1) to a fixed-width
        # courier.
        #

        lexer = QsciLexerPython()
        lexer.setDefaultFont(font)
        self.setLexer(lexer)

        text = bytearray(str.encode("Arial"))
# 32, "Courier New"         
        self.SendScintilla(QsciScintilla.SCI_STYLESETFONT, 1, text)

        # Don't want to see the horizontal scrollbar at all
        # Use raw message to Scintilla here (all messages are documented
        # here: http://www.scintilla.org/ScintillaDoc.html)
        self.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0)

        # not too small
        self.setMinimumSize(600, 450)

    def on_margin_clicked(self, nmargin, nline, modifiers):
        # Toggle marker for the line the margin was clicked on
        if self.markersAtLine(nline) != 0:
            self.markerDelete(nline, self.ARROW_MARKER_NUM)
        else:
            self.markerAdd(nline, self.ARROW_MARKER_NUM)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    editor = SimplePythonEditor()
    editor.show()
    editor.setText(open(sys.argv[0]).read())
    app.exec_()

只需将此代码复制并粘贴到一个空的 .py 文件中,然后 运行 即可。您应该会在显示器上看到以下简单的文本编辑器:

注意语法高亮是多么完美! QScintilla 当然在后台做了一些解析来实现这一点。
是否可以为这个文本编辑器制作可点击的函数和变量?每个有自尊心的IDE都有。你点击一个函数,IDE 跳转到函数定义。变量也一样。我想知道:


编辑:
λuser 注意到以下内容:

Clickable function names require full parsing with a much deeper knowledge of a programming language [..]
This is way beyond the scope of Scintilla/QScintilla. Scintilla provides a way to react when the mouse clicks somewhere on the text, but the logic of "where is the definition of a function" is not in Scintilla and probably never will be.
However, some projects are dedicated to this task, like ctags. You could simply write a wrapper around this kind of tool.

我想为 ctags 编写这样的包装器现在在我的 TODO 列表中。第一步是在用户单击函数或变量时获得反应(Qt 信号)。当您将鼠标悬停在 function/variable 上时,它可能会变成蓝色,以通知用户它是可点击的。我已经尝试过实现这一点,但由于缺少 QScintilla 文档而受阻。

那么让我们trim将问题归结为:如何使 QScintilla 文本编辑器中的函数或变量可点击(可点击定义为 'something happens')


编辑:
我现在才回到这个问题——几个月后。我一直在和我的朋友 Matic Kukovec 合作设计一个关于 QScintilla 的网站。这是一个关于如何使用它的初学者友好教程:

https://qscintilla.com/

我希望这一举措能够填补缺乏文档的空白。

语法突出显示只是 运行 源文件上的词法分析器查找标记,然后将样式属性赋予它的问题。词法分析器对编程语言有非常基本的了解,它只了解什么是数字文字、关键字、运算符、注释,仅此而已。这是一个比较简单的工作,只需使用正则表达式即可完成。

另一方面,可点击的函数名称需要对编程语言有更深入的了解才能进行完整解析,例如这是变量的声明还是使用等。此外,这可能需要解析其他未被当前编辑器打开的源文件。

这超出了 Scintilla/QScintilla 的范围。 Scintilla 提供了一种在鼠标单击文本某处时做出反应的方法,但 "where is the definition of a function" 的逻辑不在 Scintilla 中,而且可能永远不会。

但是,有些项目专门用于此任务,例如 ctags。您可以简单地围绕这种工具编写一个包装器。

我通过邮件从 Matic Kukovec 那里得到了一个有用的答案,我想在这里分享一下。 Matic Kukovec 基于 QScintilla 做出了令人难以置信的 IDE:https://github.com/matkuki/ExCo。也许它会激发更多人深入研究 QScintilla(以及可点击的变量和函数)。


热点使文本可点击。您必须使用 QScintilla.SendScintilla 函数手动设置样式。 我在编辑器中使用的示例函数 Ex.Co。 ( https://github.com/matkuki/ExCo ):

def style_hotspot(self, index_from, length, color=0xff0000):
    """Style the text from/to with a hotspot"""
    send_scintilla = 
    #Use the scintilla low level messaging system to set the hotspot
    self.SendScintilla(PyQt4.Qsci.QsciScintillaBase.SCI_STYLESETHOTSPOT, 2, True)
    self.SendScintilla(PyQt4.Qsci.QsciScintillaBase.SCI_SETHOTSPOTACTIVEFORE, True, color)
    self.SendScintilla(PyQt4.Qsci.QsciScintillaBase.SCI_SETHOTSPOTACTIVEUNDERLINE, True)
    self.SendScintilla(PyQt4.Qsci.QsciScintillaBase.SCI_STARTSTYLING, index_from, 2)
    self.SendScintilla(PyQt4.Qsci.QsciScintillaBase.SCI_SETSTYLING, length, 2)

这使得当您将鼠标悬停在 QScintilla 编辑器中的文本时可以点击它。 以上函数中的数字2为热点样式编号。 要捕获单击热点时触发的事件,请连接到这些信号:

QScintilla.SCN_HOTSPOTCLICK
QScintilla.SCN_HOTSPOTDOUBLECLICK
QScintilla.SCN_HOTSPOTRELEASECLICK

有关更多详细信息,请参阅 Scintilla 热点文档: http://www.scintilla.org/ScintillaDoc.html#SCI_STYLESETHOTSPOT 和 QScintilla 热点事件: http://pyqt.sourceforge.net/Docs/QScintilla2/classQsciScintillaBase.html#a5eff383e6fa96cbbaba6a2558b076c0b


首先非常感谢Kukovec先生!关于您的回答,我有几个问题:

(1)您的示例函数中有几处我不明白。

def style_hotspot(self, index_from, length, color=0xff0000):
    """Style the text from/to with a hotspot"""
    send_scintilla =     # you undefine send_scintilla?
    #Use the scintilla low level messaging system to set the hotspot
    self.SendScintilla(..) # What object does 'self' refer to in this
    self.SendScintilla(..) # context?
    self.SendScintilla(..)

(2) 你说"To catch the event that fires when you click the hotspot, connect to these signals:"

QScintilla.SCN_HOTSPOTCLICK
QScintilla.SCN_HOTSPOTDOUBLECLICK
QScintilla.SCN_HOTSPOTRELEASECLICK

您实际上是如何连接到这些信号的?你能举一个例子吗?我习惯了 PyQt 信号槽机制,但我从未在 QScintilla 上使用过它。看到一个例子会很有帮助:-)

(3) 也许我错过了什么,但我没有看到你在 QScintilla 中定义的函数和变量(而不是其他东西)在源代码中是可点击的?

非常感谢您的帮助:-)

一种使用带有可点击函数和变量的选项的 Pyqt5 的方法。 引用了可点击部分的脚本在带有自定义信号的 PyQt5 中看起来像这样。

PyQt4 信号

self.connect(self,SIGNAL('marginClicked(int, int, Qt::KeyboardModifiers)'), 
self.on_margin_clicked)

PyQt5 信号

self.marginClicked.connect(self.on_margin_clicked)

PyQt5

import sys

import sip
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.Qsci import QsciScintilla, QsciLexerPython


class SimplePythonEditor(QsciScintilla):
    ARROW_MARKER_NUM = 8

    def __init__(self, parent=None):
        super(SimplePythonEditor, self).__init__(parent)

        # Set the default font
        font = QFont()
        font.setFamily('Courier')
        font.setFixedPitch(True)
        font.setPointSize(10)
        self.setFont(font)
        self.setMarginsFont(font)

        # Margin 0 is used for line numbers
        fontmetrics = QFontMetrics(font)
        self.setMarginsFont(font)
        self.setMarginWidth(0, fontmetrics.width("00000") + 6)
        self.setMarginLineNumbers(0, True)
        self.setMarginsBackgroundColor(QColor("#cccccc"))

        # Clickable margin 1 for showing markers
        self.setMarginSensitivity(1, True)
        self.marginClicked.connect(self.on_margin_clicked)
        self.markerDefine(QsciScintilla.RightArrow,
            self.ARROW_MARKER_NUM)
        self.setMarkerBackgroundColor(QColor("#ee1111"),
            self.ARROW_MARKER_NUM)

        # Brace matching: enable for a brace immediately before or after
        # the current position
        #
        self.setBraceMatching(QsciScintilla.SloppyBraceMatch)

        # Current line visible with special background color
        self.setCaretLineVisible(True)
        self.setCaretLineBackgroundColor(QColor("#ffe4e4"))

        # Set Python lexer
        # Set style for Python comments (style number 1) to a fixed-width
        # courier.
        #

        lexer = QsciLexerPython()
        lexer.setDefaultFont(font)
        self.setLexer(lexer)

        text = bytearray(str.encode("Arial"))
# 32, "Courier New"
        self.SendScintilla(QsciScintilla.SCI_STYLESETFONT, 1, text)

        # Don't want to see the horizontal scrollbar at all
        # Use raw message to Scintilla here (all messages are documented
        # here: http://www.scintilla.org/ScintillaDoc.html)
        self.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0)

        # not too small
        self.setMinimumSize(600, 450)

    def on_margin_clicked(self, nmargin, nline, modifiers):
        # Toggle marker for the line the margin was clicked on
        if self.markersAtLine(nline) != 0:
            self.markerDelete(nline, self.ARROW_MARKER_NUM)
        else:
            self.markerAdd(nline, self.ARROW_MARKER_NUM)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    editor = SimplePythonEditor()
    editor.show()
    editor.setText(open(sys.argv[0]).read())
    app.exec_()

查看以下文档: https://qscintilla.com/#clickable_text

有两种方法可以使 Qscintilla 中的内容可点击 - 您可以使用热点或指示器。热点要求您覆盖底层词法分析器的默认行为,但我认为指示符对于您的用例更方便。

我建议您查看可帮助您使文本可点击的指标,并且您可以定义在点击时执行的事件处理程序。 https://qscintilla.com/#clickable_text/indicators