JackBekket / Reflexia

A code which comment and enchance itself
0 stars 2 forks source link

Functions commentary #1

Closed JackBekket closed 1 month ago

JackBekket commented 2 months ago

Try to extract functions code from .go files and run some promt on it (like comment this function)

Example:

package main

import (
 "go/ast"
 "go/parser"
 "go/token"
 "log"
 "os"
)

func main() {
 fset := token.NewFileSet()

 // Parse the .go file
 f, err := parser.ParseFile(fset, "yourfile.go", nil, 0)
 if err != nil {
  log.Fatal(err)
 }

 // Inspect the AST and print function declarations
 ast.Inspect(f, func(n ast.Node) bool {
  switch x := n.(type) {
  case *ast.FuncDecl:
   // This is a function declaration
   // Print the function name and its body
   start := fset.Position(x.Body.Lbrace)
   end := fset.Position(x.Body.Rbrace)
   log.Printf("Function: %s, Start: %s, End: %s\n", x.Name.Name, start, end)
  }
  return true
 })
}
JackBekket commented 2 months ago

Also make new snippets for commenting functions, file in general, package in general, etc in here: https://github.com/JackBekket/Reflexia/blob/main/lib/ai/ai.go