Skip to content

Commit

Permalink
Inserting corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
marigostra committed Jun 21, 2022
1 parent c26008a commit b977e65
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/main/java/org/luwrain/app/notepad/Conversations.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,10 @@ String replaceWith()
return Popups.editWithHistory(luwrain, strings.replacePopupName(), strings.replaceWithPopupPrefix(), "", replaceWithHistory);
}

String correctionSuggestion(String[] options)
{
final Object res = Popups.fixedList(luwrain, strings.correctionSuggestionsPopupName(), options);
return res != null?res.toString():null;
}

}
16 changes: 13 additions & 3 deletions src/main/java/org/luwrain/app/notepad/MainLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,21 @@ private boolean actModeProgramming()

private boolean actWordSuggestions()
{
final Lines lines = editArea.getContent();
final String word = new TextFragmentUtils(lines).getWord(editArea.getHotPointX(), editArea.getHotPointY());
final String word = new TextFragmentUtils(editArea.getContent()).getWord(editArea.getHotPointX(), editArea.getHotPointY());
if (word == null)
return false;
app.message(word);
final List<String> suggestions = spellChecking.getSpellChecker().suggestCorrections(word);
if (suggestions != null)
if (suggestions == null || suggestions.isEmpty())
return false;
final String correction = app.getConv().correctionSuggestion(suggestions.toArray(new String[suggestions.size()]));
if (correction == null)
return true;
editArea.update((lines, hotPoint)->{
final String newLine = new TextFragmentUtils(lines).replaceWord(hotPoint.getHotPointX(), hotPoint.getHotPointY(), correction);
lines.setLine(hotPoint.getHotPointY(), newLine);
return true;
});
return true;
}

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/luwrain/app/notepad/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public interface Strings
String actionNoIndents();
String actionOpen();
String actionReplace();
String actionSpellRight();
String actionSpellLeft();


String actionSaveAs();
String appName();
String cancelNarratingBeforeClosing();
Expand Down Expand Up @@ -67,9 +67,13 @@ public interface Strings


String charsetPopupPrefix();

String replacePopupName();
String replaceExpPopupPrefix();
String replaceWithPopupPrefix();

String actionSpellRight();

String actionWordSuggestions();
String correctionSuggestionsPopupName();
}
36 changes: 35 additions & 1 deletion src/main/java/org/luwrain/app/notepad/TextFragmentUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public TextFragmentUtils(Lines lines)

String getWord(int pos, int lineIndex, Predicate<Character> wordCharCond)
{
NullCheck.notNull(wordCharCond, "wordCharCond");
if (pos < 0)
throw new IllegalArgumentException("pos can't be negative");
if (lineIndex < 0)
Expand All @@ -50,11 +51,44 @@ String getWord(int pos, int lineIndex, Predicate<Character> wordCharCond)
posFrom--;
while (posTo + 1 < line.length() && wordCharCond.test(Character.valueOf(line.charAt(posTo + 1))))
posTo++;
return line.substring(posFrom, posTo);
return line.substring(posFrom, posTo + 1);
}

public String getWord(int pos, int lineIndex)
{
return getWord(pos, lineIndex, (ch)->(Character.isLetter(ch) || ch.charValue() == '-'));
}

String replaceWord(int pos, int lineIndex, String replaceWith, Predicate<Character> wordCharCond)
{
NullCheck.notNull(wordCharCond, "wordCharCond");
NullCheck.notNull(replaceWith, "replaceWith");
if (pos < 0)
throw new IllegalArgumentException("pos can't be negative");
if (lineIndex < 0)
throw new IllegalArgumentException("lineIndex can't be negative");
if (lineIndex >= lines.getLineCount())
return null;
final String line = lines.getLine(lineIndex);
if (line == null)
return null;
if (pos >= line.length())
return null;
if (!wordCharCond.test(Character.valueOf(line.charAt(pos))))
return null;
int posFrom = pos, posTo = pos;
while(posFrom > 0 && wordCharCond.test(Character.valueOf(line.charAt(posFrom - 1))))
posFrom--;
while (posTo + 1 < line.length() && wordCharCond.test(Character.valueOf(line.charAt(posTo + 1))))
posTo++;
return line.substring(0, posFrom) + replaceWith + line.substring(posTo + 1);
}

public String replaceWord(int pos, int lineIndex, String replaceWith)
{
NullCheck.notNull(replaceWith, "replaceWith");
return replaceWord(pos, lineIndex, replaceWith, (ch)->(Character.isLetter(ch) || ch.charValue() == '-'));
}


}

0 comments on commit b977e65

Please sign in to comment.