diff --git a/CHANGELOG.md b/CHANGELOG.md index c960f84802..9e1a4b57f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ ### Next version -* Added option to auto check with LanguageTool after each new line +* Added option to check paragraph with LanguageTool after a newline ### 1.1973 - 2022-10-01 diff --git a/FAQ.md b/FAQ.md index 640429b90b..707523f762 100644 --- a/FAQ.md +++ b/FAQ.md @@ -4895,7 +4895,7 @@ Texts with suggestions will be marked and if you tap on a marked suggestion, it will be shown by the keyboard if the keyboard supports this, else you can double tap or long press the marked text to show suggestions. -Since version 1.1974 there is an option to check message texts after each new line. +Since version 1.1974 there is an option to check paragraphs after a new line. The suboption *Use formal form* can be enabled to let LanguageTool suggest more formal text (business, legal, etc). diff --git a/app/src/main/assets/CHANGELOG.md b/app/src/main/assets/CHANGELOG.md index c960f84802..9e1a4b57f6 100644 --- a/app/src/main/assets/CHANGELOG.md +++ b/app/src/main/assets/CHANGELOG.md @@ -6,7 +6,7 @@ ### Next version -* Added option to auto check with LanguageTool after each new line +* Added option to check paragraph with LanguageTool after a newline ### 1.1973 - 2022-10-01 diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index b59a8f0851..f77d31243f 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -799,8 +799,13 @@ public class FragmentCompose extends FragmentBase { if (renum) StyleHelper.renumber(text, false, etBody.getContext()); - if (lt_auto) - onLanguageTool(true); + if (lt_auto) { + int start = added; + while (start > 0 && text.charAt(start - 1) != '\n') + start--; + if (start < added) + onLanguageTool(start, added, true); + } } catch (Throwable ex) { Log.e(ex); } finally { @@ -1968,7 +1973,7 @@ public class FragmentCompose extends FragmentBase { @Override public boolean onLongClick(View v) { if (lt_enabled) { - onLanguageTool(false); + onLanguageTool(0, etBody.length(), false); return true; } else return false; @@ -2568,13 +2573,13 @@ public class FragmentCompose extends FragmentBase { popupMenu.showWithIcons(context, anchor); } - private void onLanguageTool(boolean silent) { + private void onLanguageTool(int start, int end, boolean silent) { etBody.clearComposingText(); Log.i("LT running enabled=" + etBody.isSuggestionsEnabled()); Bundle args = new Bundle(); - args.putCharSequence("text", etBody.getText()); + args.putCharSequence("text", etBody.getText().subSequence(start, end)); new SimpleTask>() { private Toast toast = null; @@ -2605,7 +2610,7 @@ public class FragmentCompose extends FragmentBase { @Override protected void onExecuted(Bundle args, List suggestions) { - LanguageTool.applySuggestions(etBody, suggestions); + LanguageTool.applySuggestions(etBody, start, end, suggestions); if (!silent && (suggestions == null || suggestions.size() == 0)) @@ -5692,15 +5697,17 @@ public class FragmentCompose extends FragmentBase { Log.i("Draft content=" + draft.content); if (draft.content && state == State.NONE) { - Runnable postShow = null; - if (args.containsKey("images")) { - ArrayList images = args.getParcelableArrayList("images"); - args.remove("images"); // once + ArrayList images = args.getParcelableArrayList("images"); + args.remove("images"); // once - postShow = new Runnable() { - @Override - public void run() { - try { + Runnable postShow = new Runnable() { + @Override + public void run() { + try { + if (lt_auto) + onLanguageTool(0, etBody.length(), true); + + if (images != null) { boolean image_dialog = prefs.getBoolean("image_dialog", true); if (image_dialog) { Helper.hideKeyboard(view); @@ -5715,12 +5722,12 @@ public class FragmentCompose extends FragmentBase { fragment.show(getParentFragmentManager(), "compose:shared"); } else onAddImageFile(images); - } catch (Throwable ex) { - Log.e(ex); } + } catch (Throwable ex) { + Log.e(ex); } - }; - } + } + }; showDraft(draft, false, postShow, args.getInt("selection")); } @@ -6897,9 +6904,6 @@ public class FragmentCompose extends FragmentBase { grpBody.setVisibility(View.VISIBLE); - if (lt_auto) - onLanguageTool(true); - cbSignature.setChecked(draft.signature); tvSignature.setAlpha(draft.signature ? 1.0f : Helper.LOW_LIGHT); diff --git a/app/src/main/java/eu/faircode/email/LanguageTool.java b/app/src/main/java/eu/faircode/email/LanguageTool.java index b2fa0b550a..07a6459445 100644 --- a/app/src/main/java/eu/faircode/email/LanguageTool.java +++ b/app/src/main/java/eu/faircode/email/LanguageTool.java @@ -166,13 +166,13 @@ public class LanguageTool { } } - static void applySuggestions(EditText etBody, List suggestions) { + static void applySuggestions(EditText etBody, int start, int end, List suggestions) { Editable edit = etBody.getText(); if (edit == null) return; // https://developer.android.com/reference/android/text/style/SuggestionSpan - for (SuggestionSpanEx suggestion : edit.getSpans(0, edit.length(), SuggestionSpanEx.class)) { + for (SuggestionSpanEx suggestion : edit.getSpans(start, end, SuggestionSpanEx.class)) { Log.i("LT removing=" + suggestion); edit.removeSpan(suggestion); } @@ -183,9 +183,13 @@ public class LanguageTool { SuggestionSpan span = new SuggestionSpanEx(etBody.getContext(), suggestion.replacements.toArray(new String[0]), SuggestionSpan.FLAG_MISSPELLED); - int start = suggestion.offset; - int end = start + suggestion.length; - edit.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + int s = start + suggestion.offset; + int e = s + suggestion.length; + if (s < 0 || s > edit.length() || e < 0 || e > edit.length()) { + Log.w("LT " + s + "..." + e + " length=" + edit.length()); + continue; + } + edit.setSpan(span, s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 34af563f61..f8dfd0a833 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -751,7 +751,7 @@ Language System LanguageTool integration - Check message after each new line + Check paragraph after a new line DeepL integration VirusTotal integration Send integration diff --git a/metadata/en-US/changelogs/1973.txt b/metadata/en-US/changelogs/1973.txt index 591c106e3a..5b119ffe19 100644 --- a/metadata/en-US/changelogs/1973.txt +++ b/metadata/en-US/changelogs/1973.txt @@ -6,7 +6,7 @@ Mei long Next version -* Added option to auto check with LanguageTool after each new line +* Added option to check paragraph with LanguageTool after a newline 1.1973 - 2022-10-01