Search in sources :

Example 6 with GroovyFile

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

the class GStringTypedActionHandler method charTyped.

@Override
public Result charTyped(char c, Project project, @NotNull Editor editor, @NotNull PsiFile file) {
    if (c != '{' || project == null || !HandlerUtils.canBeInvoked(editor, project)) {
        return Result.CONTINUE;
    }
    if (!(file instanceof GroovyFile))
        return Result.CONTINUE;
    int caret = editor.getCaretModel().getOffset();
    final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    if (caret < 1)
        return Result.CONTINUE;
    HighlighterIterator iterator = highlighter.createIterator(caret - 1);
    if (iterator.getTokenType() != GroovyTokenTypes.mLCURLY)
        return Result.CONTINUE;
    iterator.retreat();
    if (iterator.atEnd() || iterator.getTokenType() != GroovyTokenTypes.mDOLLAR)
        return Result.CONTINUE;
    iterator.advance();
    if (iterator.atEnd())
        return Result.CONTINUE;
    iterator.advance();
    if (iterator.getTokenType() != GroovyTokenTypes.mGSTRING_BEGIN)
        return Result.CONTINUE;
    editor.getDocument().insertString(caret, "}");
    return Result.STOP;
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 7 with GroovyFile

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

the class GroovyShellLanguageConsoleView method prepareTypeDefinition.

@NotNull
private GrTypeDefinition prepareTypeDefinition(@NotNull GrTypeDefinition typeDefinition) {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
    GroovyFile file = factory.createGroovyFile("", false, getFile());
    return (GrTypeDefinition) file.add(typeDefinition);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with GroovyFile

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

the class GroovyShortenFQNamesProcessor method processText.

@Override
public void processText(final Project project, final Template template, final Document document, final RangeMarker templateRange, final Editor editor) {
    if (!template.isToShortenLongNames())
        return;
    PsiDocumentManager.getInstance(project).commitDocument(document);
    final PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project);
    if (file instanceof GroovyFile) {
        DumbService.getInstance(project).withAlternativeResolveEnabled(() -> {
            JavaCodeStyleManager.getInstance(project).shortenClassReferences(file, templateRange.getStartOffset(), templateRange.getEndOffset());
        });
    }
    PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
}
Also used : PsiFile(com.intellij.psi.PsiFile) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 9 with GroovyFile

use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile 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 10 with GroovyFile

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

the class GroovyScriptRunConfiguration method getRefactoringElementListener.

@Override
public RefactoringElementListener getRefactoringElementListener(PsiElement element) {
    if (scriptPath == null || !scriptPath.equals(getPathByElement(element))) {
        return null;
    }
    final PsiClass classToRun = GroovyRunnerPsiUtil.getRunningClass(element);
    if (element instanceof GroovyFile) {
        return new RefactoringElementAdapter() {

            @Override
            protected void elementRenamedOrMoved(@NotNull PsiElement newElement) {
                if (newElement instanceof GroovyFile) {
                    GroovyFile file = (GroovyFile) newElement;
                    setScriptPath(ScriptFileUtil.getScriptFilePath(file.getVirtualFile()));
                }
            }

            @Override
            public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
                elementRenamedOrMoved(newElement);
            }
        };
    } else if (element instanceof PsiClass && element.getManager().areElementsEquivalent(element, classToRun)) {
        return new RefactoringElementAdapter() {

            @Override
            protected void elementRenamedOrMoved(@NotNull PsiElement newElement) {
                setName(((PsiClass) newElement).getName());
            }

            @Override
            public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
                elementRenamedOrMoved(newElement);
            }
        };
    }
    return null;
}
Also used : PsiClass(com.intellij.psi.PsiClass) NotNull(org.jetbrains.annotations.NotNull) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) PsiElement(com.intellij.psi.PsiElement) RefactoringElementAdapter(com.intellij.refactoring.listeners.RefactoringElementAdapter)

Aggregations

GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)91 PsiFile (com.intellij.psi.PsiFile)26 PsiElement (com.intellij.psi.PsiElement)21 NotNull (org.jetbrains.annotations.NotNull)17 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)17 VirtualFile (com.intellij.openapi.vfs.VirtualFile)13 Project (com.intellij.openapi.project.Project)10 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)10 PsiClass (com.intellij.psi.PsiClass)9 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)9 Nullable (org.jetbrains.annotations.Nullable)8 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 GroovyScriptClass (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)8 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)7 Module (com.intellij.openapi.module.Module)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)6 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)6 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)6 ASTNode (com.intellij.lang.ASTNode)5