From da1c437e7a415b23755e90d2c14a0f80d32196f3 Mon Sep 17 00:00:00 2001 From: TrianguloY Date: Sat, 22 Jun 2019 21:06:02 +0200 Subject: [PATCH] Fixed crash with negative selection when using the bottom buttons in the Script Editor. Fixed also the same-reason crash in the Binding editor dialog. --- .../xgouchet/texteditor/ui/AdvancedEditText.java | 16 +++++++++++++++- .../activities/ScriptEditor.java | 12 ++++++------ .../util/BindingEditDialog.java | 4 +++- .../pierrox/lightning_launcher/util/Search.java | 6 +++--- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/app/llx/app/src/main/java/fr/xgouchet/texteditor/ui/AdvancedEditText.java b/app/llx/app/src/main/java/fr/xgouchet/texteditor/ui/AdvancedEditText.java index 0de8f8c..5608eb4 100755 --- a/app/llx/app/src/main/java/fr/xgouchet/texteditor/ui/AdvancedEditText.java +++ b/app/llx/app/src/main/java/fr/xgouchet/texteditor/ui/AdvancedEditText.java @@ -534,7 +534,21 @@ public class AdvancedEditText extends EditText implements OnKeyListener, OnGestu mHighlightedLine = line; } } - + + /** + * Like {@link EditText#getSelectionStart()} but returns the real start, even with a 'negative' selection. + */ + public int getTrueSelectionStart() { + return Math.min(super.getSelectionStart(), super.getSelectionEnd()); + } + + /** + * Like {@link EditText#getSelectionEnd()} but returns the real end, even with a 'negative' selection. + */ + public int getTrueSelectionEnd() { + return Math.max(super.getSelectionStart(), super.getSelectionEnd()); + } + /** The line numbers paint */ protected Paint mPaintNumbers; /** The line numbers paint */ diff --git a/app/llx/app/src/main/java/net/pierrox/lightning_launcher/activities/ScriptEditor.java b/app/llx/app/src/main/java/net/pierrox/lightning_launcher/activities/ScriptEditor.java index f638e4d..1108ac0 100644 --- a/app/llx/app/src/main/java/net/pierrox/lightning_launcher/activities/ScriptEditor.java +++ b/app/llx/app/src/main/java/net/pierrox/lightning_launcher/activities/ScriptEditor.java @@ -621,12 +621,12 @@ public class ScriptEditor extends ResourceWrapperActivity implements View.OnClic if(event.isShiftPressed()){ // Shift tab, decrease indent Pair selectionI = - Indentation.modifyIndent(mScriptText.getSelectionStart(), mScriptText.getSelectionEnd(), false, mScriptText.getEditableText()); + Indentation.modifyIndent(mScriptText.getTrueSelectionStart(), mScriptText.getTrueSelectionEnd(), false, mScriptText.getEditableText()); mScriptText.setSelection(selectionI.first, selectionI.second); }else if(mScriptText.hasSelection()){ // No shift tab && selection, increase indent Pair selectionI = - Indentation.modifyIndent(mScriptText.getSelectionStart(), mScriptText.getSelectionEnd(), true, mScriptText.getEditableText()); + Indentation.modifyIndent(mScriptText.getTrueSelectionStart(), mScriptText.getTrueSelectionEnd(), true, mScriptText.getEditableText()); mScriptText.setSelection(selectionI.first, selectionI.second); }else { // No shift tab && no selection, add tab char @@ -1359,8 +1359,8 @@ public class ScriptEditor extends ResourceWrapperActivity implements View.OnClic @Override public void apply(AdvancedEditText editText) { - int start = editText.getSelectionStart(); - int end = editText.getSelectionEnd(); + int start = editText.getTrueSelectionStart(); + int end = editText.getTrueSelectionEnd(); editText.getEditableText().replace(start, end, preText+postText); editText.setSelection(start + preText.length()); } @@ -1407,12 +1407,12 @@ public class ScriptEditor extends ResourceWrapperActivity implements View.OnClic switch (action) { case DEC_TAB: Pair selectionD = - Indentation.modifyIndent(editText.getSelectionStart(), editText.getSelectionEnd(), false, editText.getEditableText()); + Indentation.modifyIndent(editText.getTrueSelectionStart(), editText.getTrueSelectionEnd(), false, editText.getEditableText()); editText.setSelection(selectionD.first, selectionD.second); break; case INC_TAB: Pair selectionI = - Indentation.modifyIndent(editText.getSelectionStart(), editText.getSelectionEnd(), true, editText.getEditableText()); + Indentation.modifyIndent(editText.getTrueSelectionStart(), editText.getTrueSelectionEnd(), true, editText.getEditableText()); editText.setSelection(selectionI.first, selectionI.second); break; case SEARCH: diff --git a/app/llx/app/src/main/java/net/pierrox/lightning_launcher/util/BindingEditDialog.java b/app/llx/app/src/main/java/net/pierrox/lightning_launcher/util/BindingEditDialog.java index c33d3da..0a1feb1 100644 --- a/app/llx/app/src/main/java/net/pierrox/lightning_launcher/util/BindingEditDialog.java +++ b/app/llx/app/src/main/java/net/pierrox/lightning_launcher/util/BindingEditDialog.java @@ -374,7 +374,9 @@ public class BindingEditDialog extends AlertDialog implements DialogInterface.On } else { name = mUserVariables.get(childPosition).name; } - mFormulaEditText.getText().replace(mFormulaEditText.getSelectionStart(), mFormulaEditText.getSelectionEnd(), "$"+name); + int selstart = mFormulaEditText.getSelectionStart(); + int selend = mFormulaEditText.getSelectionEnd(); + mFormulaEditText.getText().replace(Math.min(selstart,selend), Math.max(selstart, selend), "$"+name); dismiss(); return true; } diff --git a/app/llx/app/src/main/java/net/pierrox/lightning_launcher/util/Search.java b/app/llx/app/src/main/java/net/pierrox/lightning_launcher/util/Search.java index bfb1b2f..9dca2e2 100644 --- a/app/llx/app/src/main/java/net/pierrox/lightning_launcher/util/Search.java +++ b/app/llx/app/src/main/java/net/pierrox/lightning_launcher/util/Search.java @@ -75,8 +75,8 @@ public class Search { Matcher matcher = pattern.matcher(text); if(!mChkBackwards.isChecked()) { // search fordwards - int from = mEditText.getSelectionStart(); - if (from != mEditText.getSelectionEnd()) from++; // avoids returning the current selection + int from = mEditText.getTrueSelectionStart(); + if (from != mEditText.getTrueSelectionEnd()) from++; // avoids returning the current selection if( matcher.find(from) || matcher.find(0)){ // found one just after the selection or from the beginning start = matcher.start(); @@ -85,7 +85,7 @@ public class Search { }else{ // search backwards - int until = mEditText.getSelectionEnd(); + int until = mEditText.getTrueSelectionEnd(); while( matcher.find() && matcher.end() < until){ // found match before cursor, save start = matcher.start();