Search in sources :

Example 1 with GrPackageDefinition

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

the class GroovyCompletionData method suggestPrimitiveTypes.

private static boolean suggestPrimitiveTypes(PsiElement context) {
    if (isInfixOperatorPosition(context))
        return false;
    if (isAfterForParameter(context))
        return false;
    final PsiElement parent = context.getParent();
    if (parent == null)
        return false;
    PsiElement previous = PsiImplUtil.realPrevious(parent.getPrevSibling());
    if (parent instanceof GrReferenceElement && parent.getParent() instanceof GrArgumentList) {
        PsiElement prevSibling = context.getPrevSibling();
        if (prevSibling != null && prevSibling.getNode() != null) {
            if (!TokenSets.DOTS.contains(prevSibling.getNode().getElementType())) {
                return true;
            }
        } else if (!(previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType()))) {
            return true;
        }
    }
    if (GroovyCompletionUtil.isTupleVarNameWithoutTypeDeclared(context))
        return true;
    if (previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType())) {
        return false;
    }
    if (GroovyCompletionUtil.asSimpleVariable(context) || GroovyCompletionUtil.asTypedMethod(context) || GroovyCompletionUtil.asVariableInBlock(context) || asVariableAfterModifiers(context)) {
        return true;
    }
    if ((parent instanceof GrParameter && ((GrParameter) parent).getTypeElementGroovy() == null) || parent instanceof GrReferenceElement && !(parent.getParent() instanceof GrImportStatement) && !(parent.getParent() instanceof GrPackageDefinition) && !(parent.getParent() instanceof GrArgumentList)) {
        PsiElement prevSibling = context.getPrevSibling();
        if (parent instanceof GrReferenceElement && prevSibling != null && prevSibling.getNode() != null) {
            ASTNode node = prevSibling.getNode();
            return !TokenSets.DOTS.contains(node.getElementType());
        } else {
            return true;
        }
    }
    if (PsiImplUtil.realPrevious(parent.getPrevSibling()) instanceof GrModifierList) {
        return true;
    }
    if (PsiImplUtil.realPrevious(context.getPrevSibling()) instanceof GrModifierList) {
        return true;
    }
    return parent instanceof GrExpression && parent.getParent() instanceof GroovyFile && GroovyCompletionUtil.isNewStatement(context, false);
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) ASTNode(com.intellij.lang.ASTNode) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 2 with GrPackageDefinition

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

the class GroovyCodeStyleManagerImpl method addLineFeedBefore.

protected void addLineFeedBefore(@NotNull PsiElement psiFile, @NotNull GrImportStatement result) {
    final CodeStyleSettings commonSettings = CodeStyleSettingsManager.getInstance(psiFile.getProject()).getCurrentSettings();
    final GroovyCodeStyleSettings settings = commonSettings.getCustomSettings(GroovyCodeStyleSettings.class);
    final PackageEntryTable layoutTable = settings.IMPORT_LAYOUT_TABLE;
    final PackageEntry[] entries = layoutTable.getEntries();
    PsiElement prev = result.getPrevSibling();
    while (PsiImplUtil.isWhiteSpaceOrNls(prev)) {
        prev = prev.getPrevSibling();
    }
    if (PsiImplUtil.hasElementType(prev, GroovyTokenTypes.mSEMI))
        prev = prev.getPrevSibling();
    if (PsiImplUtil.isWhiteSpaceOrNls(prev))
        prev = prev.getPrevSibling();
    ASTNode node = psiFile.getNode();
    if (prev instanceof GrImportStatement) {
        final int idx_before = getPackageEntryIdx(entries, (GrImportStatement) prev);
        final int idx = getPackageEntryIdx(entries, result);
        final int spaceCount = getMaxSpaceCount(entries, idx_before, idx);
        //skip space and semicolon after import
        if (PsiImplUtil.isWhiteSpaceOrNls(prev.getNextSibling()) && PsiImplUtil.hasElementType(prev.getNextSibling().getNextSibling(), GroovyTokenTypes.mSEMI))
            prev = prev.getNextSibling().getNextSibling();
        while (PsiImplUtil.isWhiteSpaceOrNls(prev.getNextSibling())) {
            node.removeChild(prev.getNextSibling().getNode());
        }
        node.addLeaf(GroovyTokenTypes.mNLS, StringUtil.repeat("\n", spaceCount + 1), result.getNode());
    } else if (prev instanceof GrPackageDefinition) {
        node.addLeaf(GroovyTokenTypes.mNLS, StringUtil.repeat("\n", commonSettings.BLANK_LINES_AFTER_PACKAGE), result.getNode());
    }
}
Also used : CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) PackageEntry(com.intellij.psi.codeStyle.PackageEntry) PackageEntryTable(com.intellij.psi.codeStyle.PackageEntryTable) ASTNode(com.intellij.lang.ASTNode) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) PsiElement(com.intellij.psi.PsiElement)

