wide/file/outline.go

160 lines
4.0 KiB
Go
Raw Normal View History

2019-05-17 06:28:50 +03:00
// Copyright (c) 2014-present, 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
//
2018-03-12 07:28:33 +03:00
// https://www.apache.org/licenses/LICENSE-2.0
2014-12-29 09:16:14 +03:00
//
// 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
2019-12-01 15:21:00 +03:00
"github.com/88250/gulu"
2014-12-29 09:16:14 +03:00
)
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
}
2015-03-09 09:16:46 +03:00
// GetOutlineHandler gets outfile of a go file.
func GetOutlineHandler(w http.ResponseWriter, r *http.Request) {
2019-05-24 16:04:25 +03:00
result := gulu.Ret.NewResult()
defer gulu.Ret.RetResult(w, r, result)
2014-12-29 09:16:14 +03:00
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
2019-05-24 16:52:17 +03:00
result.Code = -1
2014-12-29 09:16:14 +03:00
return
}
code := args["code"].(string)
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", code, 0)
if err != nil {
2019-05-24 16:52:17 +03:00
result.Code = -1
2014-12-30 11:51:19 +03:00
return
2014-12-29 09:16:14 +03:00
}
2015-11-24 11:30:37 +03:00
data := map[string]interface{}{}
2015-11-27 11:55:28 +03:00
result.Data = &data
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
}