diff --git a/mode/java.py b/mode/java.py index 140ea51..53e5db9 100644 --- a/mode/java.py +++ b/mode/java.py @@ -1,7 +1,7 @@ import color, mode, tab from lex import Grammar, PatternRule, RegionRule from mode.python import StringGrammar2 -from mode.c import CTabber +from mode.c import CTabber2 class JavaGrammar(Grammar): rules = [ @@ -30,7 +30,7 @@ class JavaGrammar(Grammar): PatternRule(r"eol", r"\n$"), ] -class JavaTabber(CTabber): +class JavaTabber(CTabber2): def is_base(self, y): if y == 0: return True diff --git a/tab.py b/tab.py index 2d16b4d..1f02c15 100644 --- a/tab.py +++ b/tab.py @@ -233,16 +233,18 @@ class Marker2(object): #class StackTabber2(tab.StackTabber): class StackTabber2(Tabber): - open_tokens = {'delimiter': {'{': '}', '(': ')', '[': ']'}} - close_tokens = {'delimiter': {'}': '{', ')': '(', ']': '['}} - scope_tokens = {'delimiter': Set(['{'])} - control_tokens = {'keyword': Set(['if', 'else', 'while', 'do', 'for'])} - 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'} + open_tokens = {'delimiter': {'{': '}', '(': ')', '[': ']'}} + close_tokens = {'delimiter': {'}': '{', ')': '(', ']': '['}} + scope_tokens = {'delimiter': Set(['{'])} + control_tokens = {'keyword': Set(['if', 'else', 'while', 'do', 'for'])} + 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 = {} def __init__(self, m): self.mode = m self.name = m.name() @@ -377,17 +379,30 @@ class StackTabber2(Tabber): def _handle_other_token(self, y, tokens, start, end, i, t): name, s = t.fqname(), t.string + # handle "free" tokens (strings, heredocs, etc) if name in self.start_free_tokens: self._append('free', name, None) return elif name in self.end_free_tokens: self._pop('free') + # handle macros + if name in self.start_macro_tokens: + self._append('macro', name, 0) + if i == 0: + self._save_curr_level() + return + elif name in self.end_macro_tokens: + self._pop('macro') + return + + # remove implicit continuation 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') + # add implicit continuation top = self._peek() if top and top.name in self.scope_tokens.get(top.type_, {}): if self.continue_tokens: @@ -397,9 +412,12 @@ class StackTabber2(Tabber): if s not in self.nocontinue_tokens.get(name, {}): self._append_unless('continue', name, self._get_next_level()) + if name == 'continuation': + # handle explicit continuation self._append_unless('continue', name, self._get_next_level()) elif s in self.control_tokens.get(name, {}): + # handle control keywords if i == start: self._save_curr_level() self._append_unless('control', name, self._get_next_level())