From a89dd4b612b298564712d2e0921ee6d6ca82e1c1 Mon Sep 17 00:00:00 2001 From: ntrongkha Date: Thu, 28 Dec 2023 07:03:53 +0700 Subject: [PATCH] support pkgsite delimiter --- stdsym/main.go | 9 ++- symbol.go | 4 +- symbol_test.go | 170 +++++++++++++++++++++++++++++-------------------- 3 files changed, 110 insertions(+), 73 deletions(-) diff --git a/stdsym/main.go b/stdsym/main.go index 85d9ab0..964d5b4 100644 --- a/stdsym/main.go +++ b/stdsym/main.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "flag" "log" "os" "regexp" @@ -19,6 +20,12 @@ func main() { } func run() error { + web := flag.Bool("-web", false, "support web href") + flag.Parse() + deli := "." + if *web { + deli = "#" + } stdPattern := "std" pkgs, err := gostdsym.LoadPackages(stdPattern) if err != nil { @@ -30,7 +37,7 @@ func run() error { if isSkipPackage(pattern) { continue } - out, err := gostdsym.GetPackageSymbols(pattern) + out, err := gostdsym.GetPackageSymbols(pattern, deli) if err != nil { return err } diff --git a/symbol.go b/symbol.go index b0e6b8a..bf083c0 100644 --- a/symbol.go +++ b/symbol.go @@ -27,7 +27,7 @@ func LoadPackages(pattern string) ([]string, error) { } // GetPackageSymbols extracts all exported symbols from a package. -func GetPackageSymbols(pattern string) ([]string, error) { +func GetPackageSymbols(pattern string, deli string) ([]string, error) { wd, err := os.Getwd() if err != nil { return nil, err @@ -44,7 +44,7 @@ func GetPackageSymbols(pattern string) ([]string, error) { syms = slices.Compact(syms) for i := range syms { - syms[i] = buildPkg.ImportPath + "." + syms[i] + syms[i] = buildPkg.ImportPath + deli + syms[i] } syms = append(syms, buildPkg.ImportPath) return syms, nil diff --git a/symbol_test.go b/symbol_test.go index 263c7f0..7a114b1 100644 --- a/symbol_test.go +++ b/symbol_test.go @@ -14,74 +14,77 @@ func TestAll(t *testing.T) { for _, test := range []struct { in string + deli string want []string }{ - {in: "cmp", want: []string{"cmp", "cmp.Less", "cmp.Ordered", "cmp.Compare"}}, - {in: "html/template", want: []string{ - "html/template", - "html/template.CSS", - "html/template.ErrAmbigContext", - "html/template.ErrBadHTML", - "html/template.ErrBranchEnd", - "html/template.ErrEndContext", - "html/template.ErrJSTemplate", - "html/template.ErrNoSuchTemplate", - "html/template.ErrOutputContext", - "html/template.ErrPartialCharset", - "html/template.ErrPartialEscape", - "html/template.ErrPredefinedEscaper", - "html/template.ErrRangeLoopReentry", - "html/template.ErrSlashAmbig", - "html/template.Error", - "html/template.Error.Error", - "html/template.ErrorCode", - "html/template.FuncMap", - "html/template.HTML", - "html/template.HTMLAttr", - "html/template.HTMLEscape", - "html/template.HTMLEscapeString", - "html/template.HTMLEscaper", - "html/template.IsTrue", - "html/template.JS", - "html/template.JSEscape", - "html/template.JSEscapeString", - "html/template.JSEscaper", - "html/template.JSStr", - "html/template.Must", - "html/template.New", - "html/template.OK", - "html/template.ParseFS", - "html/template.ParseFiles", - "html/template.ParseGlob", - "html/template.Srcset", - "html/template.Template", - "html/template.Template.AddParseTree", - "html/template.Template.Clone", - "html/template.Template.DefinedTemplates", - "html/template.Template.Delims", - "html/template.Template.Execute", - "html/template.Template.ExecuteTemplate", - "html/template.Template.Funcs", - "html/template.Template.Lookup", - "html/template.Template.Name", - "html/template.Template.New", - "html/template.Template.Option", - "html/template.Template.Parse", - "html/template.Template.ParseFS", - "html/template.Template.ParseFiles", - "html/template.Template.ParseGlob", - "html/template.Template.Templates", - "html/template.URL", - "html/template.URLQueryEscaper", - "html/template.eatAttrName", - "html/template.errorf", - "html/template.parseFS", - "html/template.parseFiles", - "html/template.parseGlob", - }}, + {in: "cmp", deli: ".", want: []string{"cmp", "cmp.Less", "cmp.Ordered", "cmp.Compare"}}, + {in: "html/template", + deli: ".", + want: []string{ + "html/template", + "html/template.CSS", + "html/template.ErrAmbigContext", + "html/template.ErrBadHTML", + "html/template.ErrBranchEnd", + "html/template.ErrEndContext", + "html/template.ErrJSTemplate", + "html/template.ErrNoSuchTemplate", + "html/template.ErrOutputContext", + "html/template.ErrPartialCharset", + "html/template.ErrPartialEscape", + "html/template.ErrPredefinedEscaper", + "html/template.ErrRangeLoopReentry", + "html/template.ErrSlashAmbig", + "html/template.Error", + "html/template.Error.Error", + "html/template.ErrorCode", + "html/template.FuncMap", + "html/template.HTML", + "html/template.HTMLAttr", + "html/template.HTMLEscape", + "html/template.HTMLEscapeString", + "html/template.HTMLEscaper", + "html/template.IsTrue", + "html/template.JS", + "html/template.JSEscape", + "html/template.JSEscapeString", + "html/template.JSEscaper", + "html/template.JSStr", + "html/template.Must", + "html/template.New", + "html/template.OK", + "html/template.ParseFS", + "html/template.ParseFiles", + "html/template.ParseGlob", + "html/template.Srcset", + "html/template.Template", + "html/template.Template.AddParseTree", + "html/template.Template.Clone", + "html/template.Template.DefinedTemplates", + "html/template.Template.Delims", + "html/template.Template.Execute", + "html/template.Template.ExecuteTemplate", + "html/template.Template.Funcs", + "html/template.Template.Lookup", + "html/template.Template.Name", + "html/template.Template.New", + "html/template.Template.Option", + "html/template.Template.Parse", + "html/template.Template.ParseFS", + "html/template.Template.ParseFiles", + "html/template.Template.ParseGlob", + "html/template.Template.Templates", + "html/template.URL", + "html/template.URLQueryEscaper", + "html/template.eatAttrName", + "html/template.errorf", + "html/template.parseFS", + "html/template.parseFiles", + "html/template.parseGlob", + }}, { - in: "container/list", - + in: "container/list", + deli: ".", want: []string{ "container/list", "container/list.Element", @@ -106,10 +109,36 @@ func TestAll(t *testing.T) { "container/list.List.Remove", }, }, - { - in: "context", - + in: "container/list", + deli: "#", + want: []string{ + "container/list", + "container/list#Element", + "container/list#Element.Next", + "container/list#Element.Prev", + "container/list#List", + "container/list#New", + "container/list#List.Back", + "container/list#List.Front", + "container/list#List.Init", + "container/list#List.InsertAfter", + "container/list#List.InsertBefore", + "container/list#List.Len", + "container/list#List.MoveAfter", + "container/list#List.MoveBefore", + "container/list#List.MoveToBack", + "container/list#List.MoveToFront", + "container/list#List.PushBack", + "container/list#List.PushBackList", + "container/list#List.PushFront", + "container/list#List.PushFrontList", + "container/list#List.Remove", + }, + }, + { + in: "context", + deli: ".", want: []string{ "context", "context.AfterFunc", @@ -132,7 +161,8 @@ func TestAll(t *testing.T) { }, }, { - in: "errors", + in: "errors", + deli: ".", want: []string{ "errors", "errors.Is", @@ -144,7 +174,7 @@ func TestAll(t *testing.T) { }, }, } { - got, err := GetPackageSymbols(test.in) + got, err := GetPackageSymbols(test.in, test.deli) if err != nil { t.Fatalf("want no error for MustExtract, got: %v", err) }