未验证 提交 12c62286 编写于 作者: P Phodal HUANG

refactor: refactor code

上级 d8b352a8
package cmd
import (
. "../refactor"
//. "../refactor"
. "../refactor/unused"
"github.com/spf13/cobra"
)
......@@ -14,8 +15,11 @@ var refactorCmd *cobra.Command = &cobra.Command{
path := cmd.Flag("path").Value.String()
if moveConfig != "" && path != "" {
app := NewMoveClassApp(moveConfig, path)
app.Analysis()
//app := NewMoveClassApp(moveConfig, path)
//app.Analysis()
app2 := NewRemoveUnusedImportApp(moveConfig, path)
app2.Analysis()
}
},
}
......
......@@ -17,12 +17,6 @@ type JField struct {
//StopLinePosition int
}
type JImport struct {
Name string
StartLine int
StopLine int
}
type JPkgInfo struct {
Name string
StartLine int
......
package models
type JImport struct {
Name string
StartLine int
StopLine int
}
type JMoveStruct struct {
*JFullIdentifier
Path string
Deps []JImport
}
......@@ -20,13 +20,6 @@ var currentFile string
var moveConfig string
var configPath string
type JMoveStruct struct {
*JFullIdentifier
path string
deps []JImport
}
var nodes []JMoveStruct
type MoveClassApp struct {
......@@ -114,10 +107,10 @@ func updatePackageInfo(structs []JMoveStruct, originImport string, newImport str
func updateImportSide(originImport string, newImport string) {
for index := range nodes {
node := nodes[index]
for j := range node.deps {
dep := node.deps[j]
for j := range node.Deps {
dep := node.Deps[j]
if dep.Name == originImport {
updateFile(node.path, dep.StartLine, "import "+newImport+";")
updateFile(node.Path, dep.StartLine, "import "+newImport+";")
}
}
}
......
package refactor
import (
. "./base"
"os"
)
func main() {
//cmd.Execute()
path := "examples/unused-import"
if len(os.Args) > 1 {
path = os.Args[1:][0]
}
callApp := new(JavaRefactorApp)
callApp.AnalysisPath(path)
}
package base
package unused
import (
"fmt"
......@@ -8,17 +8,30 @@ import (
"path/filepath"
"strings"
. "./models"
. "../base"
. "../base/models"
. "../utils"
)
var currentFile string
var moveConfig string
var configPath string
type JavaRefactorApp struct {
type RemoveUnusedImportApp struct {
}
func (j *JavaRefactorApp) AnalysisPath(codeDir string) {
files := GetJavaFiles(codeDir)
var nodes []JMoveStruct
func NewRemoveUnusedImportApp(config string, pPath string) *RemoveUnusedImportApp {
moveConfig = config
configPath = pPath
nodes = nil
return &RemoveUnusedImportApp{}
}
func (j *RemoveUnusedImportApp) Analysis() {
files := GetJavaFiles(configPath)
for index := range files {
file := files[index]
......@@ -36,18 +49,18 @@ func (j *JavaRefactorApp) AnalysisPath(codeDir string) {
antlr.NewParseTreeWalker().Walk(listener, context)
if node.Name != "" {
handleNode()
handleNode(node)
}
}
}
func handleNode() {
func handleNode(node *JFullIdentifier) {
var fields map[string]JField = node.GetFields()
var imports []JImport = node.GetImports()
fmt.Println(node.Pkg+"."+node.Name, imports, node.GetMethods(), fields)
if len(fields) == 0 {
//removeAllImports(imports)
removeAllImports(imports)
return
}
......@@ -69,7 +82,7 @@ func handleNode() {
//}
}
func removeAllImports(imports map[string]JImport) {
func removeAllImports(imports []JImport) {
for index := range imports {
imp := imports[index]
removeImportByLineNum(imp)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册