|
21 | 21 | from PySide2.QtWidgets import QWidget, QTextEdit, QPlainTextEdit
|
22 | 22 | from PySide2.QtGui import (QPainter, QFont, QSyntaxHighlighter, QTextFormat, QTextCharFormat, QColor, QTextCursor)
|
23 | 23 | from binaryninjaui import (getMonospaceFont, getThemeColor, ThemeColor)
|
24 |
| -from pygments import highlight, token |
25 |
| -from pygments.lexers import * |
26 |
| -from pygments.formatter import Formatter |
| 24 | +try: |
| 25 | + from pygments import highlight, token |
| 26 | + from pygments.lexers import * |
| 27 | + from pygments.formatter import Formatter |
| 28 | + |
| 29 | + class QFormatter(Formatter): |
| 30 | + |
| 31 | + def __init__(self): |
| 32 | + Formatter.__init__(self) |
| 33 | + self.pygstyles={} |
| 34 | + for token, style in self.style: |
| 35 | + tokenname = str(token) |
| 36 | + if tokenname in bnstyles.keys(): |
| 37 | + self.pygstyles[str(token)]=bnstyles[tokenname] |
| 38 | + #log_warn("MATCH: %s with %s" % (tokenname, str(token))) |
| 39 | + else: |
| 40 | + self.pygstyles[str(token)]=bnstyles['Token.Name'] |
| 41 | + #log_warn("NONE: %s with %s" % (tokenname, str(token))) |
| 42 | + |
| 43 | + def format(self, tokensource, outfile): |
| 44 | + self.data=[] |
| 45 | + for token, value in tokensource: |
| 46 | + self.data.extend([self.pygstyles[str(token)],]*len(value)) |
| 47 | + |
| 48 | + class Pylighter(QSyntaxHighlighter): |
| 49 | + |
| 50 | + def __init__(self, parent, lang): |
| 51 | + QSyntaxHighlighter.__init__(self, parent) |
| 52 | + self.formatter=QFormatter() |
| 53 | + self.lexer=get_lexer_by_name(lang) |
| 54 | + |
| 55 | + def highlightBlock(self, text): |
| 56 | + cb = self.currentBlock() |
| 57 | + p = cb.position() |
| 58 | + text=self.document().toPlainText() |
| 59 | + highlight(text,self.lexer,self.formatter) |
| 60 | + |
| 61 | + #dirty, dirty hack |
| 62 | + for i in range(len(text)): |
| 63 | + try: |
| 64 | + self.setFormat(i,1,self.formatter.data[p+i]) |
| 65 | + except IndexError: |
| 66 | + pass |
| 67 | + |
| 68 | +except: |
| 69 | + log_warn("Pygments not installed, no syntax highlighting enabled.") |
| 70 | + Pylighter=None |
27 | 71 |
|
28 | 72 |
|
29 | 73 | def bnformat(color, style=''):
|
@@ -103,45 +147,6 @@ def bnformat(color, style=''):
|
103 | 147 | 'blockNormal': getThemeColor(ThemeColor.TokenSelectionColor),
|
104 | 148 | }
|
105 | 149 |
|
106 |
| -class QFormatter(Formatter): |
107 |
| - |
108 |
| - def __init__(self): |
109 |
| - Formatter.__init__(self) |
110 |
| - self.pygstyles={} |
111 |
| - for token, style in self.style: |
112 |
| - tokenname = str(token) |
113 |
| - if tokenname in bnstyles.keys(): |
114 |
| - self.pygstyles[str(token)]=bnstyles[tokenname] |
115 |
| - #log_warn("MATCH: %s with %s" % (tokenname, str(token))) |
116 |
| - else: |
117 |
| - self.pygstyles[str(token)]=bnstyles['Token.Name'] |
118 |
| - #log_warn("NONE: %s with %s" % (tokenname, str(token))) |
119 |
| - |
120 |
| - def format(self, tokensource, outfile): |
121 |
| - self.data=[] |
122 |
| - for token, value in tokensource: |
123 |
| - self.data.extend([self.pygstyles[str(token)],]*len(value)) |
124 |
| - |
125 |
| -class Pylighter(QSyntaxHighlighter): |
126 |
| - |
127 |
| - def __init__(self, parent, lang): |
128 |
| - QSyntaxHighlighter.__init__(self, parent) |
129 |
| - self.formatter=QFormatter() |
130 |
| - self.lexer=get_lexer_by_name(lang) |
131 |
| - |
132 |
| - def highlightBlock(self, text): |
133 |
| - cb = self.currentBlock() |
134 |
| - p = cb.position() |
135 |
| - text=self.document().toPlainText() |
136 |
| - highlight(text,self.lexer,self.formatter) |
137 |
| - |
138 |
| - #dirty, dirty hack |
139 |
| - for i in range(len(text)): |
140 |
| - try: |
141 |
| - self.setFormat(i,1,self.formatter.data[p+i]) |
142 |
| - except IndexError: |
143 |
| - pass |
144 |
| - |
145 | 150 |
|
146 | 151 | class QCodeEditor(QPlainTextEdit):
|
147 | 152 | class NumberBar(QWidget):
|
|
0 commit comments