<commit_before>/*
 * Copyright (C) 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.psi.shadow

import com.intellij.openapi.extensions.ExtensionPointName
import com.intellij.psi.PsiElement

interface XpmShadowPsiElementFactory {
    companion object {
        val EP_NAME: ExtensionPointName<XpmShadowPsiElementFactoryBean> = ExtensionPointName.create(
            "uk.co.reecedunn.intellij.shadowPsiElementFactory"
        )<commit_msg>Implement an XpmShadowPsiElementFactory#create helper method.<commit_after>/*
 * Copyright (C) 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.psi.shadow

import com.intellij.openapi.extensions.ExtensionPointName
import com.intellij.openapi.util.Key
import com.intellij.psi.PsiElement
import com.intellij.psi.xml.XmlTag
import uk.co.reecedunn.intellij.plugin.core.xml.qname
import javax.xml.namespace.QName

interface XpmShadowPsiElementFactory {
    companion object {
        val EP_NAME: ExtensionPointName<XpmShadowPsiElementFactoryBean> = ExtensionPointName.create(
            "uk.co.reecedunn.intellij.shadowPsiElementFactory"
        )

        private val SHADOW_PSI_ELEMENT: Key<Pair<QName?, XpmShadowPsiElement>> = Key.create("SHADOW_PSI_ELEMENT")

        fun create(element: PsiElement): XpmShadowPsiElement? {
            val name = (element as? XmlTag)?.qname()

            element.getUserData(SHADOW_PSI_ELEMENT)?.let {
                if (it.first == name && it.second.isValid) {
                    return it.second
                }
            }

            return EP_NAME.extensionList.asSequence().map { it.getInstance().create(element) }.firstOrNull()?.let {
                element.putUserData(SHADOW_PSI_ELEMENT, name to it)
                it
            }
        }
{'fraction_non_alphanumeric': 0.08560447239692523, 'fraction_numerical': 0.005590496156533892, 'mean_word_length': 3.364329268292683, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 3}, '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': '6821404', 'n_tokens_mistral': 817, 'n_tokens_neox': 795, 'n_words': 239}