fix(selection): fix panic that can happen on selection

Andrey Nering created

Change summary

internal/tui/exp/list/list.go | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)

Detailed changes

internal/tui/exp/list/list.go 🔗

@@ -1306,6 +1306,10 @@ func (l *list[T]) findWordBoundaries(col, line int) (startCol, endCol int) {
 		}
 	}
 
+	if line < 0 || line >= len(lines) {
+		return 0, 0
+	}
+
 	currentLine := lines[line]
 	gr := uniseg.NewGraphemes(currentLine)
 	startCol = -1
@@ -1340,10 +1344,6 @@ func (l *list[T]) findParagraphBoundaries(line int) (startLine, endLine int, fou
 		line = (len(lines) - 1) - l.height + line + 1
 	}
 
-	if strings.TrimSpace(lines[line]) == "" {
-		return 0, 0, false
-	}
-
 	if l.offset > 0 {
 		if l.direction == DirectionBackward {
 			line -= l.offset
@@ -1357,6 +1357,10 @@ func (l *list[T]) findParagraphBoundaries(line int) (startLine, endLine int, fou
 		return 0, 0, false
 	}
 
+	if strings.TrimSpace(lines[line]) == "" {
+		return 0, 0, false
+	}
+
 	// Find start of paragraph (search backwards for empty line or start of text)
 	startLine = line
 	for startLine > 0 && strings.TrimSpace(lines[startLine-1]) != "" {