| 1 |
from . import * |
| 2 |
|
| 3 |
from .python import highlightPython |
| 4 |
|
| 5 |
STYLES = { |
| 6 |
'comment': textformat('blue'), |
| 7 |
'keyword': textformat('brown', 'bold'), |
| 8 |
'argument': textformat('green'), |
| 9 |
'error': textformat('white', 'bg:red'), |
| 10 |
'skipped': textformat('gray'), |
| 11 |
} |
| 12 |
|
| 13 |
MULTILINE_PYTHON = 0x01000 |
| 14 |
MULTILINE_EMBED = 0x02000 |
| 15 |
MULTILINE_SKIP = 0x04000 |
| 16 |
MULTILINE = 0x0F000 |
| 17 |
|
| 18 |
CONTINUED_PYTHON = 0x10000 |
| 19 |
CONTINUED = 0xF0000 |
| 20 |
|
| 21 |
multiline_command = { |
| 22 |
MULTILINE_PYTHON: 'python', |
| 23 |
MULTILINE_EMBED: 'embed', |
| 24 |
MULTILINE_SKIP: 'skip', |
| 25 |
} |
| 26 |
|
| 27 |
multiline_state = dict((v, k) for (k, v) in multiline_command.items()) |
| 28 |
|
| 29 |
|
| 30 |
class Highlighter(QtGui.QSyntaxHighlighter): |
| 31 |
""" |
| 32 |
Syntax highlighter for the PyMOL command language. |
| 33 |
""" |
| 34 |
|
| 35 |
def highlightBlock(self, text): |
| 36 |
import re |
| 37 |
from pymol import cmd |
| 38 |
from .pmlparser import parse_pml |
| 39 |
|
| 40 |
state = max(0, self.previousBlockState()) |
| 41 |
state_python = -1 |
| 42 |
|
| 43 |
if state & MULTILINE: |
| 44 |
com = multiline_command[state & MULTILINE] |
| 45 |
if re.match(r'(_ )?[ \t]*' + com + r' end\b', text) is not None: |
| 46 |
self.setFormat(0, len(text), STYLES['keyword']) |
| 47 |
self.setCurrentBlockState(0) |
| 48 |
elif state & MULTILINE_PYTHON: |
| 49 |
state = highlightPython(self, state & ~MULTILINE, text) |
| 50 |
self.setCurrentBlockState(state | MULTILINE_PYTHON) |
| 51 |
else: |
| 52 |
self.setFormat(0, len(text), STYLES['skipped']) |
| 53 |
self.setCurrentBlockState(state) |
| 54 |
|
| 55 |
return |
| 56 |
|
| 57 |
if state & CONTINUED: |
| 58 |
if state & CONTINUED_PYTHON: |
| 59 |
state = highlightPython(self, state & ~CONTINUED, text) |
| 60 |
state |= CONTINUED_PYTHON |
| 61 |
|
| 62 |
if not text.endswith('\\'): |
| 63 |
state = 0 |
| 64 |
|
| 65 |
self.setCurrentBlockState(state) |
| 66 |
return |
| 67 |
|
| 68 |
for hit in parse_pml(text): |
| 69 |
self.setCurrentBlockState(0) |
| 70 |
state_python = -1 |
| 71 |
|
| 72 |
if hit['type'] == 'comment': |
| 73 |
i, j = hit['expr'] |
| 74 |
self.setFormat(i, j - i, STYLES['comment']) |
| 75 |
elif hit['type'] == 'python': |
| 76 |
i = hit['args'][0] |
| 77 |
j = hit['expr'][1] |
| 78 |
if j > i and text[min(j, len(text)) - 1] == ':': |
| 79 |
# blocks not supported |
| 80 |
self.setFormat(i, j - i, STYLES['error']) |
| 81 |
else: |
| 82 |
state_python = highlightPython(self, 0, text, i, j) |
| 83 |
elif hit['type'] == 'command': |
| 84 |
i, j = hit['command'] |
| 85 |
self.setFormat(i, j - i, STYLES['keyword']) |
| 86 |
|
| 87 |
com = text[i:j] |
| 88 |
args_i = hit['args'] |
| 89 |
i = args_i[0] |
| 90 |
|
| 91 |
# arguments |
| 92 |
for aa, j in zip(cmd.auto_arg, args_i[1:]): |
| 93 |
aa = aa.get(com) |
| 94 |
if aa is not None: |
| 95 |
keywords = aa[0]().keywords |
| 96 |
for m in re.finditer(r'\S+', text[i:j]): |
| 97 |
mi = i + m.start() |
| 98 |
mj = i + m.end() |
| 99 |
arg = text[mi:mj] |
| 100 |
if arg in keywords: |
| 101 |
self.setFormat(mi, mj - mi, STYLES['argument']) |
| 102 |
|
| 103 |
i = j + 1 |
| 104 |
|
| 105 |
# warn about semantic errors |
| 106 |
if com in ('@', 'run'): |
| 107 |
mi, mj = args_i[:2] |
| 108 |
filename = text[mi:mj] |
| 109 |
badext = '.py' if com == '@' else '.pml' |
| 110 |
if filename.rstrip().endswith(badext): |
| 111 |
self.setFormat(mi, mj - mi, STYLES['error']) |
| 112 |
|
| 113 |
self.setCurrentBlockState(multiline_state.get(com, -1)) |
| 114 |
|
| 115 |
if text.endswith('\\'): |
| 116 |
if state_python != -1: |
| 117 |
self.setCurrentBlockState(CONTINUED_PYTHON | state_python) |
| 118 |
else: |
| 119 |
self.setCurrentBlockState(CONTINUED) |