From 37b550fe08bc20f8807b68870d102f39b4390221 Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Wed, 11 Nov 2015 17:52:31 -0500 Subject: [PATCH] Fix spelling not formatting words with punctuation. i.e. missing because the "word" didn't match the misspelled one detected by jazzy. --- .../tabbychat/extra/spell/Spellcheck.java | 17 ++++++++++------- .../extra/spell/SpellingFormatter.java | 19 +++++++++++-------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/main/java/mnm/mods/tabbychat/extra/spell/Spellcheck.java b/src/main/java/mnm/mods/tabbychat/extra/spell/Spellcheck.java index 65d15dc..437fb4a 100644 --- a/src/main/java/mnm/mods/tabbychat/extra/spell/Spellcheck.java +++ b/src/main/java/mnm/mods/tabbychat/extra/spell/Spellcheck.java @@ -5,9 +5,11 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; -import java.util.Set; +import java.util.Iterator; +import java.util.List; -import com.google.common.collect.Sets; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; import com.google.common.io.Closeables; import com.swabunga.spell.engine.SpellDictionary; import com.swabunga.spell.engine.SpellDictionaryHashMap; @@ -18,12 +20,12 @@ import mnm.mods.tabbychat.TabbyChat; -public class Spellcheck { +public class Spellcheck implements Iterable { private SpellChecker spellCheck; private LangDict language; private SpellDictionary userDict; - private Set errors = Sets.newHashSet(); + private List errors = Lists.newArrayList(); public Spellcheck(File userDict) { try { @@ -54,7 +56,7 @@ public void loadLanguage(LangDict lang) throws IOException { spellCheck.addSpellCheckListener(new SpellCheckListener() { @Override public synchronized void spellingError(SpellCheckEvent event) { - errors.add(event.getInvalidWord()); + errors.add(event); } }); } finally { @@ -74,8 +76,9 @@ public void checkSpelling(String string) { } } - public boolean isCorrect(String word) { - return !errors.contains(word); + @Override + public Iterator iterator() { + return ImmutableList.copyOf(errors).iterator(); } } diff --git a/src/main/java/mnm/mods/tabbychat/extra/spell/SpellingFormatter.java b/src/main/java/mnm/mods/tabbychat/extra/spell/SpellingFormatter.java index 8afe5d5..0f5bcb2 100644 --- a/src/main/java/mnm/mods/tabbychat/extra/spell/SpellingFormatter.java +++ b/src/main/java/mnm/mods/tabbychat/extra/spell/SpellingFormatter.java @@ -1,6 +1,7 @@ package mnm.mods.tabbychat.extra.spell; import com.google.common.base.Function; +import com.swabunga.spell.event.SpellCheckEvent; import mnm.mods.tabbychat.TabbyChat; import mnm.mods.util.ChatBuilder; @@ -20,16 +21,18 @@ public SpellingFormatter(Spellcheck sp) { public IChatComponent apply(String text) { if (!TabbyChat.getInstance().settings.general.spelling.enabled.getValue()) return new ChatComponentText(text); - String[] split = text.split(" "); ChatBuilder b = new ChatBuilder(); - for (String word : split) { - b.text(word); - if (!spelling.isCorrect(word)) { - b.underline(Color.RED); - } - b.text(" "); + int prev = 0; + for (SpellCheckEvent event : spelling) { + int start = event.getWordContextPosition(); + int end = event.getWordContextPosition() + event.getInvalidWord().length(); + + b.text(text.substring(prev, start)); + b.text(text.substring(start, end)).underline(Color.RED); + + prev = end; } - return b.build(); + return b.text(text.substring(prev)).build(); } }