use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.
the class GrPackageInspection method buildVisitor.
@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
return new BaseInspectionVisitor() {
@Override
public void visitFile(@NotNull GroovyFileBase file) {
if (!(file instanceof GroovyFile))
return;
if (!myCheckScripts && file.isScript())
return;
String expectedPackage = ExpectedPackageNameProviderKt.inferExpectedPackageName((GroovyFile) file);
String actual = file.getPackageName();
if (!expectedPackage.equals(actual)) {
PsiElement toHighlight = getElementToHighlight((GroovyFile) file);
if (toHighlight == null)
return;
registerError(toHighlight, "Package name mismatch. Actual: '" + actual + "', expected: '" + expectedPackage + "'", new LocalQuickFix[] { new ChangePackageQuickFix(expectedPackage), GroovyQuickFixFactory.getInstance().createGrMoveToDirFix(actual) }, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.
the class UnnecessaryQualifiedReferenceInspection method canBeSimplified.
private static boolean canBeSimplified(PsiElement element) {
if (PsiTreeUtil.getParentOfType(element, PsiComment.class) != null)
return false;
if (element instanceof GrCodeReferenceElement) {
if (PsiTreeUtil.getParentOfType(element, GrImportStatement.class, GrPackageDefinition.class) != null)
return false;
} else if (element instanceof GrReferenceExpression) {
if (!PsiImplUtil.seemsToBeQualifiedClassName((GrReferenceExpression) element))
return false;
} else {
return false;
}
final GrReferenceElement ref = (GrReferenceElement) element;
if (ref.getQualifier() == null)
return false;
if (!(ref.getContainingFile() instanceof GroovyFileBase))
return false;
final PsiElement resolved = ref.resolve();
if (!(resolved instanceof PsiClass))
return false;
final String name = ((PsiClass) resolved).getName();
if (name == null)
return false;
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(element.getProject());
final GrReferenceExpression shortedRef = factory.createReferenceExpressionFromText(name, element);
final GroovyResolveResult resolveResult = shortedRef.advancedResolve();
if (element.getManager().areElementsEquivalent(resolved, resolveResult.getElement())) {
return true;
}
final PsiClass containingClass = ((PsiClass) resolved).getContainingClass();
if (containingClass != null && !GroovyCodeStyleSettingsFacade.getInstance(containingClass.getProject()).insertInnerClassImports()) {
return false;
}
return resolveResult.getElement() == null || !resolveResult.isAccessible() || !resolveResult.isStaticsOK();
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.
the class GroovyPositionManager method findEnclosingTypeDefinition.
@Nullable
private static PsiClass findEnclosingTypeDefinition(SourcePosition position) {
PsiFile file = position.getFile();
if (!(file instanceof GroovyFileBase))
return null;
PsiElement element = file.findElementAt(position.getOffset());
while (true) {
element = PsiTreeUtil.getParentOfType(element, GrTypeDefinition.class, GroovyFileBase.class);
if (element instanceof GroovyFileBase) {
return ((GroovyFileBase) element).getScriptClass();
} else if (element instanceof GrTypeDefinition && !((GrTypeDefinition) element).isAnonymous()) {
return (GrTypeDefinition) element;
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.
the class GroovyStatementMover method getElementToMove.
@Nullable
private static GroovyPsiElement getElementToMove(GroovyFileBase file, int offset) {
offset = CharArrayUtil.shiftForward(file.getText(), offset, " \t");
PsiElement element = file.findElementAt(offset);
final GrDocComment docComment = PsiTreeUtil.getParentOfType(element, GrDocComment.class);
if (docComment != null) {
final GrDocCommentOwner owner = docComment.getOwner();
if (owner != null) {
element = owner;
}
}
if (element instanceof PsiComment) {
element = PsiTreeUtil.nextVisibleLeaf(element);
}
return (GroovyPsiElement) PsiTreeUtil.findFirstParent(element, element11 -> isMoveable(element11));
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.
the class GroovyStatementMover method allRanges.
private List<LineRange> allRanges(final GroovyPsiElement scope, final boolean stmtLevel, final boolean topLevel) {
final ArrayList<LineRange> result = new ArrayList<>();
scope.accept(new PsiRecursiveElementVisitor() {
int lastStart = -1;
private void addRange(int endLine) {
if (lastStart >= 0) {
result.add(new LineRange(lastStart, endLine));
}
lastStart = endLine;
}
@Override
public void visitElement(PsiElement element) {
if (stmtLevel && element instanceof GrCodeBlock) {
final PsiElement lBrace = ((GrCodeBlock) element).getLBrace();
if (nlsAfter(lBrace)) {
assert lBrace != null;
addRange(new LineRange(lBrace).endLine);
}
addChildRanges(((GrCodeBlock) element).getStatements());
final PsiElement rBrace = ((GrCodeBlock) element).getRBrace();
if (nlsAfter(rBrace)) {
assert rBrace != null;
final int endLine = new LineRange(rBrace).endLine;
if (lastStart >= 0) {
for (int i = lastStart + 1; i < endLine; i++) {
addRange(i);
}
}
}
} else if (stmtLevel && element instanceof GrCaseSection) {
final GrCaseLabel[] allLabels = ((GrCaseSection) element).getCaseLabels();
final GrCaseLabel label = allLabels[0];
if (nlsAfter(label)) {
addRange(new LineRange(label).endLine);
}
addChildRanges(((GrCaseSection) element).getStatements());
} else if (element instanceof GroovyFileBase) {
addChildRanges(((GroovyFileBase) element).getTopStatements());
} else if (!stmtLevel && !topLevel && element instanceof GrTypeDefinitionBody) {
addChildRanges(((GrTypeDefinitionBody) element).getMemberDeclarations());
} else {
super.visitElement(element);
}
}
private boolean shouldDigInside(GroovyPsiElement statement) {
if (stmtLevel && (statement instanceof GrMethod || statement instanceof GrTypeDefinition)) {
return false;
}
if (statement instanceof GrVariableDeclaration && !stmtLevel) {
return false;
}
return true;
}
private void addChildRanges(GroovyPsiElement[] statements) {
for (int i = 0; i < statements.length; i++) {
GroovyPsiElement statement = statements[i];
if (nlsAfter(statement)) {
final LineRange range = getLineRange(statement);
if ((i == 0 || isStatement(statements[i - 1])) && isStatement(statement)) {
for (int j = lastStart; j < range.startLine; j++) {
addRange(j + 1);
}
}
lastStart = range.startLine;
if (shouldDigInside(statement)) {
statement.accept(this);
}
addRange(range.endLine);
}
}
}
});
return result;
}
Aggregations