-
Notifications
You must be signed in to change notification settings - Fork 29
/
editor.py
executable file
·152 lines (115 loc) · 4.94 KB
/
editor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python3
import sys
import os
import logging
import argparse
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QVBoxLayout, QWidget
from PyQt5.QtGui import QPalette
def _parseCommandLine():
parser = argparse.ArgumentParser(description='Qutepart test application')
parser.add_argument('-b, --binary', action='store_true', dest='binary',
help='Use binary parser. Do ./setup.py build before using this flag')
parser.add_argument('-d, --debug', action='store_true', dest='debug', help='Enable debug output')
parser.add_argument('-q, --quit', action='store_true', dest='quit', help='Quit just after start')
parser.add_argument('-l, --language', action='store_true', dest='show_language',
help='Print detected language when starting')
parser.add_argument('-c, --completions', dest='completions',
help='A comma separated list of additional word completions')
parser.add_argument('file', help='File to open')
return parser.parse_args()
def _fixSysPath(binaryQutepart):
executablePath = os.path.abspath(__file__)
if executablePath.startswith('/home'): # if executed from the sources
qutepartDir = os.path.dirname(executablePath)
sys.path.insert(0, qutepartDir) # do not import installed modules
if binaryQutepart:
for arch in ('i686', 'x86_64'):
for versionMinor in range(3, 10):
dirPath = '{}/build/lib.linux-{}-3.{}/'.format(qutepartDir, arch, versionMinor)
sys.path.insert(0, dirPath) # use built modules
def main():
ns = _parseCommandLine()
_fixSysPath(ns.binary)
import qutepart # after correct sys.path has been set
if ns.binary and (not qutepart.binaryParserAvailable):
print("Failed to load binary parser")
return
with open(ns.file, encoding='utf-8') as file:
text = file.read()
if ns.debug:
logging.getLogger('qutepart').setLevel(logging.DEBUG)
app = QApplication(sys.argv)
window = QMainWindow()
widget = QWidget()
layout = QVBoxLayout(widget)
window.setCentralWidget(widget)
vimModeIndication = QLabel()
vimModeIndication.setAutoFillBackground(True)
layout.addWidget(vimModeIndication)
qpart = qutepart.Qutepart()
font = qpart.font()
font.setPointSize(12)
qpart.setFont(font)
qpart.vimModeEnabled = False
layout.addWidget(qpart)
def onVimModeChanged(color, text):
if color is not None:
palette = vimModeIndication.palette()
palette.setColor(QPalette.Window, color)
vimModeIndication.setPalette(palette)
vimModeIndication.setText(text)
qpart.vimModeIndicationChanged.connect(onVimModeChanged)
onVimModeChanged(*qpart.vimModeIndication)
firstLine = text.splitlines()[0] if text else None
qpart.detectSyntax(sourceFilePath=ns.file, firstLine=firstLine)
if ns.show_language:
print("Language:", qpart.language())
if ns.completions:
completions = {c.strip() for c in ns.completions.split(',')}
qpart.setCustomCompletions(completions)
qpart.lineLengthEdge = 20
qpart.drawIncorrectIndentation = True
qpart.drawAnyWhitespace = False
qpart.indentUseTabs = False
qpart.text = text
qpart.setWindowTitle(ns.file)
menu = {'File': ('printAction',),
'Bookmarks': ('toggleBookmarkAction',
'nextBookmarkAction',
'prevBookmarkAction'),
'Navigation':('scrollUpAction',
'scrollDownAction',
'selectAndScrollUpAction',
'selectAndScrollDownAction',
),
'Indentation':('increaseIndentAction',
'decreaseIndentAction',
'autoIndentLineAction',
'indentWithSpaceAction',
'unIndentWithSpaceAction'),
'Lines': ('moveLineUpAction',
'moveLineDownAction',
'deleteLineAction',
'copyLineAction',
'pasteLineAction',
'cutLineAction',
'duplicateLineAction'),
'Edit' : ('invokeCompletionAction',
'undoAction',
'redoAction'
)
}
for k, v in menu.items():
menuObject = window.menuBar().addMenu(k)
for actionName in v:
menuObject.addAction(getattr(qpart, actionName))
qpart.userWarning.connect(lambda text: window.statusBar().showMessage(text, 3000))
window.resize(800, 600)
window.show()
from PyQt5.QtCore import QTimer
if ns.quit:
QTimer.singleShot(0, app.quit)
app.exec_()
qpart.terminate()
if __name__ == '__main__':
main()