From fc04b8d1ba0b2ec9a84c2998e5ec4544bd55df87 Mon Sep 17 00:00:00 2001 From: "Ilya.Kazakevich" Date: Wed, 18 Jan 2017 01:45:58 +0300 Subject: [PATCH] New test runners: findElement renamed to toElement and moved to QualifiedNameExt according to review IDEA-CR-17341 --- .../jetbrains/extenstions/QualifiedNameExt.kt | 56 +++++++++++++++++++ .../universalTests/PyUniversalTests.kt | 40 ++----------- 2 files changed, 61 insertions(+), 35 deletions(-) create mode 100644 python/src/com/jetbrains/extenstions/QualifiedNameExt.kt diff --git a/python/src/com/jetbrains/extenstions/QualifiedNameExt.kt b/python/src/com/jetbrains/extenstions/QualifiedNameExt.kt new file mode 100644 index 000000000000..51dbfa17128f --- /dev/null +++ b/python/src/com/jetbrains/extenstions/QualifiedNameExt.kt @@ -0,0 +1,56 @@ +/* + * Copyright 2000-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 com.jetbrains.extenstions + +import com.intellij.openapi.module.Module +import com.intellij.psi.PsiElement +import com.intellij.psi.util.QualifiedName +import com.jetbrains.python.psi.PyClass +import com.jetbrains.python.psi.impl.PyPsiFacadeImpl +import com.jetbrains.python.psi.types.TypeEvalContext + +/** + * Resolves qname of any symbol to appropriate PSI element. + */ +fun QualifiedName.toElement(module: Module, context: TypeEvalContext): PsiElement? { + val facade = PyPsiFacadeImpl.getInstance(module.project) + var currentName = this + + + var element: PsiElement? = null + + // Drill as deep, as we can + var lastElement: String? = null + while (currentName.componentCount > 0 && element == null) { + + element = facade.qualifiedNameResolver(currentName).fromModule(module).withMembers().firstResult() + if (element != null) { + break + } + lastElement = this.lastComponent!! + currentName = this.removeLastComponent() + } + + if (lastElement != null && element is PyClass) { + // Drill in class + val method = element.findMethodByName(lastElement, true, context) + if (method != null) { + return method + } + + } + return element +} \ No newline at end of file diff --git a/python/src/com/jetbrains/python/testing/universalTests/PyUniversalTests.kt b/python/src/com/jetbrains/python/testing/universalTests/PyUniversalTests.kt index daa2330370f0..9ab06ea0b0f0 100644 --- a/python/src/com/jetbrains/python/testing/universalTests/PyUniversalTests.kt +++ b/python/src/com/jetbrains/python/testing/universalTests/PyUniversalTests.kt @@ -39,11 +39,14 @@ import com.intellij.openapi.util.JDOMExternalizerUtil import com.intellij.openapi.util.Pair import com.intellij.openapi.util.Ref import com.intellij.openapi.util.registry.Registry +import com.intellij.openapi.util.registry.RegistryValue import com.intellij.psi.PsiDirectory import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile import com.intellij.psi.search.GlobalSearchScope import com.intellij.psi.util.QualifiedName +import com.jetbrains.extenstions.asList +import com.jetbrains.extenstions.toElement import com.jetbrains.python.psi.PyClass import com.jetbrains.python.psi.PyFile import com.jetbrains.python.psi.PyFunction @@ -74,39 +77,6 @@ fun isUniversalModeEnabled(): Boolean = Registry.`is`("python.tests.enableUniver internal fun getAdditionalArgumentsPropertyName() = PyUniversalTestConfiguration::additionalArguments.name -/** - * Resolves qname of any symbol to appropriate PSI element. - */ -private fun findElementByQualifiedName(name: QualifiedName, module: Module, context: TypeEvalContext): PsiElement? { - val facade = PyPsiFacadeImpl.getInstance(module.project) - var currentName = name - - - var element: PsiElement? = null - - // Drill as deep, as we can - var lastElement: String? = null - while (currentName.componentCount > 0 && element == null) { - - element = facade.qualifiedNameResolver(currentName).fromModule(module).withMembers().firstResult() - if (element != null) { - break - } - lastElement = name.lastComponent!! - currentName = name.removeLastComponent() - } - - if (lastElement != null && element is PyClass) { - // Drill in class - val method = element.findMethodByName(lastElement, true, context) - if (method != null) { - return method - } - - } - return element -} - /** * @return factory chosen by user in "test runner" settings */ @@ -123,8 +93,8 @@ private object PyUniversalTestsLocator : SMTestLocator { if (scope !is ModuleWithDependenciesScope) { return ArrayList() } - val result = findElementByQualifiedName(QualifiedName.fromDottedString(path), scope.module, - TypeEvalContext.userInitiated(project, null)) + val result = QualifiedName.fromDottedString(path).toElement(scope.module, + TypeEvalContext.userInitiated(project, null)) if (result != null) { return arrayListOf(PsiLocation.fromPsiElement(result)) }