ts

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: May 15, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
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

View Source
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

func InsertComment(comment string, code []byte, pos Position) ([]byte, error)

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

func NormalizeGeneratedComment(doc string) string

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

func Prompt(input generate.Input) string

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.

func Target

func Target(identifier string) string

Target returns a human-readable representation of the given identifier, describing its type and name.

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

func (f *Finder) Find(ctx context.Context, code []byte) ([]string, error)

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.

func (*Finder) Position

func (f *Finder) Position(ctx context.Context, identifier string, code []byte) (Position, error)

Position determines the line and character position of the specified identifier within the provided code. It returns a Position struct containing the line and character information or an error if the operation fails.

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 Model

func Model(model string) Option

Model sets the model to be used by the Service in the given Option.

func WithFinder

func WithFinder(f *Finder) Option

WithFinder sets the Finder for a Service. It is an Option for configuring a Service instance.

type Position

type Position struct {
	Line      int
	Character int
}

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

func New(opts ...Option) *Service

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

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

Extensions returns a slice of supported file extensions for the Service, such as ".ts", ".tsx", ".js", ".jsx", ".mjs", and ".cjs".

func (*Service) Find

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

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

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

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.

func (*Service) Patch

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

Patch inserts a formatted documentation comment for the given identifier in the provided code and returns the modified code. The generated comment is based on the provided doc string and inserted at the position determined by the finder service.

func (*Service) Prompt

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

Prompt generates a prompt string from the provided generate.Input, which can be used in a code generation service request.

type Symbol

type Symbol string

Symbol represents a type of code construct, such as variables, classes, interfaces, functions, methods, or properties. It is used by the Finder to search for and filter specific types of symbols within code.

Jump to

Keyboard shortcuts

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