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

The following examples show how to use com.intellij.psi.PsiElement#addBefore() . 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: UtilToClassConverter.java    From needsmoredojo with Apache License 2.0 6 votes vote down vote up
public void doRefactor(JSLiteralExpression className, JSExpression[] mixins, List<JSExpressionStatement> methods, JSElement originalReturnStatement, JSVarStatement declarationVariable)
{
    PsiElement parent = originalReturnStatement.getParent();

    JSVariable variable = declarationVariable.getVariables()[0];

    String declareStatement = buildUtilPatternString(className, mixins, methods, variable.getName());
    PsiElement declareExpression = JSUtil.createStatement(parent, declareStatement);
    parent.addBefore(declareExpression, originalReturnStatement);

    // delete all of the old code
    for(JSExpressionStatement method : methods)
    {
        method.delete();
    }
    originalReturnStatement.delete();
    declarationVariable.delete();
}
 
Example 2
Source File: IgnoreHLint.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
@Override
public void invoke(@NotNull final Project project, Editor editor, final PsiFile file) throws IncorrectOperationException {
    PsiWhiteSpace newline = HaskellElementFactory.createNewLine(project);
    HaskellPpragma ppragma = HaskellElementFactory.createPpragmaFromText(
            project, "{-# ANN module (\"HLint: ignore " + hint + "\"::String) #-}");
    FileASTNode fileNode = file.getNode();

    ASTNode[] nodes;
    ASTNode node;

    // If the user has imports, place the pragma after them.
    node = fileNode.findChildByType(HaskellTypes.BODY);
    if (node != null) {
        nodes = node.getChildren(TokenSet.create(HaskellTypes.IMPDECL));
        if (nodes.length > 0) {
            PsiElement element = node.getPsi();
            element.addBefore(newline, element.addAfter(ppragma, nodes[nodes.length - 1].getPsi()));
            return;
        }
    }

    // If the user has a module declaration, place the pragma after it.
    node = fileNode.findChildByType(HaskellTypes.MODULEDECL);
    if (node != null) {
        file.addBefore(newline, file.addAfter(ppragma, node.getPsi()));
        return;
    }

    // If the user has any existing pragmas, place the pragma after them.
    nodes = fileNode.getChildren(TokenSet.create(HaskellTypes.PPRAGMA));
    if (nodes.length > 0) {
        file.addBefore(newline, file.addAfter(ppragma, nodes[nodes.length - 1].getPsi()));
        return;
    }

    // Otherwise, just insert the pragma at the top of the file.
    file.addAfter(newline, file.addBefore(ppragma, file.getFirstChild()));
}
 
Example 3
Source File: CSharpIntroduceHandler.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Nullable
public PsiElement addDeclaration(CSharpIntroduceOperation operation, PsiElement declaration)
{
	PsiElement anchor = operation.isReplaceAll() ? findAnchor(operation.getOccurrences()) : findAnchor(operation.getInitializer());
	if(anchor == null)
	{
		CommonRefactoringUtil.showErrorHint(operation.getProject(), operation.getEditor(), RefactoringBundle.getCannotRefactorMessage(null), RefactoringBundle.getCannotRefactorMessage(null),
				null);
		return null;
	}
	final PsiElement parent = anchor.getParent();
	PsiElement psiElement = parent.addBefore(declaration, anchor);
	CodeStyleManager.getInstance(declaration.getProject()).reformat(psiElement);
	return psiElement;
}
 
Example 4
Source File: JSUtil.java    From needsmoredojo with Apache License 2.0 5 votes vote down vote up
public static PsiElement addStatementBeforeElement(PsiElement parent, PsiElement element, String statement, String whitespace)
{
    ASTNode node = JSChangeUtil.createStatementFromText(parent.getProject(), statement, JSUtils.getDialect(parent.getContainingFile()));
    parent.addBefore(node.getPsi(), element);

    if(!whitespace.equals(""))
    {
        parent.addBefore(JSChangeUtil.createJSTreeFromText(parent.getProject(), whitespace).getPsi(), element);
    }

    return node.getPsi();
}
 
Example 5
Source File: PhpDocUtil.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
private static void addPhpDocTag(@NotNull PhpNamedElement forElement, @NotNull Document document, @NotNull PsiFile file, @NotNull  PsiElement beforeElement, @NotNull String annotationClass, @Nullable String tagParameter) {

        String phpDocTagName = getQualifiedName(forElement, annotationClass);
        if(phpDocTagName == null) {
            return;
        }

        String tagString = "@" + phpDocTagName;
        if(tagParameter != null) {
            tagString += "(" + tagParameter + ")";
        }

        PhpDocComment docComment = forElement.getDocComment();

        if(docComment != null)  {

            PsiElement elementToInsert = PhpPsiElementFactory.createFromText(forElement.getProject(), PhpDocTag.class, "/** " + tagString + " */\\n");
            if(elementToInsert == null) {
                return;
            }

            PsiElement fromText = PhpPsiElementFactory.createFromText(forElement.getProject(), PhpDocTokenTypes.DOC_LEADING_ASTERISK, "/** \n * @var */");
            docComment.addBefore(fromText, docComment.getLastChild());
            docComment.addBefore(elementToInsert, docComment.getLastChild());

            PsiDocumentManager.getInstance(forElement.getProject()).doPostponedOperationsAndUnblockDocument(document);
            PsiDocumentManager.getInstance(forElement.getProject()).commitDocument(document);

            return;
        }

        // new PhpDoc see PhpDocCommentGenerator
        docComment = PhpPsiElementFactory.createFromText(forElement.getProject(), PhpDocComment.class, "/**\n " + tagString + " \n */");
        if(docComment == null) {
            return;
        }

        PsiElement parent = beforeElement.getParent();
        int atOffset = beforeElement.getTextRange().getStartOffset() + 1;
        parent.addBefore(docComment, beforeElement);
        PsiDocumentManager.getInstance(forElement.getProject()).doPostponedOperationsAndUnblockDocument(document);
        PsiElement atElement = file.findElementAt(atOffset);
        if (atElement != null)            {
            PsiElement docParent = PsiTreeUtil.findFirstParent(atElement, true, element -> ((element instanceof PhpDocComment)) || ((element instanceof PhpFile)));
            if ((docParent instanceof PhpDocComment)) {
                CodeStyleManager.getInstance(forElement.getProject()).reformatNewlyAddedElement(docParent.getParent().getNode(), docParent.getNode());
            }
        }

        PsiDocumentManager.getInstance(forElement.getProject()).doPostponedOperationsAndUnblockDocument(document);
        PsiDocumentManager.getInstance(forElement.getProject()).commitDocument(document);

    }