Java Code Examples for com.intellij.psi.impl.source.SourceTreeToPsiMap#psiElementToTree()

The following examples show how to use com.intellij.psi.impl.source.SourceTreeToPsiMap#psiElementToTree() . 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: CodeStyleManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private static Pair<PsiElement, CharTable> doFindWhiteSpaceNode(@Nonnull PsiFile file, int offset) {
  ASTNode astNode = SourceTreeToPsiMap.psiElementToTree(file);
  if (!(astNode instanceof FileElement)) {
    return new Pair<>(null, null);
  }
  PsiElement elementAt = InjectedLanguageManager.getInstance(file.getProject()).findInjectedElementAt(file, offset);
  final CharTable charTable = ((FileElement)astNode).getCharTable();
  if (elementAt == null) {
    elementAt = findElementInTreeWithFormatterEnabled(file, offset);
  }

  if (elementAt == null) {
    return new Pair<>(null, charTable);
  }
  ASTNode node = elementAt.getNode();
  if (node == null || node.getElementType() != TokenType.WHITE_SPACE) {
    return new Pair<>(null, charTable);
  }
  return Pair.create(elementAt, charTable);
}
 
Example 2
Source File: CodeStyleManagerRunnable.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static TextRange getSignificantRange(final PsiFile file, final int offset) {
  final ASTNode elementAtOffset = SourceTreeToPsiMap.psiElementToTree(CodeStyleManagerImpl.findElementInTreeWithFormatterEnabled(file, offset));
  if (elementAtOffset == null) {
    int significantRangeStart = CharArrayUtil.shiftBackward(file.getText(), offset - 1, "\n\r\t ");
    return new TextRange(Math.max(significantRangeStart, 0), offset);
  }

  final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(file);
  if (builder != null) {
    final TextRange textRange = builder.getRangeAffectingIndent(file, offset, elementAtOffset);
    if (textRange != null) {
      return textRange;
    }
  }

  final TextRange elementRange = elementAtOffset.getTextRange();
  if (isWhiteSpace(elementAtOffset)) {
    return extendRangeAtStartOffset(file, elementRange);
  }

  return elementRange;
}
 
Example 3
Source File: ASTDelegatePsiElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void deleteChildRange(final PsiElement first, final PsiElement last) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
  ASTNode firstElement = SourceTreeToPsiMap.psiElementToTree(first);
  ASTNode lastElement = SourceTreeToPsiMap.psiElementToTree(last);

  LOG.assertTrue(firstElement.getTreeParent() == getNode());
  LOG.assertTrue(lastElement.getTreeParent() == getNode());
  CodeEditUtil.removeChildren(getNode(), firstElement, lastElement);
}
 
Example 4
Source File: SharedImplUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static PsiElement addRange(PsiElement thisElement,
                                  PsiElement first,
                                  PsiElement last,
                                  ASTNode anchor,
                                  Boolean before) throws IncorrectOperationException {
  CheckUtil.checkWritable(thisElement);
  final CharTable table = findCharTableByTree(SourceTreeToPsiMap.psiElementToTree(thisElement));

  TreeElement copyFirst = null;
  ASTNode copyLast = null;
  ASTNode next = SourceTreeToPsiMap.psiElementToTree(last).getTreeNext();
  ASTNode parent = null;
  for (ASTNode element = SourceTreeToPsiMap.psiElementToTree(first); element != next; element = element.getTreeNext()) {
    TreeElement elementCopy = ChangeUtil.copyElement((TreeElement)element, table);
    if (element == first.getNode()) {
      copyFirst = elementCopy;
    }
    if (element == last.getNode()) {
      copyLast = elementCopy;
    }
    if (parent == null) {
      parent = elementCopy.getTreeParent();
    }
    else {
      if(elementCopy.getElementType() == TokenType.WHITE_SPACE)
        CodeEditUtil.setNodeGenerated(elementCopy, true);
      parent.addChild(elementCopy, null);
    }
  }
  if (copyFirst == null) return null;
  copyFirst = ((CompositeElement)SourceTreeToPsiMap.psiElementToTree(thisElement)).addInternal(copyFirst, copyLast, anchor, before);
  for (TreeElement element = copyFirst; element != null; element = element.getTreeNext()) {
    element = ChangeUtil.decodeInformation(element);
    if (element.getTreePrev() == null) {
      copyFirst = element;
    }
  }
  return SourceTreeToPsiMap.treeElementToPsi(copyFirst);
}
 
Example 5
Source File: LazyParseablePsiElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void deleteChildRange(PsiElement first, PsiElement last) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
  ASTNode firstElement = SourceTreeToPsiMap.psiElementToTree(first);
  ASTNode lastElement = SourceTreeToPsiMap.psiElementToTree(last);
  LOG.assertTrue(firstElement.getTreeParent() == this);
  LOG.assertTrue(lastElement.getTreeParent() == this);
  CodeEditUtil.removeChildren(this, firstElement, lastElement);
}
 
Example 6
Source File: PsiDirectoryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void updateAddedFile(PsiFile copyPsi) throws IncorrectOperationException {
  final UpdateAddedFileProcessor processor = UpdateAddedFileProcessor.forElement(copyPsi);
  if (processor != null) {
    final TreeElement tree = (TreeElement)SourceTreeToPsiMap.psiElementToTree(copyPsi);
    if (tree != null) {
      ChangeUtil.encodeInformation(tree);
    }
    processor.update(copyPsi, null);
    if (tree != null) {
      ChangeUtil.decodeInformation(tree);
    }
  }
}
 
Example 7
Source File: CodeStyleManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void reformatText(@Nonnull PsiFile file, @Nonnull FormatTextRanges ranges, @Nullable Editor editor) throws IncorrectOperationException {
  if (ranges.isEmpty()) {
    return;
  }
  ApplicationManager.getApplication().assertWriteAccessAllowed();
  PsiDocumentManager.getInstance(getProject()).commitAllDocuments();

  CheckUtil.checkWritable(file);
  if (!SourceTreeToPsiMap.hasTreeElement(file)) {
    return;
  }

  ASTNode treeElement = SourceTreeToPsiMap.psiElementToTree(file);
  transformAllChildren(treeElement);

  LOG.assertTrue(file.isValid(), "File name: " + file.getName() + " , class: " + file.getClass().getSimpleName());

  if (editor == null) {
    editor = PsiUtilBase.findEditor(file);
  }

  CaretPositionKeeper caretKeeper = null;
  if (editor != null) {
    caretKeeper = new CaretPositionKeeper(editor, getSettings(file), file.getLanguage());
  }

  if (FormatterUtil.isFormatterCalledExplicitly()) {
    removeEndingWhiteSpaceFromEachRange(file, ranges);
  }

  formatRanges(file, ranges, ExternalFormatProcessor.useExternalFormatter(file) ? null  // do nothing, delegate the external formatting activity to post-processor
                                                                                : () -> {
                                                                                  final CodeFormatterFacade codeFormatter = new CodeFormatterFacade(getSettings(file), file.getLanguage());
                                                                                  codeFormatter.processText(file, ranges, true);
                                                                                });

  if (caretKeeper != null) {
    caretKeeper.restoreCaretPosition();
  }
}
 
Example 8
Source File: CodeStyleManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Deprecated
public boolean isLineToBeIndented(@Nonnull PsiFile file, int offset) {
  if (!SourceTreeToPsiMap.hasTreeElement(file)) {
    return false;
  }
  CharSequence chars = file.getViewProvider().getContents();
  int start = CharArrayUtil.shiftBackward(chars, offset - 1, " \t");
  if (start > 0 && chars.charAt(start) != '\n' && chars.charAt(start) != '\r') {
    return false;
  }
  int end = CharArrayUtil.shiftForward(chars, offset, " \t");
  if (end >= chars.length()) {
    return false;
  }
  ASTNode element = SourceTreeToPsiMap.psiElementToTree(findElementInTreeWithFormatterEnabled(file, end));
  if (element == null) {
    return false;
  }
  if (element.getElementType() == TokenType.WHITE_SPACE) {
    return false;
  }
  if (element.getElementType() == PlainTextTokenTypes.PLAIN_TEXT) {
    return false;
  }
  /*
  if( element.getElementType() instanceof IJspElementType )
  {
    return false;
  }
  */
  if (getSettings(file).getCommonSettings(file.getLanguage()).KEEP_FIRST_COLUMN_COMMENT && isCommentToken(element)) {
    if (IndentHelper.getInstance().getIndent(myProject, file.getFileType(), element, true) == 0) {
      return false;
    }
  }
  return true;
}
 
Example 9
Source File: PsiBasedFormattingModel.java    From consulo with Apache License 2.0 4 votes vote down vote up
public PsiBasedFormattingModel(final PsiFile file, @Nonnull final Block rootBlock, final FormattingDocumentModelImpl documentModel) {
  myASTNode = SourceTreeToPsiMap.psiElementToTree(file);
  myDocumentModel = documentModel;
  myRootBlock = rootBlock;
  myProject = file.getProject();
}