Textmanip now operates on a dot instead of a cursor
This commit is contained in:
@@ -2,7 +2,40 @@ package textmanip
|
||||
|
||||
import "unicode"
|
||||
|
||||
func WordToLeft (text []rune, cursor int) (length int) {
|
||||
type Dot struct { Start, End int }
|
||||
|
||||
func EmptyDot (position int) Dot {
|
||||
return Dot { position, position }
|
||||
}
|
||||
|
||||
func (dot Dot) Canon () Dot {
|
||||
if dot.Start > dot.End {
|
||||
return Dot { dot.End, dot.Start }
|
||||
} else {
|
||||
return dot
|
||||
}
|
||||
}
|
||||
|
||||
func (dot Dot) Empty () bool {
|
||||
return dot.Start == dot.End
|
||||
}
|
||||
|
||||
func (dot Dot) Add (delta int) Dot {
|
||||
return Dot {
|
||||
dot.Start + delta,
|
||||
dot.End + delta,
|
||||
}
|
||||
}
|
||||
|
||||
func (dot Dot) Sub (delta int) Dot {
|
||||
return Dot {
|
||||
dot.Start - delta,
|
||||
dot.End - delta,
|
||||
}
|
||||
}
|
||||
|
||||
func WordToLeft (text []rune, dot Dot) (length int) {
|
||||
cursor := dot.End
|
||||
if cursor < 1 { return }
|
||||
if cursor > len(text) { cursor = len(text) }
|
||||
|
||||
@@ -18,7 +51,8 @@ func WordToLeft (text []rune, cursor int) (length int) {
|
||||
return
|
||||
}
|
||||
|
||||
func WordToRight (text []rune, cursor int) (length int) {
|
||||
func WordToRight (text []rune, dot Dot) (length int) {
|
||||
cursor := dot.End
|
||||
if cursor < 0 { return }
|
||||
if cursor > len(text) { cursor = len(text) }
|
||||
|
||||
@@ -34,66 +68,93 @@ func WordToRight (text []rune, cursor int) (length int) {
|
||||
return
|
||||
}
|
||||
|
||||
func Backspace (text []rune, cursor int, word bool) (result []rune, moved int) {
|
||||
if cursor < 1 { return text, cursor }
|
||||
if cursor > len(text) { cursor = len(text) }
|
||||
func Backspace (text []rune, dot Dot, word bool) (result []rune, moved Dot) {
|
||||
if dot.Empty() {
|
||||
cursor := dot.End
|
||||
if cursor < 1 { return text, dot }
|
||||
if cursor > len(text) { cursor = len(text) }
|
||||
|
||||
moved = 1
|
||||
if word {
|
||||
moved = WordToLeft(text, cursor)
|
||||
}
|
||||
result = append(result, text[:cursor - moved]...)
|
||||
result = append(result, text[cursor:]...)
|
||||
moved = cursor - moved
|
||||
return
|
||||
}
|
||||
|
||||
func Delete (text []rune, cursor int, word bool) (result []rune, moved int) {
|
||||
if cursor < 0 { return text, cursor }
|
||||
if cursor > len(text) { cursor = len(text) }
|
||||
|
||||
moved = 1
|
||||
if word {
|
||||
moved = WordToRight(text, cursor)
|
||||
}
|
||||
result = append(result, text[:cursor]...)
|
||||
result = append(result, text[cursor + moved:]...)
|
||||
moved = cursor
|
||||
return
|
||||
}
|
||||
|
||||
func Type (text []rune, cursor int, character rune) (result []rune, moved int) {
|
||||
if cursor < 0 { cursor = 0 }
|
||||
if cursor > len(text) { cursor = len(text) }
|
||||
result = append(result, text[:cursor]...)
|
||||
result = append(result, character)
|
||||
if cursor < len(text) {
|
||||
distance := 1
|
||||
if word {
|
||||
distance = WordToLeft(text, dot)
|
||||
}
|
||||
result = append(result, text[:cursor - distance]...)
|
||||
result = append(result, text[cursor:]...)
|
||||
moved = EmptyDot(cursor - distance)
|
||||
} else {
|
||||
return Delete(text, dot, word)
|
||||
}
|
||||
moved = cursor + 1
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func MoveLeft (text []rune, cursor int, word bool) (moved int) {
|
||||
if cursor < 1 { return cursor }
|
||||
func Delete (text []rune, dot Dot, word bool) (result []rune, moved Dot) {
|
||||
if dot.Empty() {
|
||||
cursor := dot.End
|
||||
if cursor < 0 { return text, dot }
|
||||
if cursor > len(text) { cursor = len(text) }
|
||||
|
||||
distance := 1
|
||||
if word {
|
||||
distance = WordToRight(text, dot)
|
||||
}
|
||||
result = append(result, text[:cursor]...)
|
||||
result = append(result, text[cursor + distance:]...)
|
||||
moved = dot
|
||||
return
|
||||
} else {
|
||||
result = append(result, text[:dot.Start]...)
|
||||
result = append(result, text[dot.End:]...)
|
||||
moved = EmptyDot(dot.Start)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func Type (text []rune, dot Dot, character rune) (result []rune, moved Dot) {
|
||||
if dot.Empty() {
|
||||
cursor := dot.End
|
||||
if cursor < 0 { cursor = 0 }
|
||||
if cursor > len(text) { cursor = len(text) }
|
||||
result = append(result, text[:cursor]...)
|
||||
result = append(result, character)
|
||||
if cursor < len(text) {
|
||||
result = append(result, text[cursor:]...)
|
||||
}
|
||||
moved = EmptyDot(cursor + 1)
|
||||
return
|
||||
} else {
|
||||
result = append(result, text[:dot.Start]...)
|
||||
result = append(result, character)
|
||||
result = append(result, text[dot.End:]...)
|
||||
moved = EmptyDot(dot.Start)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func MoveLeft (text []rune, dot Dot, word bool) (moved Dot) {
|
||||
cursor := dot.Start
|
||||
|
||||
if cursor < 1 { return EmptyDot(cursor) }
|
||||
if cursor > len(text) { cursor = len(text) }
|
||||
|
||||
moved = 1
|
||||
distance := 1
|
||||
if word {
|
||||
moved = WordToLeft(text, cursor)
|
||||
distance = WordToLeft(text, dot)
|
||||
}
|
||||
moved = cursor - moved
|
||||
moved = EmptyDot(cursor - distance)
|
||||
return
|
||||
}
|
||||
|
||||
func MoveRight (text []rune, cursor int, word bool) (moved int) {
|
||||
if cursor < 0 { return cursor }
|
||||
func MoveRight (text []rune, dot Dot, word bool) (moved Dot) {
|
||||
cursor := dot.End
|
||||
|
||||
if cursor < 0 { return EmptyDot(cursor) }
|
||||
if cursor > len(text) { cursor = len(text) }
|
||||
|
||||
moved = 1
|
||||
distance := 1
|
||||
if word {
|
||||
moved = WordToRight(text, cursor)
|
||||
distance = WordToRight(text, dot)
|
||||
}
|
||||
moved = cursor + moved
|
||||
moved = EmptyDot(cursor + distance)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user