golang

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// FileExtensions represents the set of file extensions that are supported by
	// the service for processing.
	FileExtensions = []string{".go"}

	// DefaultMinification represents a sequence of steps used to progressively
	// simplify the code structure, potentially making it more concise while aiming
	// to reduce its token count without altering its functionality. It serves as
	// the initial configuration for services that perform code minification.
	DefaultMinification = []nodes.MinifyOptions{
		nodes.MinifyUnexported,
		{
			FuncBody: true,
			Exported: true,
		},
		nodes.MinifyExported,
		nodes.MinifyAll,
	}
)

Functions

func Prompt

func Prompt(input generate.PromptInput) string

Prompt generates a templated GoDoc comment block based on the provided input, which includes the identifier and associated code. It ensures that the generated comment adheres to idiomatic GoDoc conventions and instructs users on how to write descriptive comments without including technical details such as external links or source code examples. The output is designed to guide the user in documenting their code effectively while maintaining consistency with Go library documentation standards.

func Target

func Target(identifier string) string

Target constructs a string representation of a given identifier within Go source code, indicating whether it is a function, type, or variable by prefixing the identifier with an appropriate label. If the identifier does not match any of the expected formats, it returns the identifier as-is enclosed in quotes.

Types

type Finder

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

Finder locates identifiers in Go source code, taking into account options for including test functions and documented entities. It analyzes the provided code to produce a sorted list of exported names. The search can be customized through options to either include or exclude test functions and documented identifiers. When examining interface types, it also identifies and includes their exported methods. Finder returns a slice of strings representing the found identifiers and any errors encountered during the analysis process.

func NewFinder

func NewFinder(opts ...FinderOption) *Finder

NewFinder constructs a new Finder with optional configurations provided by FinderOptions. It returns a pointer to the initialized Finder.

func (*Finder) Find

func (f *Finder) Find(code []byte) ([]string, error)

Find searches through the provided code for identifiers that are eligible based on the Finder's configuration. It returns a sorted slice of strings containing these identifiers and an error if the code cannot be parsed or another issue occurs. Identifiers from function declarations, type specifications, and value specifications are included unless they are filtered out by the Finder's settings, such as excluding test functions or documented identifiers.

type FinderOption

type FinderOption func(*Finder)

FinderOption configures the behavior of a *Finder by setting its internal options. It is applied when constructing a new *Finder instance, allowing customization such as whether to include tests or documented entities in the search results.

func FindTests

func FindTests(find bool) FinderOption

FindTests configures a Finder instance to determine whether it should identify test functions during code analysis. If the provided argument is true, the Finder will include test functions in its findings; otherwise, it will exclude them. This option can be passed to NewFinder to customize the Finder's behavior.

func IncludeDocumented

func IncludeDocumented(include bool) FinderOption

IncludeDocumented configures a Finder to consider documented entities during the search. When set to true, entities with associated documentation will be included in the findings; otherwise, they will be excluded. This option is used when creating a new Finder instance.

type Option

type Option func(*Service)

Option configures a Service by setting various internal fields such as model, finder, and minification steps. It is used in conjunction with the Service constructor and other setup functions that accept optional configurations to modify the behavior of the Service instance.

func ClearComments added in v0.0.10

func ClearComments(clear bool) Option

ClearComments configures whether a *Service should remove comments from the code during processing.

func Minify

func Minify(steps []nodes.MinifyOptions) Option

Minify applies a series of transformations to Go source code represented as a byte slice to reduce its size, potentially making it more suitable for processing within token-based limitations. It returns the minified source code as a byte slice and an error if the minification process fails. If the resulting code after minification still exceeds the maximum allowed token count, an error is returned detailing the token limit and the actual token count.

func Model

func Model(m string) Option

Model configures the model identifier for a Service. It sets the underlying model that the Service will use for operations such as tokenization and code analysis.

func WithFinder

func WithFinder(f *Finder) Option

WithFinder sets the finder to be used by the Service for locating specific elements or patterns in code. It accepts a Finder and returns an Option that, when applied, configures a Service with the provided Finder.

type Service

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

Service represents an abstraction for processing and manipulating Go source code. It facilitates various operations such as finding specific elements within the code, minifying the code to reduce its token count, clearing comments from the source, generating prompts based on the provided input, and patching the documentation of code entities. The Service can be customized with different options that alter its behavior, including setting a custom finder for locating elements in the code, specifying a model for tokenization, defining minification steps, and deciding whether to clear comments during prompt generation. It provides functionality to handle file extensions associated with Go source files and performs encoding of the source code into tokens using an internal tokenizer. The Service ensures that the resultant minified code does not exceed a predefined maximum token count and allows for dynamic updates to documentation comments within the source code.

func Must

func Must(opts ...Option) *Service

Must creates a new Service with the provided options, panicking if an error occurs during its creation. It ensures that a Service is returned without the need to handle errors directly, simplifying initialization in cases where failure is not expected or cannot be recovered from. It is intended for use when the program cannot continue if the Service cannot be constructed. Must returns a *Service.

func New

func New(opts ...Option) (*Service, error)

New initializes a new Service with the provided options. It returns a pointer to the initialized Service and an error if there is any problem during the initialization.

func (*Service) Extensions

func (svc *Service) Extensions() []string

Extensions retrieves a list of file extensions that the service recognizes and works with. It returns a fresh copy of the list to avoid modifying the original list of supported extensions.

func (*Service) Find

func (svc *Service) Find(code []byte) ([]string, error)

Find locates identifiers within the provided source code and returns a list of those identifiers along with any errors encountered during the search. It defers the actual searching to the associated Finder type within the Service. If no identifiers are found or an error occurs, it may return an empty list and the corresponding error.

func (*Service) Minify

func (svc *Service) Minify(code []byte) ([]byte, error)

Minify reduces the size of the given Go source code while aiming to preserve its functionality. It applies a series of transformations defined by the service's configuration to progressively simplify and shrink the code. The method stops minifying when the code's size falls below a certain threshold measured in tokens or when no further reductions can be made without exceeding that limit. If successful, it returns the minified code; otherwise, it returns an error indicating why minification failed, such as if the resulting code still exceeds the maximum allowed token count.

func (*Service) Patch

func (svc *Service) Patch(ctx context.Context, identifier, doc string, code []byte) ([]byte, error)

Patch applies a documentation string to the declaration identified by the specified identifier within the given source code. It updates or adds documentation comments in the source code while preserving the original structure and formatting. If the identifier does not exist within the source code, Patch returns an error indicating that the node was not found. On successful application of the documentation string, Patch returns the updated source code as a byte slice along with a nil error. If an error occurs during parsing or formatting of the source code, Patch will return the error encountered.

func (*Service) Prompt

func (svc *Service) Prompt(input generate.PromptInput) string

Prompt prepares the input code by potentially clearing comments and then passes the modified input to the underlying Prompt function. If the clearComments option is enabled in the Service, it removes all comments from the input code before generating a prompt. It returns the generated output as a string.

Jump to

Keyboard shortcuts

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