syntaxgo_ast

package
v0.0.54 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2025 License: MIT Imports: 20 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateImports

func CreateImports(imports []string) string

CreateImports generates the import statements as a string from the provided package paths. CreateImports 从提供的包路径生成导入语句的字符串。

func GetPackageNameFromFile

func GetPackageNameFromFile(astFile *ast.File) (packageName string)

GetPackageNameFromFile extracts package name from an AST file node Accesses the Name field of the AST file to obtain the package name Returns the package name as a string from the parsed AST structure

GetPackageNameFromFile 从 AST 文件节点中提取包名 访问 AST 文件的 Name 字段以获取包名称 从已解析的 AST 结构返回包名字符串

func GetPackageNameFromPath

func GetPackageNameFromPath(path string) string

GetPackageNameFromPath extracts package name from a Go source file at the given path Uses optimized parsing with PackageClauseOnly mode to read just the package statement Returns the package name as a string without reading the entire file content

GetPackageNameFromPath 从给定路径的 Go 源文件中提取包名 使用优化的 PackageClauseOnly 模式解析,仅读取包声明语句 返回包名字符串,无需读取整个文件内容

func InjectImports

func InjectImports(source []byte, packages []string) []byte

InjectImports inserts the missing import paths into the provided Go source code. InjectImports 将缺失的导入路径插入到提供的 Go 源代码中。

Types

type AstBundle

type AstBundle struct {
	// contains filtered or unexported fields
}

AstBundle is a wrapper for an AST file and its corresponding FileSet. AstBundle 是一个封装 AST 文件及其对应 FileSet 的结构体。

func NewAstBundle

func NewAstBundle(fileSet *token.FileSet, astFile *ast.File) *AstBundle

NewAstBundle creates a new AstBundle. NewAstBundle 创建一个新的 AstBundle 实例。

func NewAstBundleV1

func NewAstBundleV1(data []byte) (*AstBundle, error)

NewAstBundleV1 creates an AstBundle from a byte slice. NewAstBundleV1 根据字节切片创建 AstBundle 实例。

func NewAstBundleV2

func NewAstBundleV2(fset *token.FileSet, data []byte) (*AstBundle, error)

NewAstBundleV2 creates an AstBundle from a byte slice with a provided FileSet. NewAstBundleV2 根据给定的 FileSet 和字节切片创建 AstBundle 实例。

func NewAstBundleV3

func NewAstBundleV3(fset *token.FileSet, path string) (*AstBundle, error)

NewAstBundleV3 creates an AstBundle by parsing a file from the given path. NewAstBundleV3 根据文件路径解析并创建 AstBundle 实例。

func NewAstBundleV4

func NewAstBundleV4(path string) (*AstBundle, error)

NewAstBundleV4 creates an AstBundle from a file path using a new FileSet. NewAstBundleV4 使用新的 FileSet 从文件路径创建 AstBundle 实例。

func NewAstBundleV5

func NewAstBundleV5(fset *token.FileSet, path string, mode parser.Mode) (*AstBundle, error)

NewAstBundleV5 creates an AstBundle by parsing a file with a specific parser mode. NewAstBundleV5 根据文件路径和特定的解析模式创建 AstBundle 实例。

func NewAstBundleV6

func NewAstBundleV6(path string, mode parser.Mode) (*AstBundle, error)

NewAstBundleV6 creates an AstBundle from a file path using a new FileSet and specific parser mode. NewAstBundleV6 使用新的 FileSet 和指定的解析模式从文件路径创建 AstBundle 实例。

func (*AstBundle) AddImport

func (ab *AstBundle) AddImport(path string) bool

AddImport adds a new import to the AST. AddImport 向 AST 添加新的导入路径。

func (*AstBundle) AddNamedImport

func (ab *AstBundle) AddNamedImport(name string, path string) bool

AddNamedImport adds a new named import to the AST. AddNamedImport 向 AST 添加带名称的导入路径。

func (*AstBundle) DeleteImport

func (ab *AstBundle) DeleteImport(path string) bool

DeleteImport removes an import from the AST. DeleteImport 从 AST 中删除指定的导入路径。

func (*AstBundle) DeleteNamedImport

func (ab *AstBundle) DeleteNamedImport(name string, path string) bool

DeleteNamedImport removes a named import from the AST. DeleteNamedImport 从 AST 中删除指定名称的导入路径。

func (*AstBundle) FormatSource

func (ab *AstBundle) FormatSource() ([]byte, error)

FormatSource formats the AST back into Go source code. FormatSource 将 AST 格式化为 Go 源代码。

func (*AstBundle) GetBundle

func (ab *AstBundle) GetBundle() (*ast.File, *token.FileSet)

GetBundle retrieves the AST file and FileSet from the AstBundle. GetBundle 从 AstBundle 中获取 AST 文件和 FileSet。

func (*AstBundle) GetPackageName

func (ab *AstBundle) GetPackageName() string

GetPackageName retrieves the package name from the AST. GetPackageName 从 AST 中获取包名。

func (*AstBundle) Print

func (ab *AstBundle) Print() error

Print outputs the AST structure to the console. Print 将 AST 结构打印到控制台。

func (*AstBundle) SerializeAst

func (ab *AstBundle) SerializeAst() ([]byte, error)

SerializeAst serializes the AST into a textual representation. SerializeAst 将 AST 序列化为文本表示形式。

type PackageImportOptions

type PackageImportOptions struct {
	// contains filtered or unexported fields
}

PackageImportOptions is used to manage the import paths for Go packages. PackageImportOptions 用于管理 Go 包的导入路径。

func NewPackageImportOptions

func NewPackageImportOptions() *PackageImportOptions

NewPackageImportOptions creates and returns a new PackageImportOptions instance. NewPackageImportOptions 创建并返回一个新的 PackageImportOptions 实例。

func (*PackageImportOptions) CreateImports

func (param *PackageImportOptions) CreateImports() string

CreateImports generates a string containing import statements for the given package paths. CreateImports 根据给定的包路径生成包含导入语句的字符串。

func (*PackageImportOptions) GetPkgPaths

func (param *PackageImportOptions) GetPkgPaths() []string

GetPkgPaths returns a merged list of package paths from packages, referenced types, and inferred objects. GetPkgPaths 返回从包路径、引用类型和推断对象中合并得到的包路径列表。

func (*PackageImportOptions) InjectImports

func (param *PackageImportOptions) InjectImports(source []byte) []byte

InjectImports adds required import paths into the provided Go source code. InjectImports 将必要的导入路径添加到提供的 Go 源代码中。

func (*PackageImportOptions) SetInferredObject

func (param *PackageImportOptions) SetInferredObject(object any) *PackageImportOptions

SetInferredObject adds an inferred object to the list of inferred objects. SetInferredObject 将一个推断对象添加到推断对象列表中。

func (*PackageImportOptions) SetPkgPath

func (param *PackageImportOptions) SetPkgPath(pkgPath string) *PackageImportOptions

SetPkgPath adds a package path to the list of packages to be imported. SetPkgPath 将包路径添加到待导入的包列表中。

func (*PackageImportOptions) SetPkgPaths added in v0.0.50

func (param *PackageImportOptions) SetPkgPaths(pkgPaths []string) *PackageImportOptions

SetPkgPaths adds some package paths to the list of packages to be imported. SetPkgPaths 将包路径添加到待导入的包列表中。

func (*PackageImportOptions) SetReferencedType

func (param *PackageImportOptions) SetReferencedType(reflectType reflect.Type) *PackageImportOptions

SetReferencedType adds a referenced type to the list of referenced types. SetReferencedType 将一个引用类型添加到引用类型列表中。

Jump to

Keyboard shortcuts

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