Skip to content

Commit

Permalink
Be a bit smarter about parenthesis pairs, add another preview as a test.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartinesp committed Jan 29, 2025
1 parent 34d9d64 commit da1a9d1
Showing 1 changed file with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,17 @@ private fun adjustEndIndex(spannable: SpannableString, start: Int, end: Int): In
var end = end

// Trailing punctuation found, adjust the end index
while (spannable[end - 1] in sequenceOf('.', ',', ';', ':', '!', '?') && end > start) {
while (spannable[end - 1] in sequenceOf('.', ',', ';', ':', '!', '?', '') && end > start) {
end--
}

// If the last character is a closing parenthesis, check if it's part of a pair
if (spannable[end - 1] == ')' && end > start) {
val lastChar = spannable[end - 1]
val linkifiedTextLastPath = spannable.substring(start, end).substringAfterLast('/')
val closingParenthesisCount = linkifiedTextLastPath.count { it == ')' }
val openingParenthesisCount = linkifiedTextLastPath.count { it == '(' }
// If it's not part of a pair, remove it from the link span by adjusting the end index
if (!linkifiedTextLastPath.contains('(')) {
end--
}
end -= closingParenthesisCount - openingParenthesisCount
}
return end
}
Expand All @@ -208,7 +207,19 @@ internal fun ClickableLinkTextPreview() = ElementThemedPreview {
@Composable
internal fun ClickableLinkTextWithParenthesisPreview() = ElementThemedPreview {
ClickableLinkText(
annotatedString = AnnotatedString("Does this work (url: github.com/element-hq/element-x-android/README?)?!.", ParagraphStyle()),
annotatedString = AnnotatedString("Does this work (url: github.com/element-hq/element-x-android/README?)?.", ParagraphStyle()),
linkAnnotationTag = "",
onClick = {},
onLongClick = {},
interactionSource = remember { MutableInteractionSource() },
)
}

@Preview(group = PreviewGroup.Text)
@Composable
internal fun ClickableLinkTextWithParenthesisInsideUrlPreview() = ElementThemedPreview {
ClickableLinkText(
annotatedString = AnnotatedString("Does this work ((url: github.com/element-hq/element-x-android/READ(ME)))!", ParagraphStyle()),
linkAnnotationTag = "",
onClick = {},
onLongClick = {},
Expand Down

0 comments on commit da1a9d1

Please sign in to comment.