summaryrefslogtreecommitdiff
path: root/gocomplete/tests.go
diff options
context:
space:
mode:
authorEyal Posener <[email protected]>2017-05-15 22:44:19 +0300
committerEyal Posener <[email protected]>2017-05-15 22:50:05 +0300
commitff8cd4ed39884bc6774087809e1593f36c4b90e3 (patch)
tree78a5fbc1810cfd44497c9c2c0ad9036452dfaef0 /gocomplete/tests.go
parente00c0546bc15809f7ba7c4e7a17b6be3aac56ccf (diff)
gocomplete: run only runnable go files
when typing 'go run', the completion will complete only go files which are in main package and have a main function.
Diffstat (limited to 'gocomplete/tests.go')
-rw-r--r--gocomplete/tests.go38
1 files changed, 10 insertions, 28 deletions
diff --git a/gocomplete/tests.go b/gocomplete/tests.go
index d2c32e7..a952dab 100644
--- a/gocomplete/tests.go
+++ b/gocomplete/tests.go
@@ -1,25 +1,28 @@
package main
import (
- "go/ast"
- "go/parser"
- "go/token"
"os"
"path/filepath"
+ "regexp"
"strings"
"github.com/posener/complete"
"github.com/posener/complete/match"
)
+var (
+ predictBenchmark = funcPredict(regexp.MustCompile("^Benchmark"))
+ predictTest = funcPredict(regexp.MustCompile("^(Test|Example)"))
+)
+
// predictTest predict test names.
// it searches in the current directory for all the go test files
// and then all the relevant function names.
// for test names use prefix of 'Test' or 'Example', and for benchmark
// test names use 'Benchmark'
-func predictTest(funcPrefix ...string) complete.Predictor {
+func funcPredict(funcRegexp *regexp.Regexp) complete.Predictor {
return complete.PredictFunc(func(a complete.Args) (prediction []string) {
- tests := testNames(funcPrefix)
+ tests := funcNames(funcRegexp)
for _, t := range tests {
if match.Prefix(t, a.Last) {
prediction = append(prediction, t)
@@ -30,36 +33,15 @@ func predictTest(funcPrefix ...string) complete.Predictor {
}
// get all test names in current directory
-func testNames(funcPrefix []string) (tests []string) {
+func funcNames(funcRegexp *regexp.Regexp) (tests []string) {
filepath.Walk("./", func(path string, info os.FileInfo, err error) error {
// if not a test file, skip
if !strings.HasSuffix(path, "_test.go") {
return nil
}
// inspect test file and append all the test names
- tests = append(tests, testsInFile(funcPrefix, path)...)
+ tests = append(tests, functionsInFile(path, funcRegexp)...)
return nil
})
return
}
-
-func testsInFile(funcPrefix []string, path string) (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()
- for _, prefix := range funcPrefix {
- if strings.HasPrefix(name, prefix) {
- tests = append(tests, name)
- break
- }
- }
- }
- }
- return
-}