use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.
the class GroovyCodeStyleManagerImpl method addLineFeedBefore.
protected void addLineFeedBefore(@NotNull PsiElement psiFile, @NotNull GrImportStatement result) {
final CodeStyleSettings commonSettings = CodeStyleSettingsManager.getInstance(psiFile.getProject()).getCurrentSettings();
final GroovyCodeStyleSettings settings = commonSettings.getCustomSettings(GroovyCodeStyleSettings.class);
final PackageEntryTable layoutTable = settings.IMPORT_LAYOUT_TABLE;
final PackageEntry[] entries = layoutTable.getEntries();
PsiElement prev = result.getPrevSibling();
while (PsiImplUtil.isWhiteSpaceOrNls(prev)) {
prev = prev.getPrevSibling();
}
if (PsiImplUtil.hasElementType(prev, GroovyTokenTypes.mSEMI))
prev = prev.getPrevSibling();
if (PsiImplUtil.isWhiteSpaceOrNls(prev))
prev = prev.getPrevSibling();
ASTNode node = psiFile.getNode();
if (prev instanceof GrImportStatement) {
final int idx_before = getPackageEntryIdx(entries, (GrImportStatement) prev);
final int idx = getPackageEntryIdx(entries, result);
final int spaceCount = getMaxSpaceCount(entries, idx_before, idx);
//skip space and semicolon after import
if (PsiImplUtil.isWhiteSpaceOrNls(prev.getNextSibling()) && PsiImplUtil.hasElementType(prev.getNextSibling().getNextSibling(), GroovyTokenTypes.mSEMI))
prev = prev.getNextSibling().getNextSibling();
while (PsiImplUtil.isWhiteSpaceOrNls(prev.getNextSibling())) {
node.removeChild(prev.getNextSibling().getNode());
}
node.addLeaf(GroovyTokenTypes.mNLS, StringUtil.repeat("\n", spaceCount + 1), result.getNode());
} else if (prev instanceof GrPackageDefinition) {
node.addLeaf(GroovyTokenTypes.mNLS, StringUtil.repeat("\n", commonSettings.BLANK_LINES_AFTER_PACKAGE), result.getNode());
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.
the class GroovyCodeStyleManagerImpl method getAnchorToInsertImportAfter.
@Nullable
private PsiElement getAnchorToInsertImportAfter(@NotNull GroovyFile psiFile, @NotNull GrImportStatement statement) {
final GroovyCodeStyleSettings settings = CodeStyleSettingsManager.getInstance(psiFile.getProject()).getCurrentSettings().getCustomSettings(GroovyCodeStyleSettings.class);
final PackageEntryTable layoutTable = settings.IMPORT_LAYOUT_TABLE;
final PackageEntry[] entries = layoutTable.getEntries();
GrImportStatement[] importStatements = psiFile.getImportStatements();
if (importStatements.length == 0) {
final GrPackageDefinition definition = psiFile.getPackageDefinition();
if (definition != null) {
return definition;
}
return getShellComment(psiFile);
}
final Comparator<GrImportStatement> comparator = GroovyImportOptimizer.getComparator(settings);
final int idx = getPackageEntryIdx(entries, statement);
PsiElement anchor = null;
for (GrImportStatement importStatement : importStatements) {
final int i = getPackageEntryIdx(entries, importStatement);
if (i < idx) {
anchor = importStatement;
} else if (i > idx) {
break;
} else if (comparator.compare(statement, importStatement) > 0) {
anchor = importStatement;
} else {
break;
}
}
if (anchor == null)
anchor = psiFile.getPackageDefinition();
if (anchor == null)
anchor = getShellComment(psiFile);
if (anchor == null && importStatements.length > 0)
anchor = importStatements[0].getPrevSibling();
return anchor;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.
the class GrAliasImportIntention method createTemplateImport.
private static GrImportStatement createTemplateImport(Project project, GrImportStatement context, PsiMember resolved, GroovyFileBase file) {
final PsiClass aClass = resolved.getContainingClass();
assert aClass != null;
String qname = aClass.getQualifiedName();
final String name = resolved.getName();
GrImportStatement template = GroovyPsiElementFactory.getInstance(project).createImportStatementFromText("import static " + qname + "." + name + " as aliased");
return file.addImport(template);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.
the class GrAliasImportIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
GrImportStatement context;
final PsiMember resolved;
if (element instanceof GrReferenceExpression) {
GrReferenceExpression ref = (GrReferenceExpression) element;
GroovyResolveResult result = ref.advancedResolve();
context = (GrImportStatement) result.getCurrentFileResolveContext();
assert context != null;
resolved = (PsiMember) result.getElement();
} else if (element instanceof GrImportStatement) {
context = (GrImportStatement) element;
resolved = (PsiMember) context.getImportReference().resolve();
} else {
return;
}
assert resolved != null;
doRefactoring(project, context, resolved);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.
the class GrAliasImportIntention method getElementPredicate.
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement element) {
if (element instanceof GrReferenceExpression) {
GroovyResolveResult result = ((GrReferenceExpression) element).advancedResolve();
PsiElement context = result.getCurrentFileResolveContext();
if (!(context instanceof GrImportStatement))
return false;
GrImportStatement importStatement = (GrImportStatement) context;
if (!importStatement.isStatic() || importStatement.isAliasedImport())
return false;
return true;
} else if (element instanceof GrImportStatement) {
final GrImportStatement importStatement = (GrImportStatement) element;
if (!importStatement.isStatic())
return false;
if (importStatement.isOnDemand())
return false;
if (importStatement.isAliasedImport())
return false;
return true;
}
return false;
}
};
}
Aggregations