MCPcopy Create free account
hub / github.com/gioui/gio / replace

Method replace

widget/editor.go:932–991  ·  view source on GitHub ↗

replace the text between start and end with s. Indices are in runes. It returns the number of runes inserted. addHistory controls whether this modification is recorded in the undo history. replace can modify text in positions unrelated to the cursor position.

(start, end int, s string, addHistory bool)

Source from the content-addressed store, hash-verified

930// history. replace can modify text in positions unrelated to the cursor
931// position.
932func (e *Editor) replace(start, end int, s string, addHistory bool) int {
933 length := e.text.Len()
934 if start > end {
935 start, end = end, start
936 }
937 start = min(start, length)
938 end = min(end, length)
939 replaceSize := end - start
940 el := e.Len()
941 var sc int
942 idx := 0
943 for idx < len(s) {
944 if e.MaxLen > 0 && el-replaceSize+sc >= e.MaxLen {
945 s = s[:idx]
946 break
947 }
948 _, n := utf8.DecodeRuneInString(s[idx:])
949 if e.Filter != "" && !strings.Contains(e.Filter, s[idx:idx+n]) {
950 s = s[:idx] + s[idx+n:]
951 continue
952 }
953 idx += n
954 sc++
955 }
956
957 if addHistory {
958 deleted := make([]rune, 0, replaceSize)
959 readPos := e.text.ByteOffset(start)
960 for range replaceSize {
961 ru, s, _ := e.text.ReadRuneAt(int64(readPos))
962 readPos += int64(s)
963 deleted = append(deleted, ru)
964 }
965 if e.nextHistoryIdx < len(e.history) {
966 e.history = e.history[:e.nextHistoryIdx]
967 }
968 e.history = append(e.history, modification{
969 StartRune: start,
970 ApplyContent: s,
971 ReverseContent: string(deleted),
972 })
973 e.nextHistoryIdx++
974 }
975
976 sc = e.text.Replace(start, end, s)
977 newEnd := start + sc
978 adjust := func(pos int) int {
979 switch {
980 case newEnd < pos && pos <= end:
981 pos = newEnd
982 case end < pos:
983 diff := newEnd - end
984 pos = pos + diff
985 }
986 return pos
987 }
988 e.ime.start = adjust(e.ime.start)
989 e.ime.end = adjust(e.ime.end)

Callers 6

processKeyMethod · 0.95
SetTextMethod · 0.95
DeleteMethod · 0.95
InsertMethod · 0.95
undoMethod · 0.95
redoMethod · 0.95

Calls 4

LenMethod · 0.95
ByteOffsetMethod · 0.80
ReadRuneAtMethod · 0.80
ReplaceMethod · 0.45

Tested by

no test coverage detected