From d08380fb3afe60ce12b2afa1733df232970d430c Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 9 Apr 2024 21:45:38 +0800 Subject: [PATCH 1/6] fix:ignore 'run main package' codelens of test.gox --- gopls/internal/lsp/source/code_lens_gox.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gopls/internal/lsp/source/code_lens_gox.go b/gopls/internal/lsp/source/code_lens_gox.go index fe5ea5433ed..21978912eb7 100644 --- a/gopls/internal/lsp/source/code_lens_gox.go +++ b/gopls/internal/lsp/source/code_lens_gox.go @@ -182,7 +182,7 @@ 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") { + if strings.HasSuffix(filename, "_test.go") || strings.HasSuffix(filename, "_test.gop") || strings.HasSuffix(filename, "test.gox") { return nil, nil } pgf, err := snapshot.ParseGop(ctx, fh, parser.PackageClauseOnly) From 5cc1d2271e46d72cd3e43d79cc6c44bfbd8def75 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Fri, 12 Apr 2024 19:27:34 +0800 Subject: [PATCH 2/6] update:test.gox's symbol --- gopls/internal/lsp/source/symbols_gox.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gopls/internal/lsp/source/symbols_gox.go b/gopls/internal/lsp/source/symbols_gox.go index 6e83dd37426..d4f71c0cffe 100644 --- a/gopls/internal/lsp/source/symbols_gox.go +++ b/gopls/internal/lsp/source/symbols_gox.go @@ -25,7 +25,7 @@ func GopDocumentSymbols(ctx context.Context, snapshot Snapshot, fh FileHandle) ( return nil, fmt.Errorf("getting file for GopDocumentSymbols: %w", err) } file := pgf.File - classType, _ := parserutil.GetClassType(file, fh.URI().Filename()) + classType, isTest := parserutil.GetClassType(file, fh.URI().Filename()) // Build symbols for file declarations. When encountering a declaration with // errors (typically because positions are invalid), we skip the declaration // entirely. VS Code fails to show any symbols if one of the top-level @@ -50,6 +50,9 @@ func GopDocumentSymbols(ctx context.Context, snapshot Snapshot, fh FileHandle) ( name = "Main" } } + if decl.Shadow && name == "Main" && isTest { + name = "TestMain" //test gox file + } if !decl.IsClass && decl.Recv != nil && len(decl.Recv.List) > 0 { fs.Name = fmt.Sprintf("(%s).%s", typesutil.ExprString(decl.Recv.List[0].Type), fs.Name) } else { From bf40a7108ec9e54855d930ee39c930606c182541 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 16 Apr 2024 11:48:47 +0800 Subject: [PATCH 3/6] fix:remove trans test info from symbol --- gopls/internal/lsp/source/symbols_gox.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/gopls/internal/lsp/source/symbols_gox.go b/gopls/internal/lsp/source/symbols_gox.go index d4f71c0cffe..6e83dd37426 100644 --- a/gopls/internal/lsp/source/symbols_gox.go +++ b/gopls/internal/lsp/source/symbols_gox.go @@ -25,7 +25,7 @@ func GopDocumentSymbols(ctx context.Context, snapshot Snapshot, fh FileHandle) ( return nil, fmt.Errorf("getting file for GopDocumentSymbols: %w", err) } file := pgf.File - classType, isTest := parserutil.GetClassType(file, fh.URI().Filename()) + classType, _ := parserutil.GetClassType(file, fh.URI().Filename()) // Build symbols for file declarations. When encountering a declaration with // errors (typically because positions are invalid), we skip the declaration // entirely. VS Code fails to show any symbols if one of the top-level @@ -50,9 +50,6 @@ func GopDocumentSymbols(ctx context.Context, snapshot Snapshot, fh FileHandle) ( name = "Main" } } - if decl.Shadow && name == "Main" && isTest { - name = "TestMain" //test gox file - } if !decl.IsClass && decl.Recv != nil && len(decl.Recv.List) > 0 { fs.Name = fmt.Sprintf("(%s).%s", typesutil.ExprString(decl.Recv.List[0].Type), fs.Name) } else { From 7c5ac738057d2b8945da98ab6d49b2fa12c78630 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 16 Apr 2024 11:51:48 +0800 Subject: [PATCH 4/6] update:support test.gox package test & file test --- gopls/internal/lsp/source/code_lens_gox.go | 44 +++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/gopls/internal/lsp/source/code_lens_gox.go b/gopls/internal/lsp/source/code_lens_gox.go index 21978912eb7..2682159bbf2 100644 --- a/gopls/internal/lsp/source/code_lens_gox.go +++ b/gopls/internal/lsp/source/code_lens_gox.go @@ -12,6 +12,7 @@ import ( "regexp" "strings" + "github.com/goplus/gop" "github.com/goplus/gop/ast" "github.com/goplus/gop/parser" "golang.org/x/tools/gopls/internal/goxls" @@ -182,13 +183,20 @@ 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 } + mod, err := snapshot.GopModForFile(ctx, fh.URI()) + if err != nil { + return nil, err + } pgf, err := snapshot.ParseGop(ctx, fh, parser.PackageClauseOnly) if err != nil { return nil, err } + if classType, isTest := gop.GetFileClassType(mod, pgf.File, filename); isTest { + return goxTestCodeLens(pgf, classType) + } if pgf.File.Name.Name == "main" { rng, err := pgf.PosRange(pgf.File.Pos(), pgf.File.Pos()) if err != nil { @@ -206,3 +214,37 @@ func gopCommandCodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle) ( } return nil, nil } + +func goxTestCodeLens(pgf *ParsedGopFile, classType string) ([]protocol.CodeLens, error) { + if pgf.File.Name.Name == "main" { + 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 +} From 5af9b3486962c4cb909fb5ffe141675da66e6bad Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 7 May 2024 15:10:26 +0800 Subject: [PATCH 5/6] update:GoxTestClassType --- gopls/internal/lsp/source/code_lens_gox.go | 60 +++++++++------------- gopls/internal/lsp/source/util_gox.go | 14 +++++ gopls/internal/lsp/source/util_gox_test.go | 59 +++++++++++++++++++++ 3 files changed, 97 insertions(+), 36 deletions(-) create mode 100644 gopls/internal/lsp/source/util_gox_test.go diff --git a/gopls/internal/lsp/source/code_lens_gox.go b/gopls/internal/lsp/source/code_lens_gox.go index 2682159bbf2..ba42a12ae1b 100644 --- a/gopls/internal/lsp/source/code_lens_gox.go +++ b/gopls/internal/lsp/source/code_lens_gox.go @@ -12,7 +12,6 @@ import ( "regexp" "strings" - "github.com/goplus/gop" "github.com/goplus/gop/ast" "github.com/goplus/gop/parser" "golang.org/x/tools/gopls/internal/goxls" @@ -186,15 +185,11 @@ func gopCommandCodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle) ( if strings.HasSuffix(filename, "_test.go") || strings.HasSuffix(filename, "_test.gop") { return nil, nil } - mod, err := snapshot.GopModForFile(ctx, fh.URI()) - if err != nil { - return nil, err - } pgf, err := snapshot.ParseGop(ctx, fh, parser.PackageClauseOnly) if err != nil { return nil, err } - if classType, isTest := gop.GetFileClassType(mod, pgf.File, filename); isTest { + if classType, isGoxTest := GoxTestClassType(pgf.File, filename); isGoxTest { return goxTestCodeLens(pgf, classType) } if pgf.File.Name.Name == "main" { @@ -216,35 +211,28 @@ func gopCommandCodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle) ( } func goxTestCodeLens(pgf *ParsedGopFile, classType string) ([]protocol.CodeLens, error) { - if pgf.File.Name.Name == "main" { - 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 + rng, err := pgf.PosRange(pgf.File.Pos(), pgf.File.Pos()) + if err != nil { + return nil, err } - return nil, nil + 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 } diff --git a/gopls/internal/lsp/source/util_gox.go b/gopls/internal/lsp/source/util_gox.go index 24b9dcc35eb..a306e86ef2b 100644 --- a/gopls/internal/lsp/source/util_gox.go +++ b/gopls/internal/lsp/source/util_gox.go @@ -11,6 +11,7 @@ import ( "strings" "github.com/goplus/gop/ast" + "github.com/goplus/gop/cl" "github.com/goplus/gop/printer" "github.com/goplus/gop/token" "github.com/goplus/gop/x/typesutil" @@ -287,3 +288,16 @@ func gopEmbeddedIdent(x ast.Expr) *ast.Ident { } return nil } + +func GoxTestClassType(file *ast.File, filename string) (classType string, isGoxTest bool) { + if file.IsClass { + var ext string + classType, _, ext = cl.ClassNameAndExt(filename) + if file.IsNormalGox { + isGoxTest = strings.HasSuffix(ext, "_test.gox") + } else { + isGoxTest = strings.HasSuffix(ext, "test.gox") + } + } + return +} diff --git a/gopls/internal/lsp/source/util_gox_test.go b/gopls/internal/lsp/source/util_gox_test.go new file mode 100644 index 00000000000..d65acfd717f --- /dev/null +++ b/gopls/internal/lsp/source/util_gox_test.go @@ -0,0 +1,59 @@ +package source + +import ( + "github.com/goplus/gop/ast" + + "testing" +) + +func TestGoxTestClassType(t *testing.T) { + type testData struct { + isClass bool + isNormalGox bool + isProj bool + fileName string + classType string + isGoxTest bool + } + tests := + []*testData{ + {false, false, false, "abc.gop", "", false}, + {false, false, false, "abc_test.gop", "", false}, + + {true, true, false, "abc.gox", "abc", false}, + {true, true, false, "Abc.gox", "Abc", false}, + {true, true, false, "abc_demo.gox", "abc", false}, + {true, true, false, "Abc_demo.gox", "Abc", false}, + + {true, false, false, "get.yap", "get", false}, + {true, false, false, "get_p_#id.yap", "get_p_id", false}, + {true, false, true, "main.yap", "main", false}, + + {true, true, false, "main.gox", "main", false}, + {true, true, false, "main_demo.gox", "main", false}, + {true, true, false, "abc_xtest.gox", "abc", false}, + {true, true, false, "main_xtest.gox", "main", false}, + + {true, true, false, "abc_test.gox", "abc", true}, + {true, true, false, "Abc_test.gox", "Abc", true}, + {true, true, false, "main_test.gox", "main", true}, + + {true, false, false, "abc_yap.gox", "abc", false}, + {true, false, false, "Abc_yap.gox", "Abc", false}, + {true, false, true, "main_yap.gox", "main", false}, + + {true, false, false, "abc_ytest.gox", "abc", true}, + {true, false, false, "Abc_ytest.gox", "Abc", true}, + {true, false, true, "main_ytest.gox", "main", true}, + } + for _, test := range tests { + f := &ast.File{IsClass: test.isClass, IsNormalGox: test.isNormalGox, IsProj: test.isProj} + classType, isGoxTest := GoxTestClassType(f, test.fileName) + if isGoxTest != test.isGoxTest { + t.Fatalf("%v check classType isTest want %v, got %v.", test.fileName, test.isGoxTest, isGoxTest) + } + if classType != test.classType { + t.Fatalf("%v getClassType want %v, got %v.", test.fileName, test.classType, classType) + } + } +} From 7ddb9a17e85db3898314ec0715f62d65dbfb224a Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Fri, 10 May 2024 23:41:58 +0800 Subject: [PATCH 6/6] update:codelens for proj test classfile --- gopls/internal/lsp/source/code_lens_gox.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/gopls/internal/lsp/source/code_lens_gox.go b/gopls/internal/lsp/source/code_lens_gox.go index ba42a12ae1b..0767ec3edec 100644 --- a/gopls/internal/lsp/source/code_lens_gox.go +++ b/gopls/internal/lsp/source/code_lens_gox.go @@ -215,6 +215,15 @@ func goxTestCodeLens(pgf *ParsedGopFile, classType string) ([]protocol.CodeLens, if err != nil { return nil, err } + codelens := []protocol.CodeLens{ + {Range: rng, Command: &protocol.Command{ + Title: "run test package", + Command: "gop.test.package", + }}, + } + if pgf.File.IsProj && classType == "main" { //goxls:proj gox test + return codelens, nil + } args, err := command.MarshalArgs( map[string]string{ "functionName": "Test_" + classType, @@ -223,16 +232,13 @@ func goxTestCodeLens(pgf *ParsedGopFile, classType string) ([]protocol.CodeLens, 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 + codelens = append(codelens, protocol.CodeLens{ + Range: rng, + Command: &protocol.Command{ Title: "run file tests", Command: "gop.test.cursor", Arguments: args, - }}, - } + }, + }) return codelens, nil }