srcop

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package srcop provides file mutation operations for the src tool: replace, insert, delete, comment, and edit.

Plane: shared

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(filename string, source []byte, symbolID string, depth int) ([]byte, error)

Delete removes a symbol and its doc comment from the file. Returns the modified file content.

func InsertAfter

func InsertAfter(filename string, source []byte, symbolID string, newContent []byte, depth int) ([]byte, error)

InsertAfter inserts new content after the symbol identified by symbolID. Returns the modified file content.

func InsertBefore

func InsertBefore(filename string, source []byte, symbolID string, newContent []byte, depth int) ([]byte, error)

InsertBefore inserts new content before the symbol identified by symbolID. Returns the modified file content.

func ReadComment

func ReadComment(filename string, source []byte, symbolID string, depth int) (string, error)

ReadComment extracts the doc comment attached to a symbol. Returns empty string if no doc comment exists.

func Replace

func Replace(filename string, source []byte, symbolID string, newContent []byte, depth int) ([]byte, error)

Replace replaces a symbol's byte range with new content. Returns the modified file content. Note: Replace does not remove the existing doc comment (DocStart..StartByte). The caller is responsible for including or omitting comment text in newContent. Use WriteComment to update the doc comment independently.

func WriteComment

func WriteComment(filename string, source []byte, symbolID string, comment []byte, depth int) ([]byte, error)

WriteComment replaces or adds a doc comment on a symbol. Detects comment style from file extension via treesitter.LangNameFromExt. Returns the new file content.

Types

type EditResult added in v1.0.0

type EditResult struct {
	Content    []byte       // the new file bytes
	Pass       string       // "exact"|"trim-trailing"|"trim-both"|"unicode-fold"
	IndentFrom indent.Style // zero value if reindent not attempted
	IndentTo   indent.Style
	Reindented bool
	Warnings   []string // file-level + reindent line-level warnings
}

EditResult holds the result of an edit operation including match metadata.

func Edit added in v1.0.0

func Edit(filename string, source []byte, input []byte) (*EditResult, error)

Edit applies a text replacement to source using a BEFORE/AFTER block from input. input must contain ===BEFORE=== and ===AFTER=== delimiters. It does NOT call writeAndShow — that is the caller's responsibility.

func EditDirect added in v1.0.0

func EditDirect(filename string, source, oldText, newText []byte) (*EditResult, error)

EditDirect applies a text replacement to source using raw oldText/newText bytes, bypassing the ===BEFORE===/===AFTER=== delimiter parser. It does NOT call writeAndShow — that is the caller's responsibility.

Jump to

Keyboard shortcuts

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