textbuf

package
v1.2.17 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2022 License: BSD-3-Clause Imports: 27 Imported by: 2

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 SearchContext = 30

SearchContext is how much text to include on either side of the search match

View Source
var UndoGroupDelayMSec = 250

UndoGroupDelayMSec is number of milliseconds above which a new group is started, for grouping undo events

View Source
var UndoTrace = false

UndoTrace -- set to true to get a report of undo actions

Functions

func BytesToLineStrings

func BytesToLineStrings(txt []byte, addNewLn bool) []string

BytesToLineStrings returns []string lines If addNewLn is true, each string line has a \n appended at end.

func CountWordsLines added in v0.9.14

func CountWordsLines(reader io.Reader) (words, lines int)

CountWordsLines counts the number of words (aka Fields, space-separated strings) and lines given io.Reader input

func CountWordsLinesRegion added in v0.9.14

func CountWordsLinesRegion(src [][]rune, reg Region) (words, lines int)

CountWordsLinesRegion counts the number of words (aka Fields, space-separated strings) and lines in given region of source (lines = 1 + End.Ln - Start.Ln)

func DiffLinesUnified

func DiffLinesUnified(astr, bstr []string, context int, afile, adate, bfile, bdate string) []byte

DiffLinesUnified computes the diff between two string arrays (one string per line), returning a unified diff with given amount of context (default of 3 will be used if -1), with given file names and modification dates.

func FileBytes

func FileBytes(fpath string) ([]byte, error)

FileBytes returns the bytes of given file.

func FileRegionBytes

func FileRegionBytes(fpath string, stLn, edLn int, preComments bool, lnBack int) []byte

FileRegionBytes returns the bytes of given file within given start / end lines, either of which might be 0 (in which case full file is returned). If preComments is true, it also automatically includes any comments that might exist just prior to the start line if stLn is > 0, going back a maximum of lnBack lines.

func PreCommentStart

func PreCommentStart(lns [][]byte, stLn int, comLn, comSt, comEd string, lnBack int) int

PreCommentStart returns the starting line for comment line(s) that just precede the given stLn line number within the given lines of bytes, using the given line-level and block start / end comment chars. returns stLn if nothing found. Only looks back a total of lnBack lines.

func ReCaseString added in v0.9.15

func ReCaseString(str string, c Cases) string

ReCaseString changes the case of the string according to the given case type.

func SupportedComments

func SupportedComments(fpath string) (comLn, comSt, comEd string)

SupportedComments returns the comment strings for supported file types, and returns the standard C-style comments otherwise.

Types

type AdjustPosDel

type AdjustPosDel int

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

type Cases added in v0.9.15

type Cases int32

Cases are different cases -- Lower, Upper, Camel, etc

const (
	LowerCase Cases = iota
	UpperCase

	// CamelCase is init-caps
	CamelCase

	// LowerCamelCase has first letter lower-case
	LowerCamelCase

	// SnakeCase is snake_case -- lower with underbars
	SnakeCase

	// UpperSnakeCase is SNAKE_CASE -- upper with underbars
	UpperSnakeCase

	// KebabCase is kebab-case -- lower with -'s
	KebabCase

	// CasesN is the number of textview states
	CasesN
)

func (*Cases) FromString added in v0.9.15

func (i *Cases) FromString(s string) error

func (Cases) MarshalJSON added in v0.9.15

func (ev Cases) MarshalJSON() ([]byte, error)

func (Cases) String added in v0.9.15

func (i Cases) String() string

func (*Cases) UnmarshalJSON added in v0.9.15

func (ev *Cases) UnmarshalJSON(b []byte) error

type Diffs

type Diffs []difflib.OpCode

Diffs are raw differences between text, in terms of lines, reporting a sequence of operations that would convert one buffer (a) into the other buffer (b). Each operation is either an 'r' (replace), 'd' (delete), 'i' (insert) or 'e' (equal).

func DiffLines

func DiffLines(astr, bstr []string) Diffs

DiffLines computes the diff between two string arrays (one string per line), reporting a sequence of operations that would convert buffer a into buffer b. Each operation is either an 'r' (replace), 'd' (delete), 'i' (insert) or 'e' (equal). Everything is line-based (0, offset).

func (Diffs) DiffForLine added in v0.9.13

func (di Diffs) DiffForLine(line int) (int, difflib.OpCode)

func (Diffs) Reverse added in v1.2.2

func (di Diffs) Reverse() Diffs

Reverse returns the reverse-direction diffs, switching a vs. b

func (Diffs) String

func (di Diffs) String() string

String satisfies the Stringer interface

func (Diffs) ToPatch added in v1.2.2

func (dif Diffs) ToPatch(bstr []string) Patch

ToPatch creates a Patch list from given Diffs output from DiffLines and the b strings from which the needed lines of source are copied. ApplyPatch with this on the a strings will result in the b strings. The resulting Patch is independent of bstr slice.

