使用Python为文本编辑器添加语法高亮

6 投票
3 回答
5354 浏览
提问于 2025-04-16 20:14

我正在创建一个自己的基于网页的文本编辑器,我想加入语法高亮功能。开始的时候我会先为一种语言(可能是Python)做高亮,之后我想为我能想到的所有语言都添加高亮。

如果可能的话,我想找一个相关的教程,有人知道哪里可以找到吗?

另外,如果你有其他的建议也非常欢迎。

3 个回答

1

在编程中,有时候我们会遇到一些问题,特别是在使用某些工具或库的时候。这些问题可能会让我们感到困惑,不知道该如何解决。比如,有人可能在使用某个功能时,发现它没有按照预期工作,或者出现了错误信息。这种时候,查看相关的讨论或者提问是很有帮助的。

在社区网站上,比如StackOverflow,很多人会分享他们遇到的问题和解决方案。你可以在这里找到很多有用的信息,帮助你理解问题的根源,以及如何修复它们。

总之,遇到问题时,不要害怕去寻求帮助,看看别人是怎么解决类似问题的,这样你也能学到很多东西。

 # syntax.py

    import sys

    from PyQt4.QtCore import QRegExp
    from PyQt4.QtGui import QColor, QTextCharFormat, QFont, QSyntaxHighlighter

    def format(color, style=''):
        """Return a QTextCharFormat with the given attributes.
        """
        _color = QColor()
        _color.setNamedColor(color)

        _format = QTextCharFormat()
        _format.setForeground(_color)
        if 'bold' in style:
            _format.setFontWeight(QFont.Bold)
        if 'italic' in style:
            _format.setFontItalic(True)

        return _format


    # Syntax styles that can be shared by all languages
    STYLES = {
        'keyword': format('blue'),
        'operator': format('red'),
        'brace': format('darkGray'),
        'defclass': format('black', 'bold'),
        'string': format('magenta'),
        'string2': format('darkMagenta'),
        'comment': format('darkGreen', 'italic'),
        'self': format('black', 'italic'),
        'numbers': format('brown'),
    }


    class PythonHighlighter (QSyntaxHighlighter):
        """Syntax highlighter for the Python language.
        """
        # Python keywords
        keywords = [
            'and', 'assert', 'break', 'class', 'continue', 'def',
            'del', 'elif', 'else', 'except', 'exec', 'finally',
            'for', 'from', 'global', 'if', 'import', 'in',
            'is', 'lambda', 'not', 'or', 'pass', 'print',
            'raise', 'return', 'try', 'while', 'yield',
            'None', 'True', 'False',
        ]

        # Python operators
        operators = [
            '=',
            # Comparison
            '==', '!=', '<', '<=', '>', '>=',
            # Arithmetic
            '\+', '-', '\*', '/', '//', '\%', '\*\*',
            # In-place
            '\+=', '-=', '\*=', '/=', '\%=',
            # Bitwise
            '\^', '\|', '\&', '\~', '>>', '<<',
        ]

        # Python braces
        braces = [
            '\{', '\}', '\(', '\)', '\[', '\]',
        ]
        def __init__(self, document):
            QSyntaxHighlighter.__init__(self, document)

            # Multi-line strings (expression, flag, style)
            # FIXME: The triple-quotes in these two lines will mess up the
            # syntax highlighting from this point onward
            self.tri_single = (QRegExp("'''"), 1, STYLES['string2'])
            self.tri_double = (QRegExp('"""'), 2, STYLES['string2'])

            rules = []

            # Keyword, operator, and brace rules
            rules += [(r'\b%s\b' % w, 0, STYLES['keyword'])
                for w in PythonHighlighter.keywords]
            rules += [(r'%s' % o, 0, STYLES['operator'])
                for o in PythonHighlighter.operators]
            rules += [(r'%s' % b, 0, STYLES['brace'])
                for b in PythonHighlighter.braces]

            # All other rules
            rules += [
                # 'self'
                (r'\bself\b', 0, STYLES['self']),

                # Double-quoted string, possibly containing escape sequences
                (r'"[^"\\]*(\\.[^"\\]*)*"', 0, STYLES['string']),
                # Single-quoted string, possibly containing escape sequences
                (r"'[^'\\]*(\\.[^'\\]*)*'", 0, STYLES['string']),

                # 'def' followed by an identifier
                (r'\bdef\b\s*(\w+)', 1, STYLES['defclass']),
                # 'class' followed by an identifier
                (r'\bclass\b\s*(\w+)', 1, STYLES['defclass']),

                # From '#' until a newline
                (r'#[^\n]*', 0, STYLES['comment']),

                # Numeric literals
                (r'\b[+-]?[0-9]+[lL]?\b', 0, STYLES['numbers']),
                (r'\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\b', 0, STYLES['numbers']),
                (r'\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\b', 0, STYLES['numbers']),
            ]

            # Build a QRegExp for each pattern
            self.rules = [(QRegExp(pat), index, fmt)
                for (pat, index, fmt) in rules]


        def highlightBlock(self, text):
            """Apply syntax highlighting to the given block of text.
            """
            # Do other syntax formatting
            for expression, nth, format in self.rules:
                index = expression.indexIn(text, 0)

                while index >= 0:
                    # We actually want the index of the nth match
                    index = expression.pos(nth)
                    length = expression.cap(nth).length()
                    self.setFormat(index, length, format)
                    index = expression.indexIn(text, index + length)

            self.setCurrentBlockState(0)

            # Do multi-line strings
            in_multiline = self.match_multiline(text, *self.tri_single)
            if not in_multiline:
                in_multiline = self.match_multiline(text, *self.tri_double)


        def match_multiline(self, text, delimiter, in_state, style):
            """Do highlighting of multi-line strings. ``delimiter`` should be a
            ``QRegExp`` for triple-single-quotes or triple-double-quotes, and
            ``in_state`` should be a unique integer to represent the corresponding
            state changes when inside those strings. Returns True if we're still
            inside a multi-line string when this function is finished.
            """
            # If inside triple-single quotes, start at 0
            if self.previousBlockState() == in_state:
                start = 0
                add = 0
            # Otherwise, look for the delimiter on this line
            else:
                start = delimiter.indexIn(text)
                # Move past this match
                add = delimiter.matchedLength()

            # As long as there's a delimiter match on this line...
            while start >= 0:
                # Look for the ending delimiter
                end = delimiter.indexIn(text, start + add)
                # Ending delimiter on this line?
                if end >= add:
                    length = end - start + add + delimiter.matchedLength()
                    self.setCurrentBlockState(0)
                # No; multi-line string
                else:
                    self.setCurrentBlockState(in_state)
                    length = text.length() - start + add
                # Apply formatting
                self.setFormat(start, length, style)
                # Look for the next match
                start = delimiter.indexIn(text, start + length)

            # Return True if still inside a multi-line string, False otherwise
            if self.currentBlockState() == in_state:
                return True
            else:
                return False
1

在编程中,有时候我们会遇到一些问题,像是代码运行不正常或者出现错误。这时候,我们可以在网上寻找帮助,比如在StackOverflow这个网站上。这个网站上有很多程序员分享他们的经验和解决方案。

当你在StackOverflow上提问时,记得要描述清楚你的问题,包括你遇到的错误信息、你使用的代码,以及你希望实现的目标。这样其他人才能更好地理解你的问题,并给出有效的建议。

同时,查看别人提问的内容也很有帮助,因为你可能会发现和自己遇到的问题类似的情况。通过阅读这些问题和答案,你可以学到很多解决问题的方法和技巧。

总之,StackOverflow是一个很好的学习和解决问题的地方,只要你善于利用它,就能提高自己的编程能力。

from PyQt4 import QtGui
import syntax

app = QtGui.QApplication([])
texter = QtGui.QPlainTextEdit()
highlight = syntax.PythonHighlighter(texter.document())
texter.show()

infile = open('syntax.py', 'r')
texter.setPlainText(infile.read())

app.exec_()

#use the program syntax.py to make it work I posted it
8

看看这个网站:Pygments


还有这个:highlight.js

撰写回答