From 88673fb03f498cceac67feb3ca4f72d1fc584802 Mon Sep 17 00:00:00 2001 From: Olga Strizhenko Date: Mon, 25 Dec 2017 15:02:06 +0300 Subject: [PATCH] Spellchecker: settings transfer from folders to files --- .../settings/SpellCheckerSettings.java | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/spellchecker/src/com/intellij/spellchecker/settings/SpellCheckerSettings.java b/spellchecker/src/com/intellij/spellchecker/settings/SpellCheckerSettings.java index 4f2578268221..91389203bf80 100644 --- a/spellchecker/src/com/intellij/spellchecker/settings/SpellCheckerSettings.java +++ b/spellchecker/src/com/intellij/spellchecker/settings/SpellCheckerSettings.java @@ -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 { 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 { private static final String BUNDLED_DICTIONARY_ATTR_NAME = "BundledDictionary"; // Paths + private List myOldDictionaryFoldersPaths = new ArrayList<>(); private List myCustomDictionariesPaths = new ArrayList<>(); private Set myDisabledDictionariesPaths = new HashSet<>(); @@ -77,6 +82,7 @@ public class SpellCheckerSettings implements PersistentStateComponent { @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 { 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 { 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 { 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++) {