From 699e3397d9dd2089215fb6eb6271c8c42b78bde4 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Fri, 23 Nov 2018 18:46:42 +0300 Subject: [PATCH] Warn about bad options in usage parsers in tests --- .../findJavaFieldUsages/JKFieldUsages.0.java | 2 +- .../kotlin/findUsages/OptionsParser.kt | 66 ++++++++++++++----- 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/idea/testData/findUsages/java/findJavaFieldUsages/JKFieldUsages.0.java b/idea/testData/findUsages/java/findJavaFieldUsages/JKFieldUsages.0.java index d9a47f7d636..6e90ce2f4d0 100644 --- a/idea/testData/findUsages/java/findJavaFieldUsages/JKFieldUsages.0.java +++ b/idea/testData/findUsages/java/findJavaFieldUsages/JKFieldUsages.0.java @@ -1,5 +1,5 @@ // PSI_ELEMENT: com.intellij.psi.PsiField -// OPTIONS: readAccess +// OPTIONS: skipWrite public class A { public String foo = "foo"; } \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/findUsages/OptionsParser.kt b/idea/tests/org/jetbrains/kotlin/findUsages/OptionsParser.kt index d751e201b9f..96a5cfb3c4a 100644 --- a/idea/tests/org/jetbrains/kotlin/findUsages/OptionsParser.kt +++ b/idea/tests/org/jetbrains/kotlin/findUsages/OptionsParser.kt @@ -42,7 +42,7 @@ internal enum class OptionsParser { "functionUsages" -> isMethodsUsages = true "propertyUsages" -> isFieldsUsages = true "expected" -> searchExpected = true - else -> throw IllegalStateException("Invalid option: " + s) + else -> throw IllegalStateException("Invalid option: $s") } } } @@ -68,7 +68,7 @@ internal enum class OptionsParser { searchExpected = true isUsages = true } - else -> throw IllegalStateException("Invalid option: " + s) + else -> throw IllegalStateException("Invalid option: $s") } } } @@ -86,7 +86,7 @@ internal enum class OptionsParser { "skipRead" -> isReadAccess = false "skipWrite" -> isWriteAccess = false "expected" -> searchExpected = true - else -> throw IllegalStateException("Invalid option: " + s) + else -> throw IllegalStateException("Invalid option: $s") } } } @@ -106,7 +106,7 @@ internal enum class OptionsParser { "implementingClasses" -> isImplementingClasses = true "methodUsages" -> isMethodsUsages = true "fieldUsages" -> isFieldsUsages = true - else -> throw IllegalStateException("Invalid option: " + s) + else -> throw IllegalStateException("Invalid option: $s") } } } @@ -124,7 +124,7 @@ internal enum class OptionsParser { isOverridingMethods = true isImplementingMethods = true } - else -> throw IllegalStateException("Invalid option: " + s) + else -> throw IllegalStateException("Invalid option: $s") } } } @@ -132,17 +132,39 @@ internal enum class OptionsParser { }, JAVA_FIELD { override fun parse(text: String, project: Project): FindUsagesOptions { - return JavaVariableFindUsagesOptions(project) + return JavaVariableFindUsagesOptions(project).apply { + for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) { + if (parseCommonOptions(this, s)) continue + + when (s) { + "skipRead" -> isReadAccess = false + "skipWrite" -> isWriteAccess = false + else -> throw IllegalStateException("Invalid option: `$s`") + } + } + } } }, JAVA_PACKAGE { override fun parse(text: String, project: Project): FindUsagesOptions { - return JavaPackageFindUsagesOptions(project) + return JavaPackageFindUsagesOptions(project).apply { + for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) { + if (parseCommonOptions(this, s)) continue + + throw IllegalStateException("Invalid option: `$s`") + } + } } }, DEFAULT { override fun parse(text: String, project: Project): FindUsagesOptions { - return FindUsagesOptions(project) + return FindUsagesOptions(project).apply { + for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) { + if (parseCommonOptions(this, s)) continue + + throw IllegalStateException("Invalid option: `$s`") + } + } } }; @@ -150,22 +172,32 @@ internal enum class OptionsParser { companion object { protected fun parseCommonOptions(options: JavaFindUsagesOptions, s: String): Boolean { - when (s) { - "usages" -> { - options.isUsages = true - return true - } + if (parseCommonOptions(options as FindUsagesOptions, s)) { + return true + } + + return when (s) { "skipImports" -> { options.isSkipImportStatements = true - return true + true + } + else -> false + } + } + + + protected fun parseCommonOptions(options: FindUsagesOptions, s: String): Boolean { + return when (s) { + "usages" -> { + options.isUsages = true + true } "textOccurrences" -> { options.isSearchForTextOccurrences = true - return true + true } - else -> return false + else -> false } - } fun getParserByPsiElementClass(klass: Class): OptionsParser? {