Skip to content

Commit 8f4bd5b

Browse files
committed
update:support test.gox package test & file test
1 parent 7cec065 commit 8f4bd5b

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

gopls/internal/lsp/source/code_lens_gox.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,27 +182,53 @@ func gopToggleDetailsCodeLens(ctx context.Context, snapshot Snapshot, fh FileHan
182182

183183
func gopCommandCodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.CodeLens, error) {
184184
filename := fh.URI().Filename()
185-
if strings.HasSuffix(filename, "_test.go") || strings.HasSuffix(filename, "_test.gop") || strings.HasSuffix(filename, "test.gox") {
185+
if strings.HasSuffix(filename, "_test.go") || strings.HasSuffix(filename, "_test.gop") {
186186
return nil, nil
187187
}
188+
188189
pgf, err := snapshot.ParseGop(ctx, fh, parser.PackageClauseOnly)
189190
if err != nil {
190191
return nil, err
191192
}
192193
if pgf.File.Name.Name == "main" {
194+
isTest := strings.HasSuffix(filename, "test.gox")
195+
classType, _ := parserutil.GetClassType(pgf.File, fh.URI().Filename())
196+
codelens := []protocol.CodeLens{}
193197
rng, err := pgf.PosRange(pgf.File.Pos(), pgf.File.Pos())
194198
if err != nil {
195199
return nil, err
196200
}
197201
dir := protocol.URIFromSpanURI(span.URIFromPath(filepath.Dir(fh.URI().Filename())))
198-
args := command.RunGopCommandArgs{URI: dir, Command: "run"}
199-
cmd, err := command.NewRunGopCommandCommand("run main package", args)
200-
if err != nil {
201-
return nil, err
202+
if !isTest {
203+
args := command.RunGopCommandArgs{URI: dir, Command: "run"}
204+
cmd, err := command.NewRunGopCommandCommand("run main package", args)
205+
if err != nil {
206+
return nil, err
207+
}
208+
codelens = append(codelens, protocol.CodeLens{Range: rng, Command: &cmd})
209+
} else {
210+
pattern := regexp.MustCompile(`^case(?:_)?`) //goxls: remove case or case_
211+
if regexp.MustCompile(`^case(?:_)?`).MatchString(classType) {
212+
classType = pattern.ReplaceAllString(classType, "")
213+
}
214+
args, err := command.MarshalArgs(
215+
map[string]string{
216+
"functionName": "Test_" + classType,
217+
},
218+
)
219+
if err != nil {
220+
return nil, err
221+
}
222+
codelens = append(codelens, protocol.CodeLens{Range: rng, Command: &protocol.Command{
223+
Title: "run test package",
224+
Command: "gop.test.package",
225+
}}, protocol.CodeLens{Range: rng, Command: &protocol.Command{ // goxls: add test cursor as test file
226+
Title: "run file tests",
227+
Command: "gop.test.cursor",
228+
Arguments: args,
229+
}})
202230
}
203-
return []protocol.CodeLens{
204-
{Range: rng, Command: &cmd},
205-
}, nil
231+
return codelens, nil
206232
}
207233
return nil, nil
208234
}

0 commit comments

Comments
 (0)