pmacs3/tab.py

477 lines
16 KiB
Python
Raw Normal View History

2007-10-21 20:55:43 -04:00
import regex, util
from point import Point
2008-03-14 17:17:04 -04:00
class Marker(object):
def __init__(self, name, level, y):
2007-10-21 20:55:43 -04:00
self.name = name
self.level = level
self.y = y
2007-10-21 20:55:43 -04:00
def __repr__(self):
return '<Marker(%r, %r, %r)>' % (self.name, self.level, self.y)
2007-10-21 20:55:43 -04:00
2008-03-14 17:17:04 -04:00
class Tabber(object):
2007-10-21 20:55:43 -04:00
wsre = regex.whitespace
2008-09-30 01:47:20 -04:00
wst = ('spaces', 'null', 'eol',)
2007-10-21 20:55:43 -04:00
sre = regex.space
2008-09-30 01:47:20 -04:00
st = ('spaces', 'null',)
2007-10-21 20:55:43 -04:00
def __init__(self, m):
self.mode = m
self.lines = {}
def get_highlighter(self):
return self.mode.window.buffer.highlights[self.mode.name]
2007-10-21 20:55:43 -04:00
def get_tokens(self, y):
return self.mode.window.buffer.highlights[self.mode.name].tokens[y]
2007-10-21 20:55:43 -04:00
def get_token(self, y, i):
return self.mode.window.buffer.highlights[self.mode.name].tokens[y][i]
2007-10-21 20:55:43 -04:00
def token_is_whitespace(self, y, i):
token = self.get_token(y, i)
return token.fqname() in self.wst and self.wsre.match(token.string)
def token_is_space(self, y, i):
token = self.get_token(y, i)
return token.fqname() in self.st and self.sre.match(token.string)
def get_next_left_token(self, y, i):
tokens = self.get_tokens(y)
assert i >= 0 and i < len(tokens)
for j in range(1, i):
if not self.token_is_whitespace(y, i - j):
return tokens[i - j]
return None
def get_next_right_token(self, y, i):
tokens = self.get_tokens(y)
assert i >= 0 and i < len(tokens)
for j in range(i + 1, len(tokens)):
if not self.token_is_whitespace(y, j):
return tokens[j]
return None
def is_leftmost_token(self, y, i):
return self.get_next_left_token(y, i) is None
def is_rightmost_token(self, y, i):
return self.get_next_right_token(y, i) is None
def is_only_token(self, y, i):
return self.is_leftmost_token(y, i) and self.is_rightmost_token(y, i)
def get_leftmost_token(self, y):
tokens = self.get_tokens(y)
for i in range(0, len(tokens)):
if not self.token_is_whitespace(y, i):
return tokens[i]
return None
def get_rightmost_token(self, y):
tokens = self.get_tokens(y)
i = len(tokens) - 1
for j in range(0, len(tokens)):
if not self.token_is_whitespace(y, i - j):
return tokens[i - j]
return None
def get_nonws_tokens(self, y):
tokens = self.get_tokens(y)
for i in range(0, len(tokens)):
if not self.token_is_whitespace(y, i):
yield tokens[i]
raise StopIteration
def get_nons_tokens(self, y):
tokens = self.get_tokens(y)
for i in range(0, len(tokens)):
if not self.token_is_space(y, i):
yield tokens[i]
raise StopIteration
def region_added(self, p, newlines):
self.lines = {}
def region_removed(self, p1, p2):
self.lines = {}
def is_base(self, y):
return True
def get_level(self, y):
if y in self.lines:
return self.lines[y]
else:
self._calc_level(y)
return self.lines.get(y)
def _calc_level(self, y):
pass
class StackTabber(Tabber):
def __init__(self, m):
self.mode = m
self.lines = {}
self.record = {}
self.markers = []
def get_curr_level(self):
if self.markers:
return self.markers[-1].level
else:
return 0
def region_added(self, p, newlines):
self.lines = {}
self.record = {}
self.markers = []
def region_removed(self, p1, p2):
self.lines = {}
self.record = {}
self.markers = []
def is_base(self, y):
return y == 0
def _calc_level(self, y):
# first we need to step back to find the last place where we have tab
# stops figured out, or a suitable place to start
target = y
while not self.is_base(y) and y > 0:
y -= 1
# ok now, let's do this shit
self.markers = []
currlvl = 0
while y <= target:
currlvl = self.get_curr_level()
tokens = self.get_tokens(y)
for i in range(0, len(tokens)):
currlvl = self._handle_token(currlvl, y, i)
self.lines[y] = currlvl
self.record[y] = tuple(self.markers)
y += 1
def _handle_token(self, currlvl, y, i):
token = self.get_token(y, i)
s = token.string
fqname = token.fqname()
if fqname in self.mode.closetokens and s in self.mode.closetags:
currlvl = self._handle_close_token(currlvl, y, i)
elif fqname in self.mode.opentokens and s in self.mode.opentags:
currlvl = self._handle_open_token(currlvl, y, i)
else:
currlvl = self._handle_other_token(currlvl, y, i)
return currlvl
def _handle_open_token(self, currlvl, y, i):
token = self.get_token(y, i)
rtoken = self.get_next_right_token(y, i)
if rtoken is None:
level = self.get_curr_level() + self.mode.tabwidth
else:
level = rtoken.x
self._append(token.string, level, y)
2007-10-21 20:55:43 -04:00
return currlvl
def _handle_close_token(self, currlvl, y, i):
token = self.get_token(y, i)
s1 = token.string
if not self.markers:
raise Exception, "unmatched closing token %r" % s1
s2 = self.markers[-1].name
if self.mode.closetags[s1] == s2:
self._pop()
if self.is_leftmost_token(y, i):
currlvl = self.get_curr_level()
else:
raise Exception, "mismatched closing tag %r vs %r" % (s2, s1)
return currlvl
def _handle_other_token(self, currlvl, y, i):
return currlvl
def _has_markers(self):
return len(self.markers) > 0
def _empty(self):
return len(self.markers) == 0
def _append(self, name, level, y=None):
self.markers.append(Marker(name, level, y))
2007-10-21 20:55:43 -04:00
def _peek(self):
2008-05-29 19:05:15 -04:00
if self.markers:
return self.markers[-1]
else:
return None
2008-10-22 18:25:45 -04:00
def _peek_until(self, *names):
for i in range(1, len(self.markers) + 1):
x = self.markers[-i]
if x.name in names:
return x
return None
2007-10-21 20:55:43 -04:00
def _peek_name(self):
2008-05-29 19:05:15 -04:00
if self.markers:
return self.markers[-1].name
else:
return None
2007-10-21 20:55:43 -04:00
def _peek_level(self):
2008-05-29 19:05:15 -04:00
if self.markers:
return self.markers[-1].level
else:
return None
2007-10-21 20:55:43 -04:00
def _pop(self):
self.markers.pop(-1)
2008-05-29 19:05:15 -04:00
def _pop_while(self, *names):
while self.markers and self.markers[-1].name in names:
self.markers.pop(-1)
def _pop_unless(self, *names):
if self.markers and self.markers[-1].name not in names:
self.markers.pop(-1)
2007-10-21 20:55:43 -04:00
def _pop_until(self, *names):
while self.markers:
if self.markers[-1].name in names:
self.markers.pop(-1)
return
else:
self.markers.pop(-1)
def _opt_append(self, name, level, y=None):
2007-10-21 20:55:43 -04:00
if self.markers and self.markers[-1].name == name:
pass
else:
self._append(name, level, y)
2007-10-21 20:55:43 -04:00
def _opt_pop(self, *names):
if self.markers and self.markers[-1].name in names:
self.markers.pop(-1)
2008-10-02 12:49:22 -04:00
class Marker2(object):
def __init__(self, name, type_, level, y=None):
2008-10-02 12:49:22 -04:00
self.name = name
self.type_ = type_
self.level = level
self.y = y
2008-10-02 12:49:22 -04:00
def __repr__(self):
return '<Marker2(%r, %r, %r, %r)>' % (self.name, self.type_, self.level, self.y)
2008-10-02 12:49:22 -04:00
class StackTabber2(Tabber):
is_ignored_tokens = ('spaces', 'eol', 'comment')
is_indent_tokens = ('spaces',)
2008-10-02 16:06:16 -04:00
open_tokens = {'delimiter': {'{': '}', '(': ')', '[': ']'}}
close_tokens = {'delimiter': set(['}', ')', ']'])}
2008-11-17 22:31:05 -05:00
open_scope_tokens = {'delimiter': set(['{'])}
close_scope_tokens = {'delimiter': set(['}'])}
control_tokens = {'keyword': set(['if', 'else', 'while', 'do', 'for'])}
2008-10-02 16:06:16 -04:00
end_at_eof = True
end_at_tokens = {}
continue_tokens = {}
nocontinue_tokens = {}
start_free_tokens = {'string.start': 'string.end'}
end_free_tokens = {'string.end': 'string.start'}
start_macro_tokens = {}
end_macro_tokens = {}
fixed_indent = False
2008-10-02 12:49:22 -04:00
def __init__(self, m):
self.mode = m
self.name = m.name
2008-10-02 12:49:22 -04:00
self.lines = {}
self._reset()
def region_added(self, p, newlines):
self.lines = {}
def region_removed(self, p1, p2):
self.lines = {}
def get_level(self, y):
if y not in self.lines:
self._calc_level(y)
return self.lines.get(y)
def _calc_level(self, y):
target = y
while not self._is_base(y) and y > 0:
y -= 1
self._reset()
while y <= target:
self._save_curr_level()
self._handle_tokens(y)
y += 1
def _is_base(self, y):
return y == 0
def _reset(self):
self.record = {}
self.stack = []
self.markers = self.stack
self.curr_level = 0
def _get_curr_level(self):
if self.stack:
return self.stack[-1].level
else:
return 0
def _get_next_level(self):
curr = self._get_curr_level()
if curr is None: return None
return curr + self.mode.tabwidth
2008-10-02 12:49:22 -04:00
def _save_curr_level(self):
self.curr_level = self._get_curr_level()
def _match(self, *names):
return self.stack and self.stack[-1].name in names
def _nomatch(self, *names):
return not self.stack or self.stack[-1].name not in names
2008-10-02 12:49:22 -04:00
def _pop(self, *names):
if self._match(*names):
self.stack.pop()
def _pop_while(self, *names):
while self._match(*names):
self.stack.pop()
def _pop_until(self, *names):
while self._nomatch(*names):
self.stack.pop()
def _append(self, name, type_, level, y=None):
2009-02-05 12:12:52 -05:00
self.stack.append(Marker2(name, type_, level, y))
def _append_unless(self, name, type_, level, y=None):
2008-10-02 12:49:22 -04:00
if self._nomatch(name):
2009-02-05 12:12:52 -05:00
self.stack.append(Marker2(name, type_, level, y))
2008-10-02 12:49:22 -04:00
def _peek(self):
if self.stack:
return self.stack[-1]
else:
return None
2008-10-22 18:25:45 -04:00
def _peek_until(self, *names):
for i in range(1, len(self.stack) + 1):
x = self.stack[-i]
if x.name in names:
return x
return None
2008-10-02 12:49:22 -04:00
def _get_tokens(self, y):
return self.mode.window.buffer.highlights[self.name].tokens[y]
def _handle_tokens(self, y):
tokens = self._get_tokens(y)
assert tokens
start = int(self._is_indent(tokens[0]))
end = len(tokens) - 1
while end > 0 and self._is_ignored(tokens[end]):
end -= 1
for i in range(0, end + 1 - start):
t = tokens[start + i]
if self._is_ignored(t):
pass
elif self._is_close_token(t):
self._handle_close_token(y, tokens, start, end, i, t)
elif self._is_open_token(t):
self._handle_open_token(y, tokens, start, end, i, t)
else:
self._handle_other_token(y, tokens, start, end, i, t)
self.lines[y] = self.curr_level
self.record[y] = tuple(self.stack)
def _is_indent(self, t):
return t.name in self.is_indent_tokens
2008-10-02 12:49:22 -04:00
def _is_ignored(self, t):
return t.name in self.is_ignored_tokens
2008-10-02 12:49:22 -04:00
def _is_close_token(self, t):
return t.string in self.close_tokens.get(t.name, set())
2008-10-02 12:49:22 -04:00
def _handle_close_token(self, y, tokens, start, end, i, t):
if not self.stack:
raise Exception, "unmatched %r, line %d" % (t.string, y)
while True:
marker = self.stack[-1]
if marker.name in ('control', 'continue'):
self.stack.pop()
elif marker.name in self.open_tokens[marker.type_]:
s = self.open_tokens[marker.type_][marker.name]
if s in (None, t.string):
2008-10-02 12:49:22 -04:00
self.stack.pop()
break
else:
msg = "mismatched %r, line %d (expected %r)"
raise Exception, msg % (t.string, y, s)
2008-10-02 12:49:22 -04:00
else:
raise Exception, "what? %r" % marker.name
if i == 0:
self._save_curr_level()
# if we don't want implicit continuation, return.
if self.end_at_eof:
return
# ok, if we are closing a block then this will end a "single-statement"
# block. e.g. else switch() { ... }
if t.string in self.close_scope_tokens.get(t.fqname(), set()):
self._pop('control')
# if we do want implicit continuation, see if we need it.
name, s = t.fqname(), t.string
2008-11-17 22:31:05 -05:00
top = self._peek()
atscope = True
if top:
d = self.open_scope_tokens.get(top.type_, set())
atscope = top.name in d
if (atscope and i + start == end):
d = self.nocontinue_tokens.get(name)
if d is None or d != 1 and s not in d:
2008-11-17 22:31:05 -05:00
if s not in self.close_scope_tokens.get(name, set()):
nextlvl = self._get_next_level()
self._append_unless('continue', name, nextlvl, y)
2008-10-02 12:49:22 -04:00
def _is_open_token(self, t):
return t.string in self.open_tokens.get(t.name, set())
2008-10-02 12:49:22 -04:00
def _handle_open_token(self, y, tokens, start, end, i, t):
if i == 0 and self.stack and self.stack[-1].name == 'continue':
self.stack.pop()
2008-11-17 22:31:05 -05:00
if t.string in self.open_scope_tokens.get(t.name, set()):
#self._pop_while('continue', 'control')
#self._pop_while('continue')
self._pop('continue', 'control')
2008-11-17 22:31:05 -05:00
if i == 0 and t.string in self.open_scope_tokens.get(t.name, set()):
self._save_curr_level()
if i == end - start or self.fixed_indent:
2008-10-02 12:49:22 -04:00
level = self._get_next_level()
else:
level = tokens[i + 1].x
# i'm not sure why this is necessary; seems like there is a bug
# somewhere else that this is compensating for.
if self.curr_level > 0:
level += 1
self._append(t.string, t.name, level, y)
2008-10-02 12:49:22 -04:00
def _handle_other_token(self, y, tokens, start, end, i, t):
name, s = t.fqname(), t.string
2008-10-02 16:06:16 -04:00
# handle "free" tokens (strings, heredocs, etc)
2008-10-02 12:49:22 -04:00
if name in self.start_free_tokens:
self._append('free', name, None, y)
2008-10-02 12:49:22 -04:00
return
elif name in self.end_free_tokens:
self._pop('free')
2008-10-02 16:06:16 -04:00
# handle macros
if name in self.start_macro_tokens:
self._append('macro', name, 0, y)
2008-10-02 16:06:16 -04:00
if i == 0:
self._save_curr_level()
return
elif name in self.end_macro_tokens:
self._pop('macro')
return
# remove implicit continuation
2008-10-02 12:49:22 -04:00
if self.end_at_eof and i + start == end:
self._pop_while('continue', 'control')
elif self.end_at_tokens.get(name, {}).get(s):
self._pop_while('continue', 'control')
2008-10-02 16:06:16 -04:00
# add implicit continuation
2008-10-02 12:49:22 -04:00
top = self._peek()
2008-11-17 22:31:05 -05:00
if (i + start == end and
(top and top.name in self.open_scope_tokens.get(top.type_, {}) or
not top)):
2008-10-02 12:49:22 -04:00
if self.continue_tokens:
if s in self.continue_tokens.get(name, {}):
self._append_unless('continue', name, self._get_next_level(), y)
2008-10-02 12:49:22 -04:00
elif self.nocontinue_tokens:
2008-10-06 15:05:02 -04:00
d = self.nocontinue_tokens.get(name)
if d is None or d != 1 and s not in d:
self._append_unless('continue', name, self._get_next_level(), y)
2008-10-02 12:49:22 -04:00
if name == 'continuation':
2008-10-02 16:06:16 -04:00
# handle explicit continuation
self._append_unless('continue', name, self._get_next_level(), y)
2008-10-02 12:49:22 -04:00
elif s in self.control_tokens.get(name, {}):
2008-10-02 16:06:16 -04:00
# handle control keywords
2008-10-02 12:49:22 -04:00
if i == start:
self._save_curr_level()
self._pop_while('continue');
self._append_unless('control', name, self._get_next_level(), y)