[groovy] add method to get a package name and a short name in single call

This commit is contained in:
Daniil Ovchinnikov
2017-05-15 15:55:22 +03:00
parent 33e2f7b479
commit 37dea50130
2 changed files with 32 additions and 5 deletions
@@ -16,24 +16,22 @@
package org.jetbrains.plugins.groovy.lang.psi.impl.javaView
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.PsiClass
import com.intellij.psi.impl.light.LightElement
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.plugins.groovy.transformations.isUnderTransformation
import org.jetbrains.plugins.groovy.util.getPackageAndShortName
class GroovyLightInnerClassFinder(project: Project) : GroovyClassFinder(project) {
override fun findClasses(qualifiedName: String, scope: GlobalSearchScope): Array<out PsiClass> {
val containingClassFqn = StringUtil.getPackageName(qualifiedName)
val innerClassName = StringUtil.getShortName(qualifiedName)
val (containingClassFqn, innerClassName) = getPackageAndShortName(qualifiedName)
return super.findClasses(containingClassFqn, scope).mapNotNull { findInnerLightClass(it, innerClassName) }.toTypedArray()
}
override fun findClass(qualifiedName: String, scope: GlobalSearchScope): PsiClass? {
val containingClassFqn = StringUtil.getPackageName(qualifiedName)
val (containingClassFqn, innerClassName) = getPackageAndShortName(qualifiedName)
val containingClass = super.findClass(containingClassFqn, scope) ?: return null
val innerClassName = StringUtil.getShortName(qualifiedName)
return findInnerLightClass(containingClass, innerClassName)
}
@@ -0,0 +1,29 @@
/*
* 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 org.jetbrains.plugins.groovy.util
/**
* Splits fully qualified name into whatever goes before the [delimiter] (package name) and a short name.
*
* Examples:
* - `"foo.bar.Baz" -> ("foo.bar", "Baz")`
* - `"foo.bar.Outer.Inner" -> ("foo.bar.Outer", "Inner")`
* - `"MyClassInDefaultPackage" -> ("", "MyClassInDefaultPackage")`
*/
fun getPackageAndShortName(fqn: String, delimiter: String = "."): Pair<String, String> {
val index = fqn.lastIndexOf(delimiter)
return if (index >= 0) fqn.substring(0, index) to fqn.substring(index + 1) else "" to fqn
}