15414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian/*
25414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian * Copyright (C) 2018 The Android Open Source Project
35414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian *
45414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian * Licensed under the Apache License, Version 2.0 (the "License");
55414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian * you may not use this file except in compliance with the License.
65414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian * You may obtain a copy of the License at
75414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian *
85414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian *       http://www.apache.org/licenses/LICENSE-2.0
95414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian *
105414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian * Unless required by applicable law or agreed to in writing, software
115414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian * distributed under the License is distributed on an "AS IS" BASIS,
125414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian * See the License for the specific language governing permissions and
145414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian * limitations under the License.
155414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian */
165414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian
175414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian@file:Suppress("NOTHING_TO_INLINE")
185414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian
199c80550cbbe357a89e2abeeb9c7769fcaefc3a65Jake Whartonpackage androidx.core.text
205414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian
215414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrianimport android.text.Spannable
225414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrianimport android.text.SpannableString
235414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrianimport android.text.Spanned.SPAN_INCLUSIVE_EXCLUSIVE
245414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian
255414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian/**
265414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian * Returns a new [Spannable] from [CharSequence],
275414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian * or the source itself if it is already an instance of [SpannableString].
285414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian */
295414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrianinline fun CharSequence.toSpannable(): Spannable = SpannableString.valueOf(this)
305414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian
315414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian/** Adds [span] to the entire text. */
325414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrianinline operator fun Spannable.plusAssign(span: Any) =
335414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian    setSpan(span, 0, length, SPAN_INCLUSIVE_EXCLUSIVE)
345414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian
355414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian/** Removes [span] from this text. */
365414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrianinline operator fun Spannable.minusAssign(span: Any) = removeSpan(span)
375414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian
385414238bd40e7deebeabf0e41a59eaaa05f4cdfeHendra Anggrian/** Clear all spans from this text. */
39d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Whartoninline fun Spannable.clearSpans() = getSpans<Any>().forEach { removeSpan(it) }
40d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton
41d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton/**
42d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton * Add [span] to the range [start]&hellip;[end] of the text.
43d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton *
44d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton * ```
45d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton * val s = "Hello, World!".toSpannable()
46d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton * s[0, 5] = UnderlineSpan()
47d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton * ```
48d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton *
49d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton * Note: The [end] value is exclusive.
50d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton *
51d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton * @see Spannable.setSpan
52d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton */
53d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Whartoninline operator fun Spannable.set(start: Int, end: Int, span: Any) {
54d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton    setSpan(span, start, end, SPAN_INCLUSIVE_EXCLUSIVE)
55d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton}
56d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton
57d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton/**
58d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton * Add [span] to the [range] of the text.
59d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton *
60d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton * ```
61d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton * val s = "Hello, World!".toSpannable()
62d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton * s[0..5] = UnderlineSpan()
63d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton * ```
64d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton *
65d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton * Note: The range end value is exclusive.
66d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton *
67d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton * @see Spannable.setSpan
68d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton */
69d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Whartoninline operator fun Spannable.set(range: IntRange, span: Any) {
70d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton    // This looks weird, but endInclusive is just the exact upper value.
71d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton    setSpan(span, range.start, range.endInclusive, SPAN_INCLUSIVE_EXCLUSIVE)
72d8fad98b94940d1a1657bb44366f3305bbcc7203Jake Wharton}
73