rewrite

package
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2024 License: LGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatGolangCode

func FormatGolangCode(src []byte) ([]byte, error)

FormatGolangCode 格式化代码,如果出错返回原内容

func GetNameList

func GetNameList(ids []*ast.Ident) (names []string)

GetNameList 获取多个标识的名称列表

func PrettifyGolangFile

func PrettifyGolangFile(filename string, cleanImports bool) (changed bool, err error)

PrettifyGolangFile 读出来go代码,重新写入文件

func SaveCodeToFile

func SaveCodeToFile(filename string, codeText []byte) ([]byte, error)

SaveCodeToFile 将go代码保存到文件

func TrimComment

func TrimComment(c string) string

TrimComment 去掉注释两边的空白

func WriteGolangFileCleanImports

func WriteGolangFileCleanImports(filename string, codeText []byte) ([]byte, error)

WriteGolangFileCleanImports 美化和整理导入,并输出go代码到文件

func WriteGolangFilePrettify

func WriteGolangFilePrettify(filename string, codeText []byte) ([]byte, error)

WriteGolangFilePrettify 美化并输出go代码到文件

func WritePackage

func WritePackage(pkgPath, pkgName string) error

WritePackage 将包中的Go文件格式化,如果提供了pkgname则用作新包名

Types

type CodeParser

type CodeParser struct {
	DeclNodes   []*DeclNode
	DeclIndexes map[string][]int
	*CodeSource
}

CodeParser 代码节点解析器

func NewCodeParser

func NewCodeParser() *CodeParser

NewCodeParser 创建代码节点解析器

func NewFileParser

func NewFileParser(filename string) (cp *CodeParser, err error)

NewFileParser 从文件创建解析器

func NewSourceParser

func NewSourceParser(source []byte) (cp *CodeParser, err error)

NewSourceParser 从代码创建解析器

func (*CodeParser) AllDeclNode

func (cp *CodeParser) AllDeclNode(kind string) []*DeclNode

AllDeclNode 获取指定种类的所有节点

func (*CodeParser) FindDeclNode

func (cp *CodeParser) FindDeclNode(kind string, wildcards ...string) []*DeclNode

FindDeclNode 根据名称规则查找

func (*CodeParser) GetDeclNode

func (cp *CodeParser) GetDeclNode(kind string, offset int) *DeclNode

GetDeclNode 获取指定种类的一个节点

func (*CodeParser) ParseDecls

func (cp *CodeParser) ParseDecls(kind string, limit int) bool

ParseDecls 解析指定种类的声明

type CodeSource

type CodeSource struct {
	Fileast    *ast.File
	Fileset    *token.FileSet
	Source     []byte
	Alternates []PosAlt // Source 只能替换一次,然后必须重新解析 Fileast
	*printer.Config
}

CodeSource 源码解析器

func NewCodeSource

func NewCodeSource() *CodeSource

NewCodeSource 创造源码解析器

func WriteWithImports

func WriteWithImports(pkg string, source []byte,
	imports map[string]string,
) (*CodeSource, error)

WriteWithImports 注入导入声明

func (*CodeSource) AddCode

func (cs *CodeSource) AddCode(code []byte) error

AddCode 增加新代码在原有之后

func (*CodeSource) AddImport

func (cs *CodeSource) AddImport(path, alias string) bool

AddImport 增加一个import

func (*CodeSource) AddReplace

func (cs *CodeSource) AddReplace(first, last ast.Node, code string)

AddReplace 将两个节点以及中间的部分,使用新内容代替

func (*CodeSource) AddStringCode

func (cs *CodeSource) AddStringCode(code string) error

AddStringCode 增加新代码在原有之后

func (*CodeSource) AltSource

func (cs *CodeSource) AltSource() ([]byte, bool)

AltSource 改写源码,应用事先准备的可代替代码Alternates

func (*CodeSource) CleanImports

func (cs *CodeSource) CleanImports() (removes int)

CleanImports 整理全部import代码

func (*CodeSource) DelImport

func (cs *CodeSource) DelImport(path, alias string) bool

DelImport 删除一个import

func (*CodeSource) GetComment

func (cs *CodeSource) GetComment(c *ast.CommentGroup, trim bool) string

GetComment 获取注释

func (*CodeSource) GetContent

func (cs *CodeSource) GetContent() ([]byte, error)

GetContent 获取代码内容

func (*CodeSource) GetFieldCode

func (cs *CodeSource) GetFieldCode(node *DeclNode, i int) string

GetFieldCode 获得类成员代码内容

func (*CodeSource) GetFirstFileName

func (cs *CodeSource) GetFirstFileName() string

GetFirstFileName 如果代码由多个文件组成,返回第一个文件路径

func (*CodeSource) GetNodeCode

func (cs *CodeSource) GetNodeCode(node ast.Node) string

GetNodeCode 获得节点代码内容

func (*CodeSource) GetPackage

func (cs *CodeSource) GetPackage() string

GetPackage 获取包名

func (*CodeSource) GetPackageOffset

func (cs *CodeSource) GetPackageOffset() int

GetPackageOffset 获取包名结束位置

func (*CodeSource) ResetImports

func (cs *CodeSource) ResetImports(filename string, imports map[string]string) error

ResetImports 重新注入声明,并美化代码

func (*CodeSource) SetPackage

func (cs *CodeSource) SetPackage(name string) (err error)

SetPackage 设置新的包名

func (*CodeSource) SetSource

func (cs *CodeSource) SetSource(source []byte) (err error)

SetSource 替换全部代码,并重新解析

func (*CodeSource) WriteTo

func (cs *CodeSource) WriteTo(filename string) error

WriteTo 美化代码并保存到文件

type DeclNode

type DeclNode struct {
	Token   token.Token
	Kinds   []string
	Names   []string
	Fields  []*FieldNode
	Comment *ast.CommentGroup
	Offset  int
	ast.Decl
}

DeclNode 声明节点

func NewDeclNode

func NewDeclNode(decl ast.Decl, offset int, position token.Pos) (n *DeclNode, err error)

NewDeclNode 创建声明节点

func (*DeclNode) GetKind

func (n *DeclNode) GetKind() string

GetKind 声明节点种类

func (*DeclNode) GetName

func (n *DeclNode) GetName() string

GetName 声明节点名称

func (*DeclNode) ParseFunDecl

func (n *DeclNode) ParseFunDecl(fun *ast.FuncDecl) (err error)

ParseFunDecl //解析函数节点

func (*DeclNode) ParseGenDecl

func (n *DeclNode) ParseGenDecl(gen *ast.GenDecl) (err error)

ParseGenDecl 解析类声明节点

func (*DeclNode) ParseStruct

func (n *DeclNode) ParseStruct(fields []*ast.Field) (err error)

ParseStruct 解析类成员节点

type FieldNode

type FieldNode struct {
	Names   []string
	Comment *ast.CommentGroup
	*ast.Field
}

FieldNode 类成员节点

func (*FieldNode) GetTag

func (n *FieldNode) GetTag() reflect.StructTag

GetTag 类成员的标签

type PosAlt

type PosAlt struct {
	Pos, End  token.Position
	Alternate []byte
}

PosAlt 替换位置

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL