Search in sources :

Example 11 with GrTopStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement in project intellij-community by JetBrains.

the class GroovyPsiElementFactoryImpl method createReferenceExpressionFromText.

@NotNull
@Override
public GrReferenceExpression createReferenceExpressionFromText(@NotNull String idText) {
    PsiFile file = createGroovyFileChecked(idText);
    final GrTopStatement[] statements = ((GroovyFileBase) file).getTopStatements();
    if (!(statements.length == 1 && statements[0] instanceof GrReferenceExpression))
        throw new IncorrectOperationException(idText);
    return (GrReferenceExpression) statements[0];
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrTopStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with GrTopStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement in project intellij-community by JetBrains.

the class GroovyPsiElementFactoryImpl method createReferenceNameFromText.

@Override
@NotNull
public PsiElement createReferenceNameFromText(@NotNull String refName) {
    PsiFile file = createGroovyFileChecked("a." + refName);
    GrTopStatement statement = ((GroovyFileBase) file).getTopStatements()[0];
    if (!(statement instanceof GrReferenceExpression)) {
        throw new IncorrectOperationException("Incorrect reference name: " + refName);
    }
    final PsiElement element = ((GrReferenceExpression) statement).getReferenceNameElement();
    if (element == null) {
        throw new IncorrectOperationException("Incorrect reference name: " + refName);
    }
    return element;
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrTopStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with GrTopStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement in project intellij-community by JetBrains.

the class GroovyCodeStyleManagerImpl method removeImport.

@Override
public void removeImport(@NotNull GroovyFileBase psiFile, @NotNull GrImportStatement importStatement) throws IncorrectOperationException {
    PsiElement psiElement = psiFile;
    PsiElement before = importStatement;
    while (PsiImplUtil.isWhiteSpaceOrNls(before.getPrevSibling())) {
        before = before.getPrevSibling();
    }
    if (PsiImplUtil.hasElementType(before.getPrevSibling(), GroovyTokenTypes.mSEMI))
        before = before.getPrevSibling();
    if (PsiImplUtil.isWhiteSpaceOrNls(before.getPrevSibling()))
        before = before.getPrevSibling();
    PsiElement after = importStatement;
    if (PsiImplUtil.isWhiteSpaceOrNls(after.getNextSibling()))
        after = after.getNextSibling();
    if (PsiImplUtil.hasElementType(after.getNextSibling(), GroovyTokenTypes.mSEMI))
        after = after.getNextSibling();
    while (PsiImplUtil.isWhiteSpaceOrNls(after.getNextSibling())) after = after.getNextSibling();
    if (before == null)
        before = importStatement;
    PsiElement anchor_before = before.getPrevSibling();
    PsiElement anchor_after = after.getNextSibling();
    if (before == after) {
        importStatement.delete();
    } else {
        psiFile.deleteChildRange(before, after);
    }
    if (anchor_before instanceof GrImportStatement && anchor_after instanceof GrImportStatement) {
        addLineFeedAfter(psiFile, (GrImportStatement) anchor_before);
    } else if (anchor_before != null && anchor_after != null) {
        String text = anchor_after instanceof GrTopStatement && anchor_before instanceof GrTopStatement ? "\n\n" : "\n";
        psiElement.getNode().addLeaf(GroovyTokenTypes.mNLS, text, anchor_after.getNode());
    }
}
Also used : GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) PsiElement(com.intellij.psi.PsiElement) GrTopStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement)

Aggregations

GrTopStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement)13 IncorrectOperationException (com.intellij.util.IncorrectOperationException)7 NotNull (org.jetbrains.annotations.NotNull)7 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)4 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)3 GrPackageDefinition (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition)3 PsiElement (com.intellij.psi.PsiElement)2 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)2 FileASTNode (com.intellij.lang.FileASTNode)1 Project (com.intellij.openapi.project.Project)1 PsiClass (com.intellij.psi.PsiClass)1 PsiType (com.intellij.psi.PsiType)1 StubBasedPsiElement (com.intellij.psi.StubBasedPsiElement)1 SyntheticElement (com.intellij.psi.SyntheticElement)1 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)1 Nullable (org.jetbrains.annotations.Nullable)1 GrDocComment (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)1 GrDocCommentOwner (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner)1 GrNamedElement (org.jetbrains.plugins.groovy.lang.psi.GrNamedElement)1 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)1