Example 3 with GrPackageDefinition

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

the class GroovyCodeStyleManagerImpl method getAnchorToInsertImportAfter.

@Nullable
private PsiElement getAnchorToInsertImportAfter(@NotNull GroovyFile psiFile, @NotNull GrImportStatement statement) {
    final GroovyCodeStyleSettings settings = CodeStyleSettingsManager.getInstance(psiFile.getProject()).getCurrentSettings().getCustomSettings(GroovyCodeStyleSettings.class);
    final PackageEntryTable layoutTable = settings.IMPORT_LAYOUT_TABLE;
    final PackageEntry[] entries = layoutTable.getEntries();
    GrImportStatement[] importStatements = psiFile.getImportStatements();
    if (importStatements.length == 0) {
        final GrPackageDefinition definition = psiFile.getPackageDefinition();
        if (definition != null) {
            return definition;
        }
        return getShellComment(psiFile);
    }
    final Comparator<GrImportStatement> comparator = GroovyImportOptimizer.getComparator(settings);
    final int idx = getPackageEntryIdx(entries, statement);
    PsiElement anchor = null;
    for (GrImportStatement importStatement : importStatements) {
        final int i = getPackageEntryIdx(entries, importStatement);
        if (i < idx) {
            anchor = importStatement;
        } else if (i > idx) {
            break;
        } else if (comparator.compare(statement, importStatement) > 0) {
            anchor = importStatement;
        } else {
            break;
        }
    }
    if (anchor == null)
        anchor = psiFile.getPackageDefinition();
    if (anchor == null)
        anchor = getShellComment(psiFile);
    if (anchor == null && importStatements.length > 0)
        anchor = importStatements[0].getPrevSibling();
    return anchor;
}
Also used : PackageEntry(com.intellij.psi.codeStyle.PackageEntry) PackageEntryTable(com.intellij.psi.codeStyle.PackageEntryTable) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with GrPackageDefinition

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

the class MoveGroovyClassHandler method doMoveClass.

@Override
public PsiClass doMoveClass(@NotNull PsiClass aClass, @NotNull PsiDirectory moveDestination) throws IncorrectOperationException {
    if (!aClass.getLanguage().equals(GroovyLanguage.INSTANCE))
        return null;
    PsiFile file = aClass.getContainingFile();
    if (!(file instanceof GroovyFile))
        return null;
    final PsiPackage newPackage = JavaDirectoryService.getInstance().getPackage(moveDestination);
    LOG.assertTrue(newPackage != null);
    PsiClass newClass = null;
    final String newPackageName = newPackage.getQualifiedName();
    if (aClass instanceof GroovyScriptClass) {
        final PsiClass[] classes = ((GroovyFile) file).getClasses();
        if (classes.length == 1) {
            if (!moveDestination.equals(file.getContainingDirectory())) {
                Project project = file.getProject();
                MoveFilesOrDirectoriesUtil.doMoveFile(file, moveDestination);
                DumbService.getInstance(project).completeJustSubmittedTasks();
                file = moveDestination.findFile(file.getName());
                assert file != null;
                ((PsiClassOwner) file).setPackageName(newPackageName);
            }
            return ((GroovyFile) file).getScriptClass();
        }
        //script class is moved the first from the file due to MoveClassOrPackageProcessor:88 (element sort)
        correctSelfReferences(aClass, newPackage);
        final GroovyFile newFile = generateNewScript((GroovyFile) file, newPackage);
        for (PsiElement child : file.getChildren()) {
            if (!(child instanceof GrTopStatement || child instanceof PsiComment))
                continue;
            if (child instanceof PsiClass || child instanceof GrImportStatement || child instanceof GrPackageDefinition)
                continue;
            if (child instanceof GrDocComment) {
                final GrDocCommentOwner owner = GrDocCommentUtil.findDocOwner((GrDocComment) child);
                if (owner instanceof PsiClass)
                    continue;
            }
            child.delete();
        }
        if (!moveDestination.equals(file.getContainingDirectory())) {
            moveDestination.add(newFile);
        //aClass.getManager().moveFile(newFile, moveDestination);
        }
        newClass = newFile.getClasses()[0];
        correctOldClassReferences(newClass, aClass);
    } else {
        if (!moveDestination.equals(file.getContainingDirectory()) && moveDestination.findFile(file.getName()) != null) {
            // moving second of two classes which were in the same file to a different directory (IDEADEV-3089)
            correctSelfReferences(aClass, newPackage);
            PsiFile newFile = moveDestination.findFile(file.getName());
            final FileASTNode fileNode = newFile.getNode();
            fileNode.addChild(Factory.createSingleLeafElement(GroovyTokenTypes.mNLS, "\n\n", 0, 2, null, aClass.getManager()));
            final PsiDocComment docComment = aClass.getDocComment();
            if (docComment != null) {
                newFile.add(docComment);
                fileNode.addChild(Factory.createSingleLeafElement(GroovyTokenTypes.mNLS, "\n", 0, 1, null, aClass.getManager()));
            }
            newClass = (GrTypeDefinition) newFile.add(aClass);
            correctOldClassReferences(newClass, aClass);
            aClass.delete();
        } else if (((GroovyFile) file).getClasses().length > 1) {
            correctSelfReferences(aClass, newPackage);
            Project project = aClass.getProject();
            PsiFileFactory fileFactory = PsiFileFactory.getInstance(project);
            GroovyFile newFile = (GroovyFile) moveDestination.add(fileFactory.createFileFromText(aClass.getName() + "." + GroovyFileType.DEFAULT_EXTENSION, GroovyLanguage.INSTANCE, "class XXX {}"));
            final PsiClass created = newFile.getClasses()[0];
            PsiDocComment docComment = aClass.getDocComment();
            if (docComment != null) {
                newFile.addBefore(docComment, created);
                docComment.delete();
            }
            newClass = (PsiClass) created.replace(aClass);
            setPackageDefinition((GroovyFile) file, newFile, newPackageName);
            correctOldClassReferences(newClass, aClass);
            aClass.delete();
        }
    }
    return newClass;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) GrDocCommentOwner(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) Project(com.intellij.openapi.project.Project) FileASTNode(com.intellij.lang.FileASTNode) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrTopStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement)

