summaryrefslogtreecommitdiff
path: root/gocomplete/parse.go
blob: 8111b748bc64a06cc21e1533ca71d9f132bf5510 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package main

import (
	"go/ast"
	"go/parser"
	"go/token"
	"regexp"

	"github.com/posener/complete"
)

func functionsInFile(path string, regexp *regexp.Regexp) (tests []string) {
	fset := token.NewFileSet()
	f, err := parser.ParseFile(fset, path, nil, 0)
	if err != nil {
		complete.Log("Failed parsing %s: %s", path, err)
		return nil
	}
	for _, d := range f.Decls {
		if f, ok := d.(*ast.FuncDecl); ok {
			name := f.Name.String()
			if regexp == nil || regexp.MatchString(name) {
				tests = append(tests, name)
			}
		}
	}
	return
}