Java Code Examples for com.intellij.psi.PsiElement#delete()

The following examples show how to use com.intellij.psi.PsiElement#delete() . 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: CSharpCatchStatementImpl.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@RequiredReadAction
public void deleteVariable()
{
	CSharpLocalVariable variable = getVariable();
	if(variable == null)
	{
		return;
	}

	PsiElement lparElement = variable.getPrevSibling();
	PsiElement rparElement = variable.getNextSibling();

	((CSharpLocalVariableImpl) variable).deleteInternal();

	if(PsiUtilCore.getElementType(lparElement) == CSharpTokens.LPAR)
	{
		lparElement.delete();
	}

	if(PsiUtilCore.getElementType(rparElement) == CSharpTokens.RPAR)
	{
		rparElement.delete();
	}
}
 
Example 2
Source File: CSharpCodeBodyProxyImpl.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Override
@RequiredWriteAction
public void replaceBySemicolon()
{
	ASTNode lazyNode = myMethodElement.getNode().findChildByType(CSharpElements.METHOD_BODIES);
	if(lazyNode != null)
	{
		lazyNode.getPsi().delete();
	}
	else
	{
		PsiElement element = getElement();
		if(element != null)
		{
			element.delete();
		}
	}

	myMethodElement.getNode().addLeaf(CSharpTokens.SEMICOLON, ";", null);
}
 
Example 3
Source File: CS0145.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Override
public void invoke(@Nonnull Project project, Editor editor, PsiFile file) throws IncorrectOperationException
{
	DotNetVariable element = myVariablePointer.getElement();
	if(element == null)
	{
		return;
	}

	PsiDocumentManager.getInstance(project).commitAllDocuments();
	PsiElement constantKeywordElement = element.getConstantKeywordElement();
	if(constantKeywordElement == null)
	{
		return;
	}

	PsiElement nextSibling = constantKeywordElement.getNextSibling();

	constantKeywordElement.delete();
	if(nextSibling instanceof PsiWhiteSpace)
	{
		element.getNode().removeChild(nextSibling.getNode());
	}
}
 
Example 4
Source File: GroovyDslUtil.java    From ok-gradle with Apache License 2.0 5 votes vote down vote up
static void deletePsiElement(@NotNull GradleDslElement context, @Nullable PsiElement psiElement) {
  if (psiElement == null || !psiElement.isValid()) {
    return;
  }

  PsiElement parent = psiElement.getParent();
  psiElement.delete();

  maybeDeleteIfEmpty(parent, context);

  // Now we have deleted all empty PsiElements in the Psi tree, we also need to make sure
  // to clear any invalid PsiElements in the GradleDslElement tree otherwise we will
  // be prevented from recreating these elements.
  removePsiIfInvalid(context);
}
 
Example 5
Source File: CSharpModifierListImplUtil.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
public static void removeModifier(@Nonnull CSharpModifierList modifierList, @Nonnull DotNetModifier modifier)
{
	CSharpModifier as = CSharpModifier.as(modifier);
	PsiElement modifierElement = modifierList.getModifierElement(as);
	if(modifierElement != null)
	{
		PsiElement next = modifierElement.getNextSibling();
		if(next instanceof PsiWhiteSpace)
		{
			next.delete();
		}

		modifierElement.delete();
	}
}
 
Example 6
Source File: CSharpCodeBodyProxyImpl.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
public void replace(@Nullable PsiElement newElement)
{
	ASTNode lazyNode = myMethodElement.getNode().findChildByType(CSharpElements.METHOD_BODIES);
	if(lazyNode != null)
	{
		if(newElement == null)
		{
			lazyNode.getPsi().delete();
		}
		else
		{
			lazyNode.getPsi().replace(newElement);
		}
	}
	else
	{
		PsiElement element = getElement();

		if(newElement != null)
		{
			if(element == null)
			{
				myMethodElement.add(newElement);
			}
			else
			{
				element.replace(newElement);
			}
		}
		else if(element != null)
		{
			element.delete();
		}
	}
}
 
Example 7
Source File: CS0168.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
public void invoke(@Nonnull Project project,
		@Nonnull PsiFile psiFile,
		@Nullable Editor editor,
		@Nonnull PsiElement psiElement,
		@Nonnull PsiElement psiElement1)
{
	psiElement.delete();
}
 
Example 8
Source File: IgnoreRemoveEntryFix.java    From idea-gitignore with MIT License 5 votes vote down vote up
/**
 * Handles QuickFix action invoked on {@link IgnoreEntry}.
 *
 * @param project      the {@link Project} containing the working file
 * @param file         the {@link PsiFile} containing handled entry
 * @param startElement the {@link IgnoreEntry} that will be removed
 * @param endElement   the {@link PsiElement} which is ignored in invoked action
 */
@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file,
                   @Nullable("is null when called from inspection") Editor editor,
                   @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    if (startElement instanceof IgnoreEntry) {
        removeCrlf(startElement);
        startElement.delete();
    }
}
 
Example 9
Source File: AMDPsiUtil.java    From needsmoredojo with Apache License 2.0 5 votes vote down vote up
public static void removeSingleImport(@NotNull AMDImport amdImport)
{
    JSArrayLiteralExpression literal = (JSArrayLiteralExpression) amdImport.getLiteral().getParent();
    PsiElement function = amdImport.getParameter().getParent();

    Set<PsiElement> elementsToDelete = new LinkedHashSet<PsiElement>();

    // if there is an /*NMD:Ignore*/ comment, delete it as well.
    PsiElement ignoreComment = getIgnoreCommentAfterLiteral(amdImport.getLiteral());
    if(ignoreComment != null)
    {
        elementsToDelete.add(ignoreComment);
    }

    removeParameter(amdImport.getParameter(), elementsToDelete);
    AMDPsiUtil.removeDefineLiteral(amdImport.getLiteral(), elementsToDelete);

    for(PsiElement element : elementsToDelete)
    {
        try
        {
            element.delete();
        }
        catch(Exception e)
        {
            // something happened, but it's probably not important when deleting.
        }
    }

    AMDPsiUtil.removeTrailingCommas(elementsToDelete, literal, function);
}
 
Example 10
Source File: SafeDeleteReferenceSimpleDeleteUsageInfo.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void deleteElement() throws IncorrectOperationException {
  if(isSafeDelete()) {
    PsiElement element = getElement();
    LOG.assertTrue(element != null);
    element.delete();
  }
}
 
Example 11
Source File: GroovyDslWriter.java    From ok-gradle with Apache License 2.0 4 votes vote down vote up
@Override
public PsiElement moveDslElement(@NotNull GradleDslElement element) {
  // 1. Get the anchor where we need to move the element to.
  GradleDslElement anchorAfter = element.getAnchor();

  GroovyPsiElement psiElement = ensureGroovyPsi(element.getPsiElement());
  if (psiElement == null) {
    return null;
  }

  PsiElement parentPsiElement = getParentPsi(element);
  if (parentPsiElement == null) {
    return null;
  }

  PsiElement anchor = getPsiElementForAnchor(parentPsiElement, anchorAfter);

  // 2. Create a dummy element that we can move the element to.
  GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(parentPsiElement.getProject());
  PsiElement lineTerminator = factory.createLineTerminator(1);
  PsiElement toReplace = parentPsiElement.addAfter(lineTerminator, anchor);

  // 3. Find the element we need to actually replace. The psiElement we have may be a child of what we need.
  PsiElement e = element.getPsiElement();
  while (!(e.getParent() instanceof GroovyFile || e.getParent() instanceof GrClosableBlock)) {
    // Make sure e isn't going to be set to null.
    if (e.getParent() == null) {
      e = element.getPsiElement();
      break;
    }
    e = e.getParent();
  }

  // 4. Copy the old PsiElement tree.
  PsiElement treeCopy = e.copy();

  // 5. Replace what we need to replace.
  PsiElement newTree = toReplace.replace(treeCopy);

  // 6. Delete the original tree.
  e.delete();

  // 7. Set the new PsiElement. Note: The internal state of this element will have invalid elements. It is required to reparse the file
  // to obtain the correct elements.
  element.setPsiElement(newTree);

  return element.getPsiElement();
}
 
Example 12
Source File: CSharpLocalVariableImpl.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Override
public void delete() throws IncorrectOperationException
{
	PsiElement parent = getParent();
	if(parent instanceof CSharpLocalVariableDeclarationStatement)
	{
		List<PsiElement> collectForDelete = new SmartList<PsiElement>();

		CSharpLocalVariable[] variables = ((CSharpLocalVariableDeclarationStatement) parent).getVariables();
		if(variables.length == 1)
		{
			collectForDelete.add(parent);
		}
		/*else
		{
			// first variable cant remove type
			if(variables[0] == this)
			{
				DotNetExpression initializer = getInitializer();
				if(initializer != null)
				{
					removeWithPrevSibling(initializer, CSharpTokens.EQ, collectForDelete);
					removeWithNextSibling(initializer, CSharpTokens.COMMA, collectForDelete);
					collectForDelete.add(initializer);
				}

				PsiElement nameIdentifier = getNameIdentifier();
				if(nameIdentifier != null)
				{
					collectForDelete.add(nameIdentifier);
					removeWithNextSibling(nameIdentifier, CSharpTokens.COMMA, collectForDelete);
				}
			}
			else
			{
				removeWithPrevSibling(this, CSharpTokens.COMMA, collectForDelete);
				collectForDelete.add(this);
			}
		}  */

		for(PsiElement element : collectForDelete)
		{
			element.delete();
		}
	}
	else if(parent instanceof CSharpCatchStatementImpl)
	{
		((CSharpCatchStatementImpl) parent).deleteVariable();
	}
	else
	{
		deleteInternal();
	}
}
 
Example 13
Source File: UnnecessaryEnumUnderlyingTypeInspection.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Override
public void invoke(@Nonnull Project project, @Nonnull PsiFile psiFile, @Nonnull PsiElement psiElement, @Nonnull PsiElement psiElement1)
{
	psiElement.delete();
}
 
Example 14
Source File: UnnecessarySemicolonInspection.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Override
public void invoke(@Nonnull Project project, @Nonnull PsiFile file, @Nonnull PsiElement startElement, @Nonnull PsiElement endElement)
{
	startElement.delete();
}
 
Example 15
Source File: RemoveElementQuickFix.java    From intellij-xquery with Apache License 2.0 4 votes vote down vote up
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor problemDescriptor) {
    PsiElement psiElement = problemDescriptor.getPsiElement();
    if (psiElement != null) psiElement.delete();
}