2023-01-17 23:19:10 -07:00
|
|
|
package textmanip
|
|
|
|
|
|
|
|
import "unicode"
|
|
|
|
|
2023-03-31 17:40:25 -06:00
|
|
|
// Dot represents a cursor or text selection. It has a start and end position,
|
|
|
|
// referring to where the user began and ended the selection respectively.
|
2023-02-12 23:52:31 -07:00
|
|
|
type Dot struct { Start, End int }
|
|
|
|
|
2023-03-31 17:40:25 -06:00
|
|
|
// EmptyDot returns a zero-width dot at the specified position.
|
2023-02-12 23:52:31 -07:00
|
|
|
func EmptyDot (position int) Dot {
|
|
|
|
return Dot { position, position }
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:25 -06:00
|
|
|
// Canon places the lesser value at the start, and the greater value at the end.
|
|
|
|
// Note that a canonized dot does not in all cases correspond directly to the
|
2023-03-31 18:28:53 -06:00
|
|
|
// original, because there is a semantic value to the start and end positions.
|
2023-02-12 23:52:31 -07:00
|
|
|
func (dot Dot) Canon () Dot {
|
|
|
|
if dot.Start > dot.End {
|
|
|
|
return Dot { dot.End, dot.Start }
|
|
|
|
} else {
|
|
|
|
return dot
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:25 -06:00
|
|
|
// Empty returns whether or not the
|
2023-02-12 23:52:31 -07:00
|
|
|
func (dot Dot) Empty () bool {
|
|
|
|
return dot.Start == dot.End
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:25 -06:00
|
|
|
// Add shifts the dot to the right by the specified amount.
|
2023-02-12 23:52:31 -07:00
|
|
|
func (dot Dot) Add (delta int) Dot {
|
|
|
|
return Dot {
|
|
|
|
dot.Start + delta,
|
|
|
|
dot.End + delta,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 18:28:53 -06:00
|
|
|
// Sub shifts the dot to the left by the specified amount.
|
2023-02-12 23:52:31 -07:00
|
|
|
func (dot Dot) Sub (delta int) Dot {
|
|
|
|
return Dot {
|
|
|
|
dot.Start - delta,
|
|
|
|
dot.End - delta,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:25 -06:00
|
|
|
// Constrain constrains the dot's start and end from zero to length (inclusive).
|
2023-02-13 10:55:51 -07:00
|
|
|
func (dot Dot) Constrain (length int) Dot {
|
|
|
|
if dot.Start < 0 { dot.Start = 0 }
|
|
|
|
if dot.Start > length { dot.Start = length}
|
|
|
|
if dot.End < 0 { dot.End = 0 }
|
|
|
|
if dot.End > length { dot.End = length}
|
|
|
|
return dot
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:25 -06:00
|
|
|
// Width returns how many runes the dot spans.
|
2023-03-30 19:33:49 -06:00
|
|
|
func (dot Dot) Width () int {
|
|
|
|
dot = dot.Canon()
|
|
|
|
return dot.End - dot.Start
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:25 -06:00
|
|
|
// Slice returns the subset of text that the dot covers.
|
2023-03-30 19:33:49 -06:00
|
|
|
func (dot Dot) Slice (text []rune) []rune {
|
2023-03-31 01:25:46 -06:00
|
|
|
dot = dot.Canon().Constrain(len(text))
|
2023-03-30 19:33:49 -06:00
|
|
|
return text[dot.Start:dot.End]
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:25 -06:00
|
|
|
// WordToLeft returns how far away to the left the next word boundary is from a
|
|
|
|
// given position.
|
2023-02-13 10:55:51 -07:00
|
|
|
func WordToLeft (text []rune, position int) (length int) {
|
|
|
|
if position < 1 { return }
|
|
|
|
if position > len(text) { position = len(text) }
|
2023-01-17 23:19:10 -07:00
|
|
|
|
2023-02-13 10:55:51 -07:00
|
|
|
index := position - 1
|
2023-01-17 23:19:10 -07:00
|
|
|
for index >= 0 && unicode.IsSpace(text[index]) {
|
|
|
|
length ++
|
|
|
|
index --
|
|
|
|
}
|
|
|
|
for index >= 0 && !unicode.IsSpace(text[index]) {
|
|
|
|
length ++
|
|
|
|
index --
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:25 -06:00
|
|
|
// WordToRight returns how far away to the right the next word boundary is from
|
|
|
|
// a given position.
|
2023-02-13 10:55:51 -07:00
|
|
|
func WordToRight (text []rune, position int) (length int) {
|
|
|
|
if position < 0 { return }
|
|
|
|
if position > len(text) { position = len(text) }
|
2023-01-17 23:19:10 -07:00
|
|
|
|
2023-02-13 10:55:51 -07:00
|
|
|
index := position
|
2023-01-17 23:19:10 -07:00
|
|
|
for index < len(text) && unicode.IsSpace(text[index]) {
|
|
|
|
length ++
|
|
|
|
index ++
|
|
|
|
}
|
|
|
|
for index < len(text) && !unicode.IsSpace(text[index]) {
|
|
|
|
length ++
|
|
|
|
index ++
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:25 -06:00
|
|
|
// WordAround returns a dot that surrounds the word at the specified position.
|
2023-02-13 10:55:51 -07:00
|
|
|
func WordAround (text []rune, position int) (around Dot) {
|
|
|
|
return Dot {
|
2023-03-31 18:28:53 -06:00
|
|
|
position - WordToLeft(text, position),
|
|
|
|
position + WordToRight(text, position),
|
2023-02-13 10:55:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:25 -06:00
|
|
|
// Backspace deletes the rune to the left of the dot. If word is true, it
|
|
|
|
// deletes up until the next word boundary on the left. If the dot is non-empty,
|
|
|
|
// it deletes the text inside of the dot.
|
2023-02-12 23:52:31 -07:00
|
|
|
func Backspace (text []rune, dot Dot, word bool) (result []rune, moved Dot) {
|
2023-02-13 10:55:51 -07:00
|
|
|
dot = dot.Constrain(len(text))
|
2023-02-12 23:52:31 -07:00
|
|
|
if dot.Empty() {
|
|
|
|
distance := 1
|
|
|
|
if word {
|
2023-02-13 10:55:51 -07:00
|
|
|
distance = WordToLeft(text, dot.End)
|
2023-02-12 23:52:31 -07:00
|
|
|
}
|
2023-02-13 10:55:51 -07:00
|
|
|
result = append (
|
|
|
|
result,
|
|
|
|
text[:dot.Sub(distance).Constrain(len(text)).End]...)
|
|
|
|
result = append(result, text[dot.End:]...)
|
|
|
|
moved = EmptyDot(dot.Sub(distance).Start)
|
|
|
|
return
|
2023-02-12 23:52:31 -07:00
|
|
|
} else {
|
|
|
|
return Delete(text, dot, word)
|
2023-01-17 23:19:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:25 -06:00
|
|
|
// Delete deletes the rune to the right of the dot. If word is true, it deletes
|
|
|
|
// up until the next word boundary on the right. If the dot is non-empty, it
|
|
|
|
// deletes the text inside of the dot.
|
2023-02-12 23:52:31 -07:00
|
|
|
func Delete (text []rune, dot Dot, word bool) (result []rune, moved Dot) {
|
2023-02-13 10:55:51 -07:00
|
|
|
dot = dot.Constrain(len(text))
|
2023-02-12 23:52:31 -07:00
|
|
|
if dot.Empty() {
|
|
|
|
distance := 1
|
|
|
|
if word {
|
2023-02-13 10:55:51 -07:00
|
|
|
distance = WordToRight(text, dot.End)
|
2023-02-12 23:52:31 -07:00
|
|
|
}
|
2023-02-13 10:55:51 -07:00
|
|
|
result = append(result, text[:dot.End]...)
|
|
|
|
result = append (
|
|
|
|
result,
|
|
|
|
text[dot.Add(distance).Constrain(len(text)).End:]...)
|
2023-02-12 23:52:31 -07:00
|
|
|
moved = dot
|
|
|
|
return
|
|
|
|
} else {
|
2023-02-13 10:55:51 -07:00
|
|
|
dot = dot.Canon()
|
2023-02-12 23:52:31 -07:00
|
|
|
result = append(result, text[:dot.Start]...)
|
|
|
|
result = append(result, text[dot.End:]...)
|
|
|
|
moved = EmptyDot(dot.Start)
|
|
|
|
return
|
2023-01-17 23:19:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:25 -06:00
|
|
|
// Lift removes the section of text inside of the dot, and returns a copy of it.
|
2023-03-30 19:33:49 -06:00
|
|
|
func Lift (text []rune, dot Dot) (result []rune, moved Dot, lifted []rune) {
|
|
|
|
dot = dot.Constrain(len(text))
|
|
|
|
if dot.Empty() {
|
|
|
|
moved = dot
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dot = dot.Canon()
|
|
|
|
lifted = make([]rune, dot.Width())
|
|
|
|
copy(lifted, dot.Slice(text))
|
|
|
|
result = append(result, text[:dot.Start]...)
|
|
|
|
result = append(result, text[dot.End:]...)
|
|
|
|
moved = EmptyDot(dot.Start)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:25 -06:00
|
|
|
// Type inserts one of more runes into the text at the dot position. If the dot
|
|
|
|
// is non-empty, it replaces the text inside of the dot with the new runes.
|
2023-03-31 01:25:46 -06:00
|
|
|
func Type (text []rune, dot Dot, characters ...rune) (result []rune, moved Dot) {
|
2023-02-13 10:55:51 -07:00
|
|
|
dot = dot.Constrain(len(text))
|
2023-02-12 23:52:31 -07:00
|
|
|
if dot.Empty() {
|
2023-02-13 10:55:51 -07:00
|
|
|
result = append(result, text[:dot.End]...)
|
2023-03-31 01:25:46 -06:00
|
|
|
result = append(result, characters...)
|
2023-02-13 10:55:51 -07:00
|
|
|
if dot.End < len(text) {
|
|
|
|
result = append(result, text[dot.End:]...)
|
2023-02-12 23:52:31 -07:00
|
|
|
}
|
2023-03-31 01:25:46 -06:00
|
|
|
moved = EmptyDot(dot.Add(len(characters)).End)
|
2023-02-12 23:52:31 -07:00
|
|
|
return
|
|
|
|
} else {
|
2023-02-13 10:55:51 -07:00
|
|
|
dot = dot.Canon()
|
2023-02-12 23:52:31 -07:00
|
|
|
result = append(result, text[:dot.Start]...)
|
2023-03-31 01:25:46 -06:00
|
|
|
result = append(result, characters...)
|
2023-02-12 23:52:31 -07:00
|
|
|
result = append(result, text[dot.End:]...)
|
2023-03-31 01:25:46 -06:00
|
|
|
moved = EmptyDot(dot.Add(len(characters)).Start)
|
2023-02-12 23:52:31 -07:00
|
|
|
return
|
2023-01-17 23:19:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:25 -06:00
|
|
|
// MoveLeft moves the dot left one rune. If word is true, it moves the dot to
|
|
|
|
// the next word boundary on the left.
|
2023-02-12 23:52:31 -07:00
|
|
|
func MoveLeft (text []rune, dot Dot, word bool) (moved Dot) {
|
2023-02-13 13:26:21 -07:00
|
|
|
dot = dot.Canon().Constrain(len(text))
|
|
|
|
distance := 0
|
|
|
|
if dot.Empty() {
|
|
|
|
distance = 1
|
|
|
|
}
|
2023-01-17 23:19:10 -07:00
|
|
|
if word {
|
2023-02-13 10:55:51 -07:00
|
|
|
distance = WordToLeft(text, dot.Start)
|
2023-01-17 23:19:10 -07:00
|
|
|
}
|
2023-02-13 10:55:51 -07:00
|
|
|
moved = EmptyDot(dot.Sub(distance).Start)
|
2023-01-17 23:19:10 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:25 -06:00
|
|
|
// MoveRight moves the dot right one rune. If word is true, it moves the dot to
|
|
|
|
// the next word boundary on the right.
|
2023-02-12 23:52:31 -07:00
|
|
|
func MoveRight (text []rune, dot Dot, word bool) (moved Dot) {
|
2023-02-13 13:26:21 -07:00
|
|
|
dot = dot.Canon().Constrain(len(text))
|
|
|
|
distance := 0
|
|
|
|
if dot.Empty() {
|
|
|
|
distance = 1
|
|
|
|
}
|
2023-01-17 23:19:10 -07:00
|
|
|
if word {
|
2023-02-13 10:55:51 -07:00
|
|
|
distance = WordToRight(text, dot.End)
|
2023-01-17 23:19:10 -07:00
|
|
|
}
|
2023-02-13 10:55:51 -07:00
|
|
|
moved = EmptyDot(dot.Add(distance).End)
|
2023-01-17 23:19:10 -07:00
|
|
|
return
|
|
|
|
}
|
2023-02-13 10:55:51 -07:00
|
|
|
|
2023-03-31 17:40:25 -06:00
|
|
|
// SelectLeft moves the end of the dot left one rune. If word is true, it moves
|
|
|
|
// the end of the dot to the next word boundary on the left.
|
2023-02-13 10:55:51 -07:00
|
|
|
func SelectLeft (text []rune, dot Dot, word bool) (moved Dot) {
|
|
|
|
dot = dot.Constrain(len(text))
|
|
|
|
distance := 1
|
|
|
|
if word {
|
2023-02-16 12:09:23 -07:00
|
|
|
distance = WordToLeft(text, dot.End)
|
2023-02-13 10:55:51 -07:00
|
|
|
}
|
2023-02-16 12:09:23 -07:00
|
|
|
dot.End -= distance
|
2023-02-13 10:55:51 -07:00
|
|
|
return dot
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:25 -06:00
|
|
|
// SelectRight moves the end of the dot right one rune. If word is true, it
|
|
|
|
// moves the end of the dot to the next word boundary on the right.
|
2023-02-13 10:55:51 -07:00
|
|
|
func SelectRight (text []rune, dot Dot, word bool) (moved Dot) {
|
|
|
|
dot = dot.Constrain(len(text))
|
|
|
|
distance := 1
|
|
|
|
if word {
|
|
|
|
distance = WordToRight(text, dot.End)
|
|
|
|
}
|
2023-02-16 12:09:23 -07:00
|
|
|
dot.End += distance
|
2023-02-13 10:55:51 -07:00
|
|
|
return dot
|
|
|
|
}
|