com.intellij.psi.impl.CheckUtil Java Examples

The following examples show how to use com.intellij.psi.impl.CheckUtil. 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
private static PsiElement reformatRangeImpl(final @Nonnull PsiElement element, final int startOffset, final int endOffset, boolean canChangeWhiteSpacesOnly) throws IncorrectOperationException {
  LOG.assertTrue(element.isValid());
  CheckUtil.checkWritable(element);
  if (!SourceTreeToPsiMap.hasTreeElement(element)) {
    return element;
  }

  ASTNode treeElement = element.getNode();
  final PsiFile file = element.getContainingFile();
  if (ExternalFormatProcessor.useExternalFormatter(file)) {
    return ExternalFormatProcessor.formatElement(element, TextRange.create(startOffset, endOffset), canChangeWhiteSpacesOnly);
  }

  final CodeFormatterFacade codeFormatter = new CodeFormatterFacade(getSettings(file), element.getLanguage());
  final PsiElement formatted = codeFormatter.processRange(treeElement, startOffset, endOffset).getPsi();
  return canChangeWhiteSpacesOnly ? formatted : postProcessElement(file, formatted);
}
 
Example #2
Source File: ExternalFormatterCodeStyleManager.java    From intellij with Apache License 2.0 6 votes vote down vote up
private void formatInternal(PsiFile file, Collection<TextRange> ranges) {
  ApplicationManager.getApplication().assertWriteAccessAllowed();
  PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
  documentManager.commitAllDocuments();
  CheckUtil.checkWritable(file);

  Document document = documentManager.getDocument(file);

  if (document == null) {
    return;
  }
  // If there are postponed PSI changes (e.g., during a refactoring), just abort.
  // If we apply them now, then the incoming text ranges may no longer be valid.
  if (documentManager.isDocumentBlockedByPsi(document)) {
    return;
  }

  format(file, document, ranges);
}
 
Example #3
Source File: GoogleJavaFormatCodeStyleManager.java    From google-java-format with Apache License 2.0 6 votes vote down vote up
private void formatInternal(PsiFile file, Collection<TextRange> ranges) {
  ApplicationManager.getApplication().assertWriteAccessAllowed();
  PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
  documentManager.commitAllDocuments();
  CheckUtil.checkWritable(file);

  Document document = documentManager.getDocument(file);

  if (document == null) {
    return;
  }
  // If there are postponed PSI changes (e.g., during a refactoring), just abort.
  // If we apply them now, then the incoming text ranges may no longer be valid.
  if (documentManager.isDocumentBlockedByPsi(document)) {
    return;
  }

  format(document, ranges);
}
 
Example #4
Source File: CodeStyleManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
@Nonnull
public PsiElement reformat(@Nonnull PsiElement element, boolean canChangeWhiteSpacesOnly) throws IncorrectOperationException {
  CheckUtil.checkWritable(element);
  if (!SourceTreeToPsiMap.hasTreeElement(element)) {
    return element;
  }

  ASTNode treeElement = element.getNode();
  final PsiFile file = element.getContainingFile();
  if (ExternalFormatProcessor.useExternalFormatter(file)) {
    return ExternalFormatProcessor.formatElement(element, element.getTextRange(), canChangeWhiteSpacesOnly);
  }

  final PsiElement formatted = new CodeFormatterFacade(getSettings(file), element.getLanguage(), canChangeWhiteSpacesOnly).processElement(treeElement).getPsi();
  if (!canChangeWhiteSpacesOnly) {
    return postProcessElement(file, formatted);
  }
  return formatted;
}
 
Example #5
Source File: ASTDelegatePsiElement.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void delete() throws IncorrectOperationException {
  PsiElement parent = getParent();
  if (parent instanceof ASTDelegatePsiElement) {
    CheckUtil.checkWritable(this);
    ((ASTDelegatePsiElement)parent).deleteChildInternal(getNode());
  }
  else if (parent instanceof CompositePsiElement) {
    CheckUtil.checkWritable(this);
    ((CompositePsiElement)parent).deleteChildInternal(getNode());
  }
  else if (parent instanceof PsiFile) {
    CheckUtil.checkWritable(this);
    parent.deleteChildRange(this, this);
  }
  else {
    throw new UnsupportedOperationException(getClass().getName() + " under " + (parent == null ? "null" : parent.getClass().getName()));
  }
}
 
Example #6
Source File: GroovyDslUtil.java    From ok-gradle with Apache License 2.0 5 votes vote down vote up
/**
 * This method is used in order to add elements to the back of a map,
 * it is derived from {@link ASTDelegatePsiElement#addInternal(ASTNode, ASTNode, ASTNode, Boolean)}.
 */
private static PsiElement realAddBefore(@NotNull GrListOrMap element, @NotNull PsiElement newElement, @NotNull PsiElement anchor) {
  CheckUtil.checkWritable(element);
  TreeElement elementCopy = ChangeUtil.copyToElement(newElement);
  ASTNode anchorNode = getAnchorNode(element, anchor.getNode(), true);
  ASTNode newNode = CodeEditUtil.addChildren(element.getNode(), elementCopy, elementCopy, anchorNode);
  if (newNode == null) {
    throw new IncorrectOperationException("Element cannot be added");
  }
  if (newNode instanceof TreeElement) {
    return ChangeUtil.decodeInformation((TreeElement)newNode).getPsi();
  }
  return newNode.getPsi();
}
 
Example #7
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 #8
Source File: LazyParseablePsiElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
private PsiElement addInnerBefore(final PsiElement element, final PsiElement anchor) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
  TreeElement elementCopy = ChangeUtil.copyToElement(element);
  TreeElement treeElement = addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.TRUE);
  if (treeElement != null) return ChangeUtil.decodeInformation(treeElement).getPsi();
  throw new IncorrectOperationException("Element cannot be added");
}
 
Example #9
Source File: CompositePsiElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public PsiElement addAfter(@Nonnull PsiElement element, PsiElement anchor) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
  TreeElement elementCopy = ChangeUtil.copyToElement(element);
  TreeElement treeElement = addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.FALSE);
  return ChangeUtil.decodeInformation(treeElement).getPsi();
}
 
Example #10
Source File: CompositePsiElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void delete() throws IncorrectOperationException {
  LOG.assertTrue(getTreeParent() != null, "Parent not found for " + this);
  CheckUtil.checkWritable(this);
  getTreeParent().deleteChildInternal(this);
  invalidate();
}
 
Example #11
Source File: CompositePsiElement.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 = first.getNode();
  ASTNode lastElement = last.getNode();
  LOG.assertTrue(firstElement.getTreeParent() == this);
  LOG.assertTrue(lastElement.getTreeParent() == this);
  CodeEditUtil.removeChildren(this, firstElement, lastElement);
}
 
Example #12
Source File: CompositePsiElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
private PsiElement addInnerBefore(final PsiElement element, final PsiElement anchor) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
  TreeElement elementCopy = ChangeUtil.copyToElement(element);
  TreeElement treeElement = addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.TRUE);
  if (treeElement != null) return ChangeUtil.decodeInformation(treeElement).getPsi();
  throw new IncorrectOperationException("Element cannot be added");
}
 
Example #13
Source File: LeafPsiElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void delete() throws IncorrectOperationException {
  LOG.assertTrue(getTreeParent() != null);
  CheckUtil.checkWritable(this);
  getTreeParent().deleteChildInternal(this);
  invalidate();
}
 
Example #14
Source File: PsiDirectoryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void checkSetName(String name) throws IncorrectOperationException {
  //CheckUtil.checkIsIdentifier(name);
  CheckUtil.checkWritable(this);
  VirtualFile parentFile = myFile.getParent();
  if (parentFile == null) {
    throw new IncorrectOperationException(VfsBundle.message("cannot.rename.root.directory"));
  }
  VirtualFile child = parentFile.findChild(name);
  if (child != null && !child.equals(myFile)) {
    throw new IncorrectOperationException(VfsBundle.message("file.already.exists.error", child.getPresentableUrl()));
  }
}
 
Example #15
Source File: PsiDirectoryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void checkCreateSubdirectory(@Nonnull String name) throws IncorrectOperationException {
  // TODO : another check?
  //CheckUtil.checkIsIdentifier(name);
  VirtualFile existingFile = getVirtualFile().findChild(name);
  if (existingFile != null) {
    throw new IncorrectOperationException(VfsBundle.message("file.already.exists.error", existingFile.getPresentableUrl()));
  }
  CheckUtil.checkWritable(this);
}
 
Example #16
Source File: PsiDirectoryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void checkCreateFile(@Nonnull String name) throws IncorrectOperationException {
  VirtualFile existingFile = getVirtualFile().findChild(name);
  if (existingFile != null) {
    throw new IncorrectOperationException(VfsBundle.message("file.already.exists.error", existingFile.getPresentableUrl()));
  }

  for (PsiDirectoryMethodProxy proxy : PsiDirectoryMethodProxy.EP_NAME.getExtensionList()) {
    if (!proxy.checkCreateFile(this, name)) {
      return;
    }
  }

  CheckUtil.checkWritable(this);
}
 
Example #17
Source File: OwnBufferLeafPsiElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void delete() throws IncorrectOperationException {
  LOG.assertTrue(getTreeParent() != null);
  CheckUtil.checkWritable(this);
  getTreeParent().deleteChildInternal(this);
  this.invalidate();
}
 
Example #18
Source File: OwnBufferLeafPsiElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public PsiElement replace(@Nonnull PsiElement newElement) throws IncorrectOperationException {
  LOG.assertTrue(getTreeParent() != null);
  CheckUtil.checkWritable(this);
  TreeElement elementCopy = ChangeUtil.copyToElement(newElement);
  getTreeParent().replaceChildInternal(this, elementCopy);
  elementCopy = ChangeUtil.decodeInformation(elementCopy);
  final PsiElement result = SourceTreeToPsiMap.treeElementToPsi(elementCopy);

  this.invalidate();
  return result;
}
 
Example #19
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 #20
Source File: LazyParseablePsiElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void delete() throws IncorrectOperationException {
  LOG.assertTrue(getTreeParent() != null, "Parent not found for " + this);
  CheckUtil.checkWritable(this);
  getTreeParent().deleteChildInternal(this);
  invalidate();
}
 
Example #21
Source File: SqliteMagicLightMethodBuilder.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
@Override
public PsiElement replace(@NotNull PsiElement newElement) throws IncorrectOperationException {
  // just add new element to the containing class
  final PsiClass containingClass = getContainingClass();
  if (null != containingClass) {
    CheckUtil.checkWritable(containingClass);
    return containingClass.add(newElement);
  }
  return null;
}
 
Example #22
Source File: SqliteMagicLightFieldBuilder.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
@Override
public PsiElement replace(@NotNull PsiElement newElement) throws IncorrectOperationException {
  // just add new element to the containing class
  final PsiClass containingClass = getContainingClass();
  if (null != containingClass) {
    CheckUtil.checkWritable(containingClass);
    return containingClass.add(newElement);
  }
  return null;
}
 
Example #23
Source File: LombokLightFieldBuilder.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public PsiElement replace(@NotNull PsiElement newElement) throws IncorrectOperationException {
  // just add new element to the containing class
  final PsiClass containingClass = getContainingClass();
  if (null != containingClass) {
    CheckUtil.checkWritable(containingClass);
    return containingClass.add(newElement);
  }
  return null;
}
 
Example #24
Source File: LombokLightMethodBuilder.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public PsiElement replace(@NotNull PsiElement newElement) throws IncorrectOperationException {
  // just add new element to the containing class
  final PsiClass containingClass = getContainingClass();
  if (null != containingClass) {
    CheckUtil.checkWritable(containingClass);
    return containingClass.add(newElement);
  }
  return null;
}
 
Example #25
Source File: ASTDelegatePsiElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
private PsiElement addInnerBefore(final PsiElement element, final PsiElement anchor) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
  TreeElement elementCopy = ChangeUtil.copyToElement(element);
  ASTNode treeElement = addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.TRUE);
  if (treeElement != null) {
    if (treeElement instanceof TreeElement) {
      return ChangeUtil.decodeInformation((TreeElement) treeElement).getPsi();
    }
    return treeElement.getPsi();
  }
  throw new IncorrectOperationException("Element cannot be added");
}
 
Example #26
Source File: ASTDelegatePsiElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public PsiElement addAfter(@Nonnull PsiElement element, PsiElement anchor) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
  TreeElement elementCopy = ChangeUtil.copyToElement(element);
  ASTNode treeElement = addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.FALSE);
  if (treeElement instanceof TreeElement) {
    return ChangeUtil.decodeInformation((TreeElement) treeElement).getPsi();
  }
  return treeElement.getPsi();
}
 
Example #27
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 #28
Source File: ASTDelegatePsiElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public PsiElement replace(@Nonnull final PsiElement newElement) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
  TreeElement elementCopy = ChangeUtil.copyToElement(newElement);
  if (getParent() instanceof ASTDelegatePsiElement) {
    final ASTDelegatePsiElement parentElement = (ASTDelegatePsiElement)getParent();
    parentElement.replaceChildInternal(this, elementCopy);
  }
  else {
    CodeEditUtil.replaceChild(getParent().getNode(), getNode(), elementCopy);
  }
  elementCopy = ChangeUtil.decodeInformation(elementCopy);
  return SourceTreeToPsiMap.treeElementToPsi(elementCopy);
}
 
Example #29
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 #30
Source File: SharedImplUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static PsiElement doReplace(PsiElement psiElement, TreeElement treeElement, PsiElement newElement) {
  CompositeElement treeParent = treeElement.getTreeParent();
  LOG.assertTrue(treeParent != null);
  CheckUtil.checkWritable(psiElement);
  TreeElement elementCopy = ChangeUtil.copyToElement(newElement);
  treeParent.replaceChildInternal(treeElement, elementCopy);
  elementCopy = ChangeUtil.decodeInformation(elementCopy);
  final PsiElement result = SourceTreeToPsiMap.treeElementToPsi(elementCopy);
  treeElement.invalidate();
  return result;
}