use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.
the class GroovyImportOptimizerRefactoringHelper method performOperation.
@Override
public void performOperation(final Project project, final Set<GroovyFile> files) {
final ProgressManager progressManager = ProgressManager.getInstance();
final Map<GroovyFile, Pair<List<GrImportStatement>, Set<GrImportStatement>>> redundants = new HashMap<>();
final Runnable findUnusedImports = () -> {
final ProgressIndicator progressIndicator = progressManager.getProgressIndicator();
final int total = files.size();
int i = 0;
for (final GroovyFile file : files) {
if (!file.isValid())
continue;
final VirtualFile virtualFile = file.getVirtualFile();
if (!ProjectRootManager.getInstance(project).getFileIndex().isInSource(virtualFile)) {
continue;
}
if (progressIndicator != null) {
progressIndicator.setText2(virtualFile.getPresentableUrl());
progressIndicator.setFraction((double) i++ / total);
}
ApplicationManager.getApplication().runReadAction(() -> {
final Set<GrImportStatement> usedImports = GroovyImportUtil.findUsedImports(file);
final List<GrImportStatement> validImports = PsiUtil.getValidImportStatements(file);
redundants.put(file, Pair.create(validImports, usedImports));
});
}
};
if (!progressManager.runProcessWithProgressSynchronously(findUnusedImports, "Optimizing imports (Groovy) ... ", false, project)) {
return;
}
WriteAction.run(() -> {
for (GroovyFile groovyFile : redundants.keySet()) {
if (!groovyFile.isValid())
continue;
final Pair<List<GrImportStatement>, Set<GrImportStatement>> pair = redundants.get(groovyFile);
final List<GrImportStatement> validImports = pair.getFirst();
final Set<GrImportStatement> usedImports = pair.getSecond();
for (GrImportStatement importStatement : validImports) {
if (!usedImports.contains(importStatement)) {
groovyFile.removeImport(importStatement);
}
}
}
});
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.
the class GroovyRefactoringSupportProvider method isMemberInplaceRenameAvailable.
@Override
public boolean isMemberInplaceRenameAvailable(@NotNull PsiElement element, @Nullable PsiElement context) {
if (context == null || context.getContainingFile() instanceof GroovyFile)
return false;
PsiElement parent = context.getParent();
//don't try to inplace rename aliased imported references
if (parent instanceof GrReferenceElement) {
GroovyResolveResult result = ((GrReferenceElement) parent).advancedResolve();
PsiElement fileResolveContext = result.getCurrentFileResolveContext();
if (fileResolveContext instanceof GrImportStatement && ((GrImportStatement) fileResolveContext).isAliasedImport()) {
return false;
}
}
return element instanceof GrMember;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.
the class GroovyCompletionData method suggestPrimitiveTypes.
private static boolean suggestPrimitiveTypes(PsiElement context) {
if (isInfixOperatorPosition(context))
return false;
if (isAfterForParameter(context))
return false;
final PsiElement parent = context.getParent();
if (parent == null)
return false;
PsiElement previous = PsiImplUtil.realPrevious(parent.getPrevSibling());
if (parent instanceof GrReferenceElement && parent.getParent() instanceof GrArgumentList) {
PsiElement prevSibling = context.getPrevSibling();
if (prevSibling != null && prevSibling.getNode() != null) {
if (!TokenSets.DOTS.contains(prevSibling.getNode().getElementType())) {
return true;
}
} else if (!(previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType()))) {
return true;
}
}
if (GroovyCompletionUtil.isTupleVarNameWithoutTypeDeclared(context))
return true;
if (previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType())) {
return false;
}
if (GroovyCompletionUtil.asSimpleVariable(context) || GroovyCompletionUtil.asTypedMethod(context) || GroovyCompletionUtil.asVariableInBlock(context) || asVariableAfterModifiers(context)) {
return true;
}
if ((parent instanceof GrParameter && ((GrParameter) parent).getTypeElementGroovy() == null) || parent instanceof GrReferenceElement && !(parent.getParent() instanceof GrImportStatement) && !(parent.getParent() instanceof GrPackageDefinition) && !(parent.getParent() instanceof GrArgumentList)) {
PsiElement prevSibling = context.getPrevSibling();
if (parent instanceof GrReferenceElement && prevSibling != null && prevSibling.getNode() != null) {
ASTNode node = prevSibling.getNode();
return !TokenSets.DOTS.contains(node.getElementType());
} else {
return true;
}
}
if (PsiImplUtil.realPrevious(parent.getPrevSibling()) instanceof GrModifierList) {
return true;
}
if (PsiImplUtil.realPrevious(context.getPrevSibling()) instanceof GrModifierList) {
return true;
}
return parent instanceof GrExpression && parent.getParent() instanceof GroovyFile && GroovyCompletionUtil.isNewStatement(context, false);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.
the class GroovyCompletionUtil method createLookupElements.
public static List<? extends LookupElement> createLookupElements(@NotNull GroovyResolveResult candidate, boolean afterNew, @NotNull PrefixMatcher matcher, @Nullable PsiElement position) {
final PsiElement element = candidate.getElement();
final PsiElement context = candidate.getCurrentFileResolveContext();
if (context instanceof GrImportStatement && element != null) {
if (element instanceof PsiPackage) {
return Collections.emptyList();
}
final String importedName = ((GrImportStatement) context).getImportedName();
if (importedName != null) {
if (!(matcher.prefixMatches(importedName) || element instanceof PsiMethod && getterMatches(matcher, (PsiMethod) element, importedName) || element instanceof PsiMethod && setterMatches(matcher, (PsiMethod) element, importedName))) {
return Collections.emptyList();
}
final GrCodeReferenceElement importReference = ((GrImportStatement) context).getImportReference();
if (importReference != null) {
boolean alias = ((GrImportStatement) context).isAliasedImport();
for (GroovyResolveResult r : importReference.multiResolve(false)) {
final PsiElement resolved = r.getElement();
if (context.getManager().areElementsEquivalent(resolved, element) && (alias || !(element instanceof PsiClass))) {
return generateLookupForImportedElement(candidate, importedName);
} else {
if (resolved instanceof PsiField && element instanceof PsiMethod && GroovyPropertyUtils.isAccessorFor((PsiMethod) element, (PsiField) resolved)) {
return generateLookupForImportedElement(candidate, GroovyPropertyUtils.getAccessorPrefix((PsiMethod) element) + GroovyPropertyUtils.capitalize(importedName));
}
}
}
}
}
}
String name = element instanceof PsiNamedElement ? ((PsiNamedElement) element).getName() : element.getText();
if (name == null || !matcher.prefixMatches(name)) {
return Collections.emptyList();
}
if (element instanceof PsiClass) {
return JavaClassNameCompletionContributor.createClassLookupItems((PsiClass) element, afterNew, new GroovyClassNameInsertHandler(), Conditions.<PsiClass>alwaysTrue());
}
LookupElementBuilder builder = LookupElementBuilder.create(element instanceof PsiPackage ? element : candidate, name);
return Arrays.asList(setupLookupBuilder(element, candidate.getSubstitutor(), builder, position));
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.
the class GroovyFindJarFix method getFqns.
@Override
protected Collection<String> getFqns(@NotNull GrReferenceElement ref) {
GrImportStatement importStatement = PsiTreeUtil.getParentOfType(ref.getElement(), GrImportStatement.class);
//from static imports
if (importStatement != null) {
GrCodeReferenceElement reference = importStatement.getImportReference();
if (reference != null) {
return Collections.singleton(reference.getText());
}
return Collections.emptyList();
}
if (ref.getQualifier() != null)
return Collections.emptyList();
final String className = ref.getReferenceName();
if (className == null)
return Collections.emptyList();
PsiFile file = ref.getContainingFile().getOriginalFile();
if (!(file instanceof GroovyFile))
return Collections.emptyList();
GrImportStatement[] importList = ((GroovyFile) file).getImportStatements();
for (GrImportStatement imp : importList) {
if (className.equals(imp.getImportedName())) {
GrCodeReferenceElement importReference = imp.getImportReference();
if (importReference == null)
return Collections.emptyList();
return Collections.singleton(importReference.getText());
}
}
return Collections.emptyList();
}
Aggregations