71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"ollie/tools/codeintel/internal/codeintel"
|
|
)
|
|
|
|
type args struct {
|
|
Path string `json:"path"`
|
|
Offset int `json:"offset"`
|
|
Limit int `json:"limit"`
|
|
}
|
|
|
|
func main() {
|
|
var a args
|
|
if err := json.NewDecoder(os.Stdin).Decode(&a); err != nil {
|
|
fail(err)
|
|
}
|
|
root, err := filepath.Abs(a.Path)
|
|
if err != nil {
|
|
fail(err)
|
|
}
|
|
files, err := codeintel.Files(root)
|
|
if err != nil {
|
|
fail(err)
|
|
}
|
|
start := a.Offset
|
|
if start < 0 {
|
|
fail(fmt.Errorf("offset must be non-negative"))
|
|
}
|
|
if a.Limit < 0 {
|
|
fail(fmt.Errorf("limit must be non-negative"))
|
|
}
|
|
if start >= len(files) {
|
|
start = len(files)
|
|
}
|
|
end := len(files)
|
|
if a.Limit > 0 && start+a.Limit < end {
|
|
end = start + a.Limit
|
|
}
|
|
fmt.Printf("# Codebase Overview\n\nPath: `%s`\n\n", root)
|
|
if len(files) > 0 && (start > 0 || end < len(files)) {
|
|
fmt.Printf("Files %d-%d of %d. Next offset: %d\n\n", start+1, end, len(files), end)
|
|
}
|
|
for _, f := range files[start:end] {
|
|
d, err := codeintel.Parse(f.Path, f.Language)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
fmt.Printf("## `%s` (%s)\n\n", rel(root, f.Path), f.Language)
|
|
for _, s := range codeintel.Symbols(d) {
|
|
fmt.Printf("- %s `%s` — line %d\n", strings.Repeat(" ", min(s.Depth, 4)), s.Name, s.Line)
|
|
}
|
|
fmt.Println()
|
|
d.Close()
|
|
}
|
|
}
|
|
func rel(root, path string) string { p, _ := filepath.Rel(root, path); return p }
|
|
func min(a, b int) int {
|
|
if a < b {
|
|
return a
|
|
}
|
|
return b
|
|
}
|
|
func fail(err error) { fmt.Fprintln(os.Stderr, "error:", err); os.Exit(1) }
|