From e317447ff14043f507097ab17bacd45c4e75950d Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Mon, 12 Feb 2024 17:33:51 +0100 Subject: [PATCH] do not mutate dictionaryStates GitOrigin-RevId: b7efde66abf4497947a0ca0506852ff394197084 --- .../dictionary/ProjectDictionary.java | 38 +++++++++---------- .../dictionary/UserDictionary.java | 20 ++++------ .../state/ProjectDictionaryState.java | 15 +++++--- 3 files changed, 34 insertions(+), 39 deletions(-) diff --git a/spellchecker/src/com/intellij/spellchecker/dictionary/ProjectDictionary.java b/spellchecker/src/com/intellij/spellchecker/dictionary/ProjectDictionary.java index 89a541670d7e..8071d4ca198a 100644 --- a/spellchecker/src/com/intellij/spellchecker/dictionary/ProjectDictionary.java +++ b/spellchecker/src/com/intellij/spellchecker/dictionary/ProjectDictionary.java @@ -1,4 +1,4 @@ -// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.spellchecker.dictionary; import com.intellij.util.containers.CollectionFactory; @@ -12,7 +12,7 @@ import java.util.Objects; import java.util.Set; public final class ProjectDictionary implements EditableDictionary { - @NonNls private static final String DEFAULT_CURRENT_USER_NAME = "default.user"; + private static final @NonNls String DEFAULT_CURRENT_USER_NAME = "default.user"; private static final String DEFAULT_PROJECT_DICTIONARY_NAME = "project"; private String activeName; private Set dictionaries; @@ -24,9 +24,8 @@ public final class ProjectDictionary implements EditableDictionary { this.dictionaries = dictionaries; } - @NotNull @Override - public String getName() { + public @NotNull String getName() { return DEFAULT_PROJECT_DICTIONARY_NAME; } @@ -35,11 +34,12 @@ public final class ProjectDictionary implements EditableDictionary { } @Override - @Nullable - public Boolean contains(@NotNull String word) { + public @Nullable Boolean contains(@NotNull String word) { if (dictionaries == null) { - return null; // still ("WORD_OF_ENTIRELY_UNKNOWN_LETTERS_FOR_ALL"); + // still ("WORD_OF_ENTIRELY_UNKNOWN_LETTERS_FOR_ALL"); + return null; } + int errors = 0; for (Dictionary dictionary : dictionaries) { Boolean contains = dictionary.contains(word); @@ -50,7 +50,11 @@ public final class ProjectDictionary implements EditableDictionary { return true; } } - if (errors == dictionaries.size()) return null;//("WORD_OF_ENTIRELY_UNKNOWN_LETTERS_FOR_ALL"); + + //("WORD_OF_ENTIRELY_UNKNOWN_LETTERS_FOR_ALL"); + if (errors == dictionaries.size()) { + return null; + } return false; } @@ -64,13 +68,11 @@ public final class ProjectDictionary implements EditableDictionary { getActiveDictionary().removeFromDictionary(word); } - @NotNull - private EditableDictionary getActiveDictionary() { + private @NotNull EditableDictionary getActiveDictionary() { return ensureCurrentUserDictionary(); } - @NotNull - private EditableDictionary ensureCurrentUserDictionary() { + private @NotNull EditableDictionary ensureCurrentUserDictionary() { if (activeName == null) { activeName = DEFAULT_CURRENT_USER_NAME; } @@ -85,8 +87,7 @@ public final class ProjectDictionary implements EditableDictionary { return result; } - @Nullable - private EditableDictionary getDictionaryByName(@NotNull String name) { + private @Nullable EditableDictionary getDictionaryByName(@NotNull String name) { if (dictionaries == null) { return null; } @@ -112,8 +113,7 @@ public final class ProjectDictionary implements EditableDictionary { @Override - @NotNull - public Set getWords() { + public @NotNull Set getWords() { if (dictionaries == null) { return Collections.emptySet(); } @@ -125,8 +125,7 @@ public final class ProjectDictionary implements EditableDictionary { } @Override - @NotNull - public Set getEditableWords() { + public @NotNull Set getEditableWords() { return getActiveDictionary().getWords(); } @@ -161,9 +160,8 @@ public final class ProjectDictionary implements EditableDictionary { return result; } - @NonNls @Override - public String toString() { + public @NonNls String toString() { return "ProjectDictionary{" + "activeName='" + activeName + '\'' + ", dictionaries=" + dictionaries + '}'; } } \ No newline at end of file diff --git a/spellchecker/src/com/intellij/spellchecker/dictionary/UserDictionary.java b/spellchecker/src/com/intellij/spellchecker/dictionary/UserDictionary.java index 3405e99eee71..350584bd7eed 100644 --- a/spellchecker/src/com/intellij/spellchecker/dictionary/UserDictionary.java +++ b/spellchecker/src/com/intellij/spellchecker/dictionary/UserDictionary.java @@ -1,4 +1,4 @@ -// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.spellchecker.dictionary; import com.intellij.util.containers.CollectionFactory; @@ -12,36 +12,31 @@ import java.util.Set; public final class UserDictionary implements EditableDictionary { private final String name; - @NotNull - private final Set words = CollectionFactory.createSmallMemoryFootprintSet(); + private final @NotNull Set words = CollectionFactory.createSmallMemoryFootprintSet(); public UserDictionary(@NotNull String name) { this.name = name; } - @NotNull @Override - public String getName() { + public @NotNull String getName() { return name; } @Override - @Nullable - public Boolean contains(@NotNull String word) { + public @Nullable Boolean contains(@NotNull String word) { boolean contains = words.contains(word); if (contains) return true; return null; } - @NotNull @Override - public Set getWords() { + public @NotNull Set getWords() { return words; } @Override - @NotNull - public Set getEditableWords() { + public @NotNull Set getEditableWords() { return words; } @@ -97,9 +92,8 @@ public final class UserDictionary implements EditableDictionary { return name.hashCode(); } - @NonNls @Override - public String toString() { + public @NonNls String toString() { return "UserDictionary{" + "name='" + name + '\'' + ", words.count=" + words.size() + '}'; } } diff --git a/spellchecker/src/com/intellij/spellchecker/state/ProjectDictionaryState.java b/spellchecker/src/com/intellij/spellchecker/state/ProjectDictionaryState.java index d5bb4eafadae..4921044d56fe 100644 --- a/spellchecker/src/com/intellij/spellchecker/state/ProjectDictionaryState.java +++ b/spellchecker/src/com/intellij/spellchecker/state/ProjectDictionaryState.java @@ -1,4 +1,4 @@ -// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.spellchecker.state; import com.intellij.openapi.components.PersistentStateComponent; @@ -12,6 +12,7 @@ import com.intellij.util.xmlb.annotations.Property; import com.intellij.util.xmlb.annotations.Transient; import com.intellij.util.xmlb.annotations.XCollection; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Unmodifiable; import java.util.ArrayList; import java.util.HashSet; @@ -23,22 +24,23 @@ import java.util.Set; public final class ProjectDictionaryState implements PersistentStateComponent { @Property(surroundWithTag = false) @XCollection(elementTypes = DictionaryState.class) - public List dictionaryStates = new ArrayList<>(); + public @Unmodifiable List dictionaryStates = new ArrayList<>(); private ProjectDictionary projectDictionary; - private final EventDispatcher myDictListenerEventDispatcher = + private final EventDispatcher dictListenerEventDispatcher = EventDispatcher.create(DictionaryStateListener.class); @Transient public void setProjectDictionary(ProjectDictionary projectDictionary) { - dictionaryStates.clear(); + List dictionaryStates = new ArrayList<>(); Set projectDictionaries = projectDictionary.getDictionaries(); if (projectDictionaries != null) { for (EditableDictionary dic : projectDictionary.getDictionaries()) { dictionaryStates.add(new DictionaryState(dic)); } } + this.dictionaryStates = dictionaryStates; } @Transient @@ -66,6 +68,7 @@ public final class ProjectDictionaryState implements PersistentStateComponent dictionaries = new HashSet<>(); + List dictionaryStates = this.dictionaryStates; if (dictionaryStates != null) { for (DictionaryState dictionaryState : dictionaryStates) { dictionaryState.loadState(dictionaryState); @@ -73,7 +76,7 @@ public final class ProjectDictionaryState implements PersistentStateComponent