This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

206 行
5.9 KiB

  1. """
  2. Syntax Highlighting
  3. ===================
  4. This example shows how to write a syntax-highlighted editor with Qt and Lark
  5. Requirements:
  6. PyQt5==5.10.1
  7. QScintilla==2.10.4
  8. """
  9. import sys
  10. import textwrap
  11. from PyQt5.Qt import * # noqa
  12. from PyQt5.Qsci import QsciScintilla
  13. from PyQt5.Qsci import QsciLexerCustom
  14. from lark import Lark
  15. class LexerJson(QsciLexerCustom):
  16. def __init__(self, parent=None):
  17. super().__init__(parent)
  18. self.create_parser()
  19. self.create_styles()
  20. def create_styles(self):
  21. deeppink = QColor(249, 38, 114)
  22. khaki = QColor(230, 219, 116)
  23. mediumpurple = QColor(174, 129, 255)
  24. mediumturquoise = QColor(81, 217, 205)
  25. yellowgreen = QColor(166, 226, 46)
  26. lightcyan = QColor(213, 248, 232)
  27. darkslategrey = QColor(39, 40, 34)
  28. styles = {
  29. 0: mediumturquoise,
  30. 1: mediumpurple,
  31. 2: yellowgreen,
  32. 3: deeppink,
  33. 4: khaki,
  34. 5: lightcyan
  35. }
  36. for style, color in styles.items():
  37. self.setColor(color, style)
  38. self.setPaper(darkslategrey, style)
  39. self.setFont(self.parent().font(), style)
  40. self.token_styles = {
  41. "COLON": 5,
  42. "COMMA": 5,
  43. "LBRACE": 5,
  44. "LSQB": 5,
  45. "RBRACE": 5,
  46. "RSQB": 5,
  47. "FALSE": 0,
  48. "NULL": 0,
  49. "TRUE": 0,
  50. "STRING": 4,
  51. "NUMBER": 1,
  52. }
  53. def create_parser(self):
  54. grammar = '''
  55. anons: ":" "{" "}" "," "[" "]"
  56. TRUE: "true"
  57. FALSE: "false"
  58. NULL: "NULL"
  59. %import common.ESCAPED_STRING -> STRING
  60. %import common.SIGNED_NUMBER -> NUMBER
  61. %import common.WS
  62. %ignore WS
  63. '''
  64. self.lark = Lark(grammar, parser=None, lexer='standard')
  65. # All tokens: print([t.name for t in self.lark.parser.lexer.tokens])
  66. def defaultPaper(self, style):
  67. return QColor(39, 40, 34)
  68. def language(self):
  69. return "Json"
  70. def description(self, style):
  71. return {v: k for k, v in self.token_styles.items()}.get(style, "")
  72. def styleText(self, start, end):
  73. self.startStyling(start)
  74. text = self.parent().text()[start:end]
  75. last_pos = 0
  76. try:
  77. for token in self.lark.lex(text):
  78. ws_len = token.pos_in_stream - last_pos
  79. if ws_len:
  80. self.setStyling(ws_len, 0) # whitespace
  81. token_len = len(bytearray(token, "utf-8"))
  82. self.setStyling(
  83. token_len, self.token_styles.get(token.type, 0))
  84. last_pos = token.pos_in_stream + token_len
  85. except Exception as e:
  86. print(e)
  87. class EditorAll(QsciScintilla):
  88. def __init__(self, parent=None):
  89. super().__init__(parent)
  90. # Set font defaults
  91. font = QFont()
  92. font.setFamily('Consolas')
  93. font.setFixedPitch(True)
  94. font.setPointSize(8)
  95. font.setBold(True)
  96. self.setFont(font)
  97. # Set margin defaults
  98. fontmetrics = QFontMetrics(font)
  99. self.setMarginsFont(font)
  100. self.setMarginWidth(0, fontmetrics.width("000") + 6)
  101. self.setMarginLineNumbers(0, True)
  102. self.setMarginsForegroundColor(QColor(128, 128, 128))
  103. self.setMarginsBackgroundColor(QColor(39, 40, 34))
  104. self.setMarginType(1, self.SymbolMargin)
  105. self.setMarginWidth(1, 12)
  106. # Set indentation defaults
  107. self.setIndentationsUseTabs(False)
  108. self.setIndentationWidth(4)
  109. self.setBackspaceUnindents(True)
  110. self.setIndentationGuides(True)
  111. # self.setFolding(QsciScintilla.CircledFoldStyle)
  112. # Set caret defaults
  113. self.setCaretForegroundColor(QColor(247, 247, 241))
  114. self.setCaretWidth(2)
  115. # Set selection color defaults
  116. self.setSelectionBackgroundColor(QColor(61, 61, 52))
  117. self.resetSelectionForegroundColor()
  118. # Set multiselection defaults
  119. self.SendScintilla(QsciScintilla.SCI_SETMULTIPLESELECTION, True)
  120. self.SendScintilla(QsciScintilla.SCI_SETMULTIPASTE, 1)
  121. self.SendScintilla(
  122. QsciScintilla.SCI_SETADDITIONALSELECTIONTYPING, True)
  123. lexer = LexerJson(self)
  124. self.setLexer(lexer)
  125. EXAMPLE_TEXT = textwrap.dedent("""\
  126. {
  127. "_id": "5b05ffcbcf8e597939b3f5ca",
  128. "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.",
  129. "address": "665 Malbone Street, Thornport, Louisiana, 243",
  130. "age": 23,
  131. "balance": "$3,216.91",
  132. "company": "BULLJUICE",
  133. "email": "elisekelley@bulljuice.com",
  134. "eyeColor": "brown",
  135. "gender": "female",
  136. "guid": "d3a6d865-0f64-4042-8a78-4f53de9b0707",
  137. "index": 0,
  138. "isActive": false,
  139. "isActive2": true,
  140. "latitude": -18.660714,
  141. "longitude": -85.378048,
  142. "name": "Elise Kelley",
  143. "phone": "+1 (808) 543-3966",
  144. "picture": "http://placehold.it/32x32",
  145. "registered": "2017-09-30T03:47:40 -02:00",
  146. "tags": [
  147. "et",
  148. "nostrud",
  149. "in",
  150. "fugiat",
  151. "incididunt",
  152. "labore",
  153. "nostrud"
  154. ]
  155. }\
  156. """)
  157. def main():
  158. app = QApplication(sys.argv)
  159. ex = EditorAll()
  160. ex.setWindowTitle(__file__)
  161. ex.setText(EXAMPLE_TEXT)
  162. ex.resize(800, 600)
  163. ex.show()
  164. sys.exit(app.exec_())
  165. if __name__ == "__main__":
  166. main()