Skip to content

Commit

Permalink
reformat files
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin-LL committed Mar 8, 2024
1 parent 8913dd1 commit acb8d38
Show file tree
Hide file tree
Showing 22 changed files with 92 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.util.regex.Pattern
private fun matchPattern(
text: String,
pattern: Pattern,
matchFilter: MatchFilter<Any?> = MatchFilterDefaults.NoOp
matchFilter: MatchFilter<Any?> = MatchFilterDefaults.NoOp,
): List<SimpleTextMatchResult<Any?>> {
val matcher = pattern.matcher(text)
val matches = mutableListOf<SimpleTextMatchResult<Any?>>()
Expand Down Expand Up @@ -59,7 +59,7 @@ internal actual fun getMatcherDefaults() = object : TextMatcherDefaultsInterface
val regionCode = simCountryIso ?: Locale.getDefault().country
val matches = phoneUtil.findNumbers(
text,
regionCode, PhoneNumberUtil.Leniency.POSSIBLE, Long.MAX_VALUE
regionCode, PhoneNumberUtil.Leniency.POSSIBLE, Long.MAX_VALUE,
)

matches.mapNotNull {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class AndroidTextRuleTest {
val text = "test someone@example.com test"
val rules = listOf(
TextRuleDefaults.webUrl(contextData),
TextRuleDefaults.emailAddress(contextData)
TextRuleDefaults.emailAddress(contextData),
)
val matches = rules.getAllMatches(text)

Expand All @@ -86,7 +86,7 @@ class AndroidTextRuleTest {
val rules = listOf(
TextRuleDefaults.webUrl(contextData),
TextRuleDefaults.emailAddress(contextData),
TextRuleDefaults.phoneNumber(contextData)
TextRuleDefaults.phoneNumber(contextData),
)
val matches = rules.getAllMatches(text)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ fun AutoLinkText(
val match = matches.find { it.contains(offset) }
match?.rule?.onClick?.invoke(match)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ internal object BackUpRegex {
internal val PhoneNumber = Regex(
"[\\+]?[(]?[0-9]{3}[)]?[-\\s\\.]?[0-9]{3}[-\\s\\.]?[0-9]{4,6}"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package sh.calvin.autolinktext

import androidx.compose.ui.text.SpanStyle

typealias MatchStyleProvider<T> = (match: TextMatchResult<T>) -> SpanStyle?
typealias MatchStyleProvider<T> = (match: TextMatchResult<T>) -> SpanStyle?
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package sh.calvin.autolinktext


@RequiresOptIn(
message = "In Android, [ContextData] is extended by [AndroidContextData] to provide a [Context] object.\n" +
"In other platforms, [ContextData] is a simple interface with no methods.\n" +
Expand All @@ -9,4 +8,4 @@ package sh.calvin.autolinktext
)
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class NotForAndroid
annotation class NotForAndroid
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package sh.calvin.autolinktext
open class SimpleTextMatchResult<out T>(
override val start: Int,
override val endExclusive: Int,
val data: T
val data: T,
) : OpenEndRange<Int> {
fun slice(text: String) = text.substring(start, endExclusive)
}

fun SimpleTextMatchResult(start: Int, end: Int) = SimpleTextMatchResult(start, end, null)

fun String.slice(match: SimpleTextMatchResult<*>) = match.slice(this)
fun String.slice(match: SimpleTextMatchResult<*>) = match.slice(this)
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class TextMatchResult<T>(
* The full text that was matched against
*/
fullText: String,
match: SimpleTextMatchResult<T>
match: SimpleTextMatchResult<T>,
) : this(rule, fullText.slice(match), match.start, match.endExclusive, match.data)
}

fun TextMatchResult(rule: TextRule<Nothing?>, fullText: String, start: Int, end: Int) =
TextMatchResult(rule, fullText, start, end, null)
TextMatchResult(rule, fullText, start, end, null)
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@ sealed class TextMatcher<out T> {
return function(text)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ class SimpleTextMatchResultTest {

assertEquals("abc", result)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TextMatchResultTest {
rule,
text,
3,
6
6,
)

assertEquals(rule, result.rule)
Expand All @@ -43,7 +43,7 @@ class TextMatchResultTest {
val result = TextMatchResult(
rule,
text,
textMatchResult
textMatchResult,
)

assertEquals(rule, result.rule)
Expand All @@ -62,12 +62,12 @@ class TextMatchResultTest {
val result = TextMatchResult(
rule,
text,
textMatchResult
textMatchResult,
)

assertEquals(rule, result.rule)
assertEquals(3, result.start)
assertEquals(6, result.endExclusive)
assertEquals(matchResult, result.data)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ class TextMatcherTest {
assertEquals(6, matches[1].start)
assertEquals(9, matches[1].endExclusive)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sh.calvin.autolinktext
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
import kotlin.test.assertNull

class TextRuleTest {
@Test
Expand All @@ -25,46 +26,72 @@ class TextRuleTest {
assertEquals(rules[0], matches[0].rule)
}

@Test
fun testSecondaryConstructor() {
val textMatcher = TextMatcher.RegexMatcher(Regex("123a"))
val clickHandler: MatchClickHandler<Any?> = { }
val annotationProvider: MatchAnnotationProvider<Any?> = { null }
val rule = TextRule(
textMatcher = textMatcher,
style = null,
onClick = clickHandler,
annotationProvider = annotationProvider,
)

assertEquals(textMatcher, rule.textMatcher)
assertNull(rule.styleProvider(TextMatchResult(rule, "", 0, 0, null)))
assertEquals(clickHandler, rule.onClick)
assertEquals(annotationProvider, rule.annotationProvider)
}

@Test
fun testCopy1() {
val textMatcher = TextMatcher.RegexMatcher(Regex("123a"))
val styleProvider: MatchStyleProvider<Any?> = { null }
val clickHandler: MatchClickHandler<Any?> = { }
val annotationProvider: MatchAnnotationProvider<Any?> = { null }
val rule = TextRule(
textMatcher = textMatcher,
matchStyleProvider = styleProvider,
onClick = clickHandler
styleProvider = styleProvider,
onClick = clickHandler,
annotationProvider = annotationProvider,
)
val copy = rule.copy()

assertEquals(rule.textMatcher, copy.textMatcher)
assertEquals(rule.matchStyleProvider, copy.matchStyleProvider)
assertEquals(rule.styleProvider, copy.styleProvider)
assertEquals(rule.onClick, copy.onClick)
assertEquals(rule.annotationProvider, copy.annotationProvider)
}

@Test
fun testCopy2() {
val textMatcher1 = TextMatcher.RegexMatcher(Regex("123a"))
val styleProvider1: MatchStyleProvider<Any?> = { null }
val clickHandler1: MatchClickHandler<Any?> = { }
val annotationProvider1: MatchAnnotationProvider<Any?> = { null }
val rule1 = TextRule(
textMatcher = textMatcher1,
matchStyleProvider = styleProvider1,
onClick = clickHandler1
styleProvider = styleProvider1,
onClick = clickHandler1,
annotationProvider = annotationProvider1,
)

val textMatcher2 = TextMatcher.RegexMatcher(Regex("abc"))
val styleProvider2: MatchStyleProvider<Any?> = { null }
val clickHandler2: MatchClickHandler<Any?> = { }
val annotationProvider2: MatchAnnotationProvider<Any?> = { null }
val rule2 = rule1.copy(
textMatcher = textMatcher2,
matchStyleProvider = styleProvider2,
matchClickHandler = clickHandler2
styleProvider = styleProvider2,
onClick = clickHandler2,
annotationProvider = annotationProvider2,
)

assertEquals(textMatcher2, rule2.textMatcher)
assertEquals(styleProvider2, rule2.matchStyleProvider)
assertEquals(styleProvider2, rule2.styleProvider)
assertEquals(clickHandler2, rule2.onClick)
assertEquals(annotationProvider2, rule2.annotationProvider)
}

@Test
Expand All @@ -74,44 +101,24 @@ class TextRuleTest {
val clickHandler1: MatchClickHandler<Any?> = { }
val rule1 = TextRule(
textMatcher = textMatcher1,
matchStyleProvider = styleProvider1,
onClick = clickHandler1
)

val textMatcher2 = TextMatcher.RegexMatcher(Regex("abc"))
val clickHandler2: MatchClickHandler<Any?> = { }
val rule2 = rule1.copy(
textMatcher = textMatcher2,
matchStyle = null,
matchClickHandler = clickHandler2
)

assertEquals(textMatcher2, rule2.textMatcher)
assertNotEquals(styleProvider1, rule2.matchStyleProvider)
assertEquals(clickHandler2, rule2.onClick)
}

@Test
fun testCopy4() {
val textMatcher1 = TextMatcher.RegexMatcher(Regex("123a"))
val styleProvider1: MatchStyleProvider<Any?> = { null }
val clickHandler1: MatchClickHandler<Any?> = { }
val rule1 = TextRule(
textMatcher = textMatcher1,
matchStyleProvider = styleProvider1,
onClick = clickHandler1
styleProvider = styleProvider1,
onClick = clickHandler1,
)

val textMatcher2 = TextMatcher.RegexMatcher(Regex("abc"))
val clickHandler2: MatchClickHandler<Any?> = { }
val annotationProvider2: MatchAnnotationProvider<Any?> = { null }
val rule2 = rule1.copy(
textMatcher = textMatcher2,
matchClickHandler = clickHandler2
style = null,
onClick = clickHandler2,
annotationProvider = annotationProvider2,
)

assertEquals(textMatcher2, rule2.textMatcher)
assertEquals(styleProvider1, rule2.matchStyleProvider)
assertNotEquals(styleProvider1, rule2.styleProvider)
assertEquals(clickHandler2, rule2.onClick)
assertEquals(annotationProvider2, rule2.annotationProvider)
}

@OptIn(NotForAndroid::class)
Expand Down Expand Up @@ -158,7 +165,7 @@ class TextRuleTest {
val text = "test someone@example.com test"
val rules = listOf(
TextRuleDefaults.webUrl(NullContextData),
TextRuleDefaults.emailAddress(NullContextData)
TextRuleDefaults.emailAddress(NullContextData),
)
val matches = rules.getAllMatches(text)

Expand All @@ -180,7 +187,7 @@ class TextRuleTest {
val rules = listOf(
TextRuleDefaults.webUrl(NullContextData),
TextRuleDefaults.emailAddress(NullContextData),
TextRuleDefaults.phoneNumber(NullContextData)
TextRuleDefaults.phoneNumber(NullContextData),
)
val matches = rules.getAllMatches(text)

Expand All @@ -198,4 +205,22 @@ class TextRuleTest {
assertEquals("604-555-7890", text.slice(matches[5]))
assertEquals(rules[2], matches[5].rule)
}
}

@OptIn(NotForAndroid::class)
@IgnoreAndroid
@Test
fun textWithAnnotations() {
val text = "read our privacy policy"
val rules = listOf(
TextRule(
textMatcher = TextMatcher.StringMatcher("privacy policy"),
annotationProvider = { "https://example.com/privacy" },
),
)
val matches = rules.getAllMatches(text)

assertEquals(1, matches.size)
assertEquals("someone@example.com", text.slice(matches[0]))
assertEquals(rules[1], matches[0].rule)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ object IosMatchClickHandlerDefaults : MatchClickHandlerDefaultsInterface {
}

internal actual fun getMatchClickHandlerDefaults(): MatchClickHandlerDefaultsInterface =
IosMatchClickHandlerDefaults
IosMatchClickHandlerDefaults
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ object IosTextMatcherDefaults : TextMatcherDefaultsInterface {
}
}

internal actual fun getMatcherDefaults(): TextMatcherDefaultsInterface = IosTextMatcherDefaults
internal actual fun getMatcherDefaults(): TextMatcherDefaultsInterface = IosTextMatcherDefaults
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package sh.calvin.autolinktext

object IosTextRuleDefaults : TextRuleDefaultsInterface

internal actual fun getTextRuleDefaults(): TextRuleDefaultsInterface = IosTextRuleDefaults
internal actual fun getTextRuleDefaults(): TextRuleDefaultsInterface = IosTextRuleDefaults
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ actual annotation class IgnoreJvm
actual annotation class IgnoreAndroid

@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
actual annotation class IgnoreWasmJs
actual annotation class IgnoreWasmJs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ internal actual fun getMatchClickHandlerDefaults() = object : MatchClickHandlerD
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal actual fun getMatcherDefaults() = object : TextMatcherDefaultsInterface
val regionCode = Locale.getDefault().country
val matches = phoneUtil.findNumbers(
text,
regionCode, PhoneNumberUtil.Leniency.POSSIBLE, Long.MAX_VALUE
regionCode, PhoneNumberUtil.Leniency.POSSIBLE, Long.MAX_VALUE,
)

matches.mapNotNull {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package sh.calvin.autolinktext

internal actual fun getTextRuleDefaults() = object : TextRuleDefaultsInterface {}
internal actual fun getTextRuleDefaults() = object : TextRuleDefaultsInterface {}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ actual typealias IgnoreJvm = org.junit.Ignore
actual annotation class IgnoreAndroid

@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
actual annotation class IgnoreWasmJs
actual annotation class IgnoreWasmJs
Loading

0 comments on commit acb8d38

Please sign in to comment.