@NotNull public static String getElementTextWithContext(@NotNull JetElement element) { if (element instanceof JetFile) { return element.getContainingFile().getText(); } // Find parent for element among file children PsiElement inFileParent = PsiTreeUtil.findFirstParent( element, new Condition<PsiElement>() { @Override public boolean value(PsiElement parentCandidate) { return parentCandidate != null && parentCandidate.getParent() instanceof JetFile; } }); assert inFileParent != null : "For non-file element we should always be able to find parent in file children"; int startContextOffset = inFileParent.getTextRange().getStartOffset(); int elementContextOffset = element.getTextRange().getStartOffset(); int inFileParentOffset = elementContextOffset - startContextOffset; return new StringBuilder(inFileParent.getText()) .insert(inFileParentOffset, "<caret>") .toString(); }
public static <D> void visitChildren( @NotNull JetElement element, @NotNull JetTreeVisitor<D> visitor, D data) { PsiElement child = element.getFirstChild(); while (child != null) { if (child instanceof JetElement) { ((JetElement) child).accept(visitor, data); } child = child.getNextSibling(); } }
@Nullable public static JetElement getOutermostDescendantElement( @Nullable PsiElement root, boolean first, final @NotNull Predicate<JetElement> predicate) { if (!(root instanceof JetElement)) return null; final List<JetElement> results = Lists.newArrayList(); ((JetElement) root) .accept( new JetVisitorVoid() { @Override public void visitJetElement(@NotNull JetElement element) { if (predicate.apply(element)) { //noinspection unchecked results.add(element); } else { element.acceptChildren(this); } } }); if (results.isEmpty()) return null; return first ? results.get(0) : results.get(results.size() - 1); }
@Nullable public static String getPackageName(@NotNull JetElement element) { JetFile file = (JetFile) element.getContainingFile(); JetNamespaceHeader header = PsiTreeUtil.findChildOfType(file, JetNamespaceHeader.class); return header != null ? header.getQualifiedName() : null; }
@NotNull public static Set<JetElement> findRootExpressions( @NotNull Collection<JetElement> unreachableElements) { Set<JetElement> rootElements = new HashSet<JetElement>(); final Set<JetElement> shadowedElements = new HashSet<JetElement>(); JetVisitorVoid shadowAllChildren = new JetVisitorVoid() { @Override public void visitJetElement(@NotNull JetElement element) { if (shadowedElements.add(element)) { element.acceptChildren(this); } } }; for (JetElement element : unreachableElements) { if (shadowedElements.contains(element)) continue; element.acceptChildren(shadowAllChildren); rootElements.removeAll(shadowedElements); rootElements.add(element); } return rootElements; }
public static boolean isImplicitlyUsed(@NotNull JetElement element) { PsiElement parent = element.getParent(); if (!(parent instanceof JetBlockExpression)) return true; JetBlockExpression block = (JetBlockExpression) parent; List<JetElement> statements = block.getStatements(); if (statements.get(statements.size() - 1) == element) { JetExpression expression = getDirectParentOfTypeForBlock(block, JetIfExpression.class); if (expression == null) { expression = getDirectParentOfTypeForBlock(block, JetWhenExpression.class); } if (expression == null) { expression = getDirectParentOfTypeForBlock(block, JetFunctionLiteral.class); } if (expression == null) { expression = getDirectParentOfTypeForBlock(block, JetTryExpression.class); } if (expression != null) { return isImplicitlyUsed(expression); } } return false; }
@Override public boolean apply(@Nullable JetElement input) { return input instanceof JetBlockExpression && !PsiTreeUtil.instanceOf(input.getParent(), FUNCTIONLIKE_ELEMENT_CLASSES); }