LanguageTool: check paragraph only on new line

This commit is contained in:
M66B 2022-10-01 14:44:35 +02:00
parent 9ba931a356
commit a0fb6463bf
7 changed files with 39 additions and 31 deletions

View File

@ -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

2
FAQ.md
View File

@ -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).

View File

@ -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

View File

@ -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<List<LanguageTool.Suggestion>>() {
private Toast toast = null;
@ -2605,7 +2610,7 @@ public class FragmentCompose extends FragmentBase {
@Override
protected void onExecuted(Bundle args, List<LanguageTool.Suggestion> 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<Uri> images = args.getParcelableArrayList("images");
args.remove("images"); // once
ArrayList<Uri> 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);

View File

@ -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();
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);
}
}

View File

@ -751,7 +751,7 @@
<string name="title_advanced_language">Language</string>
<string name="title_advanced_language_system">System</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_virus_total">VirusTotal integration</string>
<string name="title_advanced_send" translatable="false">Send integration</string>

View File

@ -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