Handling trailing newline characters directly in step parser. Fixes #882

This commit is contained in:
Oliver 2023-09-07 07:56:28 +02:00
parent 2845171a3b
commit e3c3ad5029
2 changed files with 23 additions and 8 deletions

View File

@ -104,8 +104,9 @@ func (s *stepState) Style() tcell.Style {
// to start with a different style or region, you can set the state accordingly // to start with a different style or region, you can set the state accordingly
// but you must then set [state.unisegState] to -1. // but you must then set [state.unisegState] to -1.
// //
// You may call uniseg.HasTrailingLineBreakInString on the last non-empty // There is no need to call uniseg.HasTrailingLineBreakInString on the last
// cluster to determine if the string ends with a hard line break. // non-empty cluster as this function will do this for you and adjust the
// returned boundaries accordingly.
func step(str string, state *stepState, opts stepOptions) (cluster, rest string, newState *stepState) { func step(str string, state *stepState, opts stepOptions) (cluster, rest string, newState *stepState) {
// Set up initial state. // Set up initial state.
if state == nil { if state == nil {
@ -126,6 +127,11 @@ func step(str string, state *stepState, opts stepOptions) (cluster, rest string,
preState := state.unisegState preState := state.unisegState
cluster, rest, state.boundaries, state.unisegState = uniseg.StepString(str, preState) cluster, rest, state.boundaries, state.unisegState = uniseg.StepString(str, preState)
state.grossLength = len(cluster) state.grossLength = len(cluster)
if rest == "" {
if !uniseg.HasTrailingLineBreakInString(cluster) {
state.boundaries &^= uniseg.MaskLine
}
}
// Parse tags. // Parse tags.
if opts != 0 { if opts != 0 {
@ -173,6 +179,11 @@ func step(str string, state *stepState, opts stepOptions) (cluster, rest string,
state.region = region state.region = region
cluster, rest, state.boundaries, state.unisegState = uniseg.StepString(str[length:], preState) cluster, rest, state.boundaries, state.unisegState = uniseg.StepString(str[length:], preState)
state.grossLength = len(cluster) + length state.grossLength = len(cluster) + length
if rest == "" {
if !uniseg.HasTrailingLineBreakInString(cluster) {
state.boundaries &^= uniseg.MaskLine
}
}
} }
// Is this an escaped tag? // Is this an escaped tag?
if escapedTagPattern.MatchString(str[length:]) { if escapedTagPattern.MatchString(str[length:]) {
@ -187,7 +198,13 @@ func step(str string, state *stepState, opts stepOptions) (cluster, rest string,
_, l := utf8.DecodeRuneInString(rest[length:]) _, l := utf8.DecodeRuneInString(rest[length:])
cluster += rest[length : length+l] cluster += rest[length : length+l]
} }
cluster, _, state.boundaries, state.unisegState = uniseg.StepString(cluster, preState) var taglessRest string
cluster, taglessRest, state.boundaries, state.unisegState = uniseg.StepString(cluster, preState)
if taglessRest == "" {
if !uniseg.HasTrailingLineBreakInString(cluster) {
state.boundaries &^= uniseg.MaskLine
}
}
} }
} }
} }
@ -526,8 +543,7 @@ func WordWrap(text string, width int) (lines []string) {
str := text str := text
for len(str) > 0 { for len(str) > 0 {
// Parse the next character. // Parse the next character.
var c string _, str, state = step(str, state, stepOptionsStyle)
c, str, state = step(str, state, stepOptionsStyle)
cWidth := state.Width() cWidth := state.Width()
// Would it exceed the line width? // Would it exceed the line width?
@ -557,7 +573,7 @@ func WordWrap(text string, width int) (lines []string) {
// Remember this split point. // Remember this split point.
lastOption = lineLength lastOption = lineLength
lastOptionWidth = lineWidth lastOptionWidth = lineWidth
} else if str != "" || c != "" && uniseg.HasTrailingLineBreakInString(c) { } else {
// We must split here. // We must split here.
lines = append(lines, strings.TrimRight(text[:lineLength], "\n\r")) lines = append(lines, strings.TrimRight(text[:lineLength], "\n\r"))
text = text[lineLength:] text = text[lineLength:]

View File

@ -6,7 +6,6 @@ import (
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
colorful "github.com/lucasb-eyer/go-colorful" colorful "github.com/lucasb-eyer/go-colorful"
"github.com/rivo/uniseg"
) )
// TabSize is the number of spaces with which a tab character will be replaced. // TabSize is the number of spaces with which a tab character will be replaced.
@ -947,7 +946,7 @@ func (t *TextView) parseAhead(width int, stop func(lineNumber int, line *textVie
st := *state st := *state
lastOptionState = &st lastOptionState = &st
} }
} else if str != "" || c != "" && uniseg.HasTrailingLineBreakInString(c) { } else {
// We must split here. // We must split here.
if stop(len(t.lineIndex)-1, lastLine) { if stop(len(t.lineIndex)-1, lastLine) {
return return