common

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DiffTypeChange 格式有变化
	DiffTypeChange DiffType = "change"

	// DiffTypeAdd 是新增内容
	DiffTypeAdd = "add"

	// DoffTypeDelete 内容被删除
	DoffTypeDelete = "delete"
)
View Source
const (
	// NameSTDIN 特殊的文件名,用于标志从 stdin 读取代码
	NameSTDIN = "stdin"

	// NameGitChange 特殊的文件名,表示查找所有当前有修改的文件
	NameGitChange = "git_change"
)

Variables

View Source
var Debug = os.Getenv("go_fmt_debug") == "1"

Debug 程序内部调试

Functions

func BuildInRewriteRules added in v0.4.4

func BuildInRewriteRules() []string

BuildInRewriteRules 获取内置的简化规则

func ConsoleGreen added in v0.4.0

func ConsoleGreen(txt string) string

ConsoleGreen 控制台绿色字符

func ConsoleGrey added in v0.4.4

func ConsoleGrey(txt string) string

ConsoleGrey 控制台灰色字符

func ConsoleRed added in v0.4.0

func ConsoleRed(txt string) string

ConsoleRed 控制台红色字符

func DebugPrintln added in v0.4.6

func DebugPrintln(depth int, v ...any)

DebugPrintln 打印调试日志

func DoNotEdit added in v0.1.20200313

func DoNotEdit(name string, src []byte) bool

DoNotEdit 该代码是否不让修改

func FindGoModPath added in v0.3.0

func FindGoModPath(fileName string) (string, error)

FindGoModPath 查找文件对应的 go.mod 文件

func GitChangeFiles added in v0.2.0

func GitChangeFiles() ([]string, error)

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 InModule added in v0.3.0

func InModule(pkg string, module string) bool

InModule 判断指定 pkg 的 是否属于 module

func ModuleByFile added in v0.3.0

func ModuleByFile(goModPath string) (string, error)

ModuleByFile 解析 go.mod 文件里的 module 的值

func ParseOneFile added in v0.4.4

func ParseOneFile(fileName string, src []byte) (*token.FileSet, *ast.File, error)

ParseOneFile 解析为 astFile

func SliceHas added in v0.5.1

func SliceHas[S ~[]T, T comparable](arr S, values ...T) bool

Types

type ConsoleColor added in v0.4.4

type ConsoleColor func(string) string

ConsoleColor 字符颜色

type DiffResult added in v0.4.0

type DiffResult interface {
	Detail() any
	String() string
}

DiffResult Diff 的结果

func Diff added in v0.4.0

func Diff(a, b string, trace bool) DiffResult

Diff 比较文本内容的不同

type DiffType added in v0.4.0

type DiffType string

DiffType diff 的类型

type ImportGroupFunc added in v0.2.0

type ImportGroupFunc func(importPath string, opt Options) int

ImportGroupFunc import 排序逻辑

type ImportGroupType added in v0.3.0

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 added in v0.3.0

type Modules []string

Modules 模块列表

func ListModules added in v0.3.0

func ListModules(dir string) (Modules, error)

ListModules 找到指定目录下的所有子 module

可能是这样的:

a.go
go.mod
+ world (目录)
	say.go    // 这个和 下面的 hello 就是两个不同的 module
+ hello (目录) // 这是一个独立的 module
	hello.go
	go.mod

func (Modules) PkgIn added in v0.3.0

func (ms Modules) PkgIn(pkg string) bool

PkgIn 判断 pkg 是否属于 模块列表范围

type Options

type Options struct {
	// ImportGroupFn 排序函数,可选,若不为空,将不会使用默认内置的规则
	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

	// 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 NewDefaultOptions added in v0.2.0

func NewDefaultOptions() *Options

NewDefaultOptions 生成默认的 options

func (*Options) AllGoFiles added in v0.2.0

func (opt *Options) AllGoFiles() ([]string, error)

AllGoFiles 获取所有的待格式化的 .go 文件

func (*Options) BindFlags added in v0.2.0

func (opt *Options) BindFlags()

BindFlags 绑定参数信息

func (*Options) Check added in v0.2.7

func (opt *Options) Check() error

Check 简称 option 是否正确

func (*Options) Clone added in v0.3.0

func (opt *Options) Clone() *Options

Clone 当执行 format 的时候,每个文件都 clone 一份

func (*Options) Format added in v0.4.5

func (opt *Options) Format(src []byte) ([]byte, error)

Format 重新格式化代码

func (*Options) GetImportGroup added in v0.2.7

func (opt *Options) GetImportGroup(t ImportGroupType) int

GetImportGroup 读取 import 分组的排序

func (*Options) Source added in v0.4.5

func (opt *Options) Source(fileSet *token.FileSet, file *ast.File) ([]byte, error)

Source 格式化文件

type Request added in v0.4.4

type Request struct {
	FSet    *token.FileSet
	AstFile *ast.File

	// FileName 文件名
	FileName string

	// Opt 处理的参数
	Opt Options
	// contains filtered or unexported fields
}

Request 一次格式化的请求

func NewTestRequest added in v0.4.7

func NewTestRequest(fileName string) *Request

NewTestRequest 给测试场景使用的,创建一个新的 request 对象

func (*Request) Clone added in v0.4.6

func (req *Request) Clone() *Request

Clone reParser it and return a new Request

func (*Request) FormatFile added in v0.4.4

func (req *Request) FormatFile() ([]byte, error)

FormatFile 将 AstFile 格式化、得到源码

func (*Request) GoVersionGEQ added in v0.4.8

func (req *Request) GoVersionGEQ(version string) bool

GoVersionGEQ 判断模块 Go 的版本是否 >= 指定版本 version: 版本号,如 1.19

func (*Request) HasDirective added in v0.6.0

func (req *Request) HasDirective(node ast.Node, name string) bool

HasDirective 判断一个节点是否有指定的指令

func (*Request) MustReParse added in v0.4.6

func (req *Request) MustReParse()

MustReParse 重新解析,若失败会 panic

func (*Request) NoFormat added in v0.6.0

func (req *Request) NoFormat(node ast.Node) bool

NoFormat 判断一个节点是否不需要执行格式化

func (*Request) ReParse added in v0.4.4

func (req *Request) ReParse() error

ReParse 重新解析

func (*Request) Save added in v0.4.6

func (req *Request) Save(name string) error

Save 保存文件

func (*Request) TokenLine added in v0.4.6

func (req *Request) TokenLine() *TokenLine

TokenLine 获取 TokenLine

type TokenLine added in v0.4.6

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

TokenLine 记录对文件的换行的处理

func (*TokenLine) AddLine added in v0.4.6

func (tf *TokenLine) AddLine(depth int, at token.Pos)

AddLine 在指定位置添加新行

func (*TokenLine) DeleteLine added in v0.4.6

func (tf *TokenLine) DeleteLine(depth int, line int)

DeleteLine 删除指定位置的新行

func (*TokenLine) Execute added in v0.4.6

func (tf *TokenLine) Execute()

Execute 将 Add、 Delete 的结果生效

Jump to

Keyboard shortcuts

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