Search in sources :

Example 71 with Nullable

use of org.jetbrains.annotations.Nullable in project intellij-community by JetBrains.

the class GroovyGeneratePropertyMissingHandler method genSetter.

@Nullable
private static GrMethod genSetter(PsiClass aClass, FileTemplate template) {
    Properties properties = FileTemplateManager.getInstance(aClass.getProject()).getDefaultProperties();
    properties.setProperty(FileTemplate.ATTRIBUTE_RETURN_TYPE, "void");
    properties.setProperty(FileTemplate.ATTRIBUTE_DEFAULT_RETURN_VALUE, "");
    properties.setProperty(FileTemplate.ATTRIBUTE_CALL_SUPER, "");
    properties.setProperty(FileTemplate.ATTRIBUTE_CLASS_NAME, aClass.getQualifiedName());
    properties.setProperty(FileTemplate.ATTRIBUTE_SIMPLE_CLASS_NAME, aClass.getName());
    properties.setProperty(FileTemplate.ATTRIBUTE_METHOD_NAME, "propertyMissing");
    String bodyText;
    try {
        bodyText = StringUtil.replace(template.getText(properties), ";", "");
    } catch (IOException e) {
        return null;
    }
    return GroovyPsiElementFactory.getInstance(aClass.getProject()).createMethodFromText("def propertyMissing(String name, def arg) {\n" + bodyText + "\n}");
}
Also used : IOException(java.io.IOException) Properties(java.util.Properties) Nullable(org.jetbrains.annotations.Nullable)

Example 72 with Nullable

use of org.jetbrains.annotations.Nullable in project intellij-community by JetBrains.

the class CreateClassActionBase method createClassByType.

@Nullable
public static GrTypeDefinition createClassByType(@NotNull final PsiDirectory directory, @NotNull final String name, @NotNull final PsiManager manager, @Nullable final PsiElement contextElement, @NotNull final String templateName, boolean allowReformatting) {
    return WriteAction.compute(() -> {
        try {
            GrTypeDefinition targetClass = null;
            try {
                PsiFile file = GroovyTemplatesFactory.createFromTemplate(directory, name, name + ".groovy", templateName, allowReformatting);
                for (PsiElement element : file.getChildren()) {
                    if (element instanceof GrTypeDefinition) {
                        targetClass = ((GrTypeDefinition) element);
                        break;
                    }
                }
                if (targetClass == null) {
                    throw new IncorrectOperationException(GroovyBundle.message("no.class.in.file.template"));
                }
            } catch (final IncorrectOperationException e) {
                ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog(GroovyBundle.message("cannot.create.class.error.text", name, e.getLocalizedMessage()), GroovyBundle.message("cannot.create.class.error.title")));
                return null;
            }
            PsiModifierList modifiers = targetClass.getModifierList();
            if (contextElement != null && !JavaPsiFacade.getInstance(manager.getProject()).getResolveHelper().isAccessible(targetClass, contextElement, null) && modifiers != null) {
                modifiers.setModifierProperty(PsiModifier.PUBLIC, true);
            }
            return targetClass;
        } catch (IncorrectOperationException e) {
            LOG.error(e);
            return null;
        }
    });
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Nullable(org.jetbrains.annotations.Nullable)

Example 73 with Nullable

use of org.jetbrains.annotations.Nullable in project intellij-community by JetBrains.

the class GroovyEnterHandler method inferStringPair.

@Nullable
private static PsiElement inferStringPair(PsiFile file, int caretOffset) {
    PsiElement stringElement = file.findElementAt(caretOffset - 1);
    if (stringElement == null)
        return null;
    ASTNode node = stringElement.getNode();
    if (node == null)
        return null;
    // For expression injection in GString like "abc ${}<caret>  abc"
    if (!INNER_STRING_TOKENS.contains(node.getElementType()) && checkGStringInjection(stringElement)) {
        stringElement = stringElement.getParent().getParent().getNextSibling();
        if (stringElement == null)
            return null;
    }
    return stringElement;
}
Also used : ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable)

Example 74 with Nullable

use of org.jetbrains.annotations.Nullable in project intellij-community by JetBrains.

the class DynamicDialog method createDocument.

@Nullable
private Document createDocument(final String text) {
    GroovyCodeFragment fragment = new GroovyCodeFragment(myProject, text);
    fragment.setContext(myContext);
    return PsiDocumentManager.getInstance(myProject).getDocument(fragment);
}
Also used : GroovyCodeFragment(org.jetbrains.plugins.groovy.debugger.fragments.GroovyCodeFragment) Nullable(org.jetbrains.annotations.Nullable)

Example 75 with Nullable

use of org.jetbrains.annotations.Nullable in project intellij-community by JetBrains.

the class GroovyScriptClassSearcher method findClass.

@Nullable
@Override
public PsiClass findClass(@NotNull PsiElement place) {
    if (place.getLanguage() == GroovyLanguage.INSTANCE) {
        PsiClass containingClass = PsiTreeUtil.getParentOfType(place, PsiClass.class, false);
        while (containingClass instanceof PsiTypeParameter) {
            containingClass = PsiTreeUtil.getParentOfType(containingClass, PsiClass.class);
        }
        if (containingClass != null)
            return containingClass;
        PsiFile file = place.getContainingFile();
        if (file instanceof GroovyFile && ((GroovyFile) file).isScript()) {
            return ((GroovyFile) file).getScriptClass();
        }
    }
    return null;
}
Also used : PsiTypeParameter(com.intellij.psi.PsiTypeParameter) PsiClass(com.intellij.psi.PsiClass) PsiFile(com.intellij.psi.PsiFile) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Nullable (org.jetbrains.annotations.Nullable)4694 VirtualFile (com.intellij.openapi.vfs.VirtualFile)812 PsiElement (com.intellij.psi.PsiElement)485 File (java.io.File)405 Project (com.intellij.openapi.project.Project)396 PsiFile (com.intellij.psi.PsiFile)320 NotNull (org.jetbrains.annotations.NotNull)259 IOException (java.io.IOException)247 Module (com.intellij.openapi.module.Module)227 ArrayList (java.util.ArrayList)178 TextRange (com.intellij.openapi.util.TextRange)156 Document (com.intellij.openapi.editor.Document)124 List (java.util.List)116 ASTNode (com.intellij.lang.ASTNode)105 IElementType (com.intellij.psi.tree.IElementType)103 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)102 XmlTag (com.intellij.psi.xml.XmlTag)96 Editor (com.intellij.openapi.editor.Editor)94 Element (org.jdom.Element)93 XmlFile (com.intellij.psi.xml.XmlFile)78