Skip to content

Commit

Permalink
fix(liveprog): Fixed undo/redo issues with the editor
Browse files Browse the repository at this point in the history
  • Loading branch information
timschneeb committed Jan 2, 2023
1 parent 03553c0 commit 77a7abb
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Color
import android.graphics.Typeface
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.Editable
import android.text.TextWatcher
import android.text.method.ScrollingMovementMethod
import android.util.TypedValue
import android.view.GestureDetector
import android.view.Menu
import android.view.MenuItem
import android.view.MotionEvent
import android.view.View
import android.widget.Button
import android.widget.Scroller
import android.widget.Toast
import androidx.appcompat.view.menu.MenuBuilder
import androidx.core.content.ContextCompat
Expand Down Expand Up @@ -48,6 +41,7 @@ import me.timschneeberger.rootlessjamesdsp.utils.ContextExtensions.sendLocalBroa
import me.timschneeberger.rootlessjamesdsp.utils.ContextExtensions.showAlert
import me.timschneeberger.rootlessjamesdsp.utils.ContextExtensions.showYesNoAlert
import me.timschneeberger.rootlessjamesdsp.utils.ContextExtensions.unregisterLocalReceiver
import timber.log.Timber
import java.util.regex.Pattern


Expand Down Expand Up @@ -87,11 +81,27 @@ class LiveprogEditorActivity : BaseActivity() {
configCodeView()
configCodeViewPlugins()

val savedPath = savedInstanceState?.getString(EXTRA_TARGET_FILE)

if(!intent.hasExtra(EXTRA_TARGET_FILE)) {
finish()
}

intent.getStringExtra(EXTRA_TARGET_FILE)?.let { load(it) }
// Only load if path not is cached
savedPath ?: intent.getStringExtra(EXTRA_TARGET_FILE)?.let { load(it) }
}

override fun onPostCreate(savedInstanceState: Bundle?) {
savedInstanceState?.getString(EXTRA_TARGET_FILE)?.let { path ->
Timber.d("Restoring parser state")
parser.load(path, skipParse = true)
parser.contents = codeView.text.toString()
parser.parse(silent = true)
isDirty = savedInstanceState.getBoolean(STATE_IS_DIRTY)
undoRedoManager.clearHistory() // TODO save history instance state correctly
}

super.onPostCreate(savedInstanceState)
}

override fun onDestroy() {
Expand Down Expand Up @@ -144,14 +154,14 @@ class LiveprogEditorActivity : BaseActivity() {
if (ret == -1) // error in @init
{
if (relativeLine >= 0 && initLine >= 0)
relativeLine += initLine
relativeLine += initLine - 1
else
relativeLine = -1
}
else if (ret == -3) // error in @sample
{
if (relativeLine >= 0 && sampleLine >= 0)
relativeLine += sampleLine
relativeLine += sampleLine - 1
else
relativeLine = -1
}
Expand Down Expand Up @@ -338,6 +348,13 @@ class LiveprogEditorActivity : BaseActivity() {
}
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState.apply {
putString(EXTRA_TARGET_FILE, parser.path)
putBoolean(STATE_IS_DIRTY, isDirty)
})
}

@SuppressLint("RestrictedApi")
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_liveprog_editor, menu)
Expand All @@ -349,10 +366,21 @@ class LiveprogEditorActivity : BaseActivity() {
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.findMenu -> launchEditorButtonSheet()
R.id.text_undo -> undoRedoManager.undo()
R.id.text_redo -> undoRedoManager.redo()
R.id.text_undo -> {
if(undoRedoManager.canUndo())
undoRedoManager.undo()
else
Toast.makeText(this, getString(R.string.editor_cannot_undo), Toast.LENGTH_SHORT).show()
}
R.id.text_redo -> {
if(undoRedoManager.canRedo())
undoRedoManager.redo()
else
Toast.makeText(this, getString(R.string.editor_cannot_redo), Toast.LENGTH_SHORT).show()
}
R.id.text_run -> run()
R.id.text_save -> save()
R.id.docs -> startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/james34602/EEL_VM#readme")))
}
return super.onOptionsItemSelected(item)
}
Expand Down Expand Up @@ -392,5 +420,6 @@ class LiveprogEditorActivity : BaseActivity() {

companion object {
const val EXTRA_TARGET_FILE = "TargetFile"
const val STATE_IS_DIRTY = "IsDirty"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class UndoRedoManager(private val textView: TextView) {
}
}

private class EditNode(var start: Int, var before: CharSequence?, var after: CharSequence?)
private data class EditNode(var start: Int, var before: CharSequence?, var after: CharSequence?)
private enum class ActionType {
INSERT, DELETE, PASTE, NOT_DEF
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class EelParser {
lastFileHash = null
}

fun load(path: String, force: Boolean = false): Boolean {
fun load(path: String, force: Boolean = false, skipParse: Boolean = false): Boolean {
if(path.isBlank()) {
reset()
return false
Expand All @@ -61,7 +61,15 @@ class EelParser {
return false
}

parse(false)
properties.clear()
tags = listOf()
hasDescription = false
description = null
lastFileHash = null

if(!skipParse) {
parse(false)
}

lastFileHash = contents?.md5
return true
Expand Down Expand Up @@ -93,7 +101,7 @@ class EelParser {
// Parse number range parameters
val rangeParamRegex = """(?<var>\w+):(?<def>-?\d+\.?\d*)?<(?<min>-?\d+\.?\d*),(?<max>-?\d+\.?\d*),?(?<step>-?\d+\.?\d*)?>(?<desc>[\s\S][^\n]*)""".toRegex()
val listParamRegex = """(?<var>\w+):(?<def>-?\d+\.?\d*)?<(?<min>-?\d+\.?\d*),(?<max>-?\d+\.?\d*),?(?<step>-?\d+\.?\d*)?\{(?<opt>[^\}]*)\}>(?<desc>[\s\S][^\n]*)""".toRegex()
contents!!.lines().forEach next@ {
contents?.lines()?.forEach next@ {
val matchRange = rangeParamRegex.find(it)
val groupsRange = matchRange?.groups
if(groupsRange != null) {
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/res/menu/menu_liveprog_editor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@
android:id="@+id/text_save"
android:icon="@drawable/ic_twotone_save_24dp"
android:title="@string/editor_save"
android:orderInCategory="4" />
android:orderInCategory="3" />

<item
android:id="@+id/findMenu"
android:icon="@drawable/ic_twotone_find_in_page_24dp"
android:orderInCategory="6"
android:orderInCategory="4"
android:title="@string/editor_find_and_replace" />

<item
android:id="@+id/docs"
android:icon="@drawable/ic_twotone_help_24dp"
android:orderInCategory="5"
android:title="@string/editor_docs" />

</menu>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@
<!-- Liveprog script editor -->
<string name="title_activity_liveprog_editor">Script editor</string>
<string name="editor_find_and_replace">Find/replace…</string>
<string name="editor_docs">Help</string>
<string name="editor_open_fail">Script file does not exist or cannot be opened.</string>
<string name="editor_run">Save and run</string>
<string name="editor_save">Save</string>
Expand All @@ -422,6 +423,8 @@
<string name="editor_save_prompt_title">Save changes?</string>
<string name="editor_save_prompt">The current script has unsaved changes. Do you want to save your changes or discard them?</string>
<string name="editor_script_launched">Script launched</string>
<string name="editor_cannot_undo">Nothing to undo.</string>
<string name="editor_cannot_redo">Nothing to redo.</string>

<!-- Blocklist -->
<string name="title_activity_blocklist">Excluded apps</string>
Expand Down

0 comments on commit 77a7abb

Please sign in to comment.