<commit_before>/*
 * Copyright 2017, 2018 <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 io.github.trubitsyn.visiblefortesting.intention

import com.intellij.codeInsight.CodeInsightBundle
import com.intellij.codeInsight.intention.BaseElementAtCaretIntentionAction
import com.intellij.ide.projectView.impl.ProjectRootsUtil
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.*
import com.intellij.psi.util.PsiUtil
import com.intellij.util.IncorrectOperationException
import io.github.trubitsyn.visiblefortesting.annotable.PsiAnnotableUtil
import io.github.trubitsyn.visiblefortesting.annotation.AnnotationTypes
import io.github.trubitsyn.visiblefortesting.annotation.base.AnnotationType
import io.github.trubitsyn.visiblefortesting.ui.ChooseAnnotationTypePopup
import org.jetbrains.annotations.NonNls

class AnnotateMethodFromTestIntention : BaseElementAtCaretIntentionAction() {
    var availableAnnotationTypes: List<AnnotationType> = emptyList()

    @NonNls
    override fun getFamilyName() = CodeInsightBundle.message("intention.add.annotation.family")

    override fun isAvailable(project: Project, editor: Editor, element: PsiElement): Boolean {

        if (!ProjectRootsUtil.isInTestSource(element.containingFile)) {
            return false
        }

        if (isMethodCallExpression(element)) {
            val call = element.parent.parent as PsiMethodCallExpression
            val method = call.resolveMethod() ?: return false
            val javaFile = element.containingFile as PsiJavaFile
            val currentPackage = JavaPsiFacade
                    .getInstance(project)
                    .findPackage(javaFile.packageName) ?: return false

            if (!PsiUtil.isAccessibleFromPackage(method, currentPackage)) {
                if (availableAnnotationTypes.isEmpty()) {
                    availableAnnotationTypes = AnnotationTypes.available(project)
                }

                if (availableAnnotationTypes.isEmpty()) {
                    return false
                }

                if (AnnotationTypes.areApplicableTo(method, availableAnnotationTypes)) {
                    text = "Annotate '${method.containingClass?.name}.${method.name}' as @VisibleForTesting"
                    return true
                }
            }
        }<commit_msg>Invert if condition<commit_after>/*
 * Copyright 2017, 2018 <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 io.github.trubitsyn.visiblefortesting.intention

import com.intellij.codeInsight.CodeInsightBundle
import com.intellij.codeInsight.intention.BaseElementAtCaretIntentionAction
import com.intellij.ide.projectView.impl.ProjectRootsUtil
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.*
import com.intellij.psi.util.PsiUtil
import com.intellij.util.IncorrectOperationException
import io.github.trubitsyn.visiblefortesting.annotable.PsiAnnotableUtil
import io.github.trubitsyn.visiblefortesting.annotation.AnnotationTypes
import io.github.trubitsyn.visiblefortesting.annotation.base.AnnotationType
import io.github.trubitsyn.visiblefortesting.ui.ChooseAnnotationTypePopup
import org.jetbrains.annotations.NonNls

class AnnotateMethodFromTestIntention : BaseElementAtCaretIntentionAction() {
    var availableAnnotationTypes: List<AnnotationType> = emptyList()

    @NonNls
    override fun getFamilyName() = CodeInsightBundle.message("intention.add.annotation.family")

    override fun isAvailable(project: Project, editor: Editor, element: PsiElement): Boolean {

        if (!ProjectRootsUtil.isInTestSource(element.containingFile)) {
            return false
        }

        if (isMethodCallExpression(element)) {
            val call = element.parent.parent as PsiMethodCallExpression
            val method = call.resolveMethod() ?: return false
            val javaFile = element.containingFile as PsiJavaFile
            val currentPackage = JavaPsiFacade
                    .getInstance(project)
                    .findPackage(javaFile.packageName) ?: return false

            if (PsiUtil.isAccessibleFromPackage(method, currentPackage)) {
                return false
            }

            if (availableAnnotationTypes.isEmpty()) {
                availableAnnotationTypes = AnnotationTypes.available(project)
            }

            if (availableAnnotationTypes.isEmpty()) {
                return false
            }

            if (AnnotationTypes.areApplicableTo(method, availableAnnotationTypes)) {
                text = "Annotate '${method.containingClass?.name}.${method.name}' as @VisibleForTesting"
                return true
            }
        }
{'fraction_non_alphanumeric': 0.07785677128489683, 'fraction_numerical': 0.0041616091555401425, 'mean_word_length': 3.2411764705882353, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '264851', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1458, 'n_words': 366}