Python 如何将折叠添加到自定义子类中?
考虑一下这个片段:Python 如何将折叠添加到自定义子类中?,python,pyqt,pyqt5,qscintilla,lark-parser,Python,Pyqt,Pyqt5,Qscintilla,Lark Parser,考虑一下这个片段: import sys import textwrap import re from PyQt5.Qt import * # noqa from PyQt5.Qsci import QsciScintilla from PyQt5.Qsci import QsciLexerCustom from lark import Lark, inline_args, Transformer class LexerJson(QsciLexerCustom): def
import sys
import textwrap
import re
from PyQt5.Qt import * # noqa
from PyQt5.Qsci import QsciScintilla
from PyQt5.Qsci import QsciLexerCustom
from lark import Lark, inline_args, Transformer
class LexerJson(QsciLexerCustom):
def __init__(self, parent=None):
super().__init__(parent)
self.create_grammar()
self.create_styles()
def create_styles(self):
deeppink = QColor(249, 38, 114)
khaki = QColor(230, 219, 116)
mediumpurple = QColor(174, 129, 255)
mediumturquoise = QColor(81, 217, 205)
yellowgreen = QColor(166, 226, 46)
lightcyan = QColor(213, 248, 232)
darkslategrey = QColor(39, 40, 34)
styles = {
0: mediumturquoise,
1: mediumpurple,
2: yellowgreen,
3: deeppink,
4: khaki,
5: lightcyan
}
for style, color in styles.items():
self.setColor(color, style)
self.setPaper(darkslategrey, style)
self.setFont(self.parent().font(), style)
self.token_styles = {
"__COLON": 5,
"__COMMA": 5,
"__FALSE1": 0,
"__LBRACE": 5,
"__LSQB": 5,
"__NULL2": 0,
"__RBRACE": 5,
"__RSQB": 5,
"__TRUE0": 0,
"ESCAPED_STRING": 4,
"SIGNED_NUMBER": 1,
}
def create_grammar(self):
grammar = '''
?start: value
?value: object
| array
| string
| SIGNED_NUMBER -> number
| "true" -> true
| "false" -> false
| "null" -> null
array : "[" [value ("," value)*] "]"
object : "{" [pair ("," pair)*] "}"
pair : string ":" value
string : ESCAPED_STRING
%import common.ESCAPED_STRING
%import common.SIGNED_NUMBER
%import common.WS
%ignore WS
'''
class TreeToJson(Transformer):
@inline_args
def string(self, s):
return s[1:-1].replace('\\"', '"')
array = list
pair = tuple
object = dict
number = inline_args(float)
def null(self, _): return None
def true(self, _): return True
def false(self, _): return False
self.lark = Lark(grammar, parser='lalr', transformer=TreeToJson())
# All tokens: print([t.name for t in self.lark.parser.lexer.tokens])
def defaultPaper(self, style):
return QColor(39, 40, 34)
def language(self):
return "Json"
def description(self, style):
return {v: k for k, v in self.token_styles.items()}.get(style, "")
def styleText(self, start, end):
self.startStyling(start)
text = self.parent().text()[start:end]
last_pos = 0
try:
for token in self.lark.lex(text):
ws_len = token.pos_in_stream - last_pos
if ws_len:
self.setStyling(ws_len, 0) # whitespace
token_len = len(bytearray(token, "utf-8"))
self.setStyling(
token_len, self.token_styles.get(token.type, 0))
last_pos = token.pos_in_stream + token_len
except Exception as e:
print(e)
class EditorAll(QsciScintilla):
def __init__(self, parent=None):
super().__init__(parent)
# Set font defaults
font = QFont()
font.setFamily('Consolas')
font.setFixedPitch(True)
font.setPointSize(8)
font.setBold(True)
self.setFont(font)
# Set margin defaults
fontmetrics = QFontMetrics(font)
self.setMarginsFont(font)
self.setMarginWidth(0, fontmetrics.width("000") + 6)
self.setMarginLineNumbers(0, True)
self.setMarginsForegroundColor(QColor(128, 128, 128))
self.setMarginsBackgroundColor(QColor(39, 40, 34))
self.setMarginType(1, self.SymbolMargin)
self.setMarginWidth(1, 12)
# Set indentation defaults
self.setIndentationsUseTabs(False)
self.setIndentationWidth(4)
self.setBackspaceUnindents(True)
self.setIndentationGuides(True)
# Set folding defaults (http://www.scintilla.org/ScintillaDoc.html#Folding)
self.setFolding(QsciScintilla.CircledFoldStyle)
# Set caret defaults
self.setCaretForegroundColor(QColor(247, 247, 241))
self.setCaretWidth(2)
# Set selection color defaults
self.setSelectionBackgroundColor(QColor(61, 61, 52))
self.resetSelectionForegroundColor()
# Set multiselection defaults
self.SendScintilla(QsciScintilla.SCI_SETMULTIPLESELECTION, True)
self.SendScintilla(QsciScintilla.SCI_SETMULTIPASTE, 1)
self.SendScintilla(
QsciScintilla.SCI_SETADDITIONALSELECTIONTYPING, True)
lexer = LexerJson(self)
self.setLexer(lexer)
def main():
app = QApplication(sys.argv)
ex = EditorAll()
ex.setWindowTitle(__file__)
ex.setText(textwrap.dedent("""\
{
"_id": "5b05ffcbcf8e597939b3f5ca",
"about": "Excepteur consequat commodo esse voluptate aute aliquip ad sint deserunt commodo eiusmod irure. Sint aliquip sit magna duis eu est culpa aliqua excepteur ut tempor nulla. Aliqua ex pariatur id labore sit. Quis sit ex aliqua veniam exercitation laboris anim adipisicing. Lorem nisi reprehenderit ullamco labore qui sit ut aliqua tempor consequat pariatur proident.",
"address": "665 Malbone Street, Thornport, Louisiana, 243",
"age": 23,
"balance": "$3,216.91",
"company": "BULLJUICE",
"email": "elisekelley@bulljuice.com",
"eyeColor": "brown",
"gender": "female",
"guid": "d3a6d865-0f64-4042-8a78-4f53de9b0707",
"index": 0,
"isActive": false,
"isActive2": true,
"latitude": -18.660714,
"longitude": -85.378048,
"name": "Elise Kelley",
"phone": "+1 (808) 543-3966",
"picture": "http://placehold.it/32x32",
"registered": "2017-09-30T03:47:40 -02:00",
"tags": [
"et",
"nostrud",
"in",
"fugiat",
"incididunt",
"labore",
"nostrud"
]
}\
"""))
ex.resize(800, 600)
ex.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
要运行上述mcve,您只需运行pip install lark parser PyQt5 qscintula
我正在试图找出如何修改LexerJson
,以便符号[]{}
支持折叠。例如,当使用一个现有类(例如折叠行为是免费提供给您的)时,您只需执行以下操作:
# http://www.scintilla.org/ScintillaDoc.html#Folding
self.setFolding(QsciScintilla.BoxedTreeFoldStyle)
lexer = QsciLexerCPP()
lexer.setFoldAtElse(True)
lexer.setFoldComments(True)
lexer.setFoldCompact(False)
lexer.setFoldPreprocessor(True)
self.setLexer(lexer)
而折叠只会是免费的。。。但是当使用自定义lexer时,就像我在发布的mcve中所做的那样,我想你必须自己实现这种行为,不幸的是,我不知道如何实现它
所以,这基本上就是问题所在,如何在自定义子类上实现折叠?我无法修复您的lexer代码,但我可以为您提供一个相同的工作示例
import sys
from PyQt5.Qt import *
from PyQt5.Qsci import QsciScintilla, QsciLexerCPP
from PyQt5.Qsci import QsciLexerCustom
if sys.hexversion < 0x020600F0:
print('python 2.6 or greater is required by this program')
sys.exit(1)
_sample = """
# Sample config file
this is a junk line
[FirstItem]
Width=100
Height=200
Colour=orange
Info=this is some
multiline
text
[SecondItem]
Width=200
Height=300
Colour=green
Info=
this is some
multiline
text
"""
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle('Custom Lexer For Config Files')
self.setGeometry(50, 200, 400, 400)
self.editor = QsciScintilla(self)
self.editor.setUtf8(True)
self.editor.setMarginWidth(2, 15)
self.editor.setFolding(True)
self.setCentralWidget(self.editor)
self.lexer = ConfigLexer(self.editor)
self.editor.setLexer(self.lexer)
self.editor.setText(_sample)
class ConfigLexer(QsciLexerCustom):
def __init__(self, parent):
QsciLexerCustom.__init__(self, parent)
self._styles = {
0: 'Default',
1: 'Comment',
2: 'Section',
3: 'Key',
4: 'Assignment',
5: 'Value',
}
for key,value in self._styles.items():
setattr(self, value, key)
self._foldcompact = True
def foldCompact(self):
return self._foldcompact
def setFoldCompact(self, enable):
self._foldcompact = bool(enable)
def language(self):
return 'Config Files'
def description(self, style):
return self._styles.get(style, '')
def defaultColor(self, style):
if style == self.Default:
return QColor('#000000')
elif style == self.Comment:
return QColor('#A0A0A0')
elif style == self.Section:
return QColor('#CC6600')
elif style == self.Key:
return QColor('#0000CC')
elif style == self.Assignment:
return QColor('#CC0000')
elif style == self.Value:
return QColor('#00CC00')
return QsciLexerCustom.defaultColor(self, style)
def defaultPaper(self, style):
if style == self.Section:
return QColor('#FFEECC')
return QsciLexerCustom.defaultPaper(self, style)
def defaultEolFill(self, style):
if style == self.Section:
return True
return QsciLexerCustom.defaultEolFill(self, style)
def defaultFont(self, style):
if style == self.Comment:
if sys.platform in ('win32', 'cygwin'):
return QFont('Comic Sans MS', 9)
return QFont('Bitstream Vera Serif', 9)
return QsciLexerCustom.defaultFont(self, style)
def styleText(self, start, end):
editor = self.editor()
if editor is None:
return
SCI = editor.SendScintilla
GETFOLDLEVEL = QsciScintilla.SCI_GETFOLDLEVEL
SETFOLDLEVEL = QsciScintilla.SCI_SETFOLDLEVEL
HEADERFLAG = QsciScintilla.SC_FOLDLEVELHEADERFLAG
LEVELBASE = QsciScintilla.SC_FOLDLEVELBASE
NUMBERMASK = QsciScintilla.SC_FOLDLEVELNUMBERMASK
WHITEFLAG = QsciScintilla.SC_FOLDLEVELWHITEFLAG
set_style = self.setStyling
source = ''
if end > editor.length():
end = editor.length()
if end > start:
source = bytearray(end - start)
SCI(QsciScintilla.SCI_GETTEXTRANGE, start, end, source)
if not source:
return
compact = self.foldCompact()
index = SCI(QsciScintilla.SCI_LINEFROMPOSITION, start)
if index > 0:
pos = SCI(QsciScintilla.SCI_GETLINEENDPOSITION, index - 1)
state = SCI(QsciScintilla.SCI_GETSTYLEAT, pos)
else:
state = self.Default
self.startStyling(start, 0x1f)
for line in source.splitlines(True):
length = len(line)
if length == 1:
whitespace = compact
state = self.Default
else:
whitespace = False
firstchar = chr(line[0])
if firstchar in '#;':
state = self.Comment
elif firstchar == '[':
state = self.Section
elif firstchar in ' \t':
if state == self.Value or state == self.Assignment:
state = self.Value
else:
whitespace = compact and line.isspace()
state = self.Default
else:
pos = line.find(b'=')
if pos < 0:
pos = line.find(b':')
else:
tmp = line.find(b':', 0, pos)
if tmp >= 0:
pos = tmp
if pos > 0:
set_style(pos, self.Key)
set_style(1, self.Assignment)
length = length - pos - 1
state = self.Value
else:
state = self.Default
set_style(length, state)
if state == self.Section:
level = LEVELBASE | HEADERFLAG
elif index > 0:
lastlevel = SCI(GETFOLDLEVEL, index - 1)
if lastlevel & HEADERFLAG:
level = LEVELBASE + 1
else:
level = lastlevel & NUMBERMASK
else:
level = LEVELBASE
if whitespace:
level |= WHITEFLAG
if level != SCI(GETFOLDLEVEL, index):
SCI(SETFOLDLEVEL, index, level)
index += 1
if index > 0:
lastlevel = SCI(GETFOLDLEVEL, index - 1)
if lastlevel & HEADERFLAG:
level = LEVELBASE + 1
else:
level = lastlevel & NUMBERMASK
else:
level = LEVELBASE
lastlevel = SCI(GETFOLDLEVEL, index)
SCI(SETFOLDLEVEL, index, level | lastlevel & ~NUMBERMASK)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
PS:Credits to对答案有任何反馈吗?这是一个很好的例子,悬赏即将开始expire@TarunLalwani好吧,如果几个小时后没有更好的答案出现,我想我会赏金并验证你的答案,但考虑到这是一个好问题,你应该已经知道为什么你的答案没有获得更多的投票。它提供了关于一般问题的足够信息,但根本没有解决mcve,所以。。。顺便说一句,起初我认为我不是赏金/验证它,但后来我认为你的答案已经提供了足够的信息来解决我的真实单词示例(glsl lexer),所以。。。我会等几个小时,但万一有人给它拍了一张照片,问题是,它花了相当长的时间,甚至找到任何有关这方面的信息。经过大量挖掘,我发现了一个有效的例子。这就是我不想花更多时间为JSON问题创建一个可行的解决方案的原因。考虑到你花了相当多的时间来寻找这个例子,以及+1,所以把答案更多地作为一个指针来帮助你继续练习。谢谢你的回答
if state == self.Section:
level = LEVELBASE | HEADERFLAG
elif index > 0:
lastlevel = SCI(GETFOLDLEVEL, index - 1)
if lastlevel & HEADERFLAG:
level = LEVELBASE + 1
else:
level = lastlevel & NUMBERMASK
else:
level = LEVELBASE
if whitespace:
level |= WHITEFLAG
if level != SCI(GETFOLDLEVEL, index):
SCI(SETFOLDLEVEL, index, level)
index += 1
if index > 0:
lastlevel = SCI(GETFOLDLEVEL, index - 1)
if lastlevel & HEADERFLAG:
level = LEVELBASE + 1
else:
level = lastlevel & NUMBERMASK
else:
level = LEVELBASE
lastlevel = SCI(GETFOLDLEVEL, index)
SCI(SETFOLDLEVEL, index, level | lastlevel & ~NUMBERMASK)