Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add space after annotation keyword #2

Merged
merged 2 commits into from
Jun 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions modelicafmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ func insertIndentBefore(rule antlr.ParserRuleContext) bool {
}
}

// insertSpaceBefore returns true if a space should be inserted before the current token
func insertSpaceBefore(currentTokenText, previousTokenText string) bool {
switch currentTokenText {
case "(":
if previousTokenText == "annotation" {
return true
}
fallthrough
default:
return !tokenInGroup(previousTokenText, noSpaceAfterTokens) &&
!tokenInGroup(currentTokenText, noSpaceBeforeTokens)
}
}

// insertNewlineBefore returns true if the rule should be on a new line
func insertNewlineBefore(rule antlr.ParserRuleContext) bool {
switch rule.(type) {
Expand All @@ -55,6 +69,8 @@ func insertNewlineBefore(rule antlr.ParserRuleContext) bool {
}

var (
// tokens which should *generally* not have a space after them
// this can be overridden in the insertSpace function
noSpaceAfterTokens = []string{
"(",
"=",
Expand All @@ -65,6 +81,8 @@ var (
";",
}

// tokens which should *generally* not have a space before them
// this can be overridden in the insertSpace function
noSpaceBeforeTokens = []string{
"(", ")",
"[", "]",
Expand All @@ -75,16 +93,6 @@ var (
".",
"-", "^", "*", "/",
}

listOpenTokens = []string{
"(",
"{",
}

listCloseTokens = []string{
")",
"}",
}
)

// tokenInGroup returns true if a token is in a given list
Expand Down Expand Up @@ -150,7 +158,7 @@ func (l *modelicaListener) writeSpaceBefore(token antlr.Token) {
l.writer.WriteString(strings.Repeat(spaceIndent, indentation))
}
l.onNewLine = false
} else if !tokenInGroup(l.previousTokenText, noSpaceAfterTokens) && !tokenInGroup(token.GetText(), noSpaceBeforeTokens) {
} else if insertSpaceBefore(token.GetText(), l.previousTokenText) {
// insert a space
l.writer.WriteString(" ")
}
Expand Down