use of com.intellij.internal.psiView.formattingblocks.BlockTreeNode in project intellij-community by JetBrains.
the class PsiViewerDialog method initMap.
private void initMap(BlockTreeNode rootBlockNode, PsiElement psiEl) {
JBTreeTraverser<BlockTreeNode> traverser = new JBTreeTraverser<>(o -> JBIterable.of(o.getChildren()));
for (BlockTreeNode block : traverser.withRoot(rootBlockNode)) {
PsiElement currentElem = null;
if (block.getBlock() instanceof ASTBlock) {
ASTNode node = ((ASTBlock) block.getBlock()).getNode();
if (node != null) {
currentElem = node.getPsi();
}
}
if (currentElem == null) {
currentElem = InjectedLanguageUtil.findElementAtNoCommit(psiEl.getContainingFile(), block.getBlock().getTextRange().getStartOffset());
}
myPsiToBlockMap.put(currentElem, block);
//nested PSI elements with same ranges will be mapped to one blockNode
// assert currentElem != null; //for Scala-language plugin etc it can be null, because formatterBlocks is not instance of ASTBlock
TextRange curTextRange = currentElem.getTextRange();
PsiElement parentElem = currentElem.getParent();
while (parentElem != null && parentElem.getTextRange().equals(curTextRange)) {
myPsiToBlockMap.put(parentElem, block);
parentElem = parentElem.getParent();
}
}
}
use of com.intellij.internal.psiView.formattingblocks.BlockTreeNode in project intellij-community by JetBrains.
the class PsiViewerDialog method findBlockNode.
@Nullable
private BlockTreeNode findBlockNode(PsiElement element) {
BlockTreeNode result = myPsiToBlockMap.get(element);
if (result == null) {
TextRange rangeInHostFile = InjectedLanguageManager.getInstance(myProject).injectedToHost(element, element.getTextRange());
result = findBlockNode(rangeInHostFile, true);
}
return result;
}
use of com.intellij.internal.psiView.formattingblocks.BlockTreeNode in project intellij-community by JetBrains.
the class PsiViewerDialog method doOKAction.
@Override
protected void doOKAction() {
if (myBlockTreeBuilder != null) {
Disposer.dispose(myBlockTreeBuilder);
}
final String text = myEditor.getDocument().getText();
myEditor.getSelectionModel().removeSelection();
myLastParsedText = text;
myLastParsedTextHashCode = text.hashCode();
myNewDocumentHashCode = myLastParsedTextHashCode;
PsiElement rootElement = parseText(text);
focusTree();
ViewerTreeStructure structure = getTreeStructure();
structure.setRootPsiElement(rootElement);
myPsiTreeBuilder.queueUpdate();
myPsiTree.setRootVisible(true);
myPsiTree.expandRow(0);
myPsiTree.setRootVisible(false);
if (!myShowBlocksCheckBox.isSelected()) {
return;
}
Block rootBlock = rootElement == null ? null : buildBlocks(rootElement);
if (rootBlock == null) {
myBlockTreeBuilder = null;
myBlockTree.setRootVisible(false);
myBlockTree.setVisible(false);
return;
}
myBlockTree.setVisible(true);
BlockTreeStructure blockTreeStructure = new BlockTreeStructure();
BlockTreeNode rootNode = new BlockTreeNode(rootBlock, null);
blockTreeStructure.setRoot(rootNode);
myBlockTreeBuilder = new BlockTreeBuilder(myBlockTree, blockTreeStructure);
myPsiToBlockMap = new HashMap<>();
final PsiElement psiFile = (getTreeStructure()).getRootPsiElement();
initMap(rootNode, psiFile);
PsiElement rootPsi = rootNode.getBlock() instanceof ASTBlock ? ((ASTBlock) rootNode.getBlock()).getNode().getPsi() : rootElement;
BlockTreeNode blockNode = myPsiToBlockMap.get(rootPsi);
if (blockNode == null) {
LOG.error(LogMessageEx.createEvent("PsiViewer: rootNode not found", "Current language: " + rootElement.getContainingFile().getLanguage(), AttachmentFactory.createAttachment(rootElement.getContainingFile().getOriginalFile().getVirtualFile())));
blockNode = findBlockNode(rootPsi);
}
blockTreeStructure.setRoot(blockNode);
myBlockTree.addTreeSelectionListener(new MyBlockTreeSelectionListener());
myBlockTree.setRootVisible(true);
myBlockTree.expandRow(0);
myBlockTreeBuilder.queueUpdate();
}
use of com.intellij.internal.psiView.formattingblocks.BlockTreeNode in project intellij-community by JetBrains.
the class PsiViewerDialog method findBlockNode.
@Nullable
private BlockTreeNode findBlockNode(TextRange range, boolean selectParentIfNotFound) {
final BlockTreeBuilder builder = myBlockTreeBuilder;
if (builder == null || !myBlockStructurePanel.isVisible()) {
return null;
}
AbstractTreeStructure treeStructure = builder.getTreeStructure();
if (treeStructure == null)
return null;
BlockTreeNode node = (BlockTreeNode) treeStructure.getRootElement();
main_loop: while (true) {
if (node.getBlock().getTextRange().equals(range)) {
return node;
}
for (BlockTreeNode child : node.getChildren()) {
if (child.getBlock().getTextRange().contains(range)) {
node = child;
continue main_loop;
}
}
return selectParentIfNotFound ? node : null;
}
}
Aggregations