textpos

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2025 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// IgnoreCase is passed to search functions to indicate case should be ignored
	IgnoreCase = true

	// UseCase is passed to search functions to indicate case is relevant
	UseCase = false
)

Variables

View Source
var (
	// PosErr represents an error text position (-1 for both line and char)
	// used as a return value for cases where error positions are possible.
	PosErr = Pos{-1, -1}

	PosZero = Pos{}
)
View Source
var MatchContext = 30

MatchContext is how much text to include on either side of the match.

View Source
var RegionZero = Region{}

Functions

func BackwardWord

func BackwardWord(txt []rune, pos, steps int) (wpos, nstep int)

BackwardWord moves position index backward by words, for given number of steps. Returns the number of steps actually moved, given the amount of text available.

func ForwardWord

func ForwardWord(txt []rune, pos, steps int) (wpos, nstep int)

ForwardWord moves position index forward by words, for given number of steps. Returns the number of steps actually moved, given the amount of text available.

func IsWordBreak

func IsWordBreak(r1, r2 rune) bool

IsWordBreak defines what counts as a word break for the purposes of selecting words. r1 is the rune in question, r2 is the rune past r1 in the direction you are moving. Pass -1 for r2 if there is no rune past r1.

func RuneIsWordBreak

func RuneIsWordBreak(r rune) bool

RuneIsWordBreak returns true if given rune counts as a word break for the purposes of selecting words.

Types

type AdjustPosDel

type AdjustPosDel int32 //enums:enum

AdjustPosDel determines what to do with positions within deleted region

const (
	// AdjustPosDelErr means return a PosErr when in deleted region.
	AdjustPosDelErr AdjustPosDel = iota

	// AdjustPosDelStart means return start of deleted region.
	AdjustPosDelStart

	// AdjustPosDelEnd means return end of deleted region.
	AdjustPosDelEnd
)

these are options for what to do with positions within deleted region for the AdjustPos function

const AdjustPosDelN AdjustPosDel = 3

AdjustPosDelN is the highest valid value for type AdjustPosDel, plus one.

func AdjustPosDelValues

func AdjustPosDelValues() []AdjustPosDel

AdjustPosDelValues returns all possible values for the type AdjustPosDel.

func (AdjustPosDel) Desc

func (i AdjustPosDel) Desc() string

Desc returns the description of the AdjustPosDel value.

func (AdjustPosDel) Int64

func (i AdjustPosDel) Int64() int64

Int64 returns the AdjustPosDel value as an int64.

func (AdjustPosDel) MarshalText

func (i AdjustPosDel) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*AdjustPosDel) SetInt64

func (i *AdjustPosDel) SetInt64(in int64)

SetInt64 sets the AdjustPosDel value from an int64.

func (*AdjustPosDel) SetString

func (i *AdjustPosDel) SetString(s string) error

SetString sets the AdjustPosDel value from its string representation, and returns an error if the string is invalid.

func (AdjustPosDel) String

func (i AdjustPosDel) String() string

String returns the string representation of this AdjustPosDel value.

func (*AdjustPosDel) UnmarshalText

func (i *AdjustPosDel) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (AdjustPosDel) Values

func (i AdjustPosDel) Values() []enums.Enum

Values returns all possible values for the type AdjustPosDel.

type Edit

type Edit struct {

	// Region for the edit, specifying the region to delete, or the size
	// of the region to insert, corresponding to the Text.
	// Also contains the Time stamp for this edit.
	Region Region

	// Text deleted or inserted, in rune lines. For Rect this is the
	// spanning character distance per line, times number of lines.
	Text [][]rune

	// Group is the optional grouping number, for grouping edits in Undo for example.
	Group int

	// Delete indicates a deletion, otherwise an insertion.
	Delete bool

	// Rect is a rectangular region with upper left corner = Region.Start
	// and lower right corner = Region.End.
	// Otherwise it is for the full continuous region.
	Rect bool
}

Edit describes an edit action to line-based text, operating on a Region of the text. Actions are only deletions and insertions (a change is a sequence of each, given normal editing processes).

func NewEditFromRunes

func NewEditFromRunes(text []rune) *Edit

NewEditFromRunes returns a 0-based edit from given runes.

func (*Edit) AdjustPos

func (te *Edit) AdjustPos(pos Pos, del AdjustPosDel) Pos

AdjustPos adjusts the given text position as a function of the edit. If the position was within a deleted region of text, del determines what is returned.

func (*Edit) AdjustPosIfAfterTime

func (te *Edit) AdjustPosIfAfterTime(pos Pos, t time.Time, del AdjustPosDel) Pos

AdjustPosIfAfterTime checks the time stamp and IfAfterTime, it adjusts the given text position as a function of the edit del determines what to do with positions within a deleted region either move to start or end of the region, or return an error.

func (*Edit) AdjustRegion

func (te *Edit) AdjustRegion(reg Region) Region

AdjustRegion adjusts the given text region as a function of the edit, including checking that the timestamp on the region is after the edit time, if the region has a valid Time stamp (otherwise always does adjustment). If the starting position is within a deleted region, it is moved to the end of the deleted region, and if the ending position was within a deleted region, it is moved to the start.

func (*Edit) Clone

func (te *Edit) Clone() *Edit

Clone returns a clone of the edit record.

func (*Edit) Copy

func (te *Edit) Copy(cp *Edit)

Copy copies from other Edit, making a clone of the source text.

func (*Edit) String

func (te *Edit) String() string

func (*Edit) ToBytes

func (te *Edit) ToBytes() []byte

ToBytes returns the Text of this edit record to a byte string, with newlines at end of each line -- nil if Text is empty

type Match

type Match struct {

	// Region of the match. Column positions are in runes.
	Region Region

	// Text surrounding the match, at most MatchContext on either side
	// (within a single line).
	Text []rune

	// TextMatch has the Range within the Text where the match is.
	TextMatch Range
}

Match records one match for search within file, positions in runes.

func NewMatch

func NewMatch(rn []rune, st, ed, ln int) Match

NewMatch returns a new Match entry for given rune line with match starting at st and ending before ed, on given line

func (*Match) String

func (m *Match) String() string

type Pos

type Pos struct {
	Line int
	Char int
}

Pos is a text position in terms of line and character index within a line, using 0-based line numbers, which are converted to 1 base for the String() representation. Char positions are always in runes, and can also be used for other units such as tokens, spans, or runs.

func (Pos) AddChar

func (ps Pos) AddChar(ch int) Pos

AddChar returns a Pos with Char number added.

func (Pos) AddLine

func (ps Pos) AddLine(ln int) Pos

AddLine returns a Pos with Line number added.

func (*Pos) FromString

func (ps *Pos) FromString(link string) bool

FromString decodes text position from a string representation of form: [#]LxxCxx. Used in e.g., URL links. Returns true if successful.

func (Pos) IsLess

func (ps Pos) IsLess(cmp Pos) bool

IsLess returns true if receiver position is less than given comparison.

func (Pos) String

func (ps Pos) String() string

String satisfies the fmt.Stringer interferace

type Range

type Range struct {
	// St is the starting index of the range.
	Start int

	// Ed is the ending index of the range.
	End int
}

Range defines a range with a start and end index, where end is typically exclusive, as in standard slice indexing and for loop conventions.

func WordAt

func WordAt(txt []rune, pos int) Range

WordAt returns the range for a word within given text starting at given position index. If the current position is a word break then go to next break after the first non-break.

func (Range) Contains

func (r Range) Contains(i int) bool

Contains returns true if range contains given index.

func (Range) Intersect

func (r Range) Intersect(o Range) Range

Intersect returns the intersection of two ranges. If they do not overlap, then the Start and End will be -1

func (Range) Len

func (r Range) Len() int

Len returns the length of the range: End - Start.

type Region

type Region struct {
	// Start is the starting position of region.
	Start Pos

	// End is the ending position of region.
	// Char position is _exclusive_ so the last char is the one before End.Char.
	// Line position is _inclusive_, so the last line is End.Line.
	End Pos

	// Time when region was set: needed for updating locations in the text based
	// on time stamp (using efficient non-pointer time).
	Time nptime.Time
}

Region is a contiguous region within a source file with lines of rune chars, defined by start and end Pos positions. End.Char position is _exclusive_ so the last char is the one before End.Char. End.Line position is _inclusive_, so the last line is End.Line. There is a Time stamp for when the region was created as valid positions into the lines source, which is critical for tracking edits in live documents.

func NewRegion

func NewRegion(stLn, stCh, edLn, edCh int) Region

NewRegion creates a new text region using separate line and char values for start and end. Sets timestamp to now.

func NewRegionLen

func NewRegionLen(start Pos, len int) Region

NewRegionLen makes a new Region from a starting point and a length along same line. Sets timestamp to now.

func NewRegionPos

func NewRegionPos(st, ed Pos) Region

NewRegionPos creates a new text region using position values. Sets timestamp to now.

func (*Region) Age

func (tr *Region) Age() time.Duration

Age returns the time interval from time.Now

func (*Region) Ago

func (tr *Region) Ago(t time.Time) time.Duration

Ago returns how long ago this Region's time stamp is relative to given time.

func (Region) Contains

func (tr Region) Contains(ps Pos) bool

Contains returns true if region contains given position.

func (Region) ContainsLine

func (tr Region) ContainsLine(ln int) bool

ContainsLine returns true if line is within region

func (*Region) FromStringURL

func (tr *Region) FromStringURL(link string) bool

FromStringURL decodes text region from a string representation of form: [#]LxxCxx-LxxCxx. Used in e.g., URL links. returns true if successful

func (Region) Intersect

func (tr Region) Intersect(or Region, endChar int) Region

Intersect returns the intersection of this region with given other region, where the other region is assumed to be the larger, constraining region, within which you are fitting the receiver region. Char level start / end are only constrained if on same Start / End line. The given endChar value is used for the end of an interior line.

func (*Region) IsAfterTime

func (tr *Region) IsAfterTime(t time.Time) bool

IsAfterTime reports if this region's time stamp is after given time value if region Time stamp has not been set, it always returns true

func (Region) IsNil

func (tr Region) IsNil() bool

IsNil checks if the region is empty, because the start is after or equal to the end.

func (Region) MoveToLine

func (tr Region) MoveToLine(ln int) Region

MoveToLine returns a new Region with the Start line set to given line.

func (Region) NumLines

func (tr Region) NumLines() int

NumLines is the number of lines in this region, based on inclusive end line.

func (Region) ShiftLines

func (tr Region) ShiftLines(ln int) Region

ShiftLines returns a new Region with the start and End lines shifted by given number of lines.

func (*Region) Since

func (tr *Region) Since(earlier *Region) time.Duration

Since returns the time interval between this Region's time stamp and that of the given earlier region's stamp.

func (*Region) String

func (tr *Region) String() string

func (*Region) TimeNow

func (tr *Region) TimeNow()

TimeNow grabs the current time as the edit time.

Jump to

Keyboard shortcuts

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