Documentation
¶
Overview ¶
Package astedit inserts code into an existing Go file without reprinting it.
The insertion point is located through the AST — never a regular expression, never a marker comment — and then the new text is SPLICED into the original bytes. The file is never re-rendered, so comments, blank lines, and the author's own formatting survive by construction rather than by a decoration model that has to be kept in step with the language.
This is the model x/tools/go/analysis uses for its suggested fixes (TextEdit{Pos, End, NewText}), and it is why this package needs no third-party dependency.
Index ¶
- func AddArgument(src []byte, fn, ident string) ([]byte, error)
- func AddCallArgument(src []byte, fn, ident string) ([]byte, error)
- func AddConstructorParam(src []byte, fn, typeName, name, typ string) ([]byte, error)
- func AddImport(src []byte, path string) ([]byte, error)
- func AddStatement(src []byte, fn, stmt string) ([]byte, error)
- func AddStructField(src []byte, typeName, name, typ string) ([]byte, error)
- func HasCall(src []byte, fn string) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddArgument ¶
AddArgument adds ident as the last argument of the call to fn — for example warren.Providers — inside the file's warren.NewModule call. When no such call exists, it is created as a new option of NewModule.
It is idempotent: an ident already present is left alone and the file is returned unchanged, which is what makes re-running a generator a no-op.
func AddCallArgument ¶
AddCallArgument adds ident as the last argument of the call to fn — for example warren.New in a main package — and errors when there is no such call. Unlike AddArgument it creates nothing: the call has to exist, because there is no module declaration to hang a new option off.
It is idempotent for the same reason.
func AddConstructorParam ¶
AddConstructorParam adds "name typ" as the last parameter of fn AND "name: name" to the &<typeName>{…} literal it returns.
Both halves are one call because either alone leaves the file uncompilable — an unused parameter, or a field assigned from nothing.
func AddImport ¶
AddImport adds an import path if it is absent, and returns the file unchanged if it is present.
func AddStatement ¶
AddStatement appends stmt as the last statement of the named function or method body.
func AddStructField ¶
AddStructField adds "name typ" as the last field of the named struct.
func HasCall ¶
HasCall reports whether src contains a call to fn — "warren.New", say.
It is how a caller asks "would AddCallArgument work on this file?" without editing it, and it runs the SAME matcher, so the answer cannot drift from the edit. `warren g module` uses it to tell an application's main package from a main that happens to live under cmd/: a module is registered in a warren.New(...) call, so a main without one is not somewhere a module can go, whoever wrote it.
An unparseable file has no call this package can edit, so it is false rather than an error — the caller is choosing between candidates, and one broken file should not fail that choice.
Types ¶
This section is empty.