wide/file/outline.go

157 lines
4.0 KiB
Go
Raw Permalink Normal View History

2015-01-18 08:59:10 +03:00
// Copyright (c) 2014-2015, b3log.org
2014-12-29 09:16:14 +03:00
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package file
import (
2015-01-03 07:39:00 +03:00
"bytes"
2014-12-29 09:16:14 +03:00
"encoding/json"
"go/ast"
"go/parser"
"go/token"
"net/http"
2015-01-03 07:39:00 +03:00
"strings"
2014-12-29 09:16:14 +03:00
"github.com/b3log/wide/util"
)
2014-12-30 07:04:16 +03:00
type element struct {
Name string
2015-01-03 07:39:00 +03:00
Line int
Ch int
2014-12-30 07:04:16 +03:00
}
2014-12-29 09:16:14 +03:00
// GetOutline gets outfile of a go file.
func GetOutline(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
data["succ"] = false
return
}
code := args["code"].(string)
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", code, 0)
if err != nil {
2014-12-30 11:51:19 +03:00
data["succ"] = false
return
2014-12-29 09:16:14 +03:00
}
2015-01-03 10:18:13 +03:00
// ast.Print(fset, f)
2014-12-30 07:04:16 +03:00
2015-01-03 07:39:00 +03:00
line, ch := getCursor(code, int(f.Name.Pos()))
data["package"] = &element{Name: f.Name.Name, Line: line, Ch: ch}
2014-12-30 07:04:16 +03:00
imports := []*element{}
for _, astImport := range f.Imports {
2015-01-03 07:39:00 +03:00
line, ch := getCursor(code, int(astImport.Path.Pos()))
2014-12-30 07:04:16 +03:00
2015-01-03 07:39:00 +03:00
imports = append(imports, &element{Name: astImport.Path.Value, Line: line, Ch: ch})
2014-12-30 07:04:16 +03:00
}
data["imports"] = imports
funcDecls := []*element{}
varDecls := []*element{}
constDecls := []*element{}
structDecls := []*element{}
interfaceDecls := []*element{}
2015-01-03 10:18:13 +03:00
typeDecls := []*element{}
2014-12-30 07:04:16 +03:00
for _, decl := range f.Decls {
switch decl.(type) {
case *ast.FuncDecl:
funcDecl := decl.(*ast.FuncDecl)
2015-01-03 07:39:00 +03:00
line, ch := getCursor(code, int(funcDecl.Name.Pos()))
2014-12-30 07:04:16 +03:00
2015-01-03 07:39:00 +03:00
funcDecls = append(funcDecls, &element{Name: funcDecl.Name.Name, Line: line, Ch: ch})
2014-12-30 07:04:16 +03:00
case *ast.GenDecl:
genDecl := decl.(*ast.GenDecl)
for _, spec := range genDecl.Specs {
switch genDecl.Tok {
case token.VAR:
variableSpec := spec.(*ast.ValueSpec)
2015-01-03 07:39:00 +03:00
for _, varName := range variableSpec.Names {
line, ch := getCursor(code, int(varName.Pos()))
varDecls = append(varDecls, &element{Name: varName.Name, Line: line, Ch: ch})
}
2014-12-30 07:04:16 +03:00
case token.TYPE:
typeSpec := spec.(*ast.TypeSpec)
2015-01-03 10:18:13 +03:00
line, ch := getCursor(code, int(typeSpec.Pos()))
2014-12-30 07:04:16 +03:00
switch typeSpec.Type.(type) {
case *ast.StructType:
2015-01-03 07:39:00 +03:00
structDecls = append(structDecls, &element{Name: typeSpec.Name.Name, Line: line, Ch: ch})
2014-12-30 07:04:16 +03:00
case *ast.InterfaceType:
2015-01-03 07:39:00 +03:00
interfaceDecls = append(interfaceDecls, &element{Name: typeSpec.Name.Name, Line: line, Ch: ch})
2015-01-03 10:18:13 +03:00
case *ast.Ident:
typeDecls = append(typeDecls, &element{Name: typeSpec.Name.Name, Line: line, Ch: ch})
2014-12-30 07:04:16 +03:00
}
case token.CONST:
constSpec := spec.(*ast.ValueSpec)
2015-01-03 07:39:00 +03:00
for _, constName := range constSpec.Names {
line, ch := getCursor(code, int(constName.Pos()))
constDecls = append(constDecls, &element{Name: constName.Name, Line: line, Ch: ch})
}
2014-12-30 07:04:16 +03:00
}
}
}
}
data["funcDecls"] = funcDecls
data["varDecls"] = varDecls
data["constDecls"] = constDecls
data["structDecls"] = structDecls
data["interfaceDecls"] = interfaceDecls
2015-01-03 10:18:13 +03:00
data["typeDecls"] = typeDecls
2014-12-29 09:16:14 +03:00
}
2015-01-03 07:39:00 +03:00
// getCursor calculates the cursor position (line, ch) by the specified offset.
//
// line is the line number, starts with 0 that means the first line
// ch is the column number, starts with 0 that means the first column
func getCursor(code string, offset int) (line, ch int) {
code = code[:offset]
lines := strings.Split(code, "\n")
line = 0
for range lines {
line++
}
var buffer bytes.Buffer
runes := []rune(lines[line-1])
for _, r := range runes {
buffer.WriteString(string(r))
}
ch = len(buffer.String())
return line - 1, ch - 1
}