Skip to content

Instantly share code, notes, and snippets.

@manveru
Created May 5, 2010 08:22
Show Gist options
  • Save manveru/390532 to your computer and use it in GitHub Desktop.
Save manveru/390532 to your computer and use it in GitHub Desktop.
Generate tags file for Go in exuberant-ctags format
// Generate a tags file for the Go Programming Language in the format used by
// exuberant-ctags. Can be used with the VIM editor to navigate Go source code
// files.
//
// usage: gotags filename [ filename... ] > tags
//
// Authors: [email protected], [email protected]
// Date: 2010-05-17
package main
import (
"container/vector"
"fmt"
"go/ast"
"go/parser"
"os"
"sort"
)
var tags = vector.StringVector{}
func output_tag(name *ast.Ident, kind byte) {
tags.Push(fmt.Sprintf("%s\t%s\t%d;\"\t%c",
name.String(), name.Position.Filename, name.Position.Line, kind))
}
func main() {
parse_files()
println("!_TAG_FILE_SORTED\t1\t")
sa := tags.Data()
sort.SortStrings(sa)
for _, s := range sa {
println(s)
}
}
const FUNC, TYPE, VAR = 'f', 't', 'v'
func parse_files() {
for i, m := 1, len(os.Args); i < m; i++ {
tree, ok := parser.ParseFile(os.Args[i], nil, nil, 0)
if ok != nil {
panic("error parsing file" + os.Args[i] + ok.String())
}
for _, node := range tree.Decls {
switch n := node.(type) {
case *ast.FuncDecl:
output_tag(n.Name, FUNC)
case *ast.GenDecl:
do_gen_decl(n)
}
}
}
}
func do_gen_decl(node *ast.GenDecl) {
for _, v := range node.Specs {
switch n := v.(type) {
case *ast.TypeSpec:
output_tag(n.Name, TYPE)
case *ast.ValueSpec:
for _, vv := range n.Names {
output_tag(vv, VAR)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment