common

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2022 License: MIT Imports: 20 Imported by: 0

Documentation

Index

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

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

FormatSource 格式化源代码

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 ParseFile

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

ParseFile 解析文件,优先尝试使用 ParseDir

func ParseOneFile added in v0.4.4

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

ParseOneFile 解析为 astFile

func PrintCode

func PrintCode(fileSet *token.FileSet, file *ast.File) (out []byte, err error)

PrintCode 输出代码

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)
	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

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 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

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) GetImportGroup added in v0.2.7

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

GetImportGroup 读取 import 分组的排序

type Request added in v0.4.4

type Request struct {
	FileName string
	FSet     *token.FileSet
	AstFile  *ast.File
	Opt      Options
}

Request 一次格式化的请求

func (*Request) FormatFile added in v0.4.4

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

FormatFile 将 AstFile 格式化、得到源码

func (*Request) ReParse added in v0.4.4

func (req *Request) ReParse() (*token.FileSet, *ast.File, error)

ReParse 重新解析

Jump to

Keyboard shortcuts

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