-
Notifications
You must be signed in to change notification settings - Fork 44
/
outline.go
133 lines (113 loc) · 3.04 KB
/
outline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2013 Chris McGee <sirnewton_01@yahoo.ca>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"go/ast"
"go/parser"
"go/token"
"net/http"
"reflect"
"strconv"
)
type Entry struct {
Line string `json:"line"`
Label string `json:"label"`
}
func outlineHandler(writer http.ResponseWriter, req *http.Request, path string, pathSegs []string) bool {
fileset := token.NewFileSet()
file, err := parser.ParseFile(fileset, "", req.Body, 0)
if err != nil {
ShowError(writer, 400, "Error parsing go source", err)
return true
}
outline := []Entry{}
ast.Inspect(file, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.FuncDecl:
if x.Pos().IsValid() {
line := strconv.FormatInt(int64(fileset.Position(x.Pos()).Line), 10)
name := x.Name.Name
if name != "" {
label := "func "
if x.Recv.NumFields() > 0 {
label = label + "(" + fileListStr(x.Recv) + ") "
}
label = label + name + "("
if x.Type.Params.NumFields() > 0 {
label = label + fileListStr(x.Type.Params)
}
label = label + ")"
if x.Type.Results.NumFields() > 0 {
if x.Type.Results.NumFields() == 1 {
label = label + " " + fileListStr(x.Type.Results)
} else {
label = label + " (" + fileListStr(x.Type.Results) + ")"
}
}
outline = append(outline, Entry{Line: line, Label: label})
}
}
case *ast.GenDecl:
if x.Tok == token.TYPE {
for _, spec := range x.Specs {
if spec.Pos().IsValid() {
typeSpec, ok := spec.(*ast.TypeSpec)
if ok {
line := strconv.FormatInt(int64(fileset.Position(spec.Pos()).Line), 10)
label := typeSpec.Name.Name
if label != "" {
label = "type " + label
outline = append(outline, Entry{Line: line, Label: label})
}
}
}
}
} else if x.Tok == token.IMPORT {
line := strconv.FormatInt(int64(fileset.Position(x.Pos()).Line), 10)
label := "IMPORT"
outline = append(outline, Entry{Line: line, Label: label})
} else if x.Tok == token.CONST {
line := strconv.FormatInt(int64(fileset.Position(x.Pos()).Line), 10)
label := "CONST"
outline = append(outline, Entry{Line: line, Label: label})
}
}
return true
})
ShowJson(writer, 200, outline)
return true
}
func fileListStr(t *ast.FieldList) string {
label := ""
for argIdx, arg := range t.List {
for nameIdx, name := range arg.Names {
label = label + name.Name
if nameIdx != len(arg.Names)-1 {
label = label + ","
}
}
if len(arg.Names) != 0 {
label = label + " "
}
label = label + typeStr(arg.Type)
if argIdx != len(t.List)-1 {
label = label + ", "
}
}
return label
}
func typeStr(t ast.Expr) string {
switch e := t.(type) {
case *ast.StarExpr:
return "*" + typeStr(e.X)
case *ast.Ident:
return e.Name
case *ast.SelectorExpr:
return typeStr(e.X) + "." + e.Sel.Name
case *ast.ArrayType:
return "[]" + typeStr(e.Elt)
default:
return "<" + reflect.TypeOf(t).String() + ">"
}
}