Java Code Examples for com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil#findElementAtNoCommit()

The following examples show how to use com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil#findElementAtNoCommit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PsiViewerDialog.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void caretPositionChanged(CaretEvent e) {
  if (!available() || myEditor.getSelectionModel().hasSelection()) return;
  final ViewerTreeStructure treeStructure = (ViewerTreeStructure)myPsiTreeBuilder.getTreeStructure();
  final PsiElement rootPsiElement = treeStructure.getRootPsiElement();
  if (rootPsiElement == null) return;
  final PsiElement rootElement = ((ViewerTreeStructure)myPsiTreeBuilder.getTreeStructure()).getRootPsiElement();
  int baseOffset = rootPsiElement.getTextRange().getStartOffset();
  final int offset = myEditor.getCaretModel().getOffset() + baseOffset;
  final PsiElement element = InjectedLanguageUtil.findElementAtNoCommit(rootElement.getContainingFile(), offset);
  if (element != null && myBlockTreeBuilder != null) {
    TextRange rangeInHostFile = InjectedLanguageManager.getInstance(myProject).injectedToHost(element, element.getTextRange());
    selectBlockNode(findBlockNode(rangeInHostFile, true));
  }
  myPsiTreeBuilder.select(element);
}
 
Example 2
Source File: FormattingDocumentModelImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public boolean containsWhiteSpaceSymbolsOnly(int startOffset, int endOffset) {
  WhiteSpaceFormattingStrategy strategy = myWhiteSpaceStrategy;
  if (strategy.check(myDocument.getCharsSequence(), startOffset, endOffset) >= endOffset) {
    return true;
  }
  PsiElement injectedElement = myFile != null ? InjectedLanguageUtil.findElementAtNoCommit(myFile, startOffset) : null;
  if (injectedElement != null) {
    Language injectedLanguage = injectedElement.getLanguage();
    if (!injectedLanguage.equals(myFile.getLanguage())) {
      WhiteSpaceFormattingStrategy localStrategy = WhiteSpaceFormattingStrategyFactory.getStrategy(injectedLanguage);
      if (localStrategy != null) {
        return localStrategy.check(myDocument.getCharsSequence(), startOffset, endOffset) >= endOffset;
      }
    }
  }
  return false;
}
 
Example 3
Source File: PsiElementRenameHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void invoke(@Nonnull Project project, Editor editor, PsiFile file, DataContext dataContext) {
  PsiElement element = getElement(dataContext);
  if (element == null) {
    element = BaseRefactoringAction.getElementAtCaret(editor, file);
  }

  if (ApplicationManager.getApplication().isUnitTestMode()) {
    final String newName = dataContext.getData(DEFAULT_NAME);
    if (newName != null) {
      rename(element, project, element, editor, newName);
      return;
    }
  }

  editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
  final PsiElement nameSuggestionContext = InjectedLanguageUtil.findElementAtNoCommit(file, editor.getCaretModel().getOffset());
  invoke(element, project, nameSuggestionContext, editor);
}
 
Example 4
Source File: PsiViewerDialog.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void initMap(BlockTreeNode rootBlockNode, PsiElement psiEl) {
    PsiElement currentElem = null;
    if (rootBlockNode.getBlock() instanceof ASTBlock) {
      ASTNode node = ((ASTBlock)rootBlockNode.getBlock()).getNode();
      if (node != null) {
        currentElem = node.getPsi();
      }
    }
    if (currentElem == null) {
      currentElem =
        InjectedLanguageUtil
          .findElementAtNoCommit(psiEl.getContainingFile(), rootBlockNode.getBlock().getTextRange().getStartOffset());
    }
    myPsiToBlockMap.put(currentElem, rootBlockNode);

//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, rootBlockNode);
      parentElem = parentElem.getParent();
    }
    for (BlockTreeNode block : rootBlockNode.getChildren()) {
      initMap(block, psiEl);
    }
  }
 
Example 5
Source File: PsiViewerDialog.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void valueChanged(TreeSelectionEvent e) {
  if (myIgnoreBlockTreeSelectionMarker > 0 || myBlockTreeBuilder == null) {
    return;
  }

  Set<?> blockElementsSet = myBlockTreeBuilder.getSelectedElements();
  if (blockElementsSet.isEmpty()) return;
  BlockTreeNode descriptor = (BlockTreeNode)blockElementsSet.iterator().next();
  PsiElement rootPsi = ((ViewerTreeStructure)myPsiTreeBuilder.getTreeStructure()).getRootPsiElement();
  int blockStart = descriptor.getBlock().getTextRange().getStartOffset();
  PsiElement currentPsiEl = InjectedLanguageUtil.findElementAtNoCommit(rootPsi.getContainingFile(), blockStart);
  int blockLength = descriptor.getBlock().getTextRange().getLength();
  while (currentPsiEl.getParent() != null &&
         currentPsiEl.getTextRange().getStartOffset() == blockStart &&
         currentPsiEl.getTextLength() != blockLength) {
    currentPsiEl = currentPsiEl.getParent();
  }
  final BlockTreeStructure treeStructure = (BlockTreeStructure)myBlockTreeBuilder.getTreeStructure();
  BlockTreeNode rootBlockNode = treeStructure.getRootElement();
  int baseOffset = 0;
  if (rootBlockNode != null) {
    baseOffset = rootBlockNode.getBlock().getTextRange().getStartOffset();
  }
  if (currentPsiEl != null) {
    TextRange range = descriptor.getBlock().getTextRange();
    range = range.shiftRight(-baseOffset);
    int start = range.getStartOffset();
    int end = range.getEndOffset();
    final int textLength = myEditor.getDocument().getTextLength();

    if (myBlockTree.hasFocus()) {
      clearSelection();
      if (end <= textLength) {
        myHighlighter = myEditor.getMarkupModel()
          .addRangeHighlighter(start, end, HighlighterLayer.LAST, myAttributes, HighlighterTargetArea.EXACT_RANGE);
        updateIntersectHighlighter(start, end);

        myEditor.getCaretModel().moveToOffset(start);
        myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
      }
    }
    updateReferences(currentPsiEl);
    if (!myPsiTree.hasFocus()) {
      myPsiTreeBuilder.select(currentPsiEl);
    }
  }
}