repomap

package module
v0.0.0-...-d5ccf16 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2025 License: MIT Imports: 38 Imported by: 0

README

go-repomap

Version: v0.0.1

A Go implementation of repository mapping functionality using tree-sitter for code analysis. This tool helps analyze and understand codebases by creating a graph representation of code definitions and references.

Credits

This project uses the following open-source libraries:

  • tree-sitter - A parser generator tool and incremental parsing library
  • go-tree-sitter - Golang bindings for tree-sitter and language grammars

Special thanks to the contributors of these projects for their excellent work.

Features

  • Code analysis using tree-sitter
  • Support for multiple programming languages:
    • Bash
    • C/C++
    • C#
    • CSS
    • Dockerfile
    • Elixir
    • Elm
    • Go
    • Groovy
    • HCL
    • HTML
    • Java
    • JavaScript/TypeScript
    • Kotlin
    • OCaml
    • PHP
    • Protocol Buffers
    • Python
    • Ruby
    • Rust
    • Scala
    • SQL
    • Svelte
    • Swift
    • TOML
    • YAML
  • Tag-based code navigation
  • Graph-based code analysis
  • Ranked tag generation

Installation

go get github.com/entrepeneur4lyf/go-repomap

Usage

package main

import (
    "fmt"
    "github.com/entrepeneur4lyf/go-repomap"
)

func main() {
    // Create a new tag index for your repository
    tagIndex := repomap.NewTagIndex("/path/to/repo")

    // Create an analyzer
    analyzer := repomap.NewTagAnalyzer(tagIndex)

    // Get ranked tags
    rankedTags := analyzer.GetRankedTags()
    fmt.Printf("Found %d tags\n", len(rankedTags))

    // Create a repo map with custom token limit
    rm := repomap.NewRepoMap().WithMapTokens(2048)
    repomap, err := rm.GetRepoMap(tagIndex)
    if err != nil {
        panic(err)
    }

    fmt.Println(repomap)
}

The generated map provides a concise overview of your codebase's structure, highlighting important code definitions and their relationships. The output is optimized to stay within the specified token limit while preserving the most relevant information.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Overview

Package repomap provides functionality for analyzing and mapping repositories. This file is intentionally empty as we're using github.com/smacker/go-tree-sitter directly instead of implementing our own tree-sitter wrappers.

Index

Constants

View Source
const REPOMAP_DEFAULT_TOKENS = 1024

Variables

This section is empty.

Functions

func CloseSmallGapsHelper

func CloseSmallGapsHelper(lines map[int]struct{}, codeSplitByLines []string, codeLen int) map[int]struct{}

TODO(codestory): Improve the name over here

func IsGitRepository

func IsGitRepository(dir string) bool

Checks if a path is a git directory, it looks for any commit hash present and gets the timestamp for it as a poor-man's check

Types

type DiGraph

type DiGraph struct {
	Nodes []string
	Edges map[NodeIndex][]Edge
}

func NewDiGraph

func NewDiGraph() *DiGraph

func (*DiGraph) AddEdge

func (g *DiGraph) AddEdge(source, target NodeIndex, weight float64) EdgeIndex

func (*DiGraph) AddNode

func (g *DiGraph) AddNode(label string) NodeIndex

func (*DiGraph) NumEdges

func (g *DiGraph) NumEdges() int

func (*DiGraph) NumNodes

func (g *DiGraph) NumNodes() int

type Edge

type Edge struct {
	Source NodeIndex
	Target NodeIndex
	Weight float64
	Index  EdgeIndex
}

type EdgeIndex

type EdgeIndex int

type FileSystem

type FileSystem interface {
	GetFiles(dir string) ([]string, error)
	ReadFile(path string) (string, error)
}

type NodeIndex

type NodeIndex int

type RankedDefinition

type RankedDefinition struct {
	Key   NodeIndex
	Value float64
}

type RankedDefinitionsMap

type RankedDefinitionsMap map[NodeIndex]float64

type RepoMap

type RepoMap struct {
	MapTokens int
}

func NewRepoMap

func NewRepoMap() *RepoMap

func (*RepoMap) GetRepoMap

func (rm *RepoMap) GetRepoMap(tagIndex *TagIndex) (string, error)

func (*RepoMap) WithMapTokens

func (rm *RepoMap) WithMapTokens(mapTokens int) *RepoMap

type RepoMapError

type RepoMapError struct {
	Err error
}

func NewFileSystemError

func NewFileSystemError(err error) *RepoMapError

func NewGraphAnalysisError

func NewGraphAnalysisError(msg string) *RepoMapError

func NewIoError

func NewIoError() *RepoMapError

func NewParseError

func NewParseError(msg string) *RepoMapError

func NewSymbolAnalysisError

func NewSymbolAnalysisError(msg string) *RepoMapError

func NewTreeGenerationError

func NewTreeGenerationError(msg string) *RepoMapError

func (*RepoMapError) Error

func (e *RepoMapError) Error() string

func (*RepoMapError) Unwrap

func (e *RepoMapError) Unwrap() error

type SimpleFileSystem

type SimpleFileSystem struct{}

func (*SimpleFileSystem) GetFiles

func (fs *SimpleFileSystem) GetFiles(dir string) ([]string, error)

func (*SimpleFileSystem) ReadFile

func (fs *SimpleFileSystem) ReadFile(path string) (string, error)

type Tag

type Tag struct {
	RelFname string
	Fname    string
	Line     int
	Name     string
	Kind     TagKind
}

func (Tag) String

func (t Tag) String() string

String implements the Stringer interface for Tag

type TagAnalyzer

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

func NewTagAnalyzer

func NewTagAnalyzer(tagIndex *TagIndex) *TagAnalyzer

func (*TagAnalyzer) DebugPrintRankedTags

func (ta *TagAnalyzer) DebugPrintRankedTags()

func (*TagAnalyzer) GetRankedTags

func (ta *TagAnalyzer) GetRankedTags() []Tag

type TagGraph

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

func NewTagGraph

func NewTagGraph() *TagGraph

func NewTagGraphFromTagIndex

func NewTagGraphFromTagIndex(tagIndex *TagIndex, mentionedIdents map[string]struct{}) *TagGraph

func (*TagGraph) CalculateAndDistributeRanks

func (tg *TagGraph) CalculateAndDistributeRanks()

func (*TagGraph) CalculatePageRanks

func (tg *TagGraph) CalculatePageRanks() []float64

func (*TagGraph) DebugRankedDefinitions

func (tg *TagGraph) DebugRankedDefinitions()

func (*TagGraph) DebugSortedDefinitions

func (tg *TagGraph) DebugSortedDefinitions()

func (*TagGraph) GenerateDotRepresentation

func (tg *TagGraph) GenerateDotRepresentation() string

func (*TagGraph) GetGraph

func (tg *TagGraph) GetGraph() *DiGraph

func (*TagGraph) GetRankedDefinitions

func (tg *TagGraph) GetRankedDefinitions() RankedDefinitionsMap

func (*TagGraph) GetSortedDefinitions

func (tg *TagGraph) GetSortedDefinitions() []RankedDefinition

func (*TagGraph) PopulateFromTagIndex

func (tg *TagGraph) PopulateFromTagIndex(tagIndex *TagIndex, mentionedIdents map[string]struct{})

func (*TagGraph) PrintDot

func (tg *TagGraph) PrintDot()

type TagIndex

type TagIndex struct {
	Defines     map[string]map[string]struct{}
	References  map[string][]string
	Definitions map[string][]Tag
	CommonTags  map[string]struct{}
	FileToTags  map[string]map[string]struct{}
	Path        string
	// contains filtered or unexported fields
}

func NewTagIndex

func NewTagIndex(path string) *TagIndex

func (*TagIndex) AddTag

func (ti *TagIndex) AddTag(tag Tag, relPath string)

func (*TagIndex) GenerateFromFiles

func (ti *TagIndex) GenerateFromFiles(ctx context.Context, files map[string][]byte) error

GenerateFromFiles generates tags from the given files

func (*TagIndex) GetFiles

func (ti *TagIndex) GetFiles(dir string) (map[string][]byte, error)

GetFiles returns a map of file paths to their contents

func (*TagIndex) PostProcessTags

func (ti *TagIndex) PostProcessTags()

type TagKind

type TagKind int
const (
	Definition TagKind = iota
	Reference
)

func (TagKind) String

func (k TagKind) String() string

type TreeContext

type TreeContext struct {
	Filename                 string
	ParentContext            bool
	ChildContext             bool
	LastLine                 bool
	Margin                   int
	MarkLois                 bool
	HeaderMax                int
	ShowTopOfFileParentScope bool
	LoiPad                   int
	Lois                     map[int]struct{}
	ShowLines                map[int]struct{}
	NumLines                 int
	Lines                    []string
	LineNumber               bool
	DoneParentScopes         map[int]struct{}
	Nodes                    [][]*tree_sitter.Node
	Scopes                   []map[int]struct{}
	Header                   [][][3]int
}

func NewTreeContext

func NewTreeContext(code string, fsFilePath string) *TreeContext

func (*TreeContext) AddChildContext

func (tc *TreeContext) AddChildContext(index int)

func (*TreeContext) AddContext

func (tc *TreeContext) AddContext()

func (*TreeContext) AddLois

func (tc *TreeContext) AddLois(lois []int)

func (*TreeContext) AddParentScopes

func (tc *TreeContext) AddParentScopes(index int, recurseDepth []int)

func (*TreeContext) ArrangeHeaders

func (tc *TreeContext) ArrangeHeaders()

func (*TreeContext) CloseSmallGaps

func (tc *TreeContext) CloseSmallGaps()

func (*TreeContext) FindAllChildren

func (tc *TreeContext) FindAllChildren(node *tree_sitter.Node) []*tree_sitter.Node

func (*TreeContext) Format

func (tc *TreeContext) Format() string

func (*TreeContext) GetLastLineOfScope

func (tc *TreeContext) GetLastLineOfScope(index int) int

func (*TreeContext) GetLois

func (tc *TreeContext) GetLois() map[int]struct{}

func (*TreeContext) Init

func (tc *TreeContext) Init(cursor *tree_sitter.TreeCursor)

func (*TreeContext) PrintState

func (tc *TreeContext) PrintState()

func (*TreeContext) Walk

func (tc *TreeContext) Walk(cursor *tree_sitter.TreeCursor)

type TreeWalker

type TreeWalker struct {
	Scopes []map[int]struct{}
	Header [][][3]int
	Nodes  [][]*tree_sitter.Node
	Tree   *tree_sitter.Tree
}

func NewTreeWalker

func NewTreeWalker(tree *tree_sitter.Tree, numLines int) *TreeWalker

func (*TreeWalker) GetHeaders

func (tw *TreeWalker) GetHeaders() [][][3]int

func (*TreeWalker) GetNodes

func (tw *TreeWalker) GetNodes() [][]*tree_sitter.Node

func (*TreeWalker) GetScopes

func (tw *TreeWalker) GetScopes() []map[int]struct{}

func (*TreeWalker) GetTree

func (tw *TreeWalker) GetTree() *tree_sitter.Tree

func (*TreeWalker) WalkTree

func (tw *TreeWalker) WalkTree(node *tree_sitter.Node)

type TreeWalker2

type TreeWalker2 struct {
	Nodes  [][]*tree_sitter.Node
	Scopes []map[int]struct{}
	Header [][][3]int
}

func NewTreeWalker2

func NewTreeWalker2(numLines int) *TreeWalker2

func (*TreeWalker2) GetAllTrueNodes

func (tw *TreeWalker2) GetAllTrueNodes() [][]*tree_sitter.Node

func (*TreeWalker2) GetNodesForLine

func (tw *TreeWalker2) GetNodesForLine(line int) []*tree_sitter.Node

func (*TreeWalker2) Walk

func (tw *TreeWalker2) Walk(cursor *tree_sitter.TreeCursor)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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