use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.
the class GroovyPositionManager method findReferenceTypeSourceImage.
@Nullable
private static GroovyPsiElement findReferenceTypeSourceImage(SourcePosition position) {
PsiFile file = position.getFile();
if (!(file instanceof GroovyFileBase))
return null;
PsiElement element = file.findElementAt(position.getOffset());
if (element == null)
return null;
return PsiTreeUtil.getParentOfType(element, GrClosableBlock.class, GrTypeDefinition.class);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.
the class GroovyEnterHandler method preprocessEnter.
@Override
public Result preprocessEnter(@NotNull PsiFile file, @NotNull Editor editor, @NotNull Ref<Integer> caretOffset, @NotNull Ref<Integer> caretAdvance, @NotNull DataContext dataContext, EditorActionHandler originalHandler) {
Document document = editor.getDocument();
Project project = file.getProject();
CaretModel caretModel = editor.getCaretModel();
if (!(file instanceof GroovyFileBase)) {
return Result.Continue;
}
int docLength = document.getTextLength();
if (docLength == 0) {
return Result.Continue;
}
final int caret = caretModel.getOffset();
final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
if (caret >= 1 && caret < docLength && CodeInsightSettings.getInstance().SMART_INDENT_ON_ENTER) {
HighlighterIterator iterator = highlighter.createIterator(caret);
iterator.retreat();
while (!iterator.atEnd() && TokenType.WHITE_SPACE == iterator.getTokenType()) {
iterator.retreat();
}
boolean afterArrow = !iterator.atEnd() && iterator.getTokenType() == GroovyTokenTypes.mCLOSABLE_BLOCK_OP;
if (afterArrow) {
originalHandler.execute(editor, dataContext);
PsiDocumentManager.getInstance(project).commitDocument(document);
CodeStyleManager.getInstance(project).adjustLineIndent(file, caretModel.getOffset());
}
iterator = highlighter.createIterator(caretModel.getOffset());
while (!iterator.atEnd() && TokenType.WHITE_SPACE == iterator.getTokenType()) {
iterator.advance();
}
if (!iterator.atEnd() && GroovyTokenTypes.mRCURLY == iterator.getTokenType()) {
PsiDocumentManager.getInstance(project).commitDocument(document);
final PsiElement element = file.findElementAt(iterator.getStart());
if (element != null && element.getNode().getElementType() == GroovyTokenTypes.mRCURLY && element.getParent() instanceof GrClosableBlock && docLength > caret && afterArrow) {
return Result.DefaultForceIndent;
}
}
if (afterArrow) {
return Result.Stop;
}
if (editor.isInsertMode() && !HandlerUtils.isReadOnly(editor) && !editor.getSelectionModel().hasSelection() && handleFlyingGeese(editor, caret, dataContext, originalHandler, file)) {
return Result.DefaultForceIndent;
}
}
if (handleEnter(editor, dataContext, project, originalHandler))
return Result.Stop;
return Result.Continue;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.
the class GroovyStatementMover method checkAvailable.
@Override
public boolean checkAvailable(@NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down) {
final Project project = file.getProject();
if (!HandlerUtils.canBeInvoked(editor, project) || !(file instanceof GroovyFileBase))
return false;
LineRange range = getLineRangeFromSelection(editor);
final Document document = editor.getDocument();
final int offset = document.getLineStartOffset(range.startLine);
final GrLiteral literal = PsiTreeUtil.findElementOfClassAtOffset(file, offset, GrLiteral.class, false);
//multiline string
if (literal != null && literal.textContains('\n'))
return false;
final GroovyPsiElement pivot = getElementToMove((GroovyFileBase) file, offset);
if (pivot == null)
return false;
final LineRange pivotRange = getLineRange(pivot);
range = new LineRange(Math.min(range.startLine, pivotRange.startLine), Math.max(range.endLine, pivotRange.endLine));
final GroovyPsiElement scope = PsiTreeUtil.getParentOfType(pivot, GrMethod.class, GrTypeDefinitionBody.class, GroovyFileBase.class);
final boolean stmtLevel = isStatement(pivot);
boolean topLevel = pivot instanceof GrTypeDefinition && pivot.getParent() instanceof GroovyFileBase;
final List<LineRange> allRanges = allRanges(scope, stmtLevel, topLevel);
LineRange prev = null;
LineRange next = null;
for (LineRange each : allRanges) {
if (each.endLine <= range.startLine) {
prev = each;
}
if (each.containsLine(range.startLine)) {
range = new LineRange(each.startLine, range.endLine);
}
if (each.startLine < range.endLine && each.endLine > range.endLine) {
range = new LineRange(range.startLine, each.endLine);
}
if (each.startLine >= range.endLine && next == null) {
next = each;
}
}
info.toMove = range;
info.toMove2 = down ? next : prev;
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.
the class GrKindWeigher method weigh.
@Override
public Comparable weigh(@NotNull LookupElement element, @NotNull CompletionLocation location) {
final PsiElement position = location.getCompletionParameters().getPosition();
if (!(position.getContainingFile() instanceof GroovyFileBase))
return null;
Object o = element.getObject();
if (o instanceof ResolveResult) {
o = ((ResolveResult) o).getElement();
}
final PsiElement parent = position.getParent();
final PsiElement qualifier = parent instanceof GrReferenceElement ? ((GrReferenceElement) parent).getQualifier() : null;
if (qualifier == null) {
if (o instanceof NamedArgumentDescriptor) {
switch(((NamedArgumentDescriptor) o).getPriority()) {
case ALWAYS_ON_TOP:
return NotQualifiedKind.onTop;
case AS_LOCAL_VARIABLE:
return NotQualifiedKind.local;
default:
return NotQualifiedKind.unknown;
}
}
if (o instanceof PsiVariable && !(o instanceof PsiField)) {
return NotQualifiedKind.local;
}
PsiTypeLookupItem item = element.as(PsiTypeLookupItem.CLASS_CONDITION_KEY);
if (item != null && item.getBracketsCount() > 0) {
return NotQualifiedKind.arrayType;
}
if (isPriorityKeyword(o))
return NotQualifiedKind.local;
if (isLightElement(o))
return NotQualifiedKind.unknown;
if (o instanceof PsiClass) {
if (((PsiClass) o).isAnnotationType() && GrMainCompletionProvider.AFTER_AT.accepts(position)) {
final GrAnnotation annotation = PsiTreeUtil.getParentOfType(position, GrAnnotation.class);
if (annotation != null) {
PsiElement annoParent = annotation.getParent();
PsiElement ownerToUse = annoParent instanceof PsiModifierList ? annoParent.getParent() : annoParent;
PsiAnnotation.TargetType[] elementTypeFields = GrAnnotationImpl.getApplicableElementTypeFields(ownerToUse);
if (AnnotationTargetUtil.findAnnotationTarget((PsiClass) o, elementTypeFields) != null) {
return NotQualifiedKind.restrictedClass;
}
}
}
if (GrMainCompletionProvider.IN_CATCH_TYPE.accepts(position) && InheritanceUtil.isInheritor((PsiClass) o, CommonClassNames.JAVA_LANG_THROWABLE)) {
return NotQualifiedKind.restrictedClass;
}
}
if (o instanceof PsiMember) {
final PsiClass containingClass = ((PsiMember) o).getContainingClass();
if (isAccessor((PsiMember) o))
return NotQualifiedKind.accessor;
if (o instanceof PsiClass && ((PsiClass) o).getContainingClass() == null || o instanceof PsiPackage)
return NotQualifiedKind.unknown;
if (o instanceof PsiClass)
return NotQualifiedKind.innerClass;
if (PsiTreeUtil.isContextAncestor(containingClass, position, false))
return NotQualifiedKind.currentClassMember;
return NotQualifiedKind.member;
}
return NotQualifiedKind.unknown;
} else {
if (o instanceof PsiEnumConstant)
return QualifiedKind.enumConstant;
if (isLightElement(o))
return QualifiedKind.unknown;
if (o instanceof PsiMember) {
if (isTrashMethod((PsiMember) o))
return QualifiedKind.unknown;
if (isAccessor((PsiMember) o))
return QualifiedKind.accessor;
if (isQualifierClassMember((PsiMember) o, qualifier)) {
return QualifiedKind.currentClassMember;
}
if (o instanceof PsiClass && ((PsiClass) o).getContainingClass() == null || o instanceof PsiPackage)
return QualifiedKind.unknown;
if (o instanceof PsiClass)
return QualifiedKind.innerClass;
return QualifiedKind.member;
}
return QualifiedKind.unknown;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.
the class GroovyBlock method getChildAttributes.
@Override
@NotNull
public ChildAttributes getChildAttributes(final int newChildIndex) {
ASTNode astNode = getNode();
final PsiElement psiParent = astNode.getPsi();
if (psiParent instanceof GroovyFileBase) {
return new ChildAttributes(Indent.getNoneIndent(), null);
}
if (psiParent instanceof GrSwitchStatement) {
List<Block> subBlocks = getSubBlocks();
if (newChildIndex > 0) {
Block block = subBlocks.get(newChildIndex - 1);
if (block instanceof GroovyBlock) {
PsiElement anchorPsi = ((GroovyBlock) block).getNode().getPsi();
if (anchorPsi instanceof GrCaseSection) {
for (GrStatement statement : ((GrCaseSection) anchorPsi).getStatements()) {
if (statement instanceof GrBreakStatement || statement instanceof GrContinueStatement || statement instanceof GrReturnStatement || statement instanceof GrThrowStatement) {
final Indent indent = GroovyIndentProcessor.getSwitchCaseIndent(myContext.getSettings());
return new ChildAttributes(indent, null);
}
}
int indentSize = myContext.getSettings().getIndentOptions().INDENT_SIZE;
final int spaces = myContext.getSettings().INDENT_CASE_FROM_SWITCH ? 2 * indentSize : indentSize;
return new ChildAttributes(Indent.getSpaceIndent(spaces), null);
}
}
}
}
if (psiParent instanceof GrCaseLabel) {
return new ChildAttributes(GroovyIndentProcessor.getSwitchCaseIndent(getContext().getSettings()), null);
}
if (psiParent instanceof GrCaseSection) {
return getSwitchIndent((GrCaseSection) psiParent, newChildIndex);
}
if (TokenSets.BLOCK_SET.contains(astNode.getElementType()) || GroovyElementTypes.SWITCH_STATEMENT.equals(astNode.getElementType())) {
return new ChildAttributes(Indent.getNormalIndent(), null);
}
if (GroovyElementTypes.CASE_SECTION.equals(astNode.getElementType())) {
return new ChildAttributes(Indent.getNormalIndent(), null);
}
if (psiParent instanceof GrBinaryExpression || psiParent instanceof GrConditionalExpression || psiParent instanceof GrCommandArgumentList || psiParent instanceof GrArgumentList || psiParent instanceof GrParameterList || psiParent instanceof GrListOrMap || psiParent instanceof GrAnnotationArgumentList || psiParent instanceof GrVariable || psiParent instanceof GrAssignmentExpression) {
return new ChildAttributes(Indent.getContinuationWithoutFirstIndent(), null);
}
if (psiParent instanceof GrDocComment || psiParent instanceof GrDocTag) {
return new ChildAttributes(Indent.getSpaceIndent(GroovyIndentProcessor.GDOC_COMMENT_INDENT), null);
}
if (psiParent instanceof GrIfStatement || psiParent instanceof GrLoopStatement) {
return new ChildAttributes(Indent.getNormalIndent(), null);
}
if (psiParent instanceof GrLabeledStatement && newChildIndex == 2) {
final Indent indent = getContext().getGroovySettings().INDENT_LABEL_BLOCKS ? Indent.getLabelIndent() : Indent.getNoneIndent();
return new ChildAttributes(indent, null);
}
return new ChildAttributes(Indent.getNoneIndent(), null);
}
Aggregations