Skip to content

Commit

Permalink
update:support test.gox package test & file test
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed Apr 17, 2024
1 parent 7cec065 commit 904b65b
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions gopls/internal/lsp/source/code_lens_gox.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,27 +182,53 @@ func gopToggleDetailsCodeLens(ctx context.Context, snapshot Snapshot, fh FileHan

func gopCommandCodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.CodeLens, error) {
filename := fh.URI().Filename()
if strings.HasSuffix(filename, "_test.go") || strings.HasSuffix(filename, "_test.gop") || strings.HasSuffix(filename, "test.gox") {
if strings.HasSuffix(filename, "_test.go") || strings.HasSuffix(filename, "_test.gop") {
return nil, nil
}

pgf, err := snapshot.ParseGop(ctx, fh, parser.PackageClauseOnly)
if err != nil {
return nil, err
}
if pgf.File.Name.Name == "main" {
isTest := strings.HasSuffix(filename, "test.gox")
classType, _ := parserutil.GetClassType(pgf.File, fh.URI().Filename())
codelens := []protocol.CodeLens{}
rng, err := pgf.PosRange(pgf.File.Pos(), pgf.File.Pos())
if err != nil {
return nil, err
}
dir := protocol.URIFromSpanURI(span.URIFromPath(filepath.Dir(fh.URI().Filename())))
args := command.RunGopCommandArgs{URI: dir, Command: "run"}
cmd, err := command.NewRunGopCommandCommand("run main package", args)
if err != nil {
return nil, err
if !isTest {
args := command.RunGopCommandArgs{URI: dir, Command: "run"}
cmd, err := command.NewRunGopCommandCommand("run main package", args)
if err != nil {
return nil, err
}
codelens = append(codelens, protocol.CodeLens{Range: rng, Command: &cmd})
} else {
pattern := regexp.MustCompile(`^case(?:_)?`) //goxls: remove case or case_
if pattern.MatchString(classType) {
classType = pattern.ReplaceAllString(classType, "")
}
args, err := command.MarshalArgs(
map[string]string{
"functionName": "Test_" + classType,
},
)
if err != nil {
return nil, err
}
codelens = append(codelens, protocol.CodeLens{Range: rng, Command: &protocol.Command{
Title: "run test package",
Command: "gop.test.package",
}}, protocol.CodeLens{Range: rng, Command: &protocol.Command{ // goxls: add test cursor as test file
Title: "run file tests",
Command: "gop.test.cursor",
Arguments: args,
}})
}
return []protocol.CodeLens{
{Range: rng, Command: &cmd},
}, nil
return codelens, nil
}
return nil, nil
}

0 comments on commit 904b65b

Please sign in to comment.