From e7c656de45f2d189cc4fac6fb63ef64ff7414840 Mon Sep 17 00:00:00 2001 From: moculus Date: Tue, 19 Jun 2007 18:45:51 +0000 Subject: [PATCH] tabbing --HG-- branch : pmacs2 --- code_examples/Reporting2.pm | 9 ++-- method.py | 8 ++-- mode_perl.py | 12 ++--- tab2.py | 89 ++++++++++++++++++++++--------------- 4 files changed, 69 insertions(+), 49 deletions(-) diff --git a/code_examples/Reporting2.pm b/code_examples/Reporting2.pm index 32495de..bce81f1 100644 --- a/code_examples/Reporting2.pm +++ b/code_examples/Reporting2.pm @@ -1,10 +1,9 @@ package TBB::Reporting2; -{ -'foo', -'bar', -} - +my $foo = { + 'foo', + 'bar', +}; foo(); diff --git a/method.py b/method.py index 15388f4..1442180 100644 --- a/method.py +++ b/method.py @@ -447,10 +447,12 @@ class DumpMarkers(Method): def _execute(self, w, **vargs): lines = [] if w.mode.tabber: - for i in range(0, len(w.mode.tabber.lines)): + keys = w.mode.tabber.lines.keys() + keys.sort() + for i in keys: line = w.mode.tabber.lines[i] lines.append("LINE %d: %r" % (i, line)) - lines.append(" %s" % repr(w.mode.tabber.markers[i])) + lines.append(" %s" % repr(w.mode.tabber.record[i])) else: lines.append("no tokens") output = "\n".join(lines) @@ -531,7 +533,7 @@ class InsertTab(Method): j = w.buffer.count_leading_whitespace(cursor.y) if i != j: KillWhitespace().execute(w) - w.insert_string(Point(0, cursor.y), ' ' * i * 4) + w.insert_string(Point(0, cursor.y), ' ' * i) else: w.goto(Point(j, cursor.y)) class KillWhitespace(Method): diff --git a/mode_perl.py b/mode_perl.py index 5d6df9f..f185f69 100644 --- a/mode_perl.py +++ b/mode_perl.py @@ -101,11 +101,13 @@ class PerlGrammar(Grammar): ] class Perl(mode2.Fundamental): - #tabbercls = tab2.Tabber - tabbercls = tab2.StackTabber - grammar = PerlGrammar() - opentags = {'(': ')', '[': ']', '{': '}'} - closetags = {')': '(', ']': '[', '}': '{'} + #tabbercls = tab2.StackTabber + tabbercls = tab2.PerlTabber + grammar = PerlGrammar() + opentoken = 'delimiter' + opentags = {'(': ')', '[': ']', '{': '}'} + closetoken = 'delimiter' + closetags = {')': '(', ']': '[', '}': '{'} def __init__(self, w): mode2.Fundamental.__init__(self, w) diff --git a/tab2.py b/tab2.py index aa7ebdc..e0fbf98 100644 --- a/tab2.py +++ b/tab2.py @@ -13,32 +13,28 @@ class Tabber: self.mode = m self.lines = {} - def is_leftmost_token(self, y, i): + def get_next_left_token(self, y, i): tokens = self.get_tokens(y) assert i >= 0 and i < len(tokens) for j in range(0, i): + m = regex.whitespace.match(tokens[i - j].string) + if not m: + 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)): m = regex.whitespace.match(tokens[j].string) if not m: - return False - return True + 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): - tokens = self.get_tokens(y) - assert i >= 0 and i < len(tokens) - for j in range(i, len(tokens)): - m = regex.whitespace.match(highlighter.tokens[y][j].string) - if not m: - return False - return True + return self.get_next_right_token(y, i) is None def is_only_token(self, y, i): - tokens = self.get_tokens(y) - assert i >= 0 and i < len(tokens) - for j in range(0, len(tokens)): - if j == i: - continue - m = regex.whitespace.match(highlighter.tokens[y][j].string) - if not m: - return False - return True + return self.is_leftmost_token(y, i) and self.is_rightmost_token(y, i) def region_added(self, p, newlines): self.lines = {} @@ -76,6 +72,8 @@ class StackTabber(Tabber): return 0 def get_tokens(self, y): return self.mode.window.buffer.highlights[self.mode.name()].tokens[y] + def get_token(self, y, i): + return self.mode.window.buffer.highlights[self.mode.name()].tokens[y][i] def region_added(self, p, newlines): self.lines = {} @@ -110,22 +108,41 @@ class StackTabber(Tabber): y += 1 def _handle_token(self, currlvl, y, i): - highlighter = self.mode.window.buffer.highlights[self.mode.name()] - token = highlighter.tokens[y][i] - s = token.string + token = self.get_token(y, i) + s = token.string - if token.name == 'delimiter': - if s in self.mode.closetags: - if not self.markers: - raise Exception, "unmatched closing token %r" % s - nam = self.markers[-1].name - if s == self.mode.opentags[nam]: - del self.markers[-1] - if self.is_leftmost_token(y, i): - currlvl = self.get_curr_level() - else: - raise Exception, "mismatched closing tag %r vs %r" % (nam, s) - elif s in self.mode.opentags: - level = self.get_curr_level() + 1 - self.markers.append(Marker(s, level)) + if token.name == self.mode.closetoken and s in self.mode.closetags: + currlvl = self._handle_open_token(currlvl, y, i) + elif token.name == self.mode.opentoken and s in self.mode.opentags: + currlvl = self._handle_close_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) + s1 = token.string + if not self.markers: + raise Exception, "unmatched closing token %r" % s1 + s2 = self.markers[-1].name + if s1 == self.mode.opentags[s2]: + del self.markers[-1] + 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_close_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() + 4 + else: + level = rtoken.x + self.markers.append(Marker(token.string, level)) + return currlvl + def _handle_other_token(self, currlvl, y, i): + return currlvl + +class PerlTabber(StackTabber): + pass