下面列出了com.intellij.psi.PsiElement#delete ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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();
}
}
@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);
}
@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());
}
}
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);
}
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();
}
}
@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();
}
}
}
@Override
public void invoke(@Nonnull Project project,
@Nonnull PsiFile psiFile,
@Nullable Editor editor,
@Nonnull PsiElement psiElement,
@Nonnull PsiElement psiElement1)
{
psiElement.delete();
}
/**
* 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();
}
}
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);
}
@Override
public void deleteElement() throws IncorrectOperationException {
if(isSafeDelete()) {
PsiElement element = getElement();
LOG.assertTrue(element != null);
element.delete();
}
}
@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();
}
@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();
}
}
@Override
public void invoke(@Nonnull Project project, @Nonnull PsiFile psiFile, @Nonnull PsiElement psiElement, @Nonnull PsiElement psiElement1)
{
psiElement.delete();
}
@Override
public void invoke(@Nonnull Project project, @Nonnull PsiFile file, @Nonnull PsiElement startElement, @Nonnull PsiElement endElement)
{
startElement.delete();
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor problemDescriptor) {
PsiElement psiElement = problemDescriptor.getPsiElement();
if (psiElement != null) psiElement.delete();
}