From 0468da31e107e8965579b993cf02ffd52ea71225 Mon Sep 17 00:00:00 2001 From: 1552980358 <1552980358@qq.com> Date: Fri, 4 Feb 2022 14:42:32 +0800 Subject: [PATCH 1/2] CodeView.kt: Optimize highlight() Editable length identification --- app/src/main/java/io/legado/app/ui/widget/code/CodeView.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/ui/widget/code/CodeView.kt b/app/src/main/java/io/legado/app/ui/widget/code/CodeView.kt index 4f2207d4b..81505c1cd 100644 --- a/app/src/main/java/io/legado/app/ui/widget/code/CodeView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/code/CodeView.kt @@ -169,7 +169,10 @@ class CodeView : AppCompatMultiAutoCompleteTextView { } private fun highlight(editable: Editable): Editable { - if (editable.isEmpty() || editable.length > 1024) return editable + // if (editable.isEmpty() || editable.length > 1024) return editable + if (editable.length !in 1 .. 1024) { + return editable + } try { clearSpans(editable) highlightErrorLines(editable) From 313ca3c2d36e14d4584decf63df4e49f4bc7f529 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 4 Feb 2022 16:44:29 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/ui/widget/code/CodeView.kt | 105 ++++++++---------- 1 file changed, 46 insertions(+), 59 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/widget/code/CodeView.kt b/app/src/main/java/io/legado/app/ui/widget/code/CodeView.kt index 81505c1cd..c07a0d56f 100644 --- a/app/src/main/java/io/legado/app/ui/widget/code/CodeView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/code/CodeView.kt @@ -20,7 +20,9 @@ import java.util.regex.Pattern import kotlin.math.roundToInt @Suppress("unused") -class CodeView : AppCompatMultiAutoCompleteTextView { +class CodeView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : + AppCompatMultiAutoCompleteTextView(context, attrs) { + private var tabWidth = 0 private var tabWidthInCharacters = 0 private var mUpdateDelayTime = 500 @@ -35,23 +37,53 @@ class CodeView : AppCompatMultiAutoCompleteTextView { private val mSyntaxPatternMap: MutableMap = HashMap() private var mIndentCharacterList = mutableListOf('{', '+', '-', '*', '/', '=') - constructor(context: Context?) : super(context!!) { - initEditorView() + private val mUpdateRunnable = Runnable { + val source = text + highlightWithoutChange(source) } - constructor(context: Context?, attrs: AttributeSet?) : super( - context!!, attrs - ) { - initEditorView() - } + private val mEditorTextWatcher: TextWatcher = object : TextWatcher { + private var start = 0 + private var count = 0 + override fun beforeTextChanged( + charSequence: CharSequence, + start: Int, + before: Int, + count: Int + ) { + this.start = start + this.count = count + } - constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super( - context!!, attrs, defStyleAttr - ) { - initEditorView() + override fun onTextChanged( + charSequence: CharSequence, + start: Int, + before: Int, + count: Int + ) { + if (!modified) return + if (highlightWhileTextChanging) { + if (mSyntaxPatternMap.isNotEmpty()) { + convertTabs(editableText, start, count) + mUpdateHandler.postDelayed(mUpdateRunnable, mUpdateDelayTime.toLong()) + } + } + if (mRemoveErrorsWhenTextChanged) removeAllErrorLines() + } + + override fun afterTextChanged(editable: Editable) { + if (!highlightWhileTextChanging) { + if (!modified) return + cancelHighlighterRender() + if (mSyntaxPatternMap.isNotEmpty()) { + convertTabs(editableText, start, count) + mUpdateHandler.postDelayed(mUpdateRunnable, mUpdateDelayTime.toLong()) + } + } + } } - private fun initEditorView() { + init { if (mAutoCompleteTokenizer == null) { mAutoCompleteTokenizer = KeywordTokenizer() } @@ -170,7 +202,7 @@ class CodeView : AppCompatMultiAutoCompleteTextView { private fun highlight(editable: Editable): Editable { // if (editable.isEmpty() || editable.length > 1024) return editable - if (editable.length !in 1 .. 1024) { + if (editable.length !in 1..1024) { return editable } try { @@ -348,51 +380,6 @@ class CodeView : AppCompatMultiAutoCompleteTextView { super.showDropDown() } - private val mUpdateRunnable = Runnable { - val source = text - highlightWithoutChange(source) - } - private val mEditorTextWatcher: TextWatcher = object : TextWatcher { - private var start = 0 - private var count = 0 - override fun beforeTextChanged( - charSequence: CharSequence, - start: Int, - before: Int, - count: Int - ) { - this.start = start - this.count = count - } - - override fun onTextChanged( - charSequence: CharSequence, - start: Int, - before: Int, - count: Int - ) { - if (!modified) return - if (highlightWhileTextChanging) { - if (mSyntaxPatternMap.isNotEmpty()) { - convertTabs(editableText, start, count) - mUpdateHandler.postDelayed(mUpdateRunnable, mUpdateDelayTime.toLong()) - } - } - if (mRemoveErrorsWhenTextChanged) removeAllErrorLines() - } - - override fun afterTextChanged(editable: Editable) { - if (!highlightWhileTextChanging) { - if (!modified) return - cancelHighlighterRender() - if (mSyntaxPatternMap.isNotEmpty()) { - convertTabs(editableText, start, count) - mUpdateHandler.postDelayed(mUpdateRunnable, mUpdateDelayTime.toLong()) - } - } - } - } - private inner class TabWidthSpan : ReplacementSpan() { override fun getSize( paint: Paint,