Documentation ¶
Index ¶
- Constants
- Variables
- func BuildInRewriteRules() []string
- func ConsoleGreen(txt string) string
- func ConsoleGrey(txt string) string
- func ConsoleRed(txt string) string
- func DoNotEdit(name string, src []byte) bool
- func FindGoModPath(fileName string) (string, error)
- func FormatSource(src []byte) ([]byte, error)
- func GitChangeFiles() ([]string, error)
- func InModule(pkg string, module string) bool
- func ModuleByFile(goModPath string) (string, error)
- func ParseFile(fileName string, src []byte) (*token.FileSet, *ast.File, error)
- func ParseOneFile(fileName string, src []byte) (*token.FileSet, *ast.File, error)
- func PrintCode(fileSet *token.FileSet, file *ast.File) (out []byte, err error)
- type ConsoleColor
- type DiffResult
- type DiffType
- type ImportGroupFunc
- type ImportGroupType
- type Modules
- type Options
- type Request
Constants ¶
View Source
const ( // DiffTypeChange 格式有变化 DiffTypeChange DiffType = "change" // DiffTypeAdd 是新增内容 DiffTypeAdd = "add" // DoffTypeDelete 内容被删除 DoffTypeDelete = "delete" )
Variables ¶
View Source
var Version = "v0.4.4 20220925"
Version 版本信息
Functions ¶
func BuildInRewriteRules ¶ added in v0.4.4
func BuildInRewriteRules() []string
BuildInRewriteRules 获取内置的简化规则
func FindGoModPath ¶ added in v0.3.0
FindGoModPath 查找文件对应的 go.mod 文件
func FormatSource ¶ added in v0.4.4
FormatSource 格式化源代码
func GitChangeFiles ¶ added in v0.2.0
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 ¶ added in v0.3.0
ModuleByFile 解析 go.mod 文件里的 module 的值
func ParseOneFile ¶ added in v0.4.4
ParseOneFile 解析为 astFile
Types ¶
type DiffResult ¶ added in v0.4.0
DiffResult Diff 的结果
type ImportGroupFunc ¶ added in v0.2.0
ImportGroupFunc import 排序逻辑
type ImportGroupType ¶ added in v0.3.0
type ImportGroupType byte
ImportGroupType import 分组类型
const ( // ImportGroupGoStandard 标准库(简称 s) ImportGroupGoStandard ImportGroupType = 's' // ImportGroupThirdParty 第三方库(简称 t) ImportGroupThirdParty ImportGroupType = 't' // ImportGroupCurrentModule 模块自身(简称 c) ImportGroupCurrentModule ImportGroupType = 'c' )
type Modules ¶ added in v0.3.0
type Modules []string
Modules 模块列表
func ListModules ¶ added in v0.3.0
ListModules 找到指定目录下的所有子 module
可能是这样的:
a.go go.mod + world (目录) say.go // 这个和 下面的 hello 就是两个不同的 module + hello (目录) // 这是一个独立的 module hello.go go.mod
type Options ¶
type Options struct { ImportGroupFn ImportGroupFunc // DisplayFormat 输出 DisplayDiff 的格式,默认为 text,还可以是 json DisplayFormat string // 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 // FieldAlignment struct 字段对齐优化,可选,默认 0 // 可选值: // 1-对发现的进行修正,同时打印日志 // 2-只打印出需优化的日志信息 FieldAlignment 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 }
Options 选项
func NewDefaultOptions ¶ added in v0.2.0
func NewDefaultOptions() *Options
NewDefaultOptions 生成默认的 options
func (*Options) AllGoFiles ¶ added in v0.2.0
AllGoFiles 获取所有的待格式化的 .go 文件
func (*Options) GetImportGroup ¶ added in v0.2.7
func (opt *Options) GetImportGroup(t ImportGroupType) int
GetImportGroup 读取 import 分组的排序
type Request ¶ added in v0.4.4
Request 一次格式化的请求
func (*Request) FormatFile ¶ added in v0.4.4
FormatFile 将 AstFile 格式化、得到源码
Click to show internal directories.
Click to hide internal directories.