better scala indentation for case, self-types, etc

--HG--
branch : pmacs2
This commit is contained in:
Erik Osheim 2012-09-13 22:00:47 -06:00
parent 7349b13195
commit d01e8adbde
1 changed files with 35 additions and 18 deletions

View File

@ -43,28 +43,45 @@ class ScalaGrammar(Grammar):
RegionRule('scala.comment', r'/\*', NestedCommentGrammar, r'\*/'), RegionRule('scala.comment', r'/\*', NestedCommentGrammar, r'\*/'),
RegionRule('scala.script', r'#!.+$', ShGrammar, r'!#'), RegionRule('scala.script', r'#!.+$', ShGrammar, r'!#'),
# determine types based on context
PatternMatchRule('x', r'(?<=[a-zA-Z0-9_ ])(:)([a-zA-Z0-9_]+)', PatternMatchRule('x', r'(?<=[a-zA-Z0-9_ ])(:)([a-zA-Z0-9_]+)',
'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:\[\( ][^:\[\( ]*)',
'scala.reserved', 'spaces', 'scala.type'),
PatternRule('scala.def', '(?<=(?<![a-zA-Z0-9_])def )[^0-9:\[\( ][^:\[\( ]*'), # class, object, and trait
PatternMatchRule('x', r'(?<![a-zA-Z0-9_])(class)( +)([^0-9:\[\( ][^:\[\( ]*)',
'scala.reserved', 'spaces', 'scala.class'),
PatternMatchRule('x', r'(?<![a-zA-Z0-9_])(object)( +)([^0-9:\[\( ][^:\[\( ]*)',
'scala.reserved', 'spaces', 'scala.object'),
PatternMatchRule('x', r'(?<![a-zA-Z0-9_])(trait)( +)([^0-9:\[\( ][^:\[\( ]*)',
'scala.reserved', 'spaces', 'scala.trait'),
RegionRule('sub', r'(?<=:)\(', SubTypeGrammar, r'\)'), # method names
PatternRule('delimiter', r'(?:;|{|}|\(|\)|,|\.|<(?![a-zA-Z_])|>|:|/|\+|-|\*|=)'), PatternMatchRule('x', r'(?<![a-zA-Z0-9_])(def)( +)([^0-9:\[\( ][^:\[\( ]*)',
'scala.reserved', 'spaces', 'scala.def'),
# package names
PatternMatchRule('x', r'(?<![a-zA-Z0-9_])(package)( +)([a-zA-Z0-9_.]+)',
'scala.reserved', 'spaces', 'scala.package'),
# used for type param lists and type parameterization
RegionRule('sub', r'\[', SubTypeGrammar, r'\]'), RegionRule('sub', r'\[', SubTypeGrammar, r'\]'),
# match various scala delimiters and operators
PatternRule('delimiter', r'(?:;|=>|{|}|\(|\)|,|\.|<(?![a-zA-Z_])|>|:|/|\+|-|\*|=)'),
# semi-hack to support XML
RegionRule('scala.inline', r'(?:^| )(?=<[a-zA-Z_])', XMLGrammar, '^[ \t]*$'), RegionRule('scala.inline', r'(?:^| )(?=<[a-zA-Z_])', XMLGrammar, '^[ \t]*$'),
PatternRule('spaces', r'(?:\t| )+'), PatternRule('spaces', r'(?:\t| )+'),
PatternRule('eol', r'\n'), PatternRule('eol', r'\n'),
PatternRule('scala.class', '(?<=(?<![a-zA-Z0-9_])class )[a-zA-Z_][a-zA-Z0-9_]*'), # these are some constants that show up a lot
PatternRule('scala.object', '(?<=(?<![a-zA-Z0-9_])object )[a-zA-Z_][a-zA-Z0-9_]*'),
PatternRule('scala.trait', '(?<=(?<![a-zA-Z0-9_])trait )[a-zA-Z_][a-zA-Z0-9_]*'),
PatternRule('scala.pseudo', '(?:true|null|false)(?!%s)' % word), PatternRule('scala.pseudo', '(?:true|null|false)(?!%s)' % word),
PatternRule('scala.reserved', '(?:yield|with|while|var|val|until|type|true|try|trait|throw|to|this|super|sealed|return|protected|private|package|override|object|null|new|match|lazy|import|implicit|if|forSome|for|finally|final|false|extends|else|do|def|class|catch|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|abstract)(?!%s)' % word),
PatternRule('scala.float', r'-?[0-9]+\.[0-9]*[Ff]?'), # FIXME PatternRule('scala.float', r'-?[0-9]+\.[0-9]*[Ff]?'), # FIXME
@ -73,7 +90,7 @@ class ScalaGrammar(Grammar):
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, '"""+'),
RegionRule('scala.string', '"', StringGrammar, '"'), RegionRule('scala.string', '"', StringGrammar, '"'),
PatternRule('scala.symbol', "'[a-zA-Z_][a-zA-Z0-9_]*"), PatternRule('scala.symbol', "'[a-zA-Z_][a-zA-Z0-9_]*"),
@ -87,13 +104,15 @@ class ScalaTabber(StackTabber2):
'sub.start': {'[': ']'}} 'sub.start': {'[': ']'}}
close_tokens = {'delimiter': {'}': '{', ')': '('}, close_tokens = {'delimiter': {'}': '{', ')': '('},
'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'])}
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'}
is_ignored_tokens = set(('spaces', 'eol', 'comment', 'comment.start', is_ignored_tokens = set(['spaces', 'eol', 'comment', 'comment.start',
'comment.data', 'comment.null', 'comment.end')) 'comment.data', 'comment.null', 'comment.end'])
is_indent_tokens = set(('spaces',)) is_indent_tokens = set(['spaces'])
fixed_indent = True
def _is_base(self, y): def _is_base(self, y):
# the first line is always safe # the first line is always safe
if y == 0: return True if y == 0: return True
@ -373,24 +392,22 @@ class Scala(Fundamental):
'scala.script.end': hi_red, 'scala.script.end': hi_red,
'scala.annotation': lo_orange, 'scala.annotation': lo_orange,
'scala.pseudo': hi_magenta,
'scala.reserved': hi_cyan, 'scala.reserved': hi_cyan,
'scala.pseudo': hi_magenta,
'scala.integer': default, 'scala.integer': default,
'scala.float': default, 'scala.float': default,
'scala.bareword': default, 'scala.bareword': default,
'scala.symbol': hi_orange, 'scala.symbol': hi_orange,
'scala.package': hi_orange,
'scala.class': hi_yellow, 'scala.class': hi_yellow,
'scala.object': hi_yellow, 'scala.object': hi_yellow,
'scala.trait': hi_yellow, 'scala.trait': hi_yellow,
'scala.def': hi_blue,
'scala.type': hi_magenta, 'scala.type': hi_magenta,
'scala.def': hi_blue,
'scala.def': hi_blue,
} }
_bindings = { _bindings = {