Skip to content

Commit

Permalink
refactor: add space after annotation keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
macintoshpie committed Jun 9, 2020
1 parent a48d19b commit 95e8917
Showing 1 changed file with 19 additions and 11 deletions.
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

0 comments on commit 95e8917

Please sign in to comment.