sourcecode

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SourceCode

type SourceCode struct {
	Lines []string
}

SourceCode is a line-by-line representation of textual source code such as Go or YAML code.

func Parse

func Parse(content any) (sc *SourceCode)

Parse splits the content by line.

func ReadFS

func ReadFS(fileSys fs.FS, fileName string) (sc *SourceCode, err error)

ReadFS reads the named source file from a file system.

func ReadFile

func ReadFile(fileName string) (sc *SourceCode, err error)

ReadFile reads the named source file from disk. The file name is resolved relative to the current working directory.

func (*SourceCode) AddConst

func (sc *SourceCode) AddConst(constDefinition string)

AddConst adds a global const to .go source code. If a const statement doesn't exist, one is created.

Example:

AddConst(`x = 0`)

Results in:

const (
	x = 0
)

func (*SourceCode) AddImport

func (sc *SourceCode) AddImport(importDefinition string)

AddImport adds an import to .go source code. If an import statement doesn't exist, one is created.

Example:

AddImport(`"net/http"`)

Results in:

import (
	"net/http"
)

func (*SourceCode) AddVar

func (sc *SourceCode) AddVar(varDefinition string)

AddVar adds a global variable to .go source code. If a var statement doesn't exist, one is created.

Example:

AddVar(`x int`)

Results in:

var (
	x int
)

func (*SourceCode) Append

func (sc *SourceCode) Append(content any)

Append appends the content to the end of the source code.

func (*SourceCode) AppendSpacer

func (sc *SourceCode) AppendSpacer()

AppendSpacer adds an empty line to the end of the source code, if one does not already exist. A spacer line is not added if the file is empty.

func (*SourceCode) Block

func (sc *SourceCode) Block(baseLine int) (comment *SourceCode, body *SourceCode)

Block returns sub source codes for the comment before before the indicated line, and the lines at or after the indicated line with same or deeper indentation.

This example returns a comment with 2 lines and a body with 10 lines when targeting the func definition line; and comment with 1 line and a body with 3 lines when targeting the if statement line.

// Comment
// More comment
func Example() string {
	body := ""
	body += "more..."
	// Nested block
	if body == "" {
		nested := 1234
	}

	return body
}

func (*SourceCode) Content

func (sc *SourceCode) Content() string

Content merges all the lines of the source code and returns it as a single string.

func (*SourceCode) CopyrightNotice

func (sc *SourceCode) CopyrightNotice() (notice *SourceCode)

CopyrightNotice returns the first comment in the source file if it contains the word "copyright". It returns nil if not found.

func (*SourceCode) Indentation

func (sc *SourceCode) Indentation(line int) string

Indentation returns the whitespace at the head of the indicated line.

func (*SourceCode) InsertAfter

func (sc *SourceCode) InsertAfter(line int, content any)

InsertAfter inserts the content after the indicated line. Line numbers are 0-based.

func (*SourceCode) InsertBefore

func (sc *SourceCode) InsertBefore(line int, content any)

InsertBefore inserts the content before the indicated line. Line numbers are 0-based.

func (*SourceCode) Len

func (sc *SourceCode) Len() (lines int)

Len returns the number of lines in the source code.

func (*SourceCode) MatchAllLines

func (sc *SourceCode) MatchAllLines(regularExpression string) (matchedLines []int)

MatchAllLines returns the 0-based line numbers that match a regular expression.

func (*SourceCode) MatchFirstLine

func (sc *SourceCode) MatchFirstLine(regularExpression string) (matchedLine int)

MatchFirstLine returns the first 0-based line number that matches a regular expression. -1 is returned if no match is found.

func (*SourceCode) MatchNextLine

func (sc *SourceCode) MatchNextLine(onOrAfterLine int, regularExpression string) (matchedLine int)

MatchNextLine returns the first 0-based line number that matches a regular expression at or after the indicated line. -1 is returned if no match is found.

func (*SourceCode) MatchPreviousLine

func (sc *SourceCode) MatchPreviousLine(onOrBeforeLine int, regularExpression string) (matchedLine int)

MatchPreviousLine returns the last 0-based line number that matches a regular expression at or before the indicated line. -1 is returned if no match is found.

func (*SourceCode) Remove

func (sc *SourceCode) Remove(lineStart int, lineEnd int)

Remove removes lines [start:end) from the source code. Line numbers are 0-based.

func (*SourceCode) Replace

func (sc *SourceCode) Replace(lineStart int, lineEnd int, newContent any)

Replace removes lines [start:end) from the source code and replaces them with the new content. Line numbers are 0-based.

func (*SourceCode) ReplaceAllMatches

func (sc *SourceCode) ReplaceAllMatches(regularExpression string, replacement string)

ReplaceAllMatches replaces all matches of the regular expression with the replacement string. The replacement string may contain ${1} placeholders that refer to captured groups in the regular expression. Matching is performed on a per-line basis.

func (*SourceCode) SortImports

func (sc *SourceCode) SortImports()

SortImports sorts the imports in .go source code.

func (*SourceCode) Sub

func (sc *SourceCode) Sub(lineStart int, lineEnd int) (sub *SourceCode)

Sub returns a new source code with the half-open line range [start:end) of the original. The sub is a copy. Changes to the original do not affect the sub, nor vice verse.

func (*SourceCode) WriteToFile

func (sc *SourceCode) WriteToFile(destFileName string) (written bool, err error)

WriteToFile writes the content of the source code to disk. The file name is resolved relative to the current OS working directory. Writing is preempted if the content of the source code was not changed.

Jump to

Keyboard shortcuts

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