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

Include end of file comments #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
44 changes: 44 additions & 0 deletions formatter/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,50 @@ func TestCompilationUnit(t *testing.T) {
}
}

func TestEndOfFileComments(t *testing.T) {
if testing.Verbose() {
log.SetLevel(log.DebugLevel)

}
tests :=
[]struct {
input string
output string
}{
{
`private class T1Exception extends Exception {} //test`,
`private class T1Exception extends Exception {}
//test`},
{
`public class MyClass { public static void noop() {}}
//test comment
// oie`,
`public class MyClass {
public static void noop() {}
}
//test comment
// oie`},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's fix this as well, when there's a token after the comment, but no more parse nodes:

public class MyClass {
	public static void noop() {}
   // No more nodes after this
}

}
for _, tt := range tests {
input := antlr.NewInputStream(tt.input)
lexer := parser.NewApexLexer(input)
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)

p := parser.NewApexParser(stream)
p.RemoveErrorListeners()
p.AddErrorListener(&testErrorListener{t: t})

v := NewFormatVisitor(stream)
out, ok := v.visitRule(p.CompilationUnit()).(string)
if !ok {
t.Errorf("Unexpected result parsing apex")
}
if out != tt.output {
t.Errorf("unexpected format. expected:\n%s\ngot:\n%s\n", tt.output, out)
}
}
}

func TestSOQL(t *testing.T) {
if testing.Verbose() {
log.SetLevel(log.DebugLevel)
Expand Down
2 changes: 1 addition & 1 deletion formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (f *Formatter) Format() error {
if f.source == nil {
src, err := readFile(f.filename, f.reader)
if err != nil {
return fmt.Errorf("Failed to read file %s: %w", f.SourceName(), err)
return fmt.Errorf("failed to read file %s: %w", f.SourceName(), err)
}
f.source = src
}
Expand Down
34 changes: 29 additions & 5 deletions formatter/visitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ import (
)

func (v *FormatVisitor) VisitCompilationUnit(ctx *parser.CompilationUnitContext) interface{} {

result := ""

if trigger := ctx.TriggerUnit(); trigger != nil {
return v.visitRule(trigger)
result = v.visitRule(trigger).(string)
}
t := ctx.TypeDeclaration()
switch {
case t.ClassDeclaration() != nil:
return fmt.Sprintf("%s%s", v.Modifiers(t.AllModifier()), v.visitRule(t.ClassDeclaration()).(string))
result = fmt.Sprintf("%s%s", v.Modifiers(t.AllModifier()), v.visitRule(t.ClassDeclaration()).(string))
case t.InterfaceDeclaration() != nil:
return fmt.Sprintf("%s%s", v.Modifiers(t.AllModifier()), v.visitRule(t.InterfaceDeclaration()).(string))
result = fmt.Sprintf("%s%s", v.Modifiers(t.AllModifier()), v.visitRule(t.InterfaceDeclaration()).(string))
case t.EnumDeclaration() != nil:
enum := t.EnumDeclaration()
constants := []string{}
Expand All @@ -27,9 +30,30 @@ func (v *FormatVisitor) VisitCompilationUnit(ctx *parser.CompilationUnitContext)
constants = append(constants, e.GetText())
}
}
return fmt.Sprintf("enum %s {%s}", v.visitRule(enum.Id()), strings.Join(constants, ", "))
result = fmt.Sprintf("enum %s {%s}", v.visitRule(enum.Id()), strings.Join(constants, ", "))
}
return ""

stop := ctx.GetStop()
if stop != nil {
eofComments := v.tokens.GetHiddenTokensToRight(stop.GetTokenIndex(), COMMENTS_CHANNEL)

if eofComments != nil {
comments := []string{}

for _, c := range eofComments {
if _, seen := v.commentsOutput[c.GetTokenIndex()]; !seen {
comments = append(comments, cleanWhitespace(c.GetText()))
v.commentsOutput[c.GetTokenIndex()] = struct{}{}
}
}

if len(comments) > 0 {
return (fmt.Sprintf("%s\n%s", result, strings.Join(comments, "\n")))
}
}
}

return result
}

func (v *FormatVisitor) VisitClassDeclaration(ctx *parser.ClassDeclarationContext) interface{} {
Expand Down