Documentation
¶
Index ¶
- func AddCode(edit *goedit.Edit, pos token.Pos, code string)
- func AddCodeAfterImportsLegacy(code string, beginMark string, endMark string, contents []string) stringdeprecated
- func AddContentAfter(content string, beginMark string, endMark string, seq []string, ...) stringdeprecated
- func AddContentAfterName(code string, beginMark string, endMark string, insertContent string) string
- func AddContentBefore(content string, beginMark string, endMark string, seq []string, ...) stringdeprecated
- func AddImport(edit *goedit.Edit, file *ast.File, name string, pkgPath string)
- func AddImportAfterName(code string, beginMark string, endMark string, name string, pkgPath string) string
- func Append(edit *goedit.Edit, file *ast.File, s string)
- func ApplyPatches(patchDir, goroot, xgoRepoRoot string, extraEnv map[string]string, ...) error
- func ApplyXgoPatchContent(source string, patchContent string) (string, error)
- func ApplyXgoPatchFile(patchFile, targetFile string) error
- func CleanPatch(content string) string
- func CleanPatchMarkers(content string, start PatchMarker, end PatchMarker) string
- func EditFile(file string, callback func(content string) (string, error)) (err error)
- func FmtLineDirective(srcFile string, line int) string
- func GetFuncInsertPosition(file *ast.File) token.Pos
- func RemoveBuildIgnore(content string) (string, error)
- func RemoveMarker(content string, marker string) (string, error)
- func ReplaceContentAfter(content string, beginMark string, endMark string, seq []string, target string, ...) string
- func UpdateContent(content string, beginMark string, endMark string, seq []string, i int, ...) string
- func UpdateContentLines(content string, beginMark string, endMark string, seq []string, i int, ...) string
- type Command
- type CommandType
- type Config
- type CopyEntry
- type FilePath
- type GenerateEntry
- type GenerateHandler
- type PatchBlock
- type PatchFile
- type PatchMarker
- type StringSliceOrSlice
- type UpdatePosition
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddContentAfterName ¶ added in v1.1.4
func AddImportAfterName ¶ added in v1.1.4
func AddImportAfterName(code string, beginMark string, endMark string, name string, pkgPath string) string
package something -> package something;import __my_fmt "fmt"
func Append ¶ added in v1.1.2
Append inserts s at the end of the buffer using its length, NOTE: when the source file has \r\n the file.End() isn't correct
func ApplyPatches ¶ added in v1.2.0
func ApplyPatches(patchDir, goroot, xgoRepoRoot string, extraEnv map[string]string, skipKinds []string, generateHandler GenerateHandler) error
ApplyPatches walks a patch directory and applies all operations to a GOROOT. Auto-discovers:
- .xgo.patch files → applied to corresponding GOROOT files
- Other files (except __config__.json) → copied one-to-one to GOROOT
- __config__.json → copy-dir instructions + generate commands
extraEnv provides variable substitution for ${VAR} in generate commands. skipKinds, if non-empty, skips generate entries whose Kind field matches any value. generateHandler is called for generate entries with non-empty Kind; it should return nil for known kinds and an error for unrecognized ones.
func ApplyXgoPatchContent ¶ added in v1.2.0
ApplyXgoPatchContent applies parsed patch content to a source string. Each <patch> block is applied independently; blocks re-parse after prior block edits. Before applying a named patch, any existing markers with that name are cleared.
func ApplyXgoPatchFile ¶ added in v1.2.0
ApplyXgoPatchFile reads a .xgo.patch file and applies it to the target file. The target file is modified in place; existing edits with the same patch name are cleared first (idempotent).
func CleanPatch ¶
CleanPatch removes all content between /*<begin and /*<end markers, and also handles <next-line-original> annotations.
func CleanPatchMarkers ¶
func CleanPatchMarkers(content string, start PatchMarker, end PatchMarker) string
func FmtLineDirective ¶
for line directive, check https://github.com/golang/go/blob/24b395119b4df7f16915b9f01a6aded647b79bbd/src/cmd/compile/doc.go#L171 this tells the compiler, next line's line number is `line`
func GetFuncInsertPosition ¶
NOTE: new func is always inserted after a position because a function or variable may have some special directive like go:embed, go:linkname... we cannot add anything before them.
func RemoveBuildIgnore ¶
func ReplaceContentAfter ¶
func UpdateContent ¶
func UpdateContentLines ¶
func UpdateContentLines(content string, beginMark string, endMark string, seq []string, i int, position UpdatePosition, addContent string) string
UpdateContentLines add content before or after the `i`'s anchor in `seq` two lines will be automatically added after `beginMark`, and before `endMark`, so you don't need to include a line in `addContent`
Types ¶
type Command ¶ added in v1.2.0
type Command struct {
Type CommandType
// For goto:
GotoTarget string // e.g. "struct Foo", "func Bar", "func (t *T) Baz", "opening {", "closing }", "field Name"
// For match / find_for_replace / replace_directive (old text):
SearchText string
// For insert_before / insert_after / replace / replace_directive (new text):
EditText string
// For copy_func / replace_directive:
CopySource string // source function name
CopyTarget string // target function name or replace_directive new text
}
Command represents a single instruction within a <patch> block.
type CommandType ¶ added in v1.2.0
type CommandType int
CommandType identifies the type of a command within a patch block.
const ( CmdGoto CommandType = iota // goto struct/func/interface/opening/closing/field CmdMatch // match <text> CmdFindForReplace // find_for_replace <text> CmdInsertBefore // insert_before <text> CmdInsertAfter // insert_after <text> CmdReplace // replace <text> (requires prior find_for_replace) CmdNewline // newline CmdCopyFunc // copy_func <source> as <target> [append to file end] CmdReplaceDirective // replace_directive <old> with <new> CmdInsertAfterLine // insert_after_line <text> (includes trailing \n in insert point) )
type Config ¶ added in v1.2.0
type Config struct {
Version string `json:"version"`
Copy []CopyEntry `json:"copy,omitempty"`
Generate []GenerateEntry `json:"generate,omitempty"`
}
Config represents the __config__.json file in a patch directory.
func LoadConfig ¶ added in v1.2.0
LoadConfig reads and parses the __config__.json file from a patch directory. Returns an empty Config if the file does not exist.
type CopyEntry ¶ added in v1.2.0
type CopyEntry struct {
From string `json:"from"` // source relative to xgo repo root
To string `json:"to"` // destination relative to GOROOT; empty = patchDir
IgnoreFiles []string `json:"ignore_files,omitempty"`
}
CopyEntry represents a directory copy instruction in __config__.json.
type GenerateEntry ¶ added in v1.2.0
type GenerateEntry struct {
Kind string `json:"kind,omitempty"`
Cmd StringSliceOrSlice `json:"cmd,omitempty"`
CmdWindows StringSliceOrSlice `json:"cmd_windows,omitempty"`
Cwd string `json:"cwd,omitempty"` // working dir relative to goroot
Comments []string `json:"comments,omitempty"`
Env map[string]string `json:"env,omitempty"`
Outputs []string `json:"outputs,omitempty"`
}
GenerateEntry represents a command to run during patching, or a kind-based operation handled by a GenerateHandler callback.
func (*GenerateEntry) EffectiveCmd ¶ added in v1.2.0
func (g *GenerateEntry) EffectiveCmd() []string
type GenerateHandler ¶ added in v1.2.0
GenerateHandler is called for generate entries with non-empty Kind. extraEnv provides the same variable map used for ${VAR} substitution. Return a nil error if the handler handled the kind; a non-nil error aborts. The handler should return an error if the kind is unrecognized.
type PatchBlock ¶ added in v1.2.0
PatchBlock represents a single <patch name>...</patch> block.
type PatchFile ¶ added in v1.2.0
type PatchFile struct {
Blocks []PatchBlock
}
PatchFile represents a parsed .xgo.patch file containing one or more <patch> blocks.
func ParseXgoPatch ¶ added in v1.2.0
ParseXgoPatch parses a .xgo.patch file content into structured blocks and commands.
type PatchMarker ¶
type StringSliceOrSlice ¶ added in v1.2.0
type StringSliceOrSlice []string
func (*StringSliceOrSlice) UnmarshalJSON ¶ added in v1.2.0
func (s *StringSliceOrSlice) UnmarshalJSON(data []byte) error
type UpdatePosition ¶
type UpdatePosition int
const ( UpdatePosition_After UpdatePosition = 0 UpdatePosition_Before UpdatePosition = 1 UpdatePosition_Replace UpdatePosition = 2 )