Example #1

Open
opened 2026-07-05 19:31:01 +00:00 by clouds · 0 comments
Owner
  26: func ExtractFunctions(sources_data *sources.SourcesData, names []string) ([]*Function, error) {
  27:     fns := make([]*Function, 0, len(names))
  28: 
  29:     filter := functionFilter(names)
  30: 
  31:     for _, pkg := range sources_data.Packages {
  32:         for _, syntax := range pkg.Syntax {
  33:             for _, decl := range syntax.Decls {
  34:                 fn, ok := decl.(*ast.FuncDecl)
  35:                 if !ok || !filter(fn) {
  36:                     continue
  37:                 }
  38: 
  39:                 name := fn.Name.Name
  40:                 file := pkg.Fset.File(fn.Pos())
  41: 
  42:                 pos := file.Position(fn.Pos())
  43:                 end := file.Position(fn.End() + 1)
  44: 
  45:                 content, err := sources_data.Content(file, &pos, &end, name)
  46:                 if err != nil {
  47:                     return fns, fmt.Errorf(
  48:                         "could not get content of function '%s' in '%s': %w",
  49:                         name, file.Name(), err,
  50:                     )
  51:                 }
  52: 
  53:                 fns = append(fns, &Function{
  54:                     Name:     name,
  55:                     PkgPath:  pkg.PkgPath,
  56:                     FileName: file.Name(),
  57:                     Lines:    file.Lines(),
  58:                     Content:  content,
  59:                     Pos:      pos,
  60:                     End:      end,
  61:                 })
  62:             }
  63:         }
  64:     }
  65: 
  66:     return fns, nil
  67: }
  68: 
<pre> <span style="color: rgb(100, 100, 100)"> 26: </span><span style="color: rgb(255, 255, 255)">func ExtractFunctions(sources_data *sources.SourcesData, names []string) ([]*Function, error) </span><span style="color: rgb(0, 166, 0)">{</span> <span style="color: rgb(100, 100, 100)"> 27: </span><span style="color: rgb(0, 166, 0)"> fns := make([]*Function, 0, len(names))</span> <span style="color: rgb(100, 100, 100)"> 28: </span><span style="color: rgb(0, 166, 0)"></span> <span style="color: rgb(100, 100, 100)"> 29: </span><span style="color: rgb(0, 166, 0)"> filter := functionFilter(names)</span> <span style="color: rgb(100, 100, 100)"> 30: </span><span style="color: rgb(0, 166, 0)"></span> <span style="color: rgb(100, 100, 100)"> 31: </span><span style="color: rgb(0, 166, 0)"> for _, pkg := range sources_data.Packages </span><span style="color: rgb(0, 179, 0)">{</span> <span style="color: rgb(100, 100, 100)"> 32: </span><span style="color: rgb(0, 179, 0)"> for _, syntax := range pkg.Syntax </span><span style="color: rgb(0, 179, 0)">{</span> <span style="color: rgb(100, 100, 100)"> 33: </span><span style="color: rgb(0, 179, 0)"> for _, decl := range syntax.Decls </span><span style="color: rgb(0, 255, 0)">{</span> <span style="color: rgb(100, 100, 100)"> 34: </span><span style="color: rgb(0, 255, 0)"> fn, ok := decl.(*ast.FuncDecl)</span> <span style="color: rgb(100, 100, 100)"> 35: </span><span style="color: rgb(0, 255, 0)"> if !ok || !filter(fn) </span><span style="color: rgb(0, 236, 0)">{</span> <span style="color: rgb(100, 100, 100)"> 36: </span><span style="color: rgb(0, 236, 0)"> continue</span><span style="color: rgb(255, 255, 255)"></span> <span style="color: rgb(100, 100, 100)"> 37: </span><span style="color: rgb(255, 255, 255)"> }</span> <span style="color: rgb(100, 100, 100)"> 38: </span><span style="color: rgb(255, 255, 255)"></span> <span style="color: rgb(100, 100, 100)"> 39: </span><span style="color: rgb(255, 255, 255)"> </span><span style="color: rgb(0, 172, 0)">name := fn.Name.Name</span> <span style="color: rgb(100, 100, 100)"> 40: </span><span style="color: rgb(0, 172, 0)"> file := pkg.Fset.File(fn.Pos())</span> <span style="color: rgb(100, 100, 100)"> 41: </span><span style="color: rgb(0, 172, 0)"></span> <span style="color: rgb(100, 100, 100)"> 42: </span><span style="color: rgb(0, 172, 0)"> pos := file.Position(fn.Pos())</span> <span style="color: rgb(100, 100, 100)"> 43: </span><span style="color: rgb(0, 172, 0)"> end := file.Position(fn.End() + 1)</span> <span style="color: rgb(100, 100, 100)"> 44: </span><span style="color: rgb(0, 172, 0)"></span> <span style="color: rgb(100, 100, 100)"> 45: </span><span style="color: rgb(0, 172, 0)"> content, err := sources_data.Content(file, &pos, &end, name)</span> <span style="color: rgb(100, 100, 100)"> 46: </span><span style="color: rgb(0, 172, 0)"> if err != nil </span><span style="color: rgb(255, 10, 10)">{</span> <span style="color: rgb(100, 100, 100)"> 47: </span><span style="color: rgb(255, 10, 10)"> return fns, fmt.Errorf(</span> <span style="color: rgb(100, 100, 100)"> 48: </span><span style="color: rgb(255, 10, 10)"> "could not get content of function '%s' in '%s': %w",</span> <span style="color: rgb(100, 100, 100)"> 49: </span><span style="color: rgb(255, 10, 10)"> name, file.Name(), err,</span> <span style="color: rgb(100, 100, 100)"> 50: </span><span style="color: rgb(255, 10, 10)"> )</span> <span style="color: rgb(100, 100, 100)"> 51: </span><span style="color: rgb(255, 10, 10)"> }</span><span style="color: rgb(255, 255, 255)"></span> <span style="color: rgb(100, 100, 100)"> 52: </span><span style="color: rgb(255, 255, 255)"></span> <span style="color: rgb(100, 100, 100)"> 53: </span><span style="color: rgb(255, 255, 255)"> </span><span style="color: rgb(0, 172, 0)">fns = append(fns, &Function{</span> <span style="color: rgb(100, 100, 100)"> 54: </span><span style="color: rgb(0, 172, 0)"> Name: name,</span> <span style="color: rgb(100, 100, 100)"> 55: </span><span style="color: rgb(0, 172, 0)"> PkgPath: pkg.PkgPath,</span> <span style="color: rgb(100, 100, 100)"> 56: </span><span style="color: rgb(0, 172, 0)"> FileName: file.Name(),</span> <span style="color: rgb(100, 100, 100)"> 57: </span><span style="color: rgb(0, 172, 0)"> Lines: file.Lines(),</span> <span style="color: rgb(100, 100, 100)"> 58: </span><span style="color: rgb(0, 172, 0)"> Content: content,</span> <span style="color: rgb(100, 100, 100)"> 59: </span><span style="color: rgb(0, 172, 0)"> Pos: pos,</span> <span style="color: rgb(100, 100, 100)"> 60: </span><span style="color: rgb(0, 172, 0)"> End: end,</span> <span style="color: rgb(100, 100, 100)"> 61: </span><span style="color: rgb(0, 172, 0)"> })</span><span style="color: rgb(255, 255, 255)"></span> <span style="color: rgb(100, 100, 100)"> 62: </span><span style="color: rgb(255, 255, 255)"> }</span> <span style="color: rgb(100, 100, 100)"> 63: </span><span style="color: rgb(255, 255, 255)"> }</span> <span style="color: rgb(100, 100, 100)"> 64: </span><span style="color: rgb(255, 255, 255)"> }</span> <span style="color: rgb(100, 100, 100)"> 65: </span><span style="color: rgb(255, 255, 255)"></span> <span style="color: rgb(100, 100, 100)"> 66: </span><span style="color: rgb(255, 255, 255)"> </span><span style="color: rgb(0, 166, 0)">return fns, nil</span><span style="color: rgb(255, 255, 255)"></span> <span style="color: rgb(100, 100, 100)"> 67: </span><span style="color: rgb(255, 255, 255)">}</span> <span style="color: rgb(100, 100, 100)"> 68: </span><span style="color: rgb(255, 255, 255)"></span> </pre>
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
clouds/color#1
No description provided.