diff --git a/mode/scala.py b/mode/scala.py index 5bb408e..36a36ae 100644 --- a/mode/scala.py +++ b/mode/scala.py @@ -49,7 +49,7 @@ class ScalaGrammar(Grammar): 'delimiter', 'scala.type'), PatternMatchRule('x', r'(?<=[a-zA-Z0-9_ ])(:)( +)([a-zA-Z0-9_]+)', 'delimiter', 'spaces', 'scala.type'), - PatternMatchRule('x', r'(?<=[^a-zA-Z0-9_])(new|extends|with)( +)([^0-9:\[\( ][^:\[\( ]*)', + PatternMatchRule('x', r'(?<=[^a-zA-Z0-9_])(new|extends|with)( +)([^{}0-9:\[\(\n\t ][^:\[\(\n\t ]*)', 'scala.reserved', 'spaces', 'scala.type'), # class, object, and trait @@ -79,11 +79,11 @@ class ScalaGrammar(Grammar): PatternRule('scala.reserved', '(?:yield|with|while|var|val|type|true|try|trait|throw|this|super|sealed|return|protected|private|package|override|object|null|new|match|macro|lazy|import|implicit|if|forSome|for|finally|final|false|extends|else|do|def|class|catch|case object|case class|case|abstract)(?!%s)' % word), - PatternRule('scala.float', r'-?[0-9]+\.[0-9]*[Ff]?'), # FIXME + PatternRule('scala.float', r'[0-9]+\.[0-9]*[Ff]?'), # FIXME - PatternRule('scala.integer', '-?(?:0|[1-9])[0-9]*[Ll]?'), - PatternRule('scala.integer', '-?0x[0-9A-Fa-f]+[Ll]?'), - PatternRule('scala.integer', '-?0[0-7]+[Ll]?'), + PatternRule('scala.integer', '(?:0|[1-9])[0-9]*[Ll]?'), + PatternRule('scala.integer', '0x[0-9A-Fa-f]+[Ll]?'), + PatternRule('scala.integer', '0[0-7]+[Ll]?'), PatternRule('scala.char', r"'(?:[^'\\]|\\u[0-9A-Fa-f]{4}|\\[0-7]{1,3}|\\[btnfr\"'\\])'"), RegionRule('scala.string', '"""', Grammar, '"""+'), @@ -101,7 +101,8 @@ class ScalaTabber(StackTabber2): close_tokens = {'delimiter': {'}': '{', ')': '('}, 'sub.end': {']': '['}} control_tokens = {'scala.reserved': set(['if', 'else', 'while', 'do', 'for'])} - case_tokens = {'scala.reserved': set(['case'])} + case_tokens = {'scala.reserved': set(['case'])} + case_delim_tokens = {'delimiter': set(['=>'])} end_at_eof = True start_free_tokens = {'string.start': 'string.end'} end_free_tokens = {'string.end': 'string.start'} diff --git a/tab.py b/tab.py index fed1fce..5b2415a 100644 --- a/tab.py +++ b/tab.py @@ -253,6 +253,7 @@ class StackTabber2(Tabber): close_test_tokens = {'delimiter': set([')'])} control_tokens = {'keyword': set(['if', 'else', 'while', 'do', 'for'])} case_tokens = {'keyword': set(['case'])} + case_delim_tokens = {'delimiter': set([':'])} end_at_eof = True end_at_tokens = {} continue_tokens = {} @@ -376,6 +377,8 @@ class StackTabber2(Tabber): if marker.name in ('control', 'continue', 'pre-control'): self.stack.pop() elif marker.name == 'case': + self.stack.pop() + elif marker.name == 'case-eol': if t.string in self.close_scope_tokens.get(t.fqname(), empty): self.stack.pop() else: @@ -388,6 +391,8 @@ class StackTabber2(Tabber): else: msg = "mismatched %r, line %d (expected %r)" raise Exception, msg % (t.string, y, s) + elif marker.name == 'pre-case': + self.stack.pop() else: raise Exception, "what? %r (%r)" % (marker, t) @@ -427,7 +432,7 @@ class StackTabber2(Tabber): # block. e.g. else switch() { ... } if t.string in self.close_scope_tokens.get(t.fqname(), empty): self._pop('control') - self._pop('case') + #self._pop('case') # if we do want implicit continuation, see if we need it. name, s = t.fqname(), t.string @@ -461,14 +466,13 @@ class StackTabber2(Tabber): open_scope = t.string in self.open_scope_tokens.get(t.name, empty) + self._pop_while('case') + if open_scope: self._pop('continue', 'control') - if i == 0: - self._save_curr_level() + if i == 0: self._save_curr_level() - if not open_scope and self.stack and self._peek().name == 'case': - level = self._get_curr_level() - elif i == end - start or self.fixed_indent: + if i == end - start or self.fixed_indent: level = self._get_next_level() else: level = tokens[i + 1].x @@ -497,15 +501,18 @@ class StackTabber2(Tabber): self._pop('macro') return + at_eol = i + start == end + # remove implicit continuation - if self.end_at_eof and i + start == end: + if self.end_at_eof and at_eol: self._pop_while('continue', 'control', 'pre-control') elif self.end_at_tokens.get(name, {}).get(s): self._pop_while('continue', 'control', 'pre-control') - # add implicit continuation top = self._peek() - if (i + start == end and + + # add implicit continuation + if (at_eol and (top and top.name in self.open_scope_tokens.get(top.type_, {}) or not top)): if self.continue_tokens: @@ -519,15 +526,23 @@ class StackTabber2(Tabber): if name == 'continuation': # handle explicit continuation self._append_unless('continue', name, self._get_next_level(), y) - elif s in self.control_tokens.get(name, {}): + elif s in self.control_tokens.get(name, empty): # handle control keywords if i == start: self._save_curr_level() self._pop_while('continue'); self._append_unless('pre-control', name, self._get_next_level(), y) - elif s in self.case_tokens.get(name, {}): - if top is not None and top.name == 'case': - self._pop('case') + elif s in self.case_tokens.get(name, empty): + if top is not None and top.name in ['case', 'case-eol']: + self._pop('case', 'case-eol') if i == 0: self._save_curr_level() - self._append_unless('case', name, self._get_next_level(), y) + self._append_unless('pre-case', name, self._get_next_level(), y) + elif s in self.case_delim_tokens.get(name, empty): + if top is not None and top.name == 'pre-case': + if at_eol: + self.stack[-1].name = 'case-eol' + self.stack[-1].s = s + else: + self.stack[-1].name = 'case' + self.stack[-1].s = s