Merge remote-tracking branch 'upstream/master'

pull/1586/head
github-actions 3 years ago
commit 9be3ef7bbe
  1. 102
      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 import kotlin.math.roundToInt
@Suppress("unused") @Suppress("unused")
class CodeView : AppCompatMultiAutoCompleteTextView { class CodeView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
AppCompatMultiAutoCompleteTextView(context, attrs) {
private var tabWidth = 0 private var tabWidth = 0
private var tabWidthInCharacters = 0 private var tabWidthInCharacters = 0
private var mUpdateDelayTime = 500 private var mUpdateDelayTime = 500
@ -35,23 +37,53 @@ class CodeView : AppCompatMultiAutoCompleteTextView {
private val mSyntaxPatternMap: MutableMap<Pattern, Int> = HashMap() private val mSyntaxPatternMap: MutableMap<Pattern, Int> = HashMap()
private var mIndentCharacterList = mutableListOf('{', '+', '-', '*', '/', '=') private var mIndentCharacterList = mutableListOf('{', '+', '-', '*', '/', '=')
constructor(context: Context?) : super(context!!) { private val mUpdateRunnable = Runnable {
initEditorView() val source = text
highlightWithoutChange(source)
} }
constructor(context: Context?, attrs: AttributeSet?) : super( private val mEditorTextWatcher: TextWatcher = object : TextWatcher {
context!!, attrs private var start = 0
private var count = 0
override fun beforeTextChanged(
charSequence: CharSequence,
start: Int,
before: Int,
count: Int
) { ) {
initEditorView() this.start = start
this.count = count
} }
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super( override fun onTextChanged(
context!!, attrs, defStyleAttr charSequence: CharSequence,
start: Int,
before: Int,
count: Int
) { ) {
initEditorView() if (!modified) return
if (highlightWhileTextChanging) {
if (mSyntaxPatternMap.isNotEmpty()) {
convertTabs(editableText, start, count)
mUpdateHandler.postDelayed(mUpdateRunnable, mUpdateDelayTime.toLong())
}
}
if (mRemoveErrorsWhenTextChanged) removeAllErrorLines()
} }
private fun initEditorView() { override fun afterTextChanged(editable: Editable) {
if (!highlightWhileTextChanging) {
if (!modified) return
cancelHighlighterRender()
if (mSyntaxPatternMap.isNotEmpty()) {
convertTabs(editableText, start, count)
mUpdateHandler.postDelayed(mUpdateRunnable, mUpdateDelayTime.toLong())
}
}
}
}
init {
if (mAutoCompleteTokenizer == null) { if (mAutoCompleteTokenizer == null) {
mAutoCompleteTokenizer = KeywordTokenizer() mAutoCompleteTokenizer = KeywordTokenizer()
} }
@ -169,7 +201,10 @@ class CodeView : AppCompatMultiAutoCompleteTextView {
} }
private fun highlight(editable: Editable): Editable { 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 { try {
clearSpans(editable) clearSpans(editable)
highlightErrorLines(editable) highlightErrorLines(editable)
@ -345,51 +380,6 @@ class CodeView : AppCompatMultiAutoCompleteTextView {
super.showDropDown() 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() { private inner class TabWidthSpan : ReplacementSpan() {
override fun getSize( override fun getSize(
paint: Paint, paint: Paint,

Loading…
Cancel
Save