<commit_before>/*
 * Copyright (C) 2019-2020 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.reecedunn.intellij.plugin.xpm.module.loader

import com.intellij.javaee.ExternalResourceManagerEx
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.components.*
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiElement
import com.intellij.util.xmlb.XmlSerializerUtil
import uk.co.reecedunn.intellij.plugin.core.data.CacheableProperty
import uk.co.reecedunn.intellij.plugin.core.progress.TaskManager
import uk.co.reecedunn.intellij.plugin.xpm.intellij.resources.XpmBundle
import uk.co.reecedunn.intellij.plugin.xdm.context.XstContext
import uk.co.reecedunn.intellij.plugin.xpm.module.path.XpmModulePath
import uk.co.reecedunn.intellij.plugin.xpm.module.path.paths
import uk.co.reecedunn.intellij.plugin.xdm.types.XsAnyUriValue
import uk.co.reecedunn.intellij.plugin.xdm.types.element
import uk.co.reecedunn.intellij.plugin.xpm.lang.XpmVendorType

data class XpmModuleLoaderSettingsData(
    var loaders: List<XpmModuleLoaderBean> = arrayListOf(
        XpmModuleLoaderBean("java", null),
        XpmModuleLoaderBean("module", "java:source"),
        XpmModuleLoaderBean("module", "java:test-source"),
        XpmModuleLoaderBean("relative", null)
    ),
    var databasePath: String = "",
    var registerSchemaFile: Boolean = true
)

@State(name = "XIJPModuleLoaderSettings", storages = [Storage(StoragePathMacros.WORKSPACE_FILE)])
class XpmModuleLoaderSettings(val project: Project) : XpmModuleLoader, PersistentStateComponent<XpmModuleLoaderSettingsData> {
    // region Settings :: Module Loaders

    private val loaders = CacheableProperty { data.loaders.mapNotNull { it.loader } }

    private fun registerModulePath(path: String) {
        if (data.loaders.find { it.name == "fixed" && it.context == path } == null) {
            (data.loaders as ArrayList).add(XpmModuleLoaderBean("fixed", path))
            loaders.invalidate()
        }
    }

    private fun unregisterModulePath(path: String) {
        data.loaders.find { it.name == "fixed" && it.context == path }?.let {
            (data.loaders as ArrayList).remove(it)
            loaders.invalidate()
        }
    }

    // endregion
    // region Settings :: Schema Files<commit_msg>Move the loader beans property out of the module loader settings data.<commit_after>/*
 * Copyright (C) 2019-2020 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.reecedunn.intellij.plugin.xpm.module.loader

import com.intellij.javaee.ExternalResourceManagerEx
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.components.*
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiElement
import com.intellij.util.xmlb.XmlSerializerUtil
import uk.co.reecedunn.intellij.plugin.core.data.CacheableProperty
import uk.co.reecedunn.intellij.plugin.core.progress.TaskManager
import uk.co.reecedunn.intellij.plugin.xpm.intellij.resources.XpmBundle
import uk.co.reecedunn.intellij.plugin.xdm.context.XstContext
import uk.co.reecedunn.intellij.plugin.xpm.module.path.XpmModulePath
import uk.co.reecedunn.intellij.plugin.xpm.module.path.paths
import uk.co.reecedunn.intellij.plugin.xdm.types.XsAnyUriValue
import uk.co.reecedunn.intellij.plugin.xdm.types.element
import uk.co.reecedunn.intellij.plugin.xpm.lang.XpmVendorType

data class XpmModuleLoaderSettingsData(
    var databasePath: String = "",
    var registerSchemaFile: Boolean = true
)

@State(name = "XIJPModuleLoaderSettings", storages = [Storage(StoragePathMacros.WORKSPACE_FILE)])
class XpmModuleLoaderSettings(val project: Project) : XpmModuleLoader, PersistentStateComponent<XpmModuleLoaderSettingsData> {
    // region Settings :: Module Loaders

    private val loaderBeans: List<XpmModuleLoaderBean> = arrayListOf(
        XpmModuleLoaderBean("java", null),
        XpmModuleLoaderBean("module", "java:source"),
        XpmModuleLoaderBean("module", "java:test-source"),
        XpmModuleLoaderBean("relative", null)
    )

    private val loaders = CacheableProperty { loaderBeans.mapNotNull { it.loader } }

    private fun registerModulePath(path: String) {
        if (loaderBeans.find { it.name == "fixed" && it.context == path } == null) {
            (loaderBeans as ArrayList).add(XpmModuleLoaderBean("fixed", path))
            loaders.invalidate()
        }
    }

    private fun unregisterModulePath(path: String) {
        loaderBeans.find { it.name == "fixed" && it.context == path }?.let {
            (loaderBeans as ArrayList).remove(it)
            loaders.invalidate()
        }
    }

    // endregion
    // region Settings :: Schema Files
{'fraction_non_alphanumeric': 0.10351232691658224, 'fraction_numerical': 0.004052684903748734, 'mean_word_length': 4.1325823223570195, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2775077', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1772, 'n_words': 403}