Documentation
¶
Index ¶
- Constants
- Variables
- func BuildInRewriteRules() []string
- func ConsoleGreen(txt string) string
- func ConsoleGrey(txt string) string
- func ConsoleRed(txt string) string
- func DebugPrintln(depth int, v ...any)
- func DoNotEdit(name string, src []byte) bool
- func FindGoModPath(fileName string) (string, error)
- func GitChangeFiles() ([]string, error)
- func InModule(pkg string, module string) bool
- func ModuleByFile(goModPath string) (string, error)
- func ParseOneFile(fileName string, src []byte) (*token.FileSet, *ast.File, error)
- func SliceHas[S ~[]T, T comparable](arr S, values ...T) bool
- type ConsoleColor
- type DiffResult
- type DiffType
- type ImportGroupFunc
- type ImportGroupType
- type Modules
- type Options
- func (opt *Options) AllGoFiles() ([]string, error)
- func (opt *Options) BindFlags()
- func (opt *Options) Check() error
- func (opt *Options) Clone() *Options
- func (opt *Options) Format(src []byte) ([]byte, error)
- func (opt *Options) GetImportGroup(t ImportGroupType) int
- func (opt *Options) Source(fileSet *token.FileSet, file *ast.File) ([]byte, error)
- type Request
- func (req *Request) Clone() *Request
- func (req *Request) FormatFile() ([]byte, error)
- func (req *Request) GoVersionGEQ(version string) bool
- func (req *Request) HasDirective(node ast.Node, name string) bool
- func (req *Request) MustReParse()
- func (req *Request) NoFormat(node ast.Node) bool
- func (req *Request) ReParse() error
- func (req *Request) Save(name string) error
- func (req *Request) TokenLine() *TokenLine
- type TokenLine
Constants ¶
const ( // DiffTypeChange 格式有变化 DiffTypeChange DiffType = "change" // DiffTypeAdd 是新增内容 DiffTypeAdd = "add" // DoffTypeDelete 内容被删除 DoffTypeDelete = "delete" )
const ( // NameSTDIN 特殊的文件名,用于标志从 stdin 读取代码 NameSTDIN = "stdin" // NameGitChange 特殊的文件名,表示查找所有当前有修改的文件 NameGitChange = "git_change" )
Variables ¶
var Debug = os.Getenv("go_fmt_debug") == "1"
Debug 程序内部调试
Functions ¶
func FindGoModPath ¶
FindGoModPath 查找文件对应的 go.mod 文件
func GitChangeFiles ¶
GitChangeFiles 获取当前git项目有变更的文件 支持如下git状态: M auth/md5_sign.go R utils/counter_test.go -> component/counter/counter_test.go A unittest/internal/monitor/bvar.apis_monitor.data ?? internal/gofmt/files.go
func ModuleByFile ¶
ModuleByFile 解析 go.mod 文件里的 module 的值
func ParseOneFile ¶
ParseOneFile 解析为 astFile
func SliceHas ¶
func SliceHas[S ~[]T, T comparable](arr S, values ...T) bool
Types ¶
type DiffResult ¶
DiffResult Diff 的结果
type ImportGroupFunc ¶
ImportGroupFunc import 排序逻辑
type ImportGroupType ¶
type ImportGroupType byte
ImportGroupType import 分组类型
const ( // ImportGroupGoStandard 标准库(简称 s),顺序编号 0 ImportGroupGoStandard ImportGroupType = 's' // ImportGroupThirdParty 第三方库(简称 t),顺序编号 1 ImportGroupThirdParty ImportGroupType = 't' // ImportGroupCurrentModule 模块自身(简称 c),顺序编号 2 ImportGroupCurrentModule ImportGroupType = 'c' )
type Modules ¶
type Modules []string
Modules 模块列表
func ListModules ¶
ListModules 找到指定目录下的所有子 module
可能是这样的:
a.go go.mod + world (目录) say.go // 这个和 下面的 hello 就是两个不同的 module + hello (目录) // 这是一个独立的 module hello.go go.mod
type Options ¶
type Options struct {
// ImportGroupFn 排序函数,可选,若不为空,将不会使用默认内置的规则
ImportGroupFn ImportGroupFunc
// DisplayFormat 输出 DisplayDiff 的格式,默认为 text,还可以是 json
DisplayFormat string
// DisplayPretty 是否打印已经格式化了或者跳过的文件
DisplayPrettyOrSkipped bool
// import 分组的排序规则,可选
// 总共 可分为 3 组,分别是 标准库(简称 s),第三方库(简称 t),模块自身(简称 c)
// stc: 默认的排序规则
// sct: Go 源码中的排序规则
ImportGroupRule string
// LocalModule 当前代码所在的 module
// 对应其 go.mod 文件中的 module 的值
LocalModule string
// ThirdModules 可选,第三方模块列表
//
// 是为解决这种情况:
// LocalModule = github.com/test
// 但是其子目录有其他的子模块,如:
// github.com/test/hello/say
// github.com/test/world
// 这个时候,在 github.com/test 里的代码,应该将 github.com/test/hello/say 归为第三方模块代码的分组
ThirdModules Modules
// 待处理的文件列表
Files []string
// 重写、简化代码的规则,可选
RewriteRules []string
TabWidth int
// Write 是否直接将格式化后的内容写入文件
Write bool
// Simplify 是否简化代码
Simplify bool
// DisplayDiff 是否只检查是否已格式化,
// 当值为 true 时,会强制设置 Write=false
DisplayDiff bool
Trace bool
// 是否将多段 import 合并为一个
MergeImports bool
// SingleLineCopyright 是否将 copyright 的多行注释格式化为单行注释
SingleLineCopyright bool
TabIndent bool
// 是否使用内置的 rewrite 规则简化代码,可选,默认 false
RewriteWithBuildIn bool
// Extra 更多额外的、高级的格式化规则
Extra bool
}
Options 选项
func (*Options) AllGoFiles ¶
AllGoFiles 获取所有的待格式化的 .go 文件
func (*Options) GetImportGroup ¶
func (opt *Options) GetImportGroup(t ImportGroupType) int
GetImportGroup 读取 import 分组的排序
type Request ¶
type Request struct {
FSet *token.FileSet
AstFile *ast.File
// FileName 文件名
FileName string
// Opt 处理的参数
Opt Options
// contains filtered or unexported fields
}
Request 一次格式化的请求
func NewTestRequest ¶
NewTestRequest 给测试场景使用的,创建一个新的 request 对象
func (*Request) FormatFile ¶
FormatFile 将 AstFile 格式化、得到源码
func (*Request) GoVersionGEQ ¶
GoVersionGEQ 判断模块 Go 的版本是否 >= 指定版本 version: 版本号,如 1.19
func (*Request) HasDirective ¶
HasDirective 判断一个节点是否有指定的指令
type TokenLine ¶
type TokenLine struct {
// contains filtered or unexported fields
}
TokenLine 记录对文件的换行的处理
func (*TokenLine) DeleteLine ¶
DeleteLine 删除指定位置的新行