codegen

package
v0.0.0-...-7b96089 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2023 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BuildStep

type BuildStep struct{}

func (*BuildStep) Process

func (bs *BuildStep) Process(ctx context.Context, bctx *build.Context) (result build.StepResult, err error)

func (*BuildStep) ProcessNode

func (bs *BuildStep) ProcessNode(ctx context.Context, bctx *build.Context, sf psi.SourceFile, root psi.Node, opts ...NodeProcessorOption) (psi.Node, error)

ProcessNode processes the given node and returns the updated node.

type NodeProcessor

type NodeProcessor struct {
	Project    project.Project // The project associated with the NodeProcessor.
	SourceFile psi.SourceFile  // The source file being processed.
	Root       psi.Node        // The root node of the AST being processed.
	FuncStack  []*NodeScope    // A stack of FunctionContexts.
	// contains filtered or unexported fields
}

NodeProcessor is responsible for processing AST nodes and generating code. It is used to configure the behavior of the NodeProcessor. Each option is a function that takes a pointer to the NodeProcessor as a parameter and modifies its properties in some way.

func (*NodeProcessor) OnEnter

func (p *NodeProcessor) OnEnter(cursor psi.Cursor) error

OnEnter method is called when entering a node during the AST traversal. It checks if the node is a container, and if so, pushes a new NodeScope onto the FuncStack. Additionally, it scans the comments of the node for TODOs and stores them in the current NodeScope.

Parameters: - cursor: The psi.Cursor representing the current node.

Returns: - bool: true to continue traversing the AST, false to stop.

OnEnter is responsible for pushing a new NodeScope onto the FuncStack if the current node is a container. Additionally, it scans the comments of the node for TODOs and stores them in the current NodeScope.

func (*NodeProcessor) OnLeave

func (p *NodeProcessor) OnLeave(cursor psi.Cursor) error

OnLeave method is called when leaving a node during the AST traversal. It checks if the node is a container, and if so, pops the top NodeScope from the FuncStack. It also checks if the current function should be processed and calls the Step method to process the function if necessary.

Parameters: - cursor: The psi.Cursor representing the current node.

Returns: - bool: true to continue traversing the AST, false to stop.

OnLeave is responsible for popping the top NodeScope from the FuncStack if the current node is a container. Additionally, it checks if the current function should be processed and calls the Step method to process the function if necessary.

func (*NodeProcessor) Step

func (p *NodeProcessor) Step(ctx context.Context, scope *NodeScope, cursor psi.Cursor) (result dst.Node, err error)

The Step function is responsible for code processing and response generation. The algorithm is divided into several groups of steps:

Acquiring and Preparing Elements: 1. Retrieve the root element from the cursor using the cursor.Element() method. 2. Formulate the todoComment with the p.prepareObjective function, which takes the NodeProcessor and NodeScope as inputs.

Cloning and Pruning: 3. Make a clone of the root with the psi.Clone method from the psi package, applied to p.Root. Prune this cloned root by iterating through each cursor element and returning true.

String Conversion and CodeGeneratorRequest Preparation: 4. Convert the stepRoot into a string format using the p.SourceFile.ToCode method. 5. Construct a new gpt.CodeGeneratorRequest object, where the Document is stepStr, the Objective is todoComment, and the Context is an empty ContextBag.

Context Setup and Invocation: 6. Craft the fullContext using the p.prepareContext function with the NodeProcessor, NodeScope, prunedRoot, and req as parameters. 7. Assign fullContext as the Context of req. 8. Execute the gpt.Invoke function using ctx and req, yielding a codeBlocks response.

Code Blocks Processing: 9. Iterate over each codeBlock in codeBlocks, performing the following:

  • If the Language attribute of the block is empty, set it to "go".
  • Create a blockName with the fmt.Sprintf function by concatenating "mergeContents_#", i, and block.Language.
  • Unescape HTML escape sequences in block.Code using the html.UnescapeString function.
  • Modify the package declaration by enclosing block.Code with "package gptimport".
  • Parse the modified code into a new Abstract Syntax Tree (AST) using p.SourceFile.Parse, with blockName and patchedCode as inputs.
  • Merge the newly created AST (newRoot.Ast().(*dst.File)) with the existing AST using the MergeFiles function.
  • For each declaration (decl) in newRoot.Children(), check if it is a function and if its name matches the current function's name.
  • If yes, replace the current declaration in the cursor with the new one using p.ReplaceDeclarationAt.
  • If no, merge the new declaration with the existing declarations using p.MergeDeclarations.

Return Processed Code: 10. Return the processed code as a dst.Node.

type NodeProcessorOption

type NodeProcessorOption func(p *NodeProcessor)

NodeProcessorOption is a function type that defines an option for the NodeProcessor.

It is used to configure the behavior of the NodeProcessor. Each option is a function that takes a pointer to the NodeProcessor as a parameter and modifies its properties in some way.

type NodeScope

type NodeScope struct {
	Processor *NodeProcessor
	Node      psi.Node
	Todos     []string
}

NodeScope represents the context of a function.

The ProcessContext stores information about the processor, node, and todos associated avec with a function.

Fields: - Processor: A pointer to the NodeProcessor struct. - Node: The psi.Node representing the current function. - Todos: A slice of strings representing the todos comments associated with the function.

func (*NodeScope) Root

func (n *NodeScope) Root() psi.Node

Jump to

Keyboard shortcuts

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