From baa75fc8d5c05c91f4d839106faeb389f4259010 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Tue, 11 Apr 2017 20:16:36 +0300 Subject: [PATCH] Refactoring: move OptionsParser to separate file --- .../findUsages/AbstractFindUsagesTest.kt | 161 ---------------- .../kotlin/findUsages/OptionsParser.kt | 179 ++++++++++++++++++ 2 files changed, 179 insertions(+), 161 deletions(-) create mode 100644 idea/tests/org/jetbrains/kotlin/findUsages/OptionsParser.kt diff --git a/idea/tests/org/jetbrains/kotlin/findUsages/AbstractFindUsagesTest.kt b/idea/tests/org/jetbrains/kotlin/findUsages/AbstractFindUsagesTest.kt index c5331e6d273..160170999e1 100644 --- a/idea/tests/org/jetbrains/kotlin/findUsages/AbstractFindUsagesTest.kt +++ b/idea/tests/org/jetbrains/kotlin/findUsages/AbstractFindUsagesTest.kt @@ -25,7 +25,6 @@ import com.intellij.lang.properties.psi.PropertiesFile import com.intellij.lang.properties.psi.Property import com.intellij.openapi.editor.markup.TextAttributes import com.intellij.openapi.extensions.Extensions -import com.intellij.openapi.project.Project import com.intellij.openapi.util.io.FileUtil import com.intellij.openapi.util.io.FileUtilRt import com.intellij.psi.* @@ -41,17 +40,12 @@ import com.intellij.usages.impl.rules.UsageTypeProvider import com.intellij.usages.rules.UsageFilteringRule import com.intellij.usages.rules.UsageGroupingRule import com.intellij.util.CommonProcessors -import org.jetbrains.kotlin.idea.findUsages.KotlinClassFindUsagesOptions -import org.jetbrains.kotlin.idea.findUsages.KotlinFindUsagesHandlerFactory -import org.jetbrains.kotlin.idea.findUsages.KotlinFunctionFindUsagesOptions -import org.jetbrains.kotlin.idea.findUsages.KotlinPropertyFindUsagesOptions import org.jetbrains.kotlin.idea.search.usagesSearch.ExpressionsOfTypeProcessor import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor import org.jetbrains.kotlin.idea.test.PluginTestCaseBase import org.jetbrains.kotlin.idea.test.TestFixtureExtension import org.jetbrains.kotlin.idea.util.ProjectRootsUtil -import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType import org.jetbrains.kotlin.test.InTextDirectivesUtils import org.jetbrains.kotlin.test.KotlinTestUtils @@ -60,161 +54,6 @@ import java.util.* abstract class AbstractFindUsagesTest : KotlinLightCodeInsightFixtureTestCase() { - protected enum class OptionsParser { - CLASS { - override fun parse(text: String, project: Project): FindUsagesOptions { - return KotlinClassFindUsagesOptions(project).apply { - isUsages = false - isSearchForTextOccurrences = false - searchConstructorUsages = false - for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) { - if (parseCommonOptions(this, s)) continue - - when (s) { - "constructorUsages" -> searchConstructorUsages = true - "derivedInterfaces" -> isDerivedInterfaces = true - "derivedClasses" -> isDerivedClasses = true - "functionUsages" -> isMethodsUsages = true - "propertyUsages" -> isFieldsUsages = true - else -> fail("Invalid option: " + s) - } - } - } - } - }, - FUNCTION { - override fun parse(text: String, project: Project): FindUsagesOptions { - return KotlinFunctionFindUsagesOptions(project).apply { - isUsages = false - for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) { - if (parseCommonOptions(this, s)) continue - - when (s) { - "overrides" -> { - isOverridingMethods = true - isImplementingMethods = true - } - "overloadUsages" -> { - isIncludeOverloadUsages = true - isUsages = true - } - else -> fail("Invalid option: " + s) - } - } - } - } - }, - PROPERTY { - override fun parse(text: String, project: Project): FindUsagesOptions { - return KotlinPropertyFindUsagesOptions(project).apply { - isUsages = false - for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) { - if (parseCommonOptions(this, s)) continue - - when (s) { - "overrides" -> searchOverrides = true - "skipRead" -> isReadAccess = false - "skipWrite" -> isWriteAccess = false - else -> fail("Invalid option: " + s) - } - } - } - } - }, - JAVA_CLASS { - override fun parse(text: String, project: Project): FindUsagesOptions { - return KotlinClassFindUsagesOptions(project).apply { - isUsages = false - searchConstructorUsages = false - for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) { - if (parseCommonOptions(this, s)) continue - - when (s) { - "derivedInterfaces" -> isDerivedInterfaces = true - "derivedClasses" -> isDerivedClasses = true - "implementingClasses" -> isImplementingClasses = true - "methodUsages" -> isMethodsUsages = true - "fieldUsages" -> isFieldsUsages = true - else -> fail("Invalid option: " + s) - } - } - } - } - }, - JAVA_METHOD { - override fun parse(text: String, project: Project): FindUsagesOptions { - return JavaMethodFindUsagesOptions(project).apply { - isUsages = false - for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) { - if (parseCommonOptions(this, s)) continue - - when (s) { - "overrides" -> { - isOverridingMethods = true - isImplementingMethods = true - } - else -> fail("Invalid option: " + s) - } - } - } - } - }, - JAVA_FIELD { - override fun parse(text: String, project: Project): FindUsagesOptions { - return JavaVariableFindUsagesOptions(project) - } - }, - JAVA_PACKAGE { - override fun parse(text: String, project: Project): FindUsagesOptions { - return JavaPackageFindUsagesOptions(project) - } - }, - DEFAULT { - override fun parse(text: String, project: Project): FindUsagesOptions { - return FindUsagesOptions(project) - } - }; - - abstract fun parse(text: String, project: Project): FindUsagesOptions - - companion object { - - protected fun parseCommonOptions(options: JavaFindUsagesOptions, s: String): Boolean { - when (s) { - "usages" -> { - options.isUsages = true - return true - } - "skipImports" -> { - options.isSkipImportStatements = true - return true - } - "textOccurrences" -> { - options.isSearchForTextOccurrences = true - return true - } - else -> return false - } - - } - - fun getParserByPsiElementClass(klass: Class): OptionsParser? { - return when (klass) { - KtNamedFunction::class.java -> FUNCTION - KtProperty::class.java, KtParameter::class.java -> PROPERTY - KtClass::class.java -> CLASS - PsiMethod::class.java -> JAVA_METHOD - PsiClass::class.java -> JAVA_CLASS - PsiField::class.java -> JAVA_FIELD - PsiPackage::class.java -> JAVA_PACKAGE - KtTypeParameter::class.java -> DEFAULT - else -> null - } - - } - } - } - override fun getProjectDescriptor(): LightProjectDescriptor = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE public override fun setUp() { diff --git a/idea/tests/org/jetbrains/kotlin/findUsages/OptionsParser.kt b/idea/tests/org/jetbrains/kotlin/findUsages/OptionsParser.kt new file mode 100644 index 00000000000..6316ed79d25 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/findUsages/OptionsParser.kt @@ -0,0 +1,179 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * 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 org.jetbrains.kotlin.findUsages + +import com.intellij.find.findUsages.* +import com.intellij.openapi.project.Project +import com.intellij.psi.* +import org.jetbrains.kotlin.idea.findUsages.KotlinClassFindUsagesOptions +import org.jetbrains.kotlin.idea.findUsages.KotlinFunctionFindUsagesOptions +import org.jetbrains.kotlin.idea.findUsages.KotlinPropertyFindUsagesOptions +import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.test.InTextDirectivesUtils + +internal enum class OptionsParser { + CLASS { + override fun parse(text: String, project: Project): FindUsagesOptions { + return KotlinClassFindUsagesOptions(project).apply { + isUsages = false + isSearchForTextOccurrences = false + searchConstructorUsages = false + for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) { + if (parseCommonOptions(this, s)) continue + + when (s) { + "constructorUsages" -> searchConstructorUsages = true + "derivedInterfaces" -> isDerivedInterfaces = true + "derivedClasses" -> isDerivedClasses = true + "functionUsages" -> isMethodsUsages = true + "propertyUsages" -> isFieldsUsages = true + else -> IllegalStateException("Invalid option: " + s) + } + } + } + } + }, + FUNCTION { + override fun parse(text: String, project: Project): FindUsagesOptions { + return KotlinFunctionFindUsagesOptions(project).apply { + isUsages = false + for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) { + if (parseCommonOptions(this, s)) continue + + when (s) { + "overrides" -> { + isOverridingMethods = true + isImplementingMethods = true + } + "overloadUsages" -> { + isIncludeOverloadUsages = true + isUsages = true + } + else -> IllegalStateException("Invalid option: " + s) + } + } + } + } + }, + PROPERTY { + override fun parse(text: String, project: Project): FindUsagesOptions { + return KotlinPropertyFindUsagesOptions(project).apply { + isUsages = false + for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) { + if (parseCommonOptions(this, s)) continue + + when (s) { + "overrides" -> searchOverrides = true + "skipRead" -> isReadAccess = false + "skipWrite" -> isWriteAccess = false + else -> IllegalStateException("Invalid option: " + s) + } + } + } + } + }, + JAVA_CLASS { + override fun parse(text: String, project: Project): FindUsagesOptions { + return KotlinClassFindUsagesOptions(project).apply { + isUsages = false + searchConstructorUsages = false + for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) { + if (parseCommonOptions(this, s)) continue + + when (s) { + "derivedInterfaces" -> isDerivedInterfaces = true + "derivedClasses" -> isDerivedClasses = true + "implementingClasses" -> isImplementingClasses = true + "methodUsages" -> isMethodsUsages = true + "fieldUsages" -> isFieldsUsages = true + else -> IllegalStateException("Invalid option: " + s) + } + } + } + } + }, + JAVA_METHOD { + override fun parse(text: String, project: Project): FindUsagesOptions { + return JavaMethodFindUsagesOptions(project).apply { + isUsages = false + for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) { + if (parseCommonOptions(this, s)) continue + + when (s) { + "overrides" -> { + isOverridingMethods = true + isImplementingMethods = true + } + else -> IllegalStateException("Invalid option: " + s) + } + } + } + } + }, + JAVA_FIELD { + override fun parse(text: String, project: Project): FindUsagesOptions { + return JavaVariableFindUsagesOptions(project) + } + }, + JAVA_PACKAGE { + override fun parse(text: String, project: Project): FindUsagesOptions { + return JavaPackageFindUsagesOptions(project) + } + }, + DEFAULT { + override fun parse(text: String, project: Project): FindUsagesOptions { + return FindUsagesOptions(project) + } + }; + + abstract fun parse(text: String, project: Project): FindUsagesOptions + + companion object { + protected fun parseCommonOptions(options: JavaFindUsagesOptions, s: String): Boolean { + when (s) { + "usages" -> { + options.isUsages = true + return true + } + "skipImports" -> { + options.isSkipImportStatements = true + return true + } + "textOccurrences" -> { + options.isSearchForTextOccurrences = true + return true + } + else -> return false + } + + } + + fun getParserByPsiElementClass(klass: Class): OptionsParser? { + return when (klass) { + KtNamedFunction::class.java -> FUNCTION + KtProperty::class.java, KtParameter::class.java -> PROPERTY + KtClass::class.java -> CLASS + PsiMethod::class.java -> JAVA_METHOD + PsiClass::class.java -> JAVA_CLASS + PsiField::class.java -> JAVA_FIELD + PsiPackage::class.java -> JAVA_PACKAGE + KtTypeParameter::class.java -> DEFAULT + else -> null + } + } + } +} \ No newline at end of file