Example 5 with GrPackageDefinition

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

the class MoveGroovyClassHandler method generateNewScript.

private static GroovyFile generateNewScript(GroovyFile file, PsiPackage newPackage) {
    for (GrImportStatement importStatement : file.getImportStatements()) {
        importStatement.delete();
    }
    final GroovyFile newFile = GroovyPsiElementFactory.getInstance(file.getProject()).createGroovyFile("", true, null);
    newFile.addRange(file.getFirstChild(), file.getLastChild());
    final PsiClass[] newFileClasses = newFile.getClasses();
    for (PsiClass psiClass : newFileClasses) {
        if (psiClass instanceof GroovyScriptClass)
            continue;
        final GrDocComment docComment = GrDocCommentUtil.findDocComment((GrDocCommentOwner) psiClass);
        if (docComment != null)
            docComment.delete();
        psiClass.delete();
    }
    final GrPackageDefinition packageDefinition = newFile.getPackageDefinition();
    if (packageDefinition != null)
        packageDefinition.delete();
    PsiElement cur = newFile.getFirstChild();
    while (cur != null && PsiImplUtil.isWhiteSpaceOrNls(cur)) {
        cur = cur.getNextSibling();
    }
    if (cur != null && cur != newFile.getFirstChild()) {
        cur = cur.getPrevSibling();
        newFile.deleteChildRange(newFile.getFirstChild(), cur);
    }
    cur = newFile.getLastChild();
    while (cur != null && PsiImplUtil.isWhiteSpaceOrNls(cur)) {
        cur = cur.getPrevSibling();
    }
    if (cur != null && cur != newFile.getLastChild()) {
        cur = cur.getNextSibling();
        newFile.deleteChildRange(cur, newFile.getLastChild());
    }
    newFile.setName(file.getName());
    setPackageDefinition(file, newFile, newPackage.getQualifiedName());
    GroovyChangeContextUtil.decodeContextInfo(newFile, null, null);
    return newFile;
}
Also used : GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)

Aggregations

GrPackageDefinition (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition)13 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)7 Nullable (org.jetbrains.annotations.Nullable)4 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)4 ASTNode (com.intellij.lang.ASTNode)3 GrTopStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement)3 PsiElement (com.intellij.psi.PsiElement)2 PackageEntry (com.intellij.psi.codeStyle.PackageEntry)2 PackageEntryTable (com.intellij.psi.codeStyle.PackageEntryTable)2 GrDocComment (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)2 GroovyDocPsiElement (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GroovyDocPsiElement)2 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)2 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)2 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)2 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)2 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)2 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)2 GroovyScriptClass (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)2 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 FileASTNode (com.intellij.lang.FileASTNode)1