use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement 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.GroovyPsiElement 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.GroovyPsiElement in project intellij-community by JetBrains.
the class GroovyIndentProcessor method getChildIndent.
/**
* Calculates indent, based on code style, between parent block and child node
*
* @param parentBlock parent block
* @param child child node
* @return indent
*/
@NotNull
public Indent getChildIndent(@NotNull final GroovyBlock parentBlock, @NotNull final ASTNode child) {
myChildType = child.getElementType();
if (parentBlock instanceof ClosureBodyBlock) {
if (myChildType == GroovyElementTypes.PARAMETERS_LIST) {
return Indent.getNoneIndent();
} else if (myChildType != GroovyTokenTypes.mLCURLY && myChildType != GroovyTokenTypes.mRCURLY) {
return Indent.getNormalIndent();
}
}
if (parentBlock instanceof GrLabelBlock) {
ASTNode first = parentBlock.getNode().getFirstChildNode();
return child == first ? Indent.getNoneIndent() : Indent.getLabelIndent();
}
if (GSTRING_TOKENS_INNER.contains(myChildType)) {
return Indent.getAbsoluteNoneIndent();
}
final PsiElement parent = parentBlock.getNode().getPsi();
if (parent instanceof GroovyPsiElement) {
myBlock = parentBlock;
myChild = child.getPsi();
((GroovyPsiElement) parent).accept(this);
if (myResult != null)
return myResult;
}
return Indent.getNoneIndent();
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class MavenGroovyPomCompletionContributor method fillCompletionVariants.
@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
final PsiElement position = parameters.getPosition();
if (!(position instanceof LeafElement))
return;
Project project = position.getProject();
VirtualFile virtualFile = parameters.getOriginalFile().getVirtualFile();
if (virtualFile == null)
return;
MavenProject mavenProject = MavenProjectsManager.getInstance(project).findProject(virtualFile);
if (mavenProject == null)
return;
List<String> methodCallInfo = MavenGroovyPomUtil.getGroovyMethodCalls(position);
if (methodCallInfo.isEmpty())
return;
StringBuilder buf = new StringBuilder();
for (String s : methodCallInfo) {
buf.append('<').append(s).append('>');
}
for (String s : ContainerUtil.reverse(methodCallInfo)) {
buf.append('<').append(s).append("/>");
}
PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText("pom.xml", XMLLanguage.INSTANCE, buf);
psiFile.putUserData(ORIGINAL_POM_FILE, virtualFile);
List<Object> variants = ContainerUtil.newArrayList();
String lastMethodCall = ContainerUtil.getLastItem(methodCallInfo);
Ref<Boolean> completeDependency = Ref.create(false);
Ref<Boolean> completeVersion = Ref.create(false);
psiFile.accept(new PsiRecursiveElementVisitor(true) {
@Override
public void visitElement(PsiElement element) {
super.visitElement(element);
if (!completeDependency.get() && element.getParent() instanceof XmlTag && "dependency".equals(((XmlTag) element.getParent()).getName())) {
if ("artifactId".equals(lastMethodCall) || "groupId".equals(lastMethodCall)) {
completeDependency.set(true);
} else if ("version".equals(lastMethodCall) || "dependency".equals(lastMethodCall)) {
completeVersion.set(true);
//completeDependency.set(true);
}
}
if (!completeDependency.get() && !completeVersion.get()) {
PsiReference[] references = getReferences(element);
for (PsiReference each : references) {
if (each instanceof GenericDomValueReference) {
Collections.addAll(variants, each.getVariants());
}
}
}
}
});
for (Object variant : variants) {
if (variant instanceof LookupElement) {
result.addElement((LookupElement) variant);
} else {
result.addElement(LookupElementBuilder.create(variant));
}
}
if (completeDependency.get()) {
MavenProjectIndicesManager indicesManager = MavenProjectIndicesManager.getInstance(project);
for (String groupId : indicesManager.getGroupIds()) {
for (String artifactId : indicesManager.getArtifactIds(groupId)) {
LookupElement builder = LookupElementBuilder.create(groupId + ':' + artifactId).withIcon(AllIcons.Nodes.PpLib).withInsertHandler(MavenDependencyInsertHandler.INSTANCE);
result.addElement(builder);
}
}
}
if (completeVersion.get()) {
consumeDependencyElement(position, closableBlock -> {
String groupId = null;
String artifactId = null;
for (GrMethodCall methodCall : PsiTreeUtil.findChildrenOfType(closableBlock, GrMethodCall.class)) {
GroovyPsiElement[] arguments = methodCall.getArgumentList().getAllArguments();
if (arguments.length != 1)
continue;
PsiReference reference = arguments[0].getReference();
if (reference == null)
continue;
String callExpression = methodCall.getInvokedExpression().getText();
String argumentValue = reference.getCanonicalText();
if ("groupId".equals(callExpression)) {
groupId = argumentValue;
} else if ("artifactId".equals(callExpression)) {
artifactId = argumentValue;
}
}
completeVersions(result, project, groupId, artifactId, "");
}, element -> {
if (element.getParent() instanceof PsiLiteral) {
Object value = ((PsiLiteral) element.getParent()).getValue();
if (value == null)
return;
String[] mavenCoordinates = value.toString().split(":");
if (mavenCoordinates.length < 3)
return;
String prefix = mavenCoordinates[0] + ':' + mavenCoordinates[1] + ':';
completeVersions(result, project, mavenCoordinates[0], mavenCoordinates[1], prefix);
}
});
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class GroovyGenerationInfo method insert.
@Override
public void insert(@NotNull PsiClass aClass, @Nullable PsiElement anchor, boolean before) throws IncorrectOperationException {
final T proto = getPsiMember();
if (proto instanceof GrMethod) {
GroovyChangeContextUtil.encodeContextInfo(((GrMethod) proto).getParameterList());
}
super.insert(aClass, anchor, before);
final T member = getPsiMember();
if (member == null)
return;
LOG.assertTrue(member instanceof GroovyPsiElement, member);
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(member.getProject());
final PsiElement prev = member.getPrevSibling();
if (prev != null && GroovyTokenTypes.mNLS == prev.getNode().getElementType()) {
prev.replace(factory.createLineTerminator(1));
} else if (prev instanceof PsiMember) {
member.getParent().getNode().addLeaf(GroovyTokenTypes.mNLS, "\n", member.getNode());
}
final PsiElement next = member.getNextSibling();
if (next != null && GroovyTokenTypes.mNLS == next.getNode().getElementType()) {
next.replace(factory.createLineTerminator(1));
} else if (next instanceof PsiMember) {
member.getParent().getNode().addLeaf(GroovyTokenTypes.mNLS, "\n", next.getNode());
}
if (member instanceof GrMethod) {
GroovyChangeContextUtil.decodeContextInfo(((GrMethod) member).getParameterList(), null, null);
}
JavaCodeStyleManager.getInstance(member.getProject()).shortenClassReferences(member);
adjustDocCommentIfExists(member);
}
Aggregations