~sircmpwn/annotatego

453e8b0128c7d13f7c38072fb43081403a083d62 — Drew DeVault 1 year, 28 days ago b258677
Link up reverse references
1 files changed, 40 insertions(+), 6 deletions(-)

M main.go
M main.go => main.go +40 -6
@@ 102,14 102,9 @@ func main() {
	}
	tree := crawlGitTree()
	annotations := make(map[string][]interface{})
	references := make(map[token.Pos][]token.Pos)
	for _, pkg := range pkgs {
		log.Printf("Annotating %s...", pkg.ID)
		// TODO
		//log.Println("defines:")
		//for ident, object := range pkg.TypesInfo.Defs {
		//	pos := fset.Position(ident.Pos())
		//	log.Printf("\t%s: %s: %T %v", pos, ident, object, object)
		//}
		for ident, object := range pkg.TypesInfo.Uses {
			pos := fset.Position(ident.Pos())
			filename := pos.Filename


@@ 132,6 127,11 @@ func main() {
			case *types.Func:
				fpos := fset.Position(object.Pos())
				if fpos.IsValid() && strings.HasPrefix(fpos.Filename, cwd) {
					if _, ok := references[object.Pos()]; !ok {
						references[object.Pos()] = nil
					}
					references[object.Pos()] = append(
						references[object.Pos()], ident.Pos())
					/* Internal reference */
					path := normalizePath(fpos.Filename)
					to = fmt.Sprintf("%s#L%d", path, fpos.Line)


@@ 156,6 156,40 @@ func main() {
			}
		}
	}
	for target, refs := range references {
		pos := fset.Position(target)
		if !pos.IsValid() || !strings.HasPrefix(pos.Filename, cwd) {
			continue
		}
		var ok bool
		path := normalizePath(pos.Filename)
		if path, ok = tree[path]; !ok {
			continue
		}
		if _, ok = annotations[path]; !ok {
			annotations[path] = nil
		}
		var content bytes.Buffer
		for _, via := range refs {
			ref := fset.Position(via)
			refpath := normalizePath(ref.Filename)
			content.WriteString(fmt.Sprintf("- [%s:%d](%s#L%d)\n",
				refpath, ref.Line, refpath, ref.Line))
		}
		var title string
		if len(refs) == 1 {
			title = fmt.Sprintf("%d reference", 1)
		} else {
			title = fmt.Sprintf("%d references", len(refs))
		}
		anno := &MarkdownAnnotation{
			Type: "markdown",
			Title: title,
			Lineno: pos.Line,
			Content: content.String(),
		}
		annotations[path] = append(annotations[path], anno)
	}
	m, err := json.Marshal(annotations)
	if err != nil {
		panic(err)