168 lines
5.8 KiB
Python
168 lines
5.8 KiB
Python
import sys
|
|
|
|
color_list = []
|
|
color_list.extend(['\033[3%dm' % x for x in range(0, 8)])
|
|
color_list.extend(['\033[3%d;1m' % x for x in range(0, 8)])
|
|
color_list.append('\033[0m')
|
|
|
|
color_names = [
|
|
'black', 'dred', 'dgreen', 'brown', 'dblue', 'dpurple', 'dcyan', 'lgrey',
|
|
'dgrey', 'lred', 'lgreen', 'yellow', 'lblue', 'lpurple', 'lcyan', 'white',
|
|
'unset',
|
|
]
|
|
|
|
color_dict ={}
|
|
for i in range(0, len(color_list)):
|
|
color_dict[color_names[i]] = color_list[i]
|
|
|
|
class Highlighter:
|
|
def __init__(self, lexer):
|
|
self.lexer = lexer
|
|
self.tokens = []
|
|
|
|
def dump(self, fmt='(%3s, %2s) | %s'):
|
|
print fmt % ('y', 'x', 'string')
|
|
for group in self.tokens:
|
|
for token in group:
|
|
print fmt % (token.y, token.x, token.string)
|
|
|
|
def display(self, token_colors={}, debug=False):
|
|
for group in self.tokens:
|
|
for token in group:
|
|
color_name = None
|
|
name_parts = token.name.split('.')
|
|
for i in range(0, len(name_parts)):
|
|
if '.'.join(name_parts[i:]) in token_colors:
|
|
color_name = token_colors['.'.join(name_parts[i:])]
|
|
break
|
|
if color_name is not None:
|
|
sys.stdout.write(color_dict[color_name])
|
|
pass
|
|
elif debug:
|
|
raise Exception, "no highlighting for %r" % token.name
|
|
else:
|
|
color_name = 'white'
|
|
sys.stdout.write(color_dict[color_name])
|
|
sys.stdout.write(token.string)
|
|
sys.stdout.write('\n')
|
|
|
|
def highlight(self, lines):
|
|
self.tokens = [[] for l in lines]
|
|
self.lexer.lex(lines, y=0, x=0)
|
|
for token in self.lexer:
|
|
self.tokens[token.y].append(token)
|
|
|
|
def update_del(self, lines, y1, x1, y2, x2):
|
|
assert y1 >= 0
|
|
assert y1 <= y2
|
|
assert y2 < len(lines)
|
|
|
|
xdelta = x2 - x1
|
|
ydelta = y2 - y1
|
|
|
|
newtokens = [[] for x in range(0, len(self.tokens) - ydelta)]
|
|
for y in range(0, y1):
|
|
newtokens[y] = self.tokens[y]
|
|
|
|
for y in range(y1, len(lines)):
|
|
while self.tokens[y]:
|
|
token = self.tokens[y].pop(0)
|
|
tx1 = token.x
|
|
tx2 = token.x + len(token.string)
|
|
if (y, tx2) <= (y1, x1):
|
|
# *| |
|
|
newtokens[y].append(token)
|
|
elif (y, tx1) >= (y2, x2):
|
|
# | |*
|
|
token.y -= ydelta
|
|
if y == y2:
|
|
token.x -= xdelta
|
|
newtokens[token.y].append(token)
|
|
elif (y, tx1) < (y1, x1):
|
|
token2 = token.copy()
|
|
if (y, tx2) <= (y2, x2):
|
|
# *|*|
|
|
s = token2.string[:x1 - tx1]
|
|
else:
|
|
# *|*|*
|
|
s = token2.string[:x1 - tx1] + token2.string[x2 - tx1:]
|
|
token2.string = s
|
|
newtokens[y].append(token2)
|
|
elif (y, tx1) < (y2, x2):
|
|
if (y, tx2) <= (y2, x2):
|
|
# |*|
|
|
pass
|
|
else:
|
|
# |*|*
|
|
token2 = token.copy()
|
|
token2.x = x1
|
|
token2.y = token2.y - ydelta
|
|
token2.string = token2.string[x2 - tx1:]
|
|
newtokens[token2.y].append(token2)
|
|
self.tokens = newtokens
|
|
|
|
def relex_del(self, lines, y1, x1, y2, x2):
|
|
self.update_del(lines, y1, x1, y2, x2)
|
|
self.lexer.lex(lines, y1, 0)
|
|
|
|
y = y1
|
|
i = 0
|
|
getnext = True
|
|
|
|
while True:
|
|
if y >= len(lines):
|
|
break
|
|
|
|
if getnext:
|
|
try:
|
|
new_token = self.lexer.next()
|
|
getnext = False
|
|
except StopIteration:
|
|
for j in range(y, len(lines)):
|
|
print 'DELETE END ROW %d[%d:]: %r' % (j, i, [x.string for x in self.tokens[j][i:]])
|
|
del self.tokens[j][i:]
|
|
i = 0
|
|
break
|
|
|
|
# if our next token is one a future line, we need to just get rid of
|
|
# all our old tokens until we get there
|
|
while new_token.y > y:
|
|
print 'DELETE MID ROW %d[%d:]: %r' % (y, i, [x.string for x in self.tokens[y][i:]])
|
|
del self.tokens[y][i:]
|
|
i = 0
|
|
y += 1
|
|
|
|
if i < len(self.tokens[y]):
|
|
old_token = self.tokens[y][i]
|
|
assert old_token.y == y
|
|
else:
|
|
old_token = None
|
|
|
|
if old_token is None:
|
|
print 'DEFAULT INSERT %d[%d]: %r' % (y, i, new_token.string)
|
|
self.tokens[y].insert(i, new_token)
|
|
i += 1
|
|
getnext = True
|
|
continue
|
|
elif old_token == new_token:
|
|
print 'MATCH %d[%d]: %r == %r' % (y, i, old_token.string, new_token.string)
|
|
i += 1
|
|
getnext = True
|
|
if new_token.y >= y2 and new_token.end_x() >= x2:
|
|
break
|
|
else:
|
|
continue
|
|
elif old_token.x < new_token.end_x():
|
|
print 'DELETE BEFORE %d[%d]: %r' % (y, i, old_token.string)
|
|
del self.tokens[y][i]
|
|
continue
|
|
elif old_token.x >= new_token.end_x():
|
|
print 'INSERT %d[%d]: %r' % (y, i, new_token.string)
|
|
self.tokens[y].insert(i, new_token)
|
|
i += 1
|
|
getnext = True
|
|
continue
|
|
else:
|
|
raise Exception, "what what?"
|
|
|