我正在尝试fold_by_level
在 QScintilla 组件上实现 SublimeText3 功能,但我不太清楚如何做到这一点,到目前为止我已经提出了以下代码:
import sys
import re
import math
from PyQt5.Qt import * # noqa
from PyQt5.Qsci import QsciScintilla
from PyQt5 import Qsci
from PyQt5.Qsci import QsciLexerCPP
class Foo(QsciScintilla):
def __init__(self, parent=None):
super().__init__(parent)
# http://www.scintilla.org/ScintillaDoc.html#Folding
self.setFolding(QsciScintilla.BoxedTreeFoldStyle)
# Indentation
self.setIndentationsUseTabs(False)
self.setIndentationWidth(4)
self.setBackspaceUnindents(True)
self.setIndentationGuides(True)
# Set the default font
self.font = QFont()
self.font.setFamily('Consolas')
self.font.setFixedPitch(True)
self.font.setPointSize(10)
self.setFont(self.font)
self.setMarginsFont(self.font)
# Margin 0 is used for line numbers
fontmetrics = QFontMetrics(self.font)
self.setMarginsFont(self.font)
self.setMarginWidth(0, fontmetrics.width("000") + 6)
self.setMarginLineNumbers(0, True)
self.setMarginsBackgroundColor(QColor("#cccccc"))
# Indentation
self.setIndentationsUseTabs(False)
self.setIndentationWidth(4)
self.setBackspaceUnindents(True)
lexer = QsciLexerCPP()
lexer.setFoldAtElse(True)
lexer.setFoldComments(True)
lexer.setFoldCompact(False)
lexer.setFoldPreprocessor(True)
self.setLexer(lexer)
QShortcut(QKeySequence("Ctrl+K, Ctrl+J"), self,
lambda level=-1: self.fold_by_level(level))
QShortcut(QKeySequence("Ctrl+K, Ctrl+1"), self,
lambda level=1: self.fold_by_level(level))
QShortcut(QKeySequence("Ctrl+K, Ctrl+2"), self,
lambda level=2: self.fold_by_level(level))
QShortcut(QKeySequence("Ctrl+K, Ctrl+3"), self,
lambda level=3: self.fold_by_level(level))
QShortcut(QKeySequence("Ctrl+K, Ctrl+4"), self,
lambda level=4: self.fold_by_level(level))
QShortcut(QKeySequence("Ctrl+K, Ctrl+5"), self,
lambda level=5: self.fold_by_level(level))
def fold_by_level(self, lvl):
if lvl < 0:
self.foldAll(True)
else:
for i in range(self.lines()):
level = self.SendScintilla(
QsciScintilla.SCI_GETFOLDLEVEL, i) & QsciScintilla.SC_FOLDLEVELNUMBERMASK
level -= 0x400
print(f"line={i+1}, level={level}")
if lvl == level:
self.foldLine(i)
def main():
app = QApplication(sys.argv)
ex = Foo()
ex.setText("""\
#include <iostream>
using namespace std;
void Function0() {
cout << "Function0";
}
void Function1() {
cout << "Function1";
}
void Function2() {
cout << "Function2";
}
void Function3() {
cout << "Function3";
}
int main(void) {
if (1) {
if (1) {
if (1) {
if (1) {
int yay;
}
}
}
}
if (1) {
if (1) {
if (1) {
if (1) {
int yay2;
}
}
}
}
return 0;
}\
""")
ex.resize(800, 600)
ex.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
我关注的文档是https://www.scintilla.org/ScintillaDoc.html#Folding和http://pyqt.sourceforge.net/Docs/QScintilla2/classQsciScintilla.html。
正如我所说,该fold_by_level
功能旨在与 SublimeText 完全一样,但我不确定 ST 的功能实现细节。无论如何,让我在 SublimeText 上测试一些基本序列之后发布一些屏幕截图,这可以澄清我在这里想要实现的目标:
序列1: {ctrl+k, ctrl+5}, {ctrl+k, ctrl+j} {ctrl+k, ctrl+4}, {ctrl+k, ctrl+j} {ctrl+k, ctrl+3}, {ctrl+k, ctrl+j} {ctrl+k, ctrl+2}, {ctrl+k, ctrl+j} {ctrl+k, ctrl+1}, {ctrl+k, ctrl+j}
序列2: {ctrl+k, ctrl+5}, {ctrl+k, ctrl+4}, {ctrl+k, ctrl+3}, {ctrl+k, ctrl+2}, {ctrl+k, ctrl+1}
我确信有更多关于 SublimeText 行为的内部细节,但如果我的示例在测试序列后表现得与在这些镜头上发布的完全一样,你可以说该功能已经变得非常方便使用。