Documentation ¶
Index ¶
- Constants
- Variables
- func InsertComment(comment string, code []byte, pos Position) ([]byte, error)
- func NormalizeGeneratedComment(doc string) string
- func Prompt(input generate.Input) string
- func Target(identifier string) string
- type Finder
- type FinderOption
- type Option
- type Position
- type Service
- func (svc *Service) Extensions() []string
- func (svc *Service) Find(code []byte) ([]string, error)
- func (svc *Service) Minify(code []byte) ([]byte, error)
- func (svc *Service) Patch(ctx context.Context, identifier, doc string, code []byte) ([]byte, error)
- func (svc *Service) Prompt(input generate.Input) string
- type Symbol
Constants ¶
const ( // Var is a [Symbol] representing a variable in the code. Var = Symbol("var") // Class is a [Symbol] representing a class construct in the code. It is used by // the [Finder] to search for and filter class symbols within code. Class = Symbol("class") // Interface is a [Symbol] representing an interface code construct, used by the // Finder to search for and filter interface symbols within code. Interface = Symbol("iface") // Func represents a function symbol in the code. It is used by the Finder to // search for and filter function symbols within code. Func = Symbol("func") // Method represents a method symbol within a code construct, used by the Finder // to search for and filter methods within the code. Method = Symbol("method") // Property represents a property symbol used by the Finder to search for and // filter properties within code. Property = Symbol("prop") )
Variables ¶
var (
FileExtensions = []string{".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs"}
)
FileExtensions is a slice of supported file extensions for TypeScript and JavaScript files, including ".ts", ".tsx", ".js", ".jsx", ".mjs", and ".cjs".
Functions ¶
func InsertComment ¶
InsertComment inserts a given comment into the provided code at the specified position. It returns the modified code as a byte slice or an error if the position is out of range. The comment will be indented to match the surrounding code.
func NormalizeGeneratedComment ¶ added in v0.0.10
NormalizeGeneratedComment removes leading and trailing whitespaces, removes comment prefixes, and collapses consecutive whitespaces in the given documentation string. It also ensures that comment tokens are not accidentally closed within the comment text.
func Prompt ¶
Prompt generates a concise TSDoc comment prompt based on the provided Input, taking into account the type of identifier (e.g. function, property, method) and its context. The generated prompt instructs the user to focus on the purpose and usage of the identifier, without including links, source code, or code examples.
Types ¶
type Finder ¶
type Finder struct {
// contains filtered or unexported fields
}
Finder is a utility that searches for specified [Symbol]s within provided code and optionally includes documented symbols. It can also find the position of a given identifier within the code.
func NewFinder ¶
func NewFinder(opts ...FinderOption) *Finder
NewFinder creates a new Finder with the provided options. A Finder searches for symbols in TypeScript code and returns their positions.
func (*Finder) Find ¶
Find searches the provided code for symbols specified in the Finder configuration, and returns a slice of strings containing the found symbols. It respects the includeDocumented flag in the Finder configuration. The search is performed within the provided context.
type FinderOption ¶
type FinderOption func(*Finder)
FinderOption is a function that configures a Finder by modifying its fields. Common options include Symbols, IncludeDocumented, and WithLogger.
func IncludeDocumented ¶
func IncludeDocumented(include bool) FinderOption
IncludeDocumented is a FinderOption that configures a Finder to include or exclude documented symbols in its search results based on the provided boolean value.
func Symbols ¶
func Symbols(symbols ...Symbol) FinderOption
Symbols returns a FinderOption that appends the provided symbols to the list of symbols the Finder should search for in the code.
func WithLogger ¶
func WithLogger(log *slog.Logger) FinderOption
WithLogger sets the logger for a Finder. It accepts an *slog.Logger and returns a FinderOption that configures the Finder to use the provided logger.
type Option ¶
type Option func(*Service)
Option is a functional option type for configuring a Service instance. Use available options like WithFinder and Model to customize the behavior of the Service.
func WithFinder ¶
WithFinder sets the Finder for a Service. It is an Option for configuring a Service instance.
type Position ¶
Position represents a specific location within a text document, identified by its line number and character position on that line.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is a type that provides utility functions for TypeScript and JavaScript files, such as finding identifiers, minifying code, generating prompts, and patching comments into the code. It can be configured with different file finders and OpenAI models using the provided options.
func New ¶
New creates a new Service instance with the provided options. If no model or finder is specified, it uses the default openai model and a new Finder instance.
func (*Service) Extensions ¶
Extensions returns a slice of supported file extensions for the Service, such as ".ts", ".tsx", ".js", ".jsx", ".mjs", and ".cjs".
func (*Service) Find ¶
Find searches the given code for TypeScript and JavaScript identifiers and returns a slice of found identifiers. It may return an error if the search fails.
func (*Service) Minify ¶
Minify takes a byte slice of code and returns a minified version of the code as a byte slice, using the associated model. It returns an error if the minification process fails.