This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

202 rader
5.9 KiB

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