Skip to content

Commit

Permalink
Fix spaces appearing in lines before global paths (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErykMroczek authored Nov 30, 2023
2 parents 54c500f + fdc865d commit 6d8972f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
17 changes: 12 additions & 5 deletions mofmt/parsing/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
ModelicaLexer.RCURLY,
ModelicaLexer.SEMICOLON,
ModelicaLexer.COMMA,
ModelicaLexer.DOT,
ModelicaLexer.COLON,
)

Expand Down Expand Up @@ -114,19 +113,27 @@ def visitTerminal(self, node: antlr.TerminalNode) -> None:
# Handle special cases
if kind == ModelicaLexer.LBRACK:
self.bracket_counter += 1
if self.prev_token != ModelicaLexer.IDENT:
self.collector.add_space()
elif kind == ModelicaLexer.RBRACK:
self.bracket_counter -= 1
elif kind == ModelicaLexer.FOR:
self.break_or_space()
if kind not in NO_SPACE_BEFORE and self.prev_token not in NO_SPACE_AFTER:
elif kind == ModelicaLexer.DOT:
# Only first dot in type specifiers etc. can be preceded with a space
if self.prev_token not in (ModelicaLexer.IDENT, ModelicaLexer.RBRACK):
self.collector.add_space()
elif kind not in NO_SPACE_BEFORE and self.prev_token not in NO_SPACE_AFTER:
self.collector.add_space()

self.collector.add_token(token.text)
if kind == ModelicaLexer.ANNOTATION:
self.collector.add_space()
self.prev_token = kind
self.prev_token_line = line

def enter_grouped_rule(self, ctx: antlr.ParserRuleContext) -> None:
"""If the rule was wrapped add info to the stack and increase indent"""
self.group_stack.append(False)
if is_multiline(ctx):
self.group_stack[-1] = True
Expand All @@ -140,6 +147,7 @@ def enter_grouped_rule(self, ctx: antlr.ParserRuleContext) -> None:
self.collector.add_indent()

def exit_grouped_rule(self, ctx: antlr.ParserRuleContext) -> None:
"""Decrease indent when leaving wrapped group"""
if self.group_stack[-1]:
if ctx.getRuleIndex() == Modelica.RULE_if_expression:
if get_preceding_token(ctx, self.stream).type == ModelicaLexer.EQUAL:
Expand All @@ -152,13 +160,15 @@ def exit_grouped_rule(self, ctx: antlr.ParserRuleContext) -> None:
self.group_stack.pop()

def break_or_space(self):
"""Insert line break or space"""
if self.group_stack[-1]:
self.collector.add_break()
else:
if self.prev_token not in NO_SPACE_AFTER:
self.collector.add_space()

def wrap_expression(self, ctx: antlr.ParserRuleContext):
"""Wrap the expression"""
next_token = get_following_token(ctx, self.stream)
# Check if there was a line break around the wrap point
if next_token.line > self.prev_token_line:
Expand Down Expand Up @@ -302,9 +312,6 @@ def enterEveryRule(self, ctx: antlr.ParserRuleContext) -> None:
):
self.break_or_space()
self.wrap_stack.append(False)
elif rule == Modelica.RULE_type_specifier:
if ctx.start.type == ModelicaLexer.DOT:
self.collector.add_space()

def exitEveryRule(self, ctx: antlr.ParserRuleContext) -> None:
"""
Expand Down
5 changes: 5 additions & 0 deletions tests/style/samples/classes/classes-input.mo
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,8 @@ end Tank;
partial record 'Quoted "record"'
import It.is.empty;
end 'Quoted "record"';

block Foo
import Modelica.Units.SI;
.Modelica.Blocks.Interfaces.BooleanInput b;
end Foo;
7 changes: 7 additions & 0 deletions tests/style/samples/classes/classes-output.mo
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,10 @@ partial record 'Quoted "record"'
import It.is.empty;

end 'Quoted "record"';

block Foo

import Modelica.Units.SI;
.Modelica.Blocks.Interfaces.BooleanInput b;

end Foo;

0 comments on commit 6d8972f

Please sign in to comment.