Spellchecker: settings transfer from folders to files

This commit is contained in:
Olga Strizhenko
2017-12-25 15:14:34 +03:00
parent bef0db8b70
commit 88673fb03f
@@ -17,11 +17,13 @@ package com.intellij.spellchecker.settings;
import com.intellij.openapi.components.*;
import com.intellij.openapi.project.Project;
import com.intellij.spellchecker.util.SPFileUtil;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import static com.intellij.openapi.util.io.FileUtilRt.extensionEquals;
import static com.intellij.openapi.util.text.StringUtil.parseInt;
@State(name = "SpellCheckerSettings", storages = @Storage(StoragePathMacros.WORKSPACE_FILE))
@@ -31,6 +33,8 @@ public class SpellCheckerSettings implements PersistentStateComponent<Element> {
private static final String FOLDERS_ATTR_NAME = "Folders";
private static final String FOLDER_ATTR_NAME = "Folder";
private static final String CUSTOM_DICTIONARIES_ATTR_NAME = "CustomDictionaries";
private static final String CUSTOM_DICTIONARY_ATTR_NAME = "CustomDictionary";
private static final String DICTIONARIES_ATTR_NAME = "Dictionaries";
private static final String DICTIONARY_ATTR_NAME = "Dictionary";
@@ -38,6 +42,7 @@ public class SpellCheckerSettings implements PersistentStateComponent<Element> {
private static final String BUNDLED_DICTIONARY_ATTR_NAME = "BundledDictionary";
// Paths
private List<String> myOldDictionaryFoldersPaths = new ArrayList<>();
private List<String> myCustomDictionariesPaths = new ArrayList<>();
private Set<String> myDisabledDictionariesPaths = new HashSet<>();
@@ -77,6 +82,7 @@ public class SpellCheckerSettings implements PersistentStateComponent<Element> {
@SuppressWarnings({"ConstantConditions"})
public Element getState() {
if (myBundledDisabledDictionariesPaths.isEmpty() &&
myOldDictionaryFoldersPaths.isEmpty() &&
myCustomDictionariesPaths.isEmpty() &&
myDisabledDictionariesPaths.isEmpty()) {
return null;
@@ -92,9 +98,15 @@ public class SpellCheckerSettings implements PersistentStateComponent<Element> {
i++;
}
// user
element.setAttribute(FOLDERS_ATTR_NAME, String.valueOf(myCustomDictionariesPaths.size()));
// remove old dictionary folder settings
element.removeAttribute(FOLDERS_ATTR_NAME);
for (int j = 0; j < myOldDictionaryFoldersPaths.size(); j++) {
element.removeAttribute(FOLDER_ATTR_NAME + j);
}
// store new dictionaries settings instead
element.setAttribute(CUSTOM_DICTIONARIES_ATTR_NAME, String.valueOf(myCustomDictionariesPaths.size()));
for (int j = 0; j < myCustomDictionariesPaths.size(); j++) {
element.setAttribute(FOLDER_ATTR_NAME + j, myCustomDictionariesPaths.get(j));
element.setAttribute(CUSTOM_DICTIONARY_ATTR_NAME + j, myCustomDictionariesPaths.get(j));
}
element.setAttribute(DICTIONARIES_ATTR_NAME, String.valueOf(myDisabledDictionariesPaths.size()));
iterator = myDisabledDictionariesPaths.iterator();
@@ -112,6 +124,7 @@ public class SpellCheckerSettings implements PersistentStateComponent<Element> {
public void loadState(@NotNull final Element element) {
myBundledDisabledDictionariesPaths.clear();
myCustomDictionariesPaths.clear();
myOldDictionaryFoldersPaths.clear();
myDisabledDictionariesPaths.clear();
try {
// bundled
@@ -120,9 +133,20 @@ public class SpellCheckerSettings implements PersistentStateComponent<Element> {
myBundledDisabledDictionariesPaths.add(element.getAttributeValue(BUNDLED_DICTIONARY_ATTR_NAME + i));
}
// user
// cover old dictionary folders settings
final int foldersSize = parseInt(element.getAttributeValue(FOLDERS_ATTR_NAME), 0);
for (int i = 0; i < foldersSize; i++) {
myCustomDictionariesPaths.add(element.getAttributeValue(FOLDER_ATTR_NAME + i));
myOldDictionaryFoldersPaths.add(element.getAttributeValue(FOLDER_ATTR_NAME + i));
}
myOldDictionaryFoldersPaths.forEach(folder -> SPFileUtil.processFilesRecursively(folder, file -> {
if(extensionEquals(file, "dic")){
myCustomDictionariesPaths.add(file);
}
}));
// cover new dictionaries settings
final int customDictSize = parseInt(element.getAttributeValue(CUSTOM_DICTIONARIES_ATTR_NAME), 0);
for (int i = 0; i < customDictSize; i++) {
myCustomDictionariesPaths.add(element.getAttributeValue(CUSTOM_DICTIONARY_ATTR_NAME + i));
}
final int scriptsSize = parseInt(element.getAttributeValue(DICTIONARIES_ATTR_NAME), 0);
for (int i = 0; i < scriptsSize; i++) {