From 904b65b1d8ba3b353f56bb01926d2b481c5ef47f Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 16 Apr 2024 11:51:48 +0800 Subject: [PATCH] update:support test.gox package test & file test --- gopls/internal/lsp/source/code_lens_gox.go | 42 +++++++++++++++++----- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/gopls/internal/lsp/source/code_lens_gox.go b/gopls/internal/lsp/source/code_lens_gox.go index 21978912eb7..7cf17c2e065 100644 --- a/gopls/internal/lsp/source/code_lens_gox.go +++ b/gopls/internal/lsp/source/code_lens_gox.go @@ -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 }