From 4e7b1bab1b41b93adee5c17d189292314a3718f0 Mon Sep 17 00:00:00 2001 From: Harshad Vedartham Date: Sun, 15 Dec 2024 18:03:10 +0530 Subject: [PATCH] Moved case functions to TextViewUtils --- .../activity/DocumentEditAndViewFragment.java | 16 ++---- .../markor/activity/MainActivity.java | 3 +- .../frontend/textview/HighlightingEditor.java | 56 ------------------- .../frontend/textview/TextViewUtils.java | 50 +++++++++++++++-- .../filebrowser/GsFileBrowserFragment.java | 1 - 5 files changed, 50 insertions(+), 76 deletions(-) diff --git a/app/src/main/java/net/gsantner/markor/activity/DocumentEditAndViewFragment.java b/app/src/main/java/net/gsantner/markor/activity/DocumentEditAndViewFragment.java index 5cec9c9409..a4819371d5 100644 --- a/app/src/main/java/net/gsantner/markor/activity/DocumentEditAndViewFragment.java +++ b/app/src/main/java/net/gsantner/markor/activity/DocumentEditAndViewFragment.java @@ -653,24 +653,16 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) { return true; } case R.id.action_toggle_case: - if (_hlEditor != null) { - _hlEditor.toggleCase(); - } + TextViewUtils.toggleSelectionCase(_hlEditor.getText()); return true; case R.id.action_switch_case: - if (_hlEditor != null) { - _hlEditor.switchCase(); - } + TextViewUtils.switchSelectionCase(_hlEditor.getText()); return true; case R.id.action_capitalize_words: - if (_hlEditor != null) { - _hlEditor.capitalizeWords(); - } + TextViewUtils.capitalizeSelectionWords(_hlEditor.getText()); return true; case R.id.action_capitalize_sentences: - if (_hlEditor != null) { - _hlEditor.capitalizeSentences(); - } + TextViewUtils.capitalizeSelectionSentences(_hlEditor.getText()); return true; default: { return super.onOptionsItemSelected(item); diff --git a/app/src/main/java/net/gsantner/markor/activity/MainActivity.java b/app/src/main/java/net/gsantner/markor/activity/MainActivity.java index beb962f3c6..d15b39fc60 100644 --- a/app/src/main/java/net/gsantner/markor/activity/MainActivity.java +++ b/app/src/main/java/net/gsantner/markor/activity/MainActivity.java @@ -124,7 +124,6 @@ public void onActivityFirstTimeVisible() { } } - @Override public void onSaveInstanceState(@NonNull final Bundle outState) { super.onSaveInstanceState(outState); @@ -334,7 +333,7 @@ public void onBackPressed() { } public String getFileBrowserTitle() { - final File file = _notebook.getCurrentFolder(); + final File file = _notebook != null ? _notebook.getCurrentFolder() : null; if (file != null && !_appSettings.getNotebookDirectory().equals(file)) { return "> " + file.getName(); } else { diff --git a/app/src/main/java/net/gsantner/markor/frontend/textview/HighlightingEditor.java b/app/src/main/java/net/gsantner/markor/frontend/textview/HighlightingEditor.java index 1202fc5e1a..72bb9f5ee4 100644 --- a/app/src/main/java/net/gsantner/markor/frontend/textview/HighlightingEditor.java +++ b/app/src/main/java/net/gsantner/markor/frontend/textview/HighlightingEditor.java @@ -39,9 +39,7 @@ import net.gsantner.opoc.format.GsTextUtils; import net.gsantner.opoc.wrapper.GsCallback; import net.gsantner.opoc.wrapper.GsTextWatcherAdapter; -import net.gsantner.markor.util.TextCasingUtils; -import java.util.Objects; import java.util.concurrent.ExecutorService; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.SynchronousQueue; @@ -320,60 +318,6 @@ private int rowEnd(final int y) { return layout == null ? 0 : layout.getLineEnd(layout.getLineForVertical(y)); } - // Text-Casing - // --------------------------------------------------------------------------------------------- - public void toggleCase() { - String text = getSelectedText(); - if (text.isEmpty()) { - text = Objects.requireNonNull(getText()).toString(); - } - String newText = TextCasingUtils.toggleCase(text); - replaceSelection(newText); - } - - public void switchCase() { - String text = getSelectedText(); - if (text.isEmpty()) { - text = Objects.requireNonNull(getText()).toString(); - } - String newText = TextCasingUtils.switchCase(text); - replaceSelection(newText); - } - - public void capitalizeWords() { - String text = getSelectedText(); - if (text.isEmpty()) { - text = Objects.requireNonNull(getText()).toString(); - } - String newText = TextCasingUtils.capitalizeWords(text); - replaceSelection(newText); - } - - public void capitalizeSentences() { - String text = getSelectedText(); - if (text.isEmpty()) { - text = Objects.requireNonNull(getText()).toString(); - } - String newText = TextCasingUtils.capitalizeSentences(text); - replaceSelection(newText); - } - - private String getSelectedText() { - int start = Math.max(0, getSelectionStart()); - int end = Math.max(0, getSelectionEnd()); - return Objects.requireNonNull(getText()).toString().substring(start, end); - } - - private void replaceSelection(String replacement) { - int start = Math.max(0, getSelectionStart()); - int end = Math.max(0, getSelectionEnd()); - if (start == end) { // If no selection is made, replace all the text in the document - setText(replacement); - } else { // Replace only the selected text - Objects.requireNonNull(getText()).replace(start, end, replacement); - } - } - // Various overrides // --------------------------------------------------------------------------------------------- public void setSaveInstanceState(final boolean save) { diff --git a/app/src/main/java/net/gsantner/markor/frontend/textview/TextViewUtils.java b/app/src/main/java/net/gsantner/markor/frontend/textview/TextViewUtils.java index 725ff3ba7a..6384eecebe 100644 --- a/app/src/main/java/net/gsantner/markor/frontend/textview/TextViewUtils.java +++ b/app/src/main/java/net/gsantner/markor/frontend/textview/TextViewUtils.java @@ -28,6 +28,7 @@ import androidx.annotation.NonNull; +import net.gsantner.markor.util.TextCasingUtils; import net.gsantner.opoc.format.GsTextUtils; import net.gsantner.opoc.util.GsContextUtils; @@ -40,7 +41,7 @@ import java.util.TreeSet; import java.util.UUID; -@SuppressWarnings({"CharsetObjectCanBeUsed", "WeakerAccess", "unused"}) +@SuppressWarnings({"WeakerAccess", "unused"}) public final class TextViewUtils { // Suppress default constructor for noninstantiability @@ -128,15 +129,24 @@ public static int[] getSelection(final CharSequence text) { } } - public static String getSelectedText(final CharSequence text) { + public static CharSequence getSelectedText(final CharSequence text) { final int[] sel = getSelection(text); - return (sel[0] >= 0 && sel[1] >= 0) ? text.subSequence(sel[0], sel[1]).toString() : ""; + return (sel[0] >= 0 && sel[1] >= 0) ? text.subSequence(sel[0], sel[1]) : ""; } - public static String getSelectedText(final TextView text) { + public static CharSequence getSelectedText(final TextView text) { return getSelectedText(text.getText()); } + public static void replaceSelection(final Editable text, final CharSequence replace) { + if (text != null && replace != null) { + final int[] sel = getSelection(text); + if (sel[0] >= 0 && sel[1] >= 0) { + text.replace(sel[0], sel[1], replace); + } + } + } + public static int[] getLineSelection(final CharSequence text, final int[] sel) { return sel != null && sel.length >= 2 ? new int[]{getLineStart(text, sel[0]), getLineEnd(text, sel[1])} : new int[]{-1, -1}; } @@ -380,7 +390,7 @@ public static void setSelectionAndShow(final EditText edit, final int... sel) { * @param title Title of note (for {{title}}) * @param selectedText Currently selected text */ - public static String interpolateSnippet(String text, final String title, final String selectedText) { + public static String interpolateSnippet(String text, final CharSequence title, final CharSequence selectedText) { final long current = System.currentTimeMillis(); final String time = GsContextUtils.instance.formatDateTime((Locale) null, "HH:mm", current); final String date = GsContextUtils.instance.formatDateTime((Locale) null, "yyyy-MM-dd", current); @@ -753,4 +763,34 @@ public static Boolean isImeOpen(final View view) { } return null; // Uncertain } + + // Text-Casing + // --------------------------------------------------------------------------------------------- + public static void toggleSelectionCase(final Editable edit) { + final String text = getSelectedText(edit).toString(); + if (!text.isEmpty()) { + replaceSelection(edit, TextCasingUtils.toggleCase(text)); + } + } + + public static void switchSelectionCase(final Editable edit) { + final String text = getSelectedText(edit).toString(); + if (!text.isEmpty()) { + replaceSelection(edit, TextCasingUtils.switchCase(text)); + } + } + + public static void capitalizeSelectionWords(final Editable edit) { + final String text = getSelectedText(edit).toString(); + if (!text.isEmpty()) { + replaceSelection(edit, TextCasingUtils.capitalizeWords(text)); + } + } + + public static void capitalizeSelectionSentences(final Editable edit) { + final String text = getSelectedText(edit).toString(); + if (!text.isEmpty()) { + replaceSelection(edit, TextCasingUtils.capitalizeSentences(text)); + } + } } diff --git a/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserFragment.java b/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserFragment.java index 2816220b39..c17b6f10bd 100644 --- a/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserFragment.java +++ b/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserFragment.java @@ -91,7 +91,6 @@ public static GsFileBrowserFragment newInstance() { private Menu _fragmentMenu; private MarkorContextUtils _cu; private Toolbar _toolbar; - private File _lastSelectedFile; //######################## //## Methods