1
1
package godot
2
2
3
3
import (
4
- "bytes"
5
4
"fmt"
6
5
"go/ast"
7
- "go/format"
8
6
"go/token"
7
+ "io/ioutil"
9
8
"strings"
10
9
)
11
10
@@ -20,12 +19,14 @@ func getComments(file *ast.File, fset *token.FileSet, scope Scope) ([]comment, e
20
19
return nil , nil
21
20
}
22
21
23
- // Render AST representation to a string
24
- var buf bytes.Buffer
25
- if err := format .Node (& buf , fset , file ); err != nil {
26
- return nil , fmt .Errorf ("render file: %v" , err )
22
+ // Read original file. This is necessary for making a replacements for
23
+ // inline comments. I couldn't find a better way to get original line
24
+ // with code and comment without reading the file. Function `Format`
25
+ // from "go/format" won't help here if the original file is not gofmt-ed.
26
+ lines , err := readFile (file , fset )
27
+ if err != nil {
28
+ return nil , fmt .Errorf ("read file: %v" , err )
27
29
}
28
- lines := strings .Split (buf .String (), "\n " )
29
30
30
31
// Check consistency to avoid checking index in each function
31
32
lastComment := file .Comments [len (file .Comments )- 1 ]
@@ -188,6 +189,16 @@ func getText(comment *ast.CommentGroup) (s string) {
188
189
return s [:len (s )- 1 ] // trim last "\n"
189
190
}
190
191
192
+ // readFile reads file and returns it's lines as strings.
193
+ func readFile (file * ast.File , fset * token.FileSet ) ([]string , error ) {
194
+ fname := fset .File (file .Package )
195
+ f , err := ioutil .ReadFile (fname .Name ())
196
+ if err != nil {
197
+ return nil , err
198
+ }
199
+ return strings .Split (string (f ), "\n " ), nil
200
+ }
201
+
191
202
// setDecl sets `decl` flag to comments which are declaration comments.
192
203
func setDecl (comments , decl []comment ) {
193
204
for _ , d := range decl {
0 commit comments