diff --git a/gopls/internal/lsp/source/code_lens_gox.go b/gopls/internal/lsp/source/code_lens_gox.go index 21978912eb7..ca37dcf30d2 100644 --- a/gopls/internal/lsp/source/code_lens_gox.go +++ b/gopls/internal/lsp/source/code_lens_gox.go @@ -182,7 +182,11 @@ 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") { + // TODO: Use parserutil.GetClassType Instead + if strings.HasSuffix(filename, "test.gox") { + return goxTestCodeLens(ctx, snapshot, fh) + } + if strings.HasSuffix(filename, "_test.go") || strings.HasSuffix(filename, "_test.gop") { return nil, nil } pgf, err := snapshot.ParseGop(ctx, fh, parser.PackageClauseOnly) @@ -206,3 +210,42 @@ func gopCommandCodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle) ( } return nil, nil } + +func goxTestCodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.CodeLens, error) { + pgf, err := snapshot.ParseGop(ctx, fh, parser.PackageClauseOnly) + if err != nil { + return nil, err + } + if pgf.File.Name.Name == "main" { + classType, _ := parserutil.GetClassType(pgf.File, fh.URI().Filename()) + rng, err := pgf.PosRange(pgf.File.Pos(), pgf.File.Pos()) + if err != nil { + return nil, err + } + 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 := []protocol.CodeLens{ + {Range: rng, Command: &protocol.Command{ + Title: "run test package", + Command: "gop.test.package", + }}, + {Range: rng, Command: &protocol.Command{ // goxls: add test cursor as test file + Title: "run file tests", + Command: "gop.test.cursor", + Arguments: args, + }}, + } + return codelens, nil + } + return nil, nil +}