From e2f5f65cf414b765c860780e4f7d9c38538b2a56 Mon Sep 17 00:00:00 2001 From: Alexey Gopachenko Date: Thu, 6 Sep 2012 21:26:22 +0200 Subject: [PATCH] Spellchecker try to straighten logic a bit (still under old API) --- .../dictionary/AggregatedDictionary.java | 14 +++- .../dictionary/ProjectDictionary.java | 9 +-- .../dictionary/UserDictionary.java | 9 ++- .../spellchecker/engine/BaseSpellChecker.java | 77 ++++++++----------- 4 files changed, 52 insertions(+), 57 deletions(-) diff --git a/plugins/spellchecker/src/com/intellij/spellchecker/dictionary/AggregatedDictionary.java b/plugins/spellchecker/src/com/intellij/spellchecker/dictionary/AggregatedDictionary.java index b825f79a0834..5d7a91c9d76d 100644 --- a/plugins/spellchecker/src/com/intellij/spellchecker/dictionary/AggregatedDictionary.java +++ b/plugins/spellchecker/src/com/intellij/spellchecker/dictionary/AggregatedDictionary.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,6 +42,14 @@ public class AggregatedDictionary implements EditableDictionary { return false; } + @Override + public String toString() { + return "AggregatedDictionary{" + + "cachedDictionary=" + cachedDictionary + + ", projectDictionary=" + projectDictionary + + '}'; + } + public boolean contains(String word) { if (word == null) { return false; @@ -82,11 +90,11 @@ public class AggregatedDictionary implements EditableDictionary { public Set getWords() { - return cachedDictionary!=null?cachedDictionary.getWords():null; + return cachedDictionary.getWords(); } public int size() { - return (cachedDictionary!=null?cachedDictionary.size():0); + return (cachedDictionary.size()); } @Nullable diff --git a/plugins/spellchecker/src/com/intellij/spellchecker/dictionary/ProjectDictionary.java b/plugins/spellchecker/src/com/intellij/spellchecker/dictionary/ProjectDictionary.java index df9157f1b77f..ab895e8cfa6b 100644 --- a/plugins/spellchecker/src/com/intellij/spellchecker/dictionary/ProjectDictionary.java +++ b/plugins/spellchecker/src/com/intellij/spellchecker/dictionary/ProjectDictionary.java @@ -60,21 +60,18 @@ public class ProjectDictionary implements EditableDictionary { if (word == null || dictionaries == null) { return false; } - int negatives = 0; + int errors = 0; for (Dictionary dictionary : dictionaries) { try { if (dictionary.contains(word)) { return true; } - else { - negatives++; - } } catch (EncodingException e) { - //System.out.println("e.getMessage() = " + e.getMessage() + " " + word); + errors++; } } - if (negatives==dictionaries.size()) throw new EncodingException("WORD_OF_ENTIRELY_UNKNOWN_LETTERS_FOR_ALL"); + if (errors==dictionaries.size()) throw new EncodingException("WORD_OF_ENTIRELY_UNKNOWN_LETTERS_FOR_ALL"); return false; } diff --git a/plugins/spellchecker/src/com/intellij/spellchecker/dictionary/UserDictionary.java b/plugins/spellchecker/src/com/intellij/spellchecker/dictionary/UserDictionary.java index 6bb0137a66fb..ca83588764a5 100644 --- a/plugins/spellchecker/src/com/intellij/spellchecker/dictionary/UserDictionary.java +++ b/plugins/spellchecker/src/com/intellij/spellchecker/dictionary/UserDictionary.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,6 +15,7 @@ */ package com.intellij.spellchecker.dictionary; +import com.intellij.spellchecker.compress.EncodingException; import com.intellij.util.Consumer; import gnu.trove.THashSet; import org.jetbrains.annotations.NotNull; @@ -39,11 +40,13 @@ public class UserDictionary implements EditableDictionary { } public boolean contains(String word) { - return words.contains(word); + boolean contains = words.contains(word); + if(contains) return true; + throw new EncodingException("WE_CARE_ABOUT_CONTAINS_ONLY"); } public int size() { - return words == null ? 0 : words.size(); + return words.size(); } @Nullable diff --git a/plugins/spellchecker/src/com/intellij/spellchecker/engine/BaseSpellChecker.java b/plugins/spellchecker/src/com/intellij/spellchecker/engine/BaseSpellChecker.java index c612459c767e..476e518891d3 100644 --- a/plugins/spellchecker/src/com/intellij/spellchecker/engine/BaseSpellChecker.java +++ b/plugins/spellchecker/src/com/intellij/spellchecker/engine/BaseSpellChecker.java @@ -36,7 +36,6 @@ import com.intellij.spellchecker.dictionary.Loader; import com.intellij.util.Consumer; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.ui.UIUtil; -import gnu.trove.THashSet; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -49,7 +48,7 @@ public class BaseSpellChecker implements SpellCheckerEngine { private final Transformation transform = new Transformation(); - private final Set dictionaries = new THashSet(); + private final Set dictionaries = new HashSet(); private final List bundledDictionaries = ContainerUtil.createEmptyCOWList(); private final Metrics metrics = new LevenshteinDistance(); @@ -70,23 +69,9 @@ public class BaseSpellChecker implements SpellCheckerEngine { } } else { - loadFixedDictionary(loader); - } - - } - - private void loadFixedDictionary(final @NotNull Loader loader) { - /*if (ApplicationManager.getApplication().isUnitTestMode()) { loadCompressedDictionary(loader); } - else { - ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { - public void run() { - loadCompressedDictionary(loader); - } - }); - }*/ - loadCompressedDictionary(loader); + } private void loadCompressedDictionary(@NotNull Loader loader) { @@ -248,45 +233,47 @@ public class BaseSpellChecker implements SpellCheckerEngine { return result; } - private static boolean isCorrect(@NotNull String transformed, @Nullable Collection dictionaries) { + /** + * @param transformed + * @param dictionaries + * @return -1 (all)failed / 0 (any) ok / >0 all alien + */ + private static int isCorrect(@NotNull String transformed, @Nullable Collection dictionaries) { if (dictionaries == null) { - return true; + return -1; } - for (Object o : dictionaries) { - if (o instanceof Dictionary) { - boolean result = isCorrect(transformed, (Dictionary)o); - if (result) { - return true; - } + //System.out.println("dictionaries = " + dictionaries); + int errors = 0; + for (Dictionary dictionary : dictionaries) { + try { + if (dictionary == null) continue; + //System.out.print("\tBSC.isCorrect " + transformed + " " + dictionary); + boolean contains = dictionary.contains(transformed); + //System.out.println("\tcontains = " + contains); + if (contains) return 0; + } + catch (EncodingException e) { + ++errors; + //System.out.println(e.getMessage() + " " + transformed); + //return true; } - - } - return false; - } - - private static boolean isCorrect(@NotNull String transformed, @Nullable Dictionary dictionary) { - if (dictionary == null) { - return true; - } - - try { - return dictionary.contains(transformed); - } - catch (EncodingException e) { - //System.out.println("e.getMessage() = " + e.getMessage() + " " + transformed); - return true; } + if(errors==dictionaries.size()) return errors; + return -1; } public boolean isCorrect(@NotNull String word) { + //System.out.println("---\n"+word); final String transformed = transform.transform(word); - if (transformed == null) { + if (myLoadingDictionaries.get() || transformed == null) { return true; } - return myLoadingDictionaries.get() || isCorrect(transformed, bundledDictionaries) || isCorrect(transformed, dictionaries); - - + int bundled = isCorrect(transformed, bundledDictionaries); + int user = isCorrect(transformed, dictionaries); + //System.out.println("bundled = " + bundled); + //System.out.println("user = " + user); + return bundled == 0 || user==0 || bundled>0 && user>0; }