goindex

package module
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 7, 2024 License: MIT Imports: 4 Imported by: 0

README

goindex - Package for indexing go files

This module provides tools to manipulate go source files by indexing and extracting sections of code. This is useful when you are dealing with large files and want to extract type related sections into a separate file.

Quick start

$ go install github.com/gregoryv/goindex/cmd/...@latest

Index contents of a go file

$ index complex.go
complex.go 0 18 package testdata
complex.go 18 31 import
complex.go 31 54 // Decoupled comment
complex.go 54 96 func NewBoat() *Boat
complex.go 96 132 type Boat struct
complex.go 132 282 func (me *Boat) Model() string
complex.go 282 369 func DoSomething(v interface{ X() }) (interface{ S() int }, error)
complex.go 369 392 // Decoupled comment

then grab Boat related sections using a combination of grep and grab

$ goindex complex.go | grep Boat | grab
func NewBoat() *Boat {
        return &Boat{}
}

type Boat struct {
        model string
}

// Func comment
func (b *Boat) Model() string {
        if b.model == "" {
                return fmt.Sprintf("%s", "unknown")
        }
        // Inline comment
        return b.model
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cursor

type Cursor struct {
	// contains filtered or unexported fields
}

func NewCursor

func NewCursor(s *scanner.Scanner) *Cursor

NewCursor returns a cursor for the given scanner. The scanner should not be used outside of the cursor.

func (*Cursor) At

func (c *Cursor) At(file *token.File) int

func (*Cursor) InsideBrace

func (c *Cursor) InsideBrace() bool

func (*Cursor) InsideParen

func (c *Cursor) InsideParen() bool

func (*Cursor) Lit

func (c *Cursor) Lit() string

func (*Cursor) Next

func (c *Cursor) Next() bool

Next returns true until token.EOF is found

func (*Cursor) Pos

func (c *Cursor) Pos() token.Pos

func (*Cursor) Token

func (c *Cursor) Token() token.Token

type Section

type Section struct {
	// contains filtered or unexported fields
}

Section defines a section within a Go source file

func Index

func Index(src []byte) []Section

Index parses the given go source into sections. A section can be - comment - import - type - func If a comment is coupled to e.g. a func it's included in that section.

Example
src := []byte(`package x
// Greet returns a greeting
func Greet() string { return "hello" }`)

for _, s := range Index(src) {
	fmt.Println(s.String())
}
Output:

0 10 1 package x
10 76 3 func Greet() string

func (*Section) From

func (s *Section) From() int

From returns the starting position of this section

func (*Section) Grab

func (s *Section) Grab(src []byte) []byte

Grab returns the the sections src[From:To]

Example
src := []byte(`package x
// Greet returns a greeting
func Greet() string { return "hello" }`)

sections := Index(src)
os.Stdout.Write(sections[1].Grab(src))
Output:

// Greet returns a greeting
func Greet() string { return "hello" }

func (*Section) IsEmpty

func (s *Section) IsEmpty(src []byte) bool

IsEmpty returns true if the sections has no characters after bytes.TrimSpace has been applied.

func (*Section) Label added in v0.4.0

func (s *Section) Label() string

Label returns short value of this section, e.g. for funcs only the signature

func (*Section) Line added in v0.4.0

func (s *Section) Line() int

Line returns the line where this section starts. Lines begin with number 1.

func (*Section) String

func (s *Section) String() string

func (*Section) To

func (s *Section) To() int

To returns the end position of this section

Directories

Path Synopsis
cmd
goto
Command goto opens file on specific line
Command goto opens file on specific line
gotoi
Command goto opens file on specific line
Command goto opens file on specific line
grab
Command grab extracts sections from a file by byte range
Command grab extracts sections from a file by byte range
index
Command index indexes source files into sections
Command index indexes source files into sections

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL