use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.
the class GrReferenceExpressionImpl method bindToElementViaStaticImport.
@Override
public GrReferenceExpression bindToElementViaStaticImport(@NotNull PsiMember member) {
if (getQualifier() != null) {
throw new IncorrectOperationException("Reference has qualifier");
}
if (StringUtil.isEmpty(getReferenceName())) {
throw new IncorrectOperationException("Reference has empty name");
}
PsiClass containingClass = member.getContainingClass();
if (containingClass == null) {
throw new IncorrectOperationException("Member has no containing class");
}
final PsiFile file = getContainingFile();
if (file instanceof GroovyFile) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
String text = "import static " + containingClass.getQualifiedName() + "." + member.getName();
final GrImportStatement statement = factory.createImportStatementFromText(text);
((GroovyFile) file).addImport(statement);
}
return this;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.
the class GrStubUtils method getAliasMapping.
@NotNull
private static Map<String, String> getAliasMapping(@Nullable PsiFile file) {
if (!(file instanceof GroovyFile))
return Collections.emptyMap();
return CachedValuesManager.getCachedValue(file, () -> {
Map<String, String> mapping = ContainerUtil.newHashMap();
for (GrImportStatement importStatement : ((GroovyFile) file).getImportStatements()) {
if (importStatement.getImportReference() != null && !importStatement.isStatic() && importStatement.isAliasedImport()) {
String importName = importStatement.getImportReference().getClassNameText();
String importedName = importStatement.getImportedName();
if (importedName != null) {
mapping.put(importedName, importName);
}
}
}
return CachedValueProvider.Result.create(mapping, file);
});
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.
the class GroovyCodeFragmentFactory method stripImports.
private static String stripImports(String text, GroovyFile toEval) {
GrImportStatement[] imports = toEval.getImportStatements();
for (int i = imports.length - 1; i >= 0; i--) {
TextRange range = imports[i].getTextRange();
text = text.substring(0, range.getStartOffset()) + text.substring(range.getEndOffset(), text.length());
}
return StringUtil.escapeStringCharacters(text);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.
the class GroovyReferenceCopyPasteProcessor method addReferenceData.
@Override
protected void addReferenceData(PsiFile file, int startOffset, PsiElement element, ArrayList<ReferenceData> to) {
if (element instanceof GrReferenceElement) {
if (((GrReferenceElement) element).getQualifier() == null) {
final GroovyResolveResult resolveResult = ((GrReferenceElement) element).advancedResolve();
final PsiElement refElement = resolveResult.getElement();
if (refElement != null) {
if (refElement instanceof PsiClass) {
if (refElement.getContainingFile() != element.getContainingFile()) {
final String qName = ((PsiClass) refElement).getQualifiedName();
if (qName != null) {
addReferenceData(element, to, startOffset, qName, null);
}
}
} else if (resolveResult.getCurrentFileResolveContext() instanceof GrImportStatement && ((GrImportStatement) resolveResult.getCurrentFileResolveContext()).isStatic()) {
final String classQName = ((PsiMember) refElement).getContainingClass().getQualifiedName();
final String name = ((PsiNamedElement) refElement).getName();
if (classQName != null && name != null) {
addReferenceData(element, to, startOffset, classQName, name);
}
}
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.
the class GroovyExtractMethodHandler method findConflicts.
private static boolean findConflicts(InitialInfo info) {
//new ConflictsDialog()
final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
final PsiElement declarationOwner = info.getContext().getParent();
GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
super.visitReferenceExpression(referenceExpression);
GroovyResolveResult resolveResult = referenceExpression.advancedResolve();
PsiElement resolveContext = resolveResult.getCurrentFileResolveContext();
if (resolveContext != null && !(resolveContext instanceof GrImportStatement) && !PsiTreeUtil.isAncestor(declarationOwner, resolveContext, true) && !skipResult(resolveResult)) {
conflicts.putValue(referenceExpression, GroovyRefactoringBundle.message("ref.0.will.not.be.resolved.outside.of.current.context", referenceExpression.getText()));
}
}
//skip 'print' and 'println'
private boolean skipResult(GroovyResolveResult result) {
PsiElement element = result.getElement();
if (element instanceof PsiMethod) {
String name = ((PsiMethod) element).getName();
if (!name.startsWith("print"))
return false;
if (element instanceof GrGdkMethod)
element = ((GrGdkMethod) element).getStaticMethod();
PsiClass aClass = ((PsiMethod) element).getContainingClass();
if (aClass != null) {
String qname = aClass.getQualifiedName();
return GroovyCommonClassNames.DEFAULT_GROOVY_METHODS.equals(qname);
}
}
return false;
}
};
GrStatement[] statements = info.getStatements();
for (GrStatement statement : statements) {
statement.accept(visitor);
}
if (conflicts.isEmpty())
return false;
if (ApplicationManager.getApplication().isUnitTestMode()) {
throw new BaseRefactoringProcessor.ConflictsInTestsException(conflicts.values());
}
ConflictsDialog dialog = new ConflictsDialog(info.getProject(), conflicts);
dialog.show();
return !dialog.isOK();
}
Aggregations