Skip to content

Commit bb164d3

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

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

gopls/internal/lsp/source/code_lens_gox.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,11 @@ 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+
// TODO: Use parserutil.GetClassType Instead
186+
if strings.HasSuffix(filename, "test.gox") {
187+
return goxTestCodeLens(ctx, snapshot, fh)
188+
}
189+
if strings.HasSuffix(filename, "_test.go") || strings.HasSuffix(filename, "_test.gop") {
186190
return nil, nil
187191
}
188192
pgf, err := snapshot.ParseGop(ctx, fh, parser.PackageClauseOnly)
@@ -206,3 +210,42 @@ func gopCommandCodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle) (
206210
}
207211
return nil, nil
208212
}
213+
214+
func goxTestCodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.CodeLens, error) {
215+
pgf, err := snapshot.ParseGop(ctx, fh, parser.PackageClauseOnly)
216+
if err != nil {
217+
return nil, err
218+
}
219+
if pgf.File.Name.Name == "main" {
220+
classType, _ := parserutil.GetClassType(pgf.File, fh.URI().Filename())
221+
rng, err := pgf.PosRange(pgf.File.Pos(), pgf.File.Pos())
222+
if err != nil {
223+
return nil, err
224+
}
225+
pattern := regexp.MustCompile(`^case(?:_)?`) //goxls: remove case or case_
226+
if pattern.MatchString(classType) {
227+
classType = pattern.ReplaceAllString(classType, "")
228+
}
229+
args, err := command.MarshalArgs(
230+
map[string]string{
231+
"functionName": "Test_" + classType,
232+
},
233+
)
234+
if err != nil {
235+
return nil, err
236+
}
237+
codelens := []protocol.CodeLens{
238+
{Range: rng, Command: &protocol.Command{
239+
Title: "run test package",
240+
Command: "gop.test.package",
241+
}},
242+
{Range: rng, Command: &protocol.Command{ // goxls: add test cursor as test file
243+
Title: "run file tests",
244+
Command: "gop.test.cursor",
245+
Arguments: args,
246+
}},
247+
}
248+
return codelens, nil
249+
}
250+
return nil, nil
251+
}

0 commit comments

Comments
 (0)