type Edit

type Edit struct {
	Reg    Region   `` /* 198-byte string literal not displayed */
	Text   [][]rune `` /* 136-byte string literal not displayed */
	Group  int      `desc:"optional grouping number, for grouping edits in Undo for example"`
	Delete bool     `desc:"action is either a deletion or an insertion"`
	Rect   bool     `` /* 154-byte string literal not displayed */
}

Edit describes an edit action to a buffer -- this is the data passed via signals to viewers of the buffer. Actions are only deletions and insertions (a change is a sequence of those, given normal editing processes). The TextBuf always reflects the current state *after* the edit.

func (*Edit) AdjustPos

func (te *Edit) AdjustPos(pos lex.Pos, del AdjustPosDel) lex.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 lex.Pos, t time.Time, del AdjustPosDel) lex.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) AdjustReg

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

AdjustReg 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. If the region becomes empty, RegionNil will be returned.

func (*Edit) Clone added in v0.9.15

func (te *Edit) Clone() *Edit

Clone returns a clone of the edit record.

func (*Edit) CopyFrom added in v0.9.15

func (te *Edit) CopyFrom(cp *Edit)

Copy copies from other Edit

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 {
	Reg  Region `desc:"region surrounding the match -- column positions are in runes, not bytes"`
	Text []byte `desc:"text surrounding the match, at most FileSearchContext on either side (within a single line)"`
}

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 Search(reader io.Reader, find []byte, ignoreCase bool) (int, []Match)

Search looks for a string (no regexp) from an io.Reader input stream, using given case-sensitivity. Returns number of occurrences and specific match position list. Column positions are in runes.

func SearchByteLinesRegexp added in v1.0.2

func SearchByteLinesRegexp(src [][]byte, re *regexp.Regexp) (int, []Match)

SearchByteLinesRegexp looks for a regexp within lines of bytes, with given case-sensitivity returning number of occurrences and specific match position list. Column positions are in runes.

func SearchFile

func SearchFile(filename string, find []byte, ignoreCase bool) (int, []Match)

SearchFile looks for a string (no regexp) within a file, in a case-sensitive way, returning number of occurrences and specific match position list -- column positions are in runes.

func SearchFileRegexp added in v1.0.2

func SearchFileRegexp(filename string, re *regexp.Regexp) (int, []Match)

SearchFileRegexp looks for a string (using regexp) within a file, returning number of occurrences and specific match position list -- column positions are in runes.

func SearchLexItems

func SearchLexItems(src [][]rune, lexs []lex.Line, find []byte, ignoreCase bool) (int, []Match)

SearchLexItems looks for a string (no regexp), as entire lexically tagged items, with given case-sensitivity returning number of occurrences and specific match position list. Column positions are in runes.

func SearchRegexp added in v1.0.2

func SearchRegexp(reader io.Reader, re *regexp.Regexp) (int, []Match)

SearchRegexp looks for a string (using regexp) from an io.Reader input stream. Returns number of occurrences and specific match position list. Column positions are in runes.

func SearchRuneLines

func SearchRuneLines(src [][]rune, find []byte, ignoreCase bool) (int, []Match)

SearchRuneLines looks for a string (no regexp) within lines of runes, with given case-sensitivity returning number of occurrences and specific match position list. Column positions are in runes.

type Opts

type Opts struct {
	gi.EditorPrefs `desc:"editor prefs from gogi prefs"`
	CommentLn      string `desc:"character(s) that start a single-line comment -- if empty then multi-line comment syntax will be used"`
	CommentSt      string `desc:"character(s) that start a multi-line comment or one that requires both start and end"`
	CommentEd      string `desc:"character(s) that end a multi-line comment or one that requires both start and end"`
}

Opts contains options for TextBufs -- contains everything necessary to conditionalize editing of a given text file

func (*Opts) CommentStrs

func (tb *Opts) CommentStrs() (comst, comed string)

CommentStrs returns the comment start and end strings, using line-based CommentLn first if set and falling back on multi-line / general purpose start / end syntax

func (*Opts) ConfigSupported

func (tb *Opts) ConfigSupported(sup filecat.Supported) bool

ConfigSupported configures options based on the supported language info in GoPi returns true if supported

func (*Opts) IndentChar

func (tb *Opts) IndentChar() indent.Char

IndentChar returns the indent character based on SpaceIndent option

type Patch added in v1.2.2

type Patch []*PatchRec

Patch is a collection of patch records needed to turn original a buffer into b

func (Patch) Apply added in v1.2.2

func (pt Patch) Apply(astr []string) []string

Apply applies given Patch to given file as list of strings this does no checking except range checking so it won't crash so if input string is not appropriate for given Patch, results may be nonsensical.

func (Patch) NumBlines added in v1.2.2

func (pt Patch) NumBlines() int

NumBlines returns the total number of Blines source code in the patch

type PatchRec added in v1.2.2

type PatchRec struct {
	Op     difflib.OpCode `desc:"diff operation: 'r', 'd', 'i', 'e'"`
	Blines []string       `desc:"lines from B buffer needed for 'r' and 'i' operations"`
}

PatchRec is a self-contained record of a DiffLines result that contains the source lines of the b buffer needed to patch a into b

type Region

type Region struct {
	Start lex.Pos     `desc:"starting position"`
	End   lex.Pos     `desc:"ending position: line number is *inclusive* but character position is *exclusive* (-1)"`
	Time  nptime.Time `` /* 131-byte string literal not displayed */
}

Region represents a text region as a start / end position, and includes a Time stamp for when the region was created as valid positions into the TextBuf. The character end position is an *exclusive* position (i.e., the region ends at the character just prior to that character) but the lines are always *inclusive* (i.e., it is the actual line, not the next line).

var RegionNil Region

RegionNil is the empty (zero) text region -- all zeros

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, and also sets the time stamp to now

func NewRegionLen

func NewRegionLen(start lex.Pos, len int) Region

NewRegionLen makes a new Region from a starting point and a length along same line

func NewRegionPos

func NewRegionPos(st, ed lex.Pos) Region

NewRegionPos creates a new text region using position values and also sets the time stamp to now

func (*Region) AgeMSec

func (tr *Region) AgeMSec() int

AgeMSec returns the time interval in milliseconds from time.Now()

func (*Region) AgoMSec

func (tr *Region) AgoMSec(t time.Time) int

AgoMSec returns how long ago this Region's time stamp is relative to given time, in milliseconds.

func (*Region) Contains

func (tr *Region) Contains(ln int) bool

Contains returns true if line is within region

func (*Region) FromString

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

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

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) IsSameLine

func (tr *Region) IsSameLine() bool

IsSameLine returns true if region starts and ends on the same line

func (*Region) SinceMSec

func (tr *Region) SinceMSec(earlier *Region) int

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

func (*Region) TimeNow

func (tr *Region) TimeNow()

TimeNow grabs the current time as the edit time

type Undo

type Undo struct {
	Off       bool       `desc:"if true, saving and using undos is turned off (e.g., inactive buffers)"`
	Stack     []*Edit    `desc:"undo stack of edits"`
	UndoStack []*Edit    `desc:"undo stack of *undo* edits -- added to whenever an Undo is done -- for emacs-style undo"`
	Pos       int        `desc:"undo position in stack"`
	Group     int        `desc:"group counter"`
	Mu        sync.Mutex `json:"-" xml:"-" desc:"mutex protecting all updates"`
}

Undo is the TextBuf undo manager

func (*Undo) AdjustPos

func (un *Undo) AdjustPos(pos lex.Pos, t time.Time, del AdjustPosDel) lex.Pos

AdjustPos adjusts given text position, which was recorded at given time for any edits that have taken place since that time (using the Undo stack). 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 (*Undo) AdjustReg

func (un *Undo) AdjustReg(reg Region) Region

AdjustReg adjusts given text region for any edits that have taken place since time stamp on region (using the Undo stack). If region was wholly within a deleted region, then RegionNil will be returned -- otherwise it is clipped appropriately as function of deletes.

func (*Undo) NewGroup

func (un *Undo) NewGroup()

NewGroup increments the Group counter so subsequent undos will be grouped separately

func (*Undo) RedoNext

func (un *Undo) RedoNext() *Edit

RedoNext returns the current item on Stack for Redo, and increments the position returns nil if at end of stack.

func (*Undo) RedoNextIfGroup

func (un *Undo) RedoNextIfGroup(gp int) *Edit

RedoNextIfGroup returns the current item on Stack for Redo if it is same group and increments the position. returns nil if at end of stack.

func (*Undo) Reset

func (un *Undo) Reset()

Reset clears all undo records

func (*Undo) Save

func (un *Undo) Save(tbe *Edit)

Save saves given edit to undo stack, with current group marker unless timer interval exceeds UndoGroupDelayMSec since last item.

func (*Undo) SaveUndo

func (un *Undo) SaveUndo(tbe *Edit)

SaveUndo saves given edit to UndoStack (stack of undoes that have have undone..) for emacs mode.

func (*Undo) UndoPop

func (un *Undo) UndoPop() *Edit

UndoPop pops the top item off of the stack for use in Undo. returns nil if none.

func (*Undo) UndoPopIfGroup

func (un *Undo) UndoPopIfGroup(gp int) *Edit

UndoPopIfGroup pops the top item off of the stack if it is the same as given group

func (*Undo) UndoStackSave

func (un *Undo) UndoStackSave()

UndoStackSave if EmacsUndo mode is active, saves the UndoStack to the regular Undo stack, at the end, and moves undo to the very end. Undo is a constant stream..

Jump to

Keyboard shortcuts

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