<commit_before>/*
 * Copyright (C) 2017 <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.
 */
@file:Suppress("PackageName")

package uk.co.reecedunn.intellij.plugin.xquery.inspections.xpath.XPST0003

import com.intellij.codeInspection.*
import com.intellij.openapi.util.Pair
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.util.SmartList
import uk.co.reecedunn.intellij.plugin.core.sequences.walkTree
import uk.co.reecedunn.intellij.plugin.xpath.ast.xpath.XPathEQName
import uk.co.reecedunn.intellij.plugin.xpath.ast.xpath.XPathFunctionCall
import uk.co.reecedunn.intellij.plugin.xpath.ast.xpath.XPathNamedFunctionRef
import uk.co.reecedunn.intellij.plugin.xquery.ast.xquery.XQueryFunctionDecl
import uk.co.reecedunn.intellij.plugin.xquery.ast.xquery.XQueryModule
import uk.co.reecedunn.intellij.plugin.xquery.lang.MarkLogic
import uk.co.reecedunn.intellij.plugin.xquery.lang.Scripting
import uk.co.reecedunn.intellij.plugin.xquery.lang.XQuery
import uk.co.reecedunn.intellij.plugin.xquery.lexer.IXQueryKeywordOrNCNameType
import uk.co.reecedunn.intellij.plugin.xquery.parser.XQueryElementType
import uk.co.reecedunn.intellij.plugin.xquery.resources.XQueryBundle
import uk.co.reecedunn.intellij.plugin.xquery.settings.XQueryProjectSettings

class ReservedFunctionNameInspection : LocalInspectionTool() {
    private fun getLocalName(name: XPathEQName?): Pair<PsiElement, IXQueryKeywordOrNCNameType.KeywordType>? {
        if (name != null && name.node.elementType === XQueryElementType.NCNAME) {
            val localname = name.firstChild
            val type = localname.node.elementType
            if (type is IXQueryKeywordOrNCNameType) {
                return Pair(localname, type.keywordType)
            }
        }
        return null
    }

    override fun getDisplayName(): String =
        XQueryBundle.message("inspection.XPST0003.reserved-function-name.display-name")

    override fun getDescriptionFileName(): String? =
            "$id.html"

    override fun checkFile(file: PsiFile, manager: InspectionManager, isOnTheFly: Boolean): Array<ProblemDescriptor>? {
        if (file !is XQueryModule) return null

        val settings = XQueryProjectSettings.getInstance(file.getProject())
        val product = settings.product
        val productVersion = settings.productVersion

        val descriptors = SmartList<ProblemDescriptor>()
        file.walkTree().forEach { element ->
            val localname = when {
                element is XPathFunctionCall -> getLocalName(element.functionName)
                element is XQueryFunctionDecl -> getLocalName(element.functionName)
                element is XPathNamedFunctionRef -> getLocalName(element.functionName)
                else -> null
            }
            when (localname?.second) {
                IXQueryKeywordOrNCNameType.KeywordType.MARKLOGIC70_RESERVED_FUNCTION_NAME -> {
                    if (product.conformsTo(productVersion, MarkLogic.VERSION_7_0)) {
                        val description = XQueryBundle.message("inspection.XPST0003.reserved-function-name.message", MarkLogic.VERSION_7_0)
                        descriptors.add(manager.createProblemDescriptor(localname.first, description, null as LocalQuickFix?, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly))
                    }
                }
                IXQueryKeywordOrNCNameType.KeywordType.MARKLOGIC80_RESERVED_FUNCTION_NAME -> {
                    if (product.conformsTo(productVersion, MarkLogic.VERSION_8_0)) {
                        val description = XQueryBundle.message("inspection.XPST0003.reserved-function-name.message", MarkLogic.VERSION_8_0)
                        descriptors.add(manager.createProblemDescriptor(localname.first, description, null as LocalQuickFix?, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly))
                    }
                }
                IXQueryKeywordOrNCNameType.KeywordType.SCRIPTING10_RESERVED_FUNCTION_NAME -> {
                    if (product.conformsTo(productVersion, Scripting.NOTE_1_0_20140918)) {
                        val description = XQueryBundle.message("inspection.XPST0003.reserved-function-name.message", Scripting.NOTE_1_0_20140918)
                        descriptors.add(manager.createProblemDescriptor(localname.first, description, null as LocalQuickFix?, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly))
                    }<commit_msg>Simplify when clause.<commit_after>/*
 * Copyright (C) 2017 <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.
 */
@file:Suppress("PackageName")

package uk.co.reecedunn.intellij.plugin.xquery.inspections.xpath.XPST0003

import com.intellij.codeInspection.*
import com.intellij.openapi.util.Pair
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.util.SmartList
import uk.co.reecedunn.intellij.plugin.core.sequences.walkTree
import uk.co.reecedunn.intellij.plugin.xpath.ast.xpath.XPathEQName
import uk.co.reecedunn.intellij.plugin.xpath.ast.xpath.XPathFunctionCall
import uk.co.reecedunn.intellij.plugin.xpath.ast.xpath.XPathNamedFunctionRef
import uk.co.reecedunn.intellij.plugin.xquery.ast.xquery.XQueryFunctionDecl
import uk.co.reecedunn.intellij.plugin.xquery.ast.xquery.XQueryModule
import uk.co.reecedunn.intellij.plugin.xquery.lang.MarkLogic
import uk.co.reecedunn.intellij.plugin.xquery.lang.Scripting
import uk.co.reecedunn.intellij.plugin.xquery.lang.XQuery
import uk.co.reecedunn.intellij.plugin.xquery.lexer.IXQueryKeywordOrNCNameType
import uk.co.reecedunn.intellij.plugin.xquery.parser.XQueryElementType
import uk.co.reecedunn.intellij.plugin.xquery.resources.XQueryBundle
import uk.co.reecedunn.intellij.plugin.xquery.settings.XQueryProjectSettings

class ReservedFunctionNameInspection : LocalInspectionTool() {
    private fun getLocalName(name: XPathEQName?): Pair<PsiElement, IXQueryKeywordOrNCNameType.KeywordType>? {
        if (name != null && name.node.elementType === XQueryElementType.NCNAME) {
            val localname = name.firstChild
            val type = localname.node.elementType
            if (type is IXQueryKeywordOrNCNameType) {
                return Pair(localname, type.keywordType)
            }
        }
        return null
    }

    override fun getDisplayName(): String =
        XQueryBundle.message("inspection.XPST0003.reserved-function-name.display-name")

    override fun getDescriptionFileName(): String? =
            "$id.html"

    override fun checkFile(file: PsiFile, manager: InspectionManager, isOnTheFly: Boolean): Array<ProblemDescriptor>? {
        if (file !is XQueryModule) return null

        val settings = XQueryProjectSettings.getInstance(file.getProject())
        val product = settings.product
        val productVersion = settings.productVersion

        val descriptors = SmartList<ProblemDescriptor>()
        file.walkTree().forEach { element ->
            val localname = when (element) {
                is XPathFunctionCall -> getLocalName(element.functionName)
                is XQueryFunctionDecl -> getLocalName(element.functionName)
                is XPathNamedFunctionRef -> getLocalName(element.functionName)
                else -> null
            }
            when (localname?.second) {
                IXQueryKeywordOrNCNameType.KeywordType.MARKLOGIC70_RESERVED_FUNCTION_NAME -> {
                    if (product.conformsTo(productVersion, MarkLogic.VERSION_7_0)) {
                        val description = XQueryBundle.message("inspection.XPST0003.reserved-function-name.message", MarkLogic.VERSION_7_0)
                        descriptors.add(manager.createProblemDescriptor(localname.first, description, null as LocalQuickFix?, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly))
                    }
                }
                IXQueryKeywordOrNCNameType.KeywordType.MARKLOGIC80_RESERVED_FUNCTION_NAME -> {
                    if (product.conformsTo(productVersion, MarkLogic.VERSION_8_0)) {
                        val description = XQueryBundle.message("inspection.XPST0003.reserved-function-name.message", MarkLogic.VERSION_8_0)
                        descriptors.add(manager.createProblemDescriptor(localname.first, description, null as LocalQuickFix?, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly))
                    }
                }
                IXQueryKeywordOrNCNameType.KeywordType.SCRIPTING10_RESERVED_FUNCTION_NAME -> {
                    if (product.conformsTo(productVersion, Scripting.NOTE_1_0_20140918)) {
                        val description = XQueryBundle.message("inspection.XPST0003.reserved-function-name.message", Scripting.NOTE_1_0_20140918)
                        descriptors.add(manager.createProblemDescriptor(localname.first, description, null as LocalQuickFix?, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly))
                    }
{'fraction_non_alphanumeric': 0.08635630794034664, 'fraction_numerical': 0.012494961708988311, 'mean_word_length': 3.2414529914529915, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '4561370', 'n_tokens_mistral': 2933, 'n_tokens_neox': 2785, 'n_words': 508}