55 "go/parser"
66 "go/token"
77 "path/filepath"
8- "strings"
98 "time"
109
1110 "golang.org/x/tools/go/packages"
@@ -33,37 +32,30 @@ func NewCache(log logutils.Log) *Cache {
3332 }
3433}
3534
36- func (c Cache ) Get (filename string ) * File {
37- return c .m [filepath .Clean (filename )]
38- }
39-
40- func (c Cache ) keys () []string {
35+ func (c Cache ) ParsedFilenames () []string {
4136 var keys []string
4237 for k := range c .m {
4338 keys = append (keys , k )
4439 }
4540 return keys
4641}
4742
48- func (c Cache ) GetOrParse (filename string , fset * token.FileSet ) * File {
49- if ! filepath .IsAbs (filename ) {
50- absFilename , err := filepath .Abs (filename )
51- if err != nil {
52- c .log .Warnf ("Can't abs-ify filename %s: %s" , filename , err )
53- } else {
54- filename = absFilename
55- }
43+ func (c Cache ) normalizeFilename (filename string ) string {
44+ if filepath .IsAbs (filename ) {
45+ return filepath .Clean (filename )
5646 }
5747
58- f := c .m [filename ]
59- if f != nil {
60- return f
48+ absFilename , err := filepath .Abs (filename )
49+ if err != nil {
50+ c .log .Warnf ("Can't abs-ify filename %s: %s" , filename , err )
51+ return filename
6152 }
6253
63- c .log .Infof ("Parse AST for file %s on demand, existing files are %s" ,
64- filename , strings .Join (c .keys (), "," ))
65- c .parseFile (filename , fset )
66- return c .m [filename ]
54+ return absFilename
55+ }
56+
57+ func (c Cache ) Get (filename string ) * File {
58+ return c .m [c .normalizeFilename (filename )]
6759}
6860
6961func (c Cache ) GetAllValidFiles () []* File {
@@ -81,6 +73,18 @@ func (c *Cache) prepareValidFiles() {
8173 c .s = files
8274}
8375
76+ func LoadFromFilenames (log logutils.Log , filenames ... string ) * Cache {
77+ c := NewCache (log )
78+
79+ fset := token .NewFileSet ()
80+ for _ , filename := range filenames {
81+ c .parseFile (filename , fset )
82+ }
83+
84+ c .prepareValidFiles ()
85+ return c
86+ }
87+
8488func LoadFromPackages (pkgs []* packages.Package , log logutils.Log ) (* Cache , error ) {
8589 c := NewCache (log )
8690
@@ -127,6 +131,8 @@ func (c *Cache) parseFile(filePath string, fset *token.FileSet) {
127131 fset = token .NewFileSet ()
128132 }
129133
134+ filePath = c .normalizeFilename (filePath )
135+
130136 // comments needed by e.g. golint
131137 f , err := parser .ParseFile (fset , filePath , nil , parser .ParseComments )
132138 c .m [filePath ] = & File {
0 commit comments