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}");
}
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;
}
});
}
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;
}
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);
}
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;
}
Aggregations