germ

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2025 License: MIT Imports: 19 Imported by: 0

README

Graph Entity Repo Map (germ)

A Go library for building repository code maps using AST/CST. Particularly usefuel with AI-powered coding assistants.

[!CAUTION] This project is still very much in development

Overview

Germ (Graph Entity Repo Map) is a Go library designed to analyze and map code repositories by leveraging Abstract Syntax Trees (AST) and Concrete Syntax Trees (CST). It enables AI-powered tools to understand code structures, dependencies, and relationships efficiently.

Features

  • Graph-based repository mapping: Extracts entities and relationships from source code.
  • AST/CST parsing: Supports deep syntax analysis for code intelligence.
  • AI-friendly representation: Generates structured data suitable for AI coding assistants.
  • Language support: Primarily focused on Go, with extensibility for other languages.
  • Efficient and scalable: Designed for large repositories with minimal performance overhead.

Installation

go get github.com/cyber-nic/germ

Usage

Exlucding Files and Folers

Use a .gitignore or create a git-compatible .astignore. Alternatily copy the .astignore from this repo into yours.

Example

See cmd/main.go for a working example.

License

MIT License

Documentation

Overview

Package germ contains the core logic for the germ tool

Package germ contains the core logic for the germ tool

Index

Constants

View Source
const (
	// TagKindDef is the kind of tag that represents a definition
	TagKindDef = "def"
	// TagKindRef is the kind of tag that represents a reference
	TagKindRef = "ref"
)

Variables

This section is empty.

Functions

func DisableGlobIgnore added in v0.3.0

func DisableGlobIgnore() func(*RepoMap)

DisableGlobIgnore disables the global ignore file

func FindGitRoot added in v0.3.0

func FindGitRoot(start string) (string, error)

FindGitRoot walks upward from the given path until it finds a directory containing a ".git" folder.

func Verbose added in v0.3.0

func Verbose(value bool) func(*RepoMap)

Verbose enables verbose output for debugging.

func WithContentPrefix added in v0.3.0

func WithContentPrefix(value string) func(*RepoMap)

WithContentPrefix sets the repository content prefix.

func WithGlobIgnoreFilePath added in v0.3.0

func WithGlobIgnoreFilePath(value string) func(*RepoMap)

WithGlobIgnoreFilePath sets the glob ignore file path. Ignored if DisableGlobIgnore is set

func WithLastLineContext added in v0.3.0

func WithLastLineContext(value bool) func(*RepoMap)

WithLastLineContext enables or disables the inclusion of the context delimited by the last line in the output.

func WithLineNumber added in v0.3.0

func WithLineNumber(value bool) func(*RepoMap)

WithLineNumber enables or disables line numbers in the output.

func WithLinesOfInterestMarked added in v0.3.0

func WithLinesOfInterestMarked(value bool) func(*RepoMap)

WithLinesOfInterestMarked enables or disables the marking of lines of interest in the output.

func WithLinesOfInterestPadding added in v0.3.0

func WithLinesOfInterestPadding(value int) func(*RepoMap)

WithLinesOfInterestPadding sets the number of lines of padding around lines of interest.

func WithLogLevel added in v0.3.0

func WithLogLevel(value int) func(*RepoMap)

WithLogLevel sets the log level for the RepoMap

func WithMapMulNoFiles added in v0.3.0

func WithMapMulNoFiles(value int) func(*RepoMap)

WithMapMulNoFiles sets the number of files to multiply the map by.

func WithMaxContextWindow added in v0.3.0

func WithMaxContextWindow(value int) func(*RepoMap)

WithMaxContextWindow set the maximum context window.

func WithMaxTokens added in v0.3.0

func WithMaxTokens(value int) func(*RepoMap)

WithMaxTokens sets the map's maximum number of tokens.

func WithParentContext added in v0.3.0

func WithParentContext(value bool) func(*RepoMap)

WithParentContext enables or disables the inclusion of parent context in the output.

Types

type DefRank

type DefRank struct {
	// contains filtered or unexported fields
}

DefRank is a struct to hold the rank for a definition

type EdgeRank

type EdgeRank struct {
	// contains filtered or unexported fields
}

EdgeRank is a struct to hold the edge data for distributing rank

type ModelStub

type ModelStub struct{}

ModelStub simulates the main_model used in Python code (for token_count, etc.).

func (*ModelStub) TokenCount

func (m *ModelStub) TokenCount(text string) int

TokenCount is a naive token estimator. Real code might call tiktoken or other logic.

type RepoMap

type RepoMap struct {
	// contains filtered or unexported fields
}

RepoMap is the Go equivalent of the Python class `RepoMap`.

func NewRepoMap

func NewRepoMap(root string, mainModel *ModelStub, options ...func(*RepoMap),
) *RepoMap

NewRepoMap is the repo map constructor.

func (*RepoMap) Generate added in v0.3.0

func (r *RepoMap) Generate(
	chatFiles, otherFiles []string,
	mentionedFnames, mentionedIdents map[string]bool,
) string

Generate is the top-level function (mirroring the Python method) that produces the “repo content”.

func (*RepoMap) GetFileTags

func (r *RepoMap) GetFileTags(fname, relFname string, filter TagFilter) ([]Tag, error)

GetFileTags calls GetTagsRaw and filters out short names and common words.

func (*RepoMap) GetRankedTagsMap

func (r *RepoMap) GetRankedTagsMap(
	chatFnames, otherFnames []string,
	maxMapTokens int,
	mentionedFnames, mentionedIdents map[string]bool,
) string

GetRankedTagsMap orchestrates calls to getRankedTags and toTree to produce the final “map” string.

func (*RepoMap) GetRelFname

func (r *RepoMap) GetRelFname(fname string) string

GetRelFname returns fname relative to r.Root. If that fails, returns fname as-is.

func (*RepoMap) GetRepoFiles added in v0.3.0

func (r *RepoMap) GetRepoFiles(path string) ([]string, string)

GetRepoFiles gathers all files in a directory (or a single file) and returns two values: the slice of file paths and a tree-like string representing the folder structure.

func (*RepoMap) GetTagsRaw

func (r *RepoMap) GetTagsRaw(fname, relFname string, filter TagFilter) ([]Tag, error)

GetTagsRaw parses the file with Tree-sitter and extracts "function definitions"

func (*RepoMap) LoadQuery

func (r *RepoMap) LoadQuery(lang *sitter.Language, langID string) (*sitter.Query, error)

LoadQuery loads the Tree-sitter query text and compiles a sitter.Query.

func (*RepoMap) TokenCount

func (r *RepoMap) TokenCount(text string) float64

TokenCount tries to mimic how the Python code estimates tokens (split into short vs. large).

type Tag

type Tag struct {
	FileName string
	FilePath string
	Line     int
	Name     string
	Kind     string
}

Tag represents a “tag” extracted from a source file.

func GetTagsFromQueryCapture

func GetTagsFromQueryCapture(relFname, fname string, q *sitter.Query, tree *sitter.Tree, sourceCode []byte, filter TagFilter) []Tag

GetTagsFromQueryCapture extracts tags from the result of a Tree-sitter query on a given file. It iterates through the captures returned by the Tree-sitter query cursor and collects definitions (def) and references (ref). All other captures are ignored. filter is a function that accepts the name of a capture and returns bool false if it should be skipped.

type TagFilter

type TagFilter func(name string) bool

TagFilter is a function that accepts the name of a capture and returns false if it should be skipped.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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