This commit is contained in:
Valere 2019-11-20 17:32:36 +01:00 committed by Benoit Marty
parent 38b93c527b
commit 97766404d6
1 changed files with 5 additions and 10 deletions

View File

@ -24,8 +24,6 @@ import java.util.*
* *
* For now only support UserMentionSpans (TODO rooms, room aliases, etc...) * For now only support UserMentionSpans (TODO rooms, room aliases, etc...)
*/ */
object TextPillsUtils { object TextPillsUtils {
private data class MentionLinkSpec(val span: UserMentionSpan, val start: Int, val end: Int) private data class MentionLinkSpec(val span: UserMentionSpan, val start: Int, val end: Int)
@ -34,7 +32,6 @@ object TextPillsUtils {
private const val MENTION_SPAN_TO_MD_TEMPLATE = "[%2\$s](https://matrix.to/#/%1\$s)" private const val MENTION_SPAN_TO_MD_TEMPLATE = "[%2\$s](https://matrix.to/#/%1\$s)"
/** /**
* Detects if transformable spans are present in the text. * Detects if transformable spans are present in the text.
* @return the transformed String or null if no Span found * @return the transformed String or null if no Span found
@ -60,7 +57,7 @@ object TextPillsUtils {
?.takeIf { it.isNotEmpty() } ?.takeIf { it.isNotEmpty() }
?: return null ?: return null
//we need to prune overlaps! // we need to prune overlaps!
pruneOverlaps(pills) pruneOverlaps(pills)
return buildString { return buildString {
@ -83,22 +80,20 @@ object TextPillsUtils {
val b = links[i + 1] val b = links[i + 1]
var remove = -1 var remove = -1
//test if there is an overlap // test if there is an overlap
if (b.start in a.start until a.end) { if (b.start in a.start until a.end) {
when { when {
b.end <= a.end -> b.end <= a.end ->
//b is inside a -> b should be removed // b is inside a -> b should be removed
remove = i + 1 remove = i + 1
a.end - a.start > b.end - b.start -> a.end - a.start > b.end - b.start ->
//overlap and a is bigger -> b should be removed // overlap and a is bigger -> b should be removed
remove = i + 1 remove = i + 1
a.end - a.start < b.end - b.start -> a.end - a.start < b.end - b.start ->
//overlap and a is smaller -> a should be removed // overlap and a is smaller -> a should be removed
remove = i remove = i
} }
if (remove != -1) { if (remove != -1) {
links.removeAt(remove) links.removeAt(remove)
len-- len--