// Copyright (c) 2026 Storm Dragon // Authors: Storm Dragon, Angus Kola // SPDX-License-Identifier: GPL-3.0-or-later package atspi import ( "context" "strings" "unicode" "unicode/utf8" ) // Match Python web elementLinesAreSingleWords/Chars: CSS text can expose one // token per native LINE despite belonging to one logical line. The caller // excludes editable and preformatted text and caches the bounded decision. func documentLinesAreTokens(ctx context.Context, source reviewTextSource, content string) (bool, error) { count := utf8.RuneCountInString(content) if count < 2 || count > maxReviewLineCharacters || float64(strings.Count(content, "\ufffc"))/float64(count) > .3 { return false, nil } words := strings.FieldsFunc(content, func(value rune) bool { return unicode.IsSpace(value) || value == '\ufffc' }) singleWords, singleChars := len(words) > 1, true for offset, attempts := 0, 0; offset < count; attempts++ { if err := ctx.Err(); err != nil { return false, err } if attempts >= maxReviewRanges { return false, nil } line, err := source.line(ctx, offset) if err != nil { return false, err } if line.Start < 0 || line.Start > offset || line.End <= offset || line.End > count { return false, nil } value := strings.TrimSpace(line.Text) singleWords = singleWords && len(strings.Fields(line.Text)) == 1 singleChars = singleChars && (value == "" || value == "\ufffc" || value == "�" || utf8.RuneCountInString(value) == 1) if !singleWords && !singleChars { return false, nil } offset = line.End } return singleWords || singleChars, ctx.Err() }