LanguageTool: check paragraph only on new line

pull/209/head
M66B 2 years ago
parent 9ba931a356
commit a0fb6463bf

@ -6,7 +6,7 @@
### Next version ### 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 ### 1.1973 - 2022-10-01

@ -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, 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. 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). The suboption *Use formal form* can be enabled to let LanguageTool suggest more formal text (business, legal, etc).

@ -6,7 +6,7 @@
### Next version ### 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 ### 1.1973 - 2022-10-01

@ -799,8 +799,13 @@ public class FragmentCompose extends FragmentBase {
if (renum) if (renum)
StyleHelper.renumber(text, false, etBody.getContext()); StyleHelper.renumber(text, false, etBody.getContext());
if (lt_auto) if (lt_auto) {
onLanguageTool(true); int start = added;
while (start > 0 && text.charAt(start - 1) != '\n')
start--;
if (start < added)
onLanguageTool(start, added, true);
}
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} finally { } finally {
@ -1968,7 +1973,7 @@ public class FragmentCompose extends FragmentBase {
@Override @Override
public boolean onLongClick(View v) { public boolean onLongClick(View v) {
if (lt_enabled) { if (lt_enabled) {
onLanguageTool(false); onLanguageTool(0, etBody.length(), false);
return true; return true;
} else } else
return false; return false;
@ -2568,13 +2573,13 @@ public class FragmentCompose extends FragmentBase {
popupMenu.showWithIcons(context, anchor); popupMenu.showWithIcons(context, anchor);
} }
private void onLanguageTool(boolean silent) { private void onLanguageTool(int start, int end, boolean silent) {
etBody.clearComposingText(); etBody.clearComposingText();
Log.i("LT running enabled=" + etBody.isSuggestionsEnabled()); Log.i("LT running enabled=" + etBody.isSuggestionsEnabled());
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putCharSequence("text", etBody.getText()); args.putCharSequence("text", etBody.getText().subSequence(start, end));
new SimpleTask<List<LanguageTool.Suggestion>>() { new SimpleTask<List<LanguageTool.Suggestion>>() {
private Toast toast = null; private Toast toast = null;
@ -2605,7 +2610,7 @@ public class FragmentCompose extends FragmentBase {
@Override @Override
protected void onExecuted(Bundle args, List<LanguageTool.Suggestion> suggestions) { protected void onExecuted(Bundle args, List<LanguageTool.Suggestion> suggestions) {
LanguageTool.applySuggestions(etBody, suggestions); LanguageTool.applySuggestions(etBody, start, end, suggestions);
if (!silent && if (!silent &&
(suggestions == null || suggestions.size() == 0)) (suggestions == null || suggestions.size() == 0))
@ -5692,15 +5697,17 @@ public class FragmentCompose extends FragmentBase {
Log.i("Draft content=" + draft.content); Log.i("Draft content=" + draft.content);
if (draft.content && state == State.NONE) { if (draft.content && state == State.NONE) {
Runnable postShow = null; ArrayList<Uri> images = args.getParcelableArrayList("images");
if (args.containsKey("images")) { args.remove("images"); // once
ArrayList<Uri> images = args.getParcelableArrayList("images");
args.remove("images"); // once
postShow = new Runnable() { Runnable postShow = new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
if (lt_auto)
onLanguageTool(0, etBody.length(), true);
if (images != null) {
boolean image_dialog = prefs.getBoolean("image_dialog", true); boolean image_dialog = prefs.getBoolean("image_dialog", true);
if (image_dialog) { if (image_dialog) {
Helper.hideKeyboard(view); Helper.hideKeyboard(view);
@ -5715,12 +5722,12 @@ public class FragmentCompose extends FragmentBase {
fragment.show(getParentFragmentManager(), "compose:shared"); fragment.show(getParentFragmentManager(), "compose:shared");
} else } else
onAddImageFile(images); onAddImageFile(images);
} catch (Throwable ex) {
Log.e(ex);
} }
} catch (Throwable ex) {
Log.e(ex);
} }
}; }
} };
showDraft(draft, false, postShow, args.getInt("selection")); showDraft(draft, false, postShow, args.getInt("selection"));
} }
@ -6897,9 +6904,6 @@ public class FragmentCompose extends FragmentBase {
grpBody.setVisibility(View.VISIBLE); grpBody.setVisibility(View.VISIBLE);
if (lt_auto)
onLanguageTool(true);
cbSignature.setChecked(draft.signature); cbSignature.setChecked(draft.signature);
tvSignature.setAlpha(draft.signature ? 1.0f : Helper.LOW_LIGHT); tvSignature.setAlpha(draft.signature ? 1.0f : Helper.LOW_LIGHT);

@ -166,13 +166,13 @@ public class LanguageTool {
} }
} }
static void applySuggestions(EditText etBody, List<Suggestion> suggestions) { static void applySuggestions(EditText etBody, int start, int end, List<Suggestion> suggestions) {
Editable edit = etBody.getText(); Editable edit = etBody.getText();
if (edit == null) if (edit == null)
return; return;
// https://developer.android.com/reference/android/text/style/SuggestionSpan // 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); Log.i("LT removing=" + suggestion);
edit.removeSpan(suggestion); edit.removeSpan(suggestion);
} }
@ -183,9 +183,13 @@ public class LanguageTool {
SuggestionSpan span = new SuggestionSpanEx(etBody.getContext(), SuggestionSpan span = new SuggestionSpanEx(etBody.getContext(),
suggestion.replacements.toArray(new String[0]), suggestion.replacements.toArray(new String[0]),
SuggestionSpan.FLAG_MISSPELLED); SuggestionSpan.FLAG_MISSPELLED);
int start = suggestion.offset; int s = start + suggestion.offset;
int end = start + suggestion.length; int e = s + suggestion.length;
edit.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 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);
} }
} }

@ -751,7 +751,7 @@
<string name="title_advanced_language">Language</string> <string name="title_advanced_language">Language</string>
<string name="title_advanced_language_system">System</string> <string name="title_advanced_language_system">System</string>
<string name="title_advanced_lt">LanguageTool integration</string> <string name="title_advanced_lt">LanguageTool integration</string>
<string name="title_advanced_lt_auto">Check message after each new line</string> <string name="title_advanced_lt_auto">Check paragraph after a new line</string>
<string name="title_advanced_deepl">DeepL integration</string> <string name="title_advanced_deepl">DeepL integration</string>
<string name="title_advanced_virus_total">VirusTotal integration</string> <string name="title_advanced_virus_total">VirusTotal integration</string>
<string name="title_advanced_send" translatable="false">Send integration</string> <string name="title_advanced_send" translatable="false">Send integration</string>

@ -6,7 +6,7 @@ Mei long
Next version 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 1.1973 - 2022-10-01

Loading…
Cancel
Save