Skip to content

Commit

Permalink
Moved case functions to TextViewUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Dec 15, 2024
1 parent 63f6c15 commit 4e7b1ba
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ public void onActivityFirstTimeVisible() {
}
}


@Override
public void onSaveInstanceState(@NonNull final Bundle outState) {
super.onSaveInstanceState(outState);
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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};
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ public static GsFileBrowserFragment newInstance() {
private Menu _fragmentMenu;
private MarkorContextUtils _cu;
private Toolbar _toolbar;
private File _lastSelectedFile;

//########################
//## Methods
Expand Down

0 comments on commit 4e7b1ba

Please sign in to comment.