@@ -4,17 +4,9 @@ import (
44 "fmt"
55 "go/format"
66 "io"
7- "sort"
87 "strings"
98)
109
11- // Identifier represents a program entity such as a function, struct, method,
12- // interface, etc. that can be exported by a package or kept private.
13- type Identifier interface {
14- Ident () string
15- IsExported () bool
16- }
17-
1810// Package represents a go package containing functions and types such as
1911// structs and interfaces.
2012type Package struct {
@@ -24,14 +16,8 @@ type Package struct {
2416 Types []TypeDef `json:"types"`
2517}
2618
27- // OrderIdents arranges package entities, sorting them first by whether they are
28- // exported or unexported, and then alphabetically.
29- func (p Package ) OrderIdents () {
30- p .orderFuncs ()
31- }
32-
3319// Source returns the formatted package signature source.
34- func (p Package ) Source () (string , error ) {
20+ func (p * Package ) Source () (string , error ) {
3521 formatted , err := format .Source ([]byte (p .String ()))
3622 if err != nil {
3723 return "" , fmt .Errorf ("formatting source: %w" , err )
@@ -41,7 +27,7 @@ func (p Package) Source() (string, error) {
4127}
4228
4329// String returns the unformatted package signature source.
44- func (p Package ) String () string {
30+ func (p * Package ) String () string {
4531 var b strings.Builder
4632
4733 if p .Doc != "" {
@@ -63,17 +49,6 @@ func (p Package) String() string {
6349 return b .String ()
6450}
6551
66- func (p Package ) orderFuncs () {
67- sortFn := func (i , j int ) bool {
68- funcI := p .Funcs [i ]
69- funcJ := p .Funcs [j ]
70-
71- return funcI .Less (funcJ )
72- }
73-
74- sort .SliceStable (p .Funcs , sortFn )
75- }
76-
7752// Func represents a function or a struct method if the Receiver field contains
7853// a pointer to a [FuncReceiver].
7954type Func struct {
@@ -87,30 +62,15 @@ type Func struct {
8762}
8863
8964// Ident returns the function's name.
90- //
91- // Part of the [Identifier] interface implementation.
9265func (f Func ) Ident () string {
9366 return f .Name
9467}
9568
9669// IsExported returns true if the function is exported.
97- //
98- // Part of the [Identifier] interface implementation.
9970func (f Func ) IsExported () bool {
10071 return isExportedIdent (f .Name )
10172}
10273
103- // Less returns true if Func must sort before other Func.
104- func (f Func ) Less (other Func ) bool {
105- if f .IsExported () && ! other .IsExported () {
106- return true
107- } else if ! f .IsExported () && other .IsExported () {
108- return false
109- }
110-
111- return f .Ident () > other .Ident ()
112- }
113-
11474// String returns the function signature code.
11575func (f Func ) String () string {
11676 var b strings.Builder
@@ -136,6 +96,7 @@ func (f Func) String() string {
13696 return b .String ()
13797}
13898
99+ // TypeDef represents a type definition.
139100type TypeDef struct {
140101 Type string `json:"type"`
141102 Name string `json:"name"`
@@ -151,6 +112,17 @@ type TypeDef struct {
151112 Methods []Func `json:"methods,omitempty"`
152113}
153114
115+ // Ident returns the type definition's name.
116+ func (td TypeDef ) Ident () string {
117+ return td .Name
118+ }
119+
120+ // IsExported returns true if the type definition is exported.
121+ func (td TypeDef ) IsExported () bool {
122+ return isExportedIdent (td .Name )
123+ }
124+
125+ // String returns the type definition code.
154126func (td TypeDef ) String () string {
155127 var b strings.Builder
156128
@@ -191,15 +163,11 @@ type Field struct {
191163}
192164
193165// Ident returns the name of the struct field.
194- //
195- // Part of the [Identifier] interface implementation.
196166func (sf Field ) Ident () string {
197167 return sf .Names [0 ]
198168}
199169
200170// IsExported returns true if the struct field is exported.
201- //
202- // Part of the [Identifier] interface implementation.
203171func (sf Field ) IsExported () bool {
204172 return isExportedIdent (sf .Names [0 ])
205173}
0 commit comments