private static void addReferencesInRange( ArrayList<ASTNode> array, TreeElement parent, int startOffset, int endOffset) { if (parent.getElementType() == JavaElementType.JAVA_CODE_REFERENCE || parent.getElementType() == JavaElementType.REFERENCE_EXPRESSION) { array.add(parent); return; } if (parent.getPsi() instanceof PsiFile && JspPsiUtil.isInJspFile(parent.getPsi())) { final JspFile jspFile = JspPsiUtil.getJspFile(parent.getPsi()); JspClass jspClass = (JspClass) jspFile.getJavaClass(); addReferencesInRange(array, (TreeElement) jspClass.getNode(), startOffset, endOffset); return; } addReferencesInRangeForComposite(array, parent, startOffset, endOffset); }
private static boolean spacesOnly(@Nullable TreeElement node) { if (node == null) return false; if (isWhitespaceOrEmpty(node)) return true; PsiElement psi = node.getPsi(); if (psi == null) { return false; } Language language = psi.getLanguage(); return WhiteSpaceFormattingStrategyFactory.getStrategy(language).containsWhitespacesOnly(node); }
@Nullable public static ASTNode findDocComment(@NotNull CompositeElement element) { TreeElement node = element.getFirstChildNode(); while (node != null && (isWhitespaceOrComment(node) && !(node.getPsi() instanceof PsiDocComment))) { node = node.getTreeNext(); } if (node != null && node.getElementType() == JavaDocElementType.DOC_COMMENT) { return node; } else { return null; } }
private TreeElement addInternal(TreeElement child, ASTNode anchor, boolean before) throws IncorrectOperationException { final PomModel model = PomManager.getModel(getProject()); if (anchor != null && child.getElementType() == XmlElementType.XML_TEXT) { XmlText psi = null; if (anchor.getPsi() instanceof XmlText) { psi = (XmlText) anchor.getPsi(); } else { final ASTNode other = before ? anchor.getTreePrev() : anchor.getTreeNext(); if (other != null && other.getPsi() instanceof XmlText) { before = !before; psi = (XmlText) other.getPsi(); } } if (psi != null) { if (before) { psi.insertText(((XmlText) child.getPsi()).getValue(), 0); } else { psi.insertText(((XmlText) child.getPsi()).getValue(), psi.getValue().length()); } return (TreeElement) psi.getNode(); } } LOG.assertTrue(child.getPsi() instanceof XmlAttribute || child.getPsi() instanceof XmlTagChild); final InsertTransaction transaction; if (child.getElementType() == XmlElementType.XML_ATTRIBUTE) { transaction = new InsertAttributeTransaction(child, anchor, before, model); } else if (anchor == null) { transaction = getBodyInsertTransaction(child); } else { transaction = new GenericInsertTransaction(child, anchor, before); } model.runTransaction(transaction); return transaction.getFirstInserted(); }
@Override public PsiElement getMirror() { TreeElement mirrorTreeElement = myMirrorFileElement; if (mirrorTreeElement == null) { synchronized (myMirrorLock) { mirrorTreeElement = myMirrorFileElement; if (mirrorTreeElement == null) { VirtualFile file = getVirtualFile(); PsiClass[] classes = getClasses(); String fileName = (classes.length > 0 ? classes[0].getName() : file.getNameWithoutExtension()) + JavaFileType.DOT_DEFAULT_EXTENSION; final Document document = FileDocumentManager.getInstance().getDocument(file); assert document != null : file.getUrl(); CharSequence mirrorText = document.getImmutableCharSequence(); boolean internalDecompiler = StringUtil.startsWith(mirrorText, BANNER); PsiFileFactory factory = PsiFileFactory.getInstance(getManager().getProject()); PsiFile mirror = factory.createFileFromText(fileName, JavaLanguage.INSTANCE, mirrorText, false, false); mirror.putUserData(PsiUtil.FILE_LANGUAGE_LEVEL_KEY, getLanguageLevel()); mirrorTreeElement = SourceTreeToPsiMap.psiToTreeNotNull(mirror); try { final TreeElement finalMirrorTreeElement = mirrorTreeElement; ProgressManager.getInstance() .executeNonCancelableSection( new Runnable() { @Override public void run() { setMirror(finalMirrorTreeElement); putUserData(CLS_DOCUMENT_LINK_KEY, document); } }); } catch (InvalidMirrorException e) { //noinspection ThrowableResultOfMethodCallIgnored LOG.error(file.getUrl(), internalDecompiler ? e : wrapException(e, file)); } ((PsiFileImpl) mirror).setOriginalFile(this); myMirrorFileElement = mirrorTreeElement; } } } return mirrorTreeElement.getPsi(); }
private void insertOuters(TreeElement root, Lexer lexer, final CharTable table) { TreePatcher patcher = TREE_PATCHER.forLanguage(root.getPsi().getLanguage()); int treeOffset = 0; LeafElement leaf = TreeUtil.findFirstLeaf(root); while (lexer.getTokenType() != null) { IElementType tt = lexer.getTokenType(); if (tt != myTemplateElementType) { while (leaf != null && treeOffset < lexer.getTokenStart()) { treeOffset += leaf.getTextLength(); if (treeOffset > lexer.getTokenStart()) { leaf = patcher.split( leaf, leaf.getTextLength() - (treeOffset - lexer.getTokenStart()), table); treeOffset = lexer.getTokenStart(); } leaf = (LeafElement) TreeUtil.nextLeaf(leaf); } if (leaf == null) break; final OuterLanguageElementImpl newLeaf = createOuterLanguageElement(lexer, table, myOuterElementType); patcher.insert(leaf.getTreeParent(), leaf, newLeaf); leaf.getTreeParent().subtreeChanged(); leaf = newLeaf; } lexer.advance(); } if (lexer.getTokenType() != null) { assert lexer.getTokenType() != myTemplateElementType; final OuterLanguageElementImpl newLeaf = createOuterLanguageElement(lexer, table, myOuterElementType); ((CompositeElement) root).rawAddChildren(newLeaf); ((CompositeElement) root).subtreeChanged(); } }