[groovy] imports: update contributor API

This commit is contained in:
Daniil Ovchinnikov
2017-11-27 19:40:36 +03:00
parent ab790a07a2
commit abdb9be673
9 changed files with 66 additions and 52 deletions
@@ -7,8 +7,9 @@ import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
import org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase;
import org.jetbrains.plugins.groovy.lang.resolve.GrImportContributor;
import org.jetbrains.plugins.groovy.lang.resolve.ImportType;
import org.jetbrains.plugins.groovy.lang.resolve.imports.GroovyFileImports;
import org.jetbrains.plugins.groovy.lang.resolve.imports.GroovyImports;
import org.jetbrains.plugins.groovy.lang.resolve.imports.StarImport;
import java.util.LinkedHashSet;
@@ -43,11 +44,10 @@ public class GroovyImportHelper {
final LinkedHashSet<String> result = new LinkedHashSet<>();
ContainerUtil.addAll(result, GroovyFileBase.IMPLICITLY_IMPORTED_PACKAGES);
for (GrImportContributor contributor : GrImportContributor.EP_NAME.getExtensions()) {
result.addAll(ContainerUtil.mapNotNull(
contributor.getImports(file),
i -> i.getType() == ImportType.STAR ? i.getName() : null
));
final GroovyFileImports fileImports = GroovyImports.getImports(file);
for (StarImport starImport : fileImports.getStarImports()) {
if (!fileImports.isImplicit(starImport)) continue;
result.add(starImport.getPackageFqn());
}
return result;
@@ -1,29 +1,38 @@
/*
* Copyright 2000-2016 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.
*/
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.groovy.lang.resolve;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
import org.jetbrains.plugins.groovy.lang.resolve.imports.*;
import java.util.Collection;
import java.util.List;
public interface GrImportContributor {
@Deprecated
public interface GrImportContributor extends org.jetbrains.plugins.groovy.lang.resolve.imports.GrImportContributor {
ExtensionPointName<GrImportContributor> EP_NAME = ExtensionPointName.create("org.intellij.groovy.importContributor");
@NotNull
@Override
default List<GroovyImport> getFileImports(@NotNull GroovyFile file) {
return ContainerUtil.mapNotNull(getImports(file), it -> {
switch (it.getType()) {
case REGULAR:
return new RegularImport(it.getName());
case STATIC:
String classFqn = StringUtil.getPackageName(it.getName());
String memberName = StringUtil.getShortName(it.getName());
return new StaticImport(classFqn, memberName);
case STAR:
return new StarImport(it.getName());
case STATIC_STAR:
return new StaticStarImport(it.getName());
default:
return null;
}
});
}
@NotNull
Collection<Import> getImports(@NotNull GroovyFile file);
@@ -2,12 +2,18 @@
package org.jetbrains.plugins.groovy.lang.resolve
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile
import org.jetbrains.plugins.groovy.lang.resolve.imports.GrImportContributor
import org.jetbrains.plugins.groovy.lang.resolve.imports.GroovyImport
import org.jetbrains.plugins.groovy.lang.resolve.imports.StarImport
@Deprecated("see org.jetbrains.plugins.groovy.lang.resolve.imports.GroovyImport")
class Import(
val name: String,
val type: ImportType = ImportType.REGULAR
val name: String,
@Suppress("DEPRECATION")
val type: ImportType = ImportType.REGULAR
)
@Deprecated("see org.jetbrains.plugins.groovy.lang.resolve.imports.GroovyImport")
enum class ImportType {
/**
* Class
@@ -31,9 +37,5 @@ abstract class GrImportContributorBase : GrImportContributor {
abstract fun appendImplicitlyImportedPackages(file: GroovyFile): List<String>
final override fun getImports(file: GroovyFile): Collection<Import> {
return appendImplicitlyImportedPackages(file).map {
Import(it, ImportType.STAR)
}
}
final override fun getFileImports(file: GroovyFile): List<GroovyImport> = appendImplicitlyImportedPackages(file).map(::StarImport)
}
@@ -0,0 +1,16 @@
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.groovy.lang.resolve.imports;
import com.intellij.openapi.extensions.ExtensionPointName;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
import java.util.List;
public interface GrImportContributor {
ExtensionPointName<GrImportContributor> EP_NAME = ExtensionPointName.create("org.intellij.groovy.importContributor");
@NotNull
List<GroovyImport> getFileImports(@NotNull GroovyFile file);
}
@@ -28,6 +28,8 @@ interface GroovyFileImports {
fun processDefaultImports(processor: PsiScopeProcessor, state: ResolveState, place: PsiElement): Boolean
fun isImplicit(import: GroovyImport): Boolean
fun findUnneecessaryStatements(): Collection<GrImportStatement>
fun findUnresolvedStatements(names: Collection<String>): Collection<GrImportStatement>
@@ -63,6 +63,8 @@ internal class GroovyFileImportsImpl(
return defaultImports.doProcess(processor, state, place)
}
override fun isImplicit(import: GroovyImport): Boolean = !importToStatement.containsKey(import)
override fun findUnneecessaryStatements(): Collection<GrImportStatement> {
return statementToImport.filterValues { it.isUnnecessary(this) }.keys
}
@@ -1,13 +1,9 @@
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.groovy.lang.resolve.imports.impl
import com.intellij.openapi.util.text.StringUtil.getPackageName
import com.intellij.openapi.util.text.StringUtil.getShortName
import com.intellij.util.reverse
import org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase
import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement
import org.jetbrains.plugins.groovy.lang.resolve.Import
import org.jetbrains.plugins.groovy.lang.resolve.ImportType
import org.jetbrains.plugins.groovy.lang.resolve.imports.*
class GroovyImportCollector(private val file: GroovyFileBase) {
@@ -50,7 +46,7 @@ class GroovyImportCollector(private val file: GroovyFileBase) {
getMap(ImportKind.StaticStar)[import.classFqn] = import
}
private fun addImport(import: GroovyImport) = when (import) {
fun addImport(import: GroovyImport) = when (import) {
is RegularImport -> addRegularImport(import)
is StaticImport -> addStaticImport(import)
is StarImport -> addStarImport(import)
@@ -60,8 +56,6 @@ class GroovyImportCollector(private val file: GroovyFileBase) {
fun addRegularImport(classFqn: String, name: String) = addRegularImport(RegularImport(classFqn, name))
fun addStaticImport(classFqn: String, memberName: String) = addStaticImport(classFqn, memberName, memberName)
fun addStaticImport(classFqn: String, memberName: String, name: String) = addStaticImport(StaticImport(classFqn, memberName, name))
fun addStarImport(packageFqn: String) = addStarImport(StarImport(packageFqn))
@@ -74,16 +68,6 @@ class GroovyImportCollector(private val file: GroovyFileBase) {
addImport(import)
}
internal fun addImportFromContributor(contributedImport: Import) {
val name = contributedImport.name
when (contributedImport.type) {
ImportType.REGULAR -> addRegularImport(name, getShortName(name))
ImportType.STATIC -> addStaticImport(getPackageName(name), getShortName(name))
ImportType.STAR -> addStarImport(name)
ImportType.STATIC_STAR -> addStaticStarImport(name)
}
}
fun build(): GroovyFileImports = GroovyFileImportsImpl(
file,
imports.mapValues { (_, map) -> map.values.toList() }.toMap(),
@@ -11,7 +11,6 @@ import org.jetbrains.plugins.groovy.lang.psi.GroovyFile
import org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase
import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement
import org.jetbrains.plugins.groovy.lang.psi.util.ErrorUtil
import org.jetbrains.plugins.groovy.lang.resolve.GrImportContributor
import org.jetbrains.plugins.groovy.lang.resolve.imports.impl.GroovyImportCollector
import org.jetbrains.plugins.groovy.lang.resolve.imports.impl.RegularImportHashingStrategy
import org.jetbrains.plugins.groovy.lang.resolve.imports.impl.StarImportHashingStrategy
@@ -38,7 +37,7 @@ private fun GroovyFile.doGetImports(): GroovyFileImports {
}
for (contributor in GrImportContributor.EP_NAME.extensions) {
contributor.getImports(this).forEach(collector::addImportFromContributor)
contributor.getFileImports(this).forEach(collector::addImport)
}
return collector.build()
+1 -1
View File
@@ -33,7 +33,7 @@
<extensionPoints>
<extensionPoint name="methodComparator" interface="org.jetbrains.plugins.groovy.lang.resolve.GrMethodComparator"/>
<extensionPoint name="membersContributor" interface="org.jetbrains.plugins.groovy.lang.resolve.NonCodeMembersContributor"/>
<extensionPoint name="importContributor" interface="org.jetbrains.plugins.groovy.lang.resolve.GrImportContributor"/>
<extensionPoint name="importContributor" interface="org.jetbrains.plugins.groovy.lang.resolve.imports.GrImportContributor"/>
<extensionPoint name="astTransformationSupport" interface="org.jetbrains.plugins.groovy.transformations.AstTransformationSupport"/>
<extensionPoint name="renameHelper" interface="org.jetbrains.plugins.groovy.refactoring.rename.GrRenameHelper"/>
<extensionPoint name="closureMissingMethodContributor"