more updates to scala mode and indentation

--HG--
branch : pmacs2
This commit is contained in:
Erik Osheim 2012-09-21 08:01:05 -06:00
parent 61d34834de
commit 79192e1352
2 changed files with 36 additions and 20 deletions

View File

@ -49,7 +49,7 @@ class ScalaGrammar(Grammar):
'delimiter', 'scala.type'), 'delimiter', 'scala.type'),
PatternMatchRule('x', r'(?<=[a-zA-Z0-9_ ])(:)( +)([a-zA-Z0-9_]+)', PatternMatchRule('x', r'(?<=[a-zA-Z0-9_ ])(:)( +)([a-zA-Z0-9_]+)',
'delimiter', 'spaces', 'scala.type'), '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'), 'scala.reserved', 'spaces', 'scala.type'),
# class, object, and trait # 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.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', '(?:0|[1-9])[0-9]*[Ll]?'),
PatternRule('scala.integer', '-?0x[0-9A-Fa-f]+[Ll]?'), PatternRule('scala.integer', '0x[0-9A-Fa-f]+[Ll]?'),
PatternRule('scala.integer', '-?0[0-7]+[Ll]?'), PatternRule('scala.integer', '0[0-7]+[Ll]?'),
PatternRule('scala.char', r"'(?:[^'\\]|\\u[0-9A-Fa-f]{4}|\\[0-7]{1,3}|\\[btnfr\"'\\])'"), PatternRule('scala.char', r"'(?:[^'\\]|\\u[0-9A-Fa-f]{4}|\\[0-7]{1,3}|\\[btnfr\"'\\])'"),
RegionRule('scala.string', '"""', Grammar, '"""+'), RegionRule('scala.string', '"""', Grammar, '"""+'),
@ -102,6 +102,7 @@ class ScalaTabber(StackTabber2):
'sub.end': {']': '['}} 'sub.end': {']': '['}}
control_tokens = {'scala.reserved': set(['if', 'else', 'while', 'do', 'for'])} 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 end_at_eof = True
start_free_tokens = {'string.start': 'string.end'} start_free_tokens = {'string.start': 'string.end'}
end_free_tokens = {'string.end': 'string.start'} end_free_tokens = {'string.end': 'string.start'}

43
tab.py
View File

@ -253,6 +253,7 @@ class StackTabber2(Tabber):
close_test_tokens = {'delimiter': set([')'])} close_test_tokens = {'delimiter': set([')'])}
control_tokens = {'keyword': set(['if', 'else', 'while', 'do', 'for'])} control_tokens = {'keyword': set(['if', 'else', 'while', 'do', 'for'])}
case_tokens = {'keyword': set(['case'])} case_tokens = {'keyword': set(['case'])}
case_delim_tokens = {'delimiter': set([':'])}
end_at_eof = True end_at_eof = True
end_at_tokens = {} end_at_tokens = {}
continue_tokens = {} continue_tokens = {}
@ -376,6 +377,8 @@ class StackTabber2(Tabber):
if marker.name in ('control', 'continue', 'pre-control'): if marker.name in ('control', 'continue', 'pre-control'):
self.stack.pop() self.stack.pop()
elif marker.name == 'case': elif marker.name == 'case':
self.stack.pop()
elif marker.name == 'case-eol':
if t.string in self.close_scope_tokens.get(t.fqname(), empty): if t.string in self.close_scope_tokens.get(t.fqname(), empty):
self.stack.pop() self.stack.pop()
else: else:
@ -388,6 +391,8 @@ class StackTabber2(Tabber):
else: else:
msg = "mismatched %r, line %d (expected %r)" msg = "mismatched %r, line %d (expected %r)"
raise Exception, msg % (t.string, y, s) raise Exception, msg % (t.string, y, s)
elif marker.name == 'pre-case':
self.stack.pop()
else: else:
raise Exception, "what? %r (%r)" % (marker, t) raise Exception, "what? %r (%r)" % (marker, t)
@ -427,7 +432,7 @@ class StackTabber2(Tabber):
# block. e.g. else switch() { ... } # block. e.g. else switch() { ... }
if t.string in self.close_scope_tokens.get(t.fqname(), empty): if t.string in self.close_scope_tokens.get(t.fqname(), empty):
self._pop('control') self._pop('control')
self._pop('case') #self._pop('case')
# if we do want implicit continuation, see if we need it. # if we do want implicit continuation, see if we need it.
name, s = t.fqname(), t.string 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) open_scope = t.string in self.open_scope_tokens.get(t.name, empty)
self._pop_while('case')
if open_scope: if open_scope:
self._pop('continue', 'control') self._pop('continue', 'control')
if i == 0: if i == 0: self._save_curr_level()
self._save_curr_level()
if not open_scope and self.stack and self._peek().name == 'case': if i == end - start or self.fixed_indent:
level = self._get_curr_level()
elif i == end - start or self.fixed_indent:
level = self._get_next_level() level = self._get_next_level()
else: else:
level = tokens[i + 1].x level = tokens[i + 1].x
@ -497,15 +501,18 @@ class StackTabber2(Tabber):
self._pop('macro') self._pop('macro')
return return
at_eol = i + start == end
# remove implicit continuation # 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') self._pop_while('continue', 'control', 'pre-control')
elif self.end_at_tokens.get(name, {}).get(s): elif self.end_at_tokens.get(name, {}).get(s):
self._pop_while('continue', 'control', 'pre-control') self._pop_while('continue', 'control', 'pre-control')
# add implicit continuation
top = self._peek() 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 (top and top.name in self.open_scope_tokens.get(top.type_, {}) or
not top)): not top)):
if self.continue_tokens: if self.continue_tokens:
@ -519,15 +526,23 @@ class StackTabber2(Tabber):
if name == 'continuation': if name == 'continuation':
# handle explicit continuation # handle explicit continuation
self._append_unless('continue', name, self._get_next_level(), y) 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 # handle control keywords
if i == start: if i == start:
self._save_curr_level() self._save_curr_level()
self._pop_while('continue'); self._pop_while('continue');
self._append_unless('pre-control', name, self._get_next_level(), y) self._append_unless('pre-control', name, self._get_next_level(), y)
elif s in self.case_tokens.get(name, {}): elif s in self.case_tokens.get(name, empty):
if top is not None and top.name == 'case': if top is not None and top.name in ['case', 'case-eol']:
self._pop('case') self._pop('case', 'case-eol')
if i == 0: if i == 0:
self._save_curr_level() 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