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 25, 2024
1 parent 7cec065 commit bb164d3
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion gopls/internal/lsp/source/code_lens_gox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

0 comments on commit bb164d3

Please sign in to comment.