Java Code Examples for com.intellij.psi.impl.CheckUtil#checkWritable()

The following examples show how to use com.intellij.psi.impl.CheckUtil#checkWritable() . 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
@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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
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 13
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 14
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 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: ASTDelegatePsiElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void checkAdd(@Nonnull final PsiElement element) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
}
 
Example 17
Source File: ASTDelegatePsiElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void checkDelete() throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
}
 
Example 18
Source File: CompositePsiElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public final void checkAdd(@Nonnull PsiElement element) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
}
 
Example 19
Source File: LazyParseablePsiElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public final void checkAdd(@Nonnull PsiElement element) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
}
 
Example 20
Source File: CompositePsiElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void checkDelete() throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
}