Skip to content

Commit 7f47e38

Browse files
committed
Fixed broken code highlighter for space trimming, code cleanup
1 parent e8db973 commit 7f47e38

5 files changed

Lines changed: 38 additions & 31 deletions

File tree

codeview/src/main/java/io/github/kbiakov/codeview/CodeView.kt

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,29 @@ import io.github.kbiakov.codeview.adapters.Options
2020
*/
2121
class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs) {
2222

23-
private val vShadowRight: View
24-
private val vShadowBottomLine: View
25-
private val vShadowBottomContent: View
26-
2723
private val vCodeList: RecyclerView
24+
private val vShadows: List<View>
2825

2926
/**
3027
* Primary constructor.
3128
*/
3229
init {
3330
inflate(context, R.layout.layout_code_view, this)
31+
checkInitialAnimation(attrs)
32+
33+
// TODO: add shadow color customization
34+
vShadows = listOf(
35+
R.id.v_shadow_right,
36+
R.id.v_shadow_bottom_line,
37+
R.id.v_shadow_bottom_content
38+
).map(this::findViewById)
3439

40+
vCodeList = findViewById(R.id.rv_code_content) as RecyclerView
41+
vCodeList.layoutManager = LinearLayoutManager(context)
42+
vCodeList.isNestedScrollingEnabled = true
43+
}
44+
45+
private fun checkInitialAnimation(attrs: AttributeSet) {
3546
if (visibility == VISIBLE && isAnimateOnStart(context, attrs)) {
3647
alpha = Const.Alpha.Invisible
3748

@@ -41,15 +52,6 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
4152
} else {
4253
alpha = Const.Alpha.Initial
4354
}
44-
45-
// TODO: add shadow color customization
46-
vShadowRight = findViewById(R.id.v_shadow_right)
47-
vShadowBottomLine = findViewById(R.id.v_shadow_bottom_line)
48-
vShadowBottomContent = findViewById(R.id.v_shadow_bottom_content)
49-
50-
vCodeList = findViewById(R.id.rv_code_content) as RecyclerView
51-
vCodeList.layoutManager = LinearLayoutManager(context)
52-
vCodeList.isNestedScrollingEnabled = true
5355
}
5456

5557
/**
@@ -58,13 +60,11 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
5860
*/
5961
private fun highlight() {
6062
getAdapter()?.highlight {
61-
6263
animate()
6364
.setDuration(Const.DefaultDelay * 2)
6465
.alpha(.1f)
65-
6666
delayed {
67-
animate().alpha(1f)
67+
animate().alpha(Const.Alpha.Visible)
6868
getAdapter()?.notifyDataSetChanged()
6969
}
7070
}
@@ -78,10 +78,7 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
7878
*/
7979
private fun setupShadows(isShadows: Boolean) {
8080
val visibility = if (isShadows) VISIBLE else GONE
81-
82-
vShadowRight.visibility = visibility
83-
vShadowBottomLine.visibility = visibility
84-
vShadowBottomContent.visibility = visibility
81+
vShadows.forEach { it.visibility = visibility }
8582
}
8683

8784
// - Initialization

codeview/src/main/java/io/github/kbiakov/codeview/Utils.kt

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import android.content.Context
44
import android.os.Handler
55
import android.os.Looper
66
import android.text.Html
7+
import android.text.SpannableString
78
import android.text.Spanned
9+
import android.text.TextUtils
10+
import android.util.Log
811
import android.util.TypedValue
912
import java.io.BufferedReader
1013
import java.io.InputStreamReader
11-
import java.util.*
1214
import java.util.concurrent.Executors
15+
import java.util.stream.IntStream
1316

1417
object Const {
1518
val DefaultDelay = 250L
@@ -54,7 +57,7 @@ fun extractLines(source: String) = listOf(*source.split("\n").toTypedArray())
5457
* @param idx Index to slice
5558
* @return Pair of lists with head and tail
5659
*/
57-
fun <T> List<T>.slice(idx: Int) = Pair(this.subList(0, idx), this.subList(idx, this.lastIndex))
60+
fun <T> List<T>.slice(idx: Int) = Pair(subList(0, idx), subList(idx, lastIndex))
5861

5962
/**
6063
* Get HTML from string.
@@ -63,13 +66,21 @@ fun <T> List<T>.slice(idx: Int) = Pair(this.subList(0, idx), this.subList(idx, t
6366
* @return Spanned HTML string
6467
*/
6568
@Suppress("deprecation")
66-
fun html(content: String) =
67-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
68-
Html.fromHtml(content.htmlSafe(), Html.FROM_HTML_MODE_LEGACY)
69-
else
70-
Html.fromHtml(content.htmlSafe())
69+
fun html(content: String): Spanned {
70+
val spanned = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
71+
Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY)
72+
else
73+
Html.fromHtml(content)
74+
val spaces = content.startSpacesForTaggedString()
75+
return SpannableString(TextUtils.concat(spaces, spanned))
76+
}
7177

72-
fun String.htmlSafe() = replace(" ", "&nbsp;")
78+
private fun String.startSpacesForTaggedString(): String {
79+
val startIdx = indexOf('>') + 1
80+
val escaped = substring(startIdx)
81+
val count = escaped.indexOf(escaped.trim())
82+
return " ".repeat(count)
83+
}
7384

7485
object Thread {
7586
/**

codeview/src/main/java/io/github/kbiakov/codeview/adapters/AbstractCodeAdapter.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
146146
private fun highlighting(language: String, onReady: () -> Unit) {
147147
// TODO: highlight by 10 lines
148148
val code = CodeHighlighter.highlight(language, options.code, options.theme)
149-
150149
updateContent(code, onReady)
151150
}
152151

codeview/src/main/java/io/github/kbiakov/codeview/highlight/CodeHighlighter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ fun String.withFontParams(color: String?): String {
261261

262262
// put tag on the borders (end & start of line, ..., end of tag)
263263
do { // until closing tag is reached
264-
val part = substring(idx..newIdx - 1).inFontTag(color).plus("\n")
264+
val part = substring(idx .. newIdx - 1).inFontTag(color).plus("\n")
265265
parametrizedString.append(part)
266266

267267
idx = newIdx + 1

codeview/src/main/res/values/dimens.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
<dimen name="line_num_width">32dp</dimen>
44
<dimen name="line_height">24dp</dimen>
55
<dimen name="line_text_size">12sp</dimen>
6-
</resources>
6+
</resources>

0 commit comments

Comments
 (0)