use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class GroovyOverrideImplementUtil method setupTraitMethodBody.
private static void setupTraitMethodBody(Project project, GrMethod resultMethod, GrTraitMethod traitMethod) {
PsiClass traitClass = traitMethod.getPrototype().getContainingClass();
StringBuilder builder = new StringBuilder();
builder.append("\nreturn ");
builder.append(traitClass.getQualifiedName());
builder.append(".super.");
builder.append(traitMethod.getName());
builder.append("(");
GrParameter[] parameters = resultMethod.getParameters();
for (GrParameter parameter : parameters) {
builder.append(parameter.getName()).append(",");
}
if (parameters.length > 0) {
builder.replace(builder.length() - 1, builder.length(), ")\n");
} else {
builder.append(")\n");
}
GroovyFile file = GroovyPsiElementFactory.getInstance(project).createGroovyFile(builder, false, null);
GrOpenBlock block = resultMethod.getBlock();
block.getNode().addChildren(file.getFirstChild().getNode(), null, block.getRBrace().getNode());
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class GroovyOverrideImplementUtil method setupOverridingMethodBody.
private static void setupOverridingMethodBody(Project project, PsiMethod method, GrMethod resultMethod, FileTemplate template, PsiSubstitutor substitutor) {
final PsiType returnType = substitutor.substitute(getSuperReturnType(method));
String returnTypeText = "";
if (returnType != null) {
returnTypeText = returnType.getPresentableText();
}
Properties properties = FileTemplateManager.getInstance(project).getDefaultProperties();
properties.setProperty(FileTemplate.ATTRIBUTE_RETURN_TYPE, returnTypeText);
properties.setProperty(FileTemplate.ATTRIBUTE_DEFAULT_RETURN_VALUE, PsiTypesUtil.getDefaultValueOfType(returnType));
properties.setProperty(FileTemplate.ATTRIBUTE_CALL_SUPER, callSuper(method, resultMethod));
JavaTemplateUtil.setClassAndMethodNameProperties(properties, method.getContainingClass(), resultMethod);
try {
String bodyText = StringUtil.replace(template.getText(properties), ";", "");
GroovyFile file = GroovyPsiElementFactory.getInstance(project).createGroovyFile("\n " + bodyText + "\n", false, null);
GrOpenBlock block = resultMethod.getBlock();
block.getNode().addChildren(file.getFirstChild().getNode(), null, block.getRBrace().getNode());
} catch (IOException e) {
LOG.error(e);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class ClassNameDiffersFromFileNamePredicate method satisfiedBy.
@Override
public boolean satisfiedBy(PsiElement element) {
final PsiElement parent = element.getParent();
if (!(parent instanceof GrTypeDefinition))
return false;
if (((GrTypeDefinition) parent).getNameIdentifierGroovy() != element)
return false;
final String name = ((GrTypeDefinition) parent).getName();
if (name == null || name.isEmpty())
return false;
if (myClassConsumer != null)
myClassConsumer.consume(((GrTypeDefinition) parent));
final PsiFile file = element.getContainingFile();
if (!(file instanceof GroovyFile))
return false;
if (!file.isPhysical())
return false;
if (name.equals(FileUtil.getNameWithoutExtension(file.getName())))
return false;
if (mySearchForClassInMultiClassFile) {
return ((GroovyFile) file).getClasses().length > 1;
} else {
return !((GroovyFile) file).isScript();
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class AntTasksProvider method getAntTasks.
public static Set<LightMethodBuilder> getAntTasks(PsiElement place) {
final PsiFile file = place.getContainingFile();
if (!(file instanceof GroovyFile)) {
return Collections.emptySet();
}
return CachedValuesManager.getManager(file.getProject()).getCachedValue(file, GANT_METHODS, () -> {
Map<String, Class> antObjects = getAntObjects((GroovyFile) file);
final Set<LightMethodBuilder> methods = new HashSet<>();
final Project project = file.getProject();
final PsiType closureType = TypesUtil.createType(GroovyCommonClassNames.GROOVY_LANG_CLOSURE, file);
final PsiClassType stringType = TypesUtil.createType(CommonClassNames.JAVA_LANG_STRING, file);
for (String name : antObjects.keySet()) {
methods.add(new AntBuilderMethod(file, name, closureType, antObjects.get(name), stringType));
}
return CachedValueProvider.Result.create(methods, PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT, ProjectRootManager.getInstance(project));
}, false);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class GroovyScriptRunConfigurationProducer method createConfigurationByElement.
@Override
protected RunnerAndConfigurationSettings createConfigurationByElement(final Location location, final ConfigurationContext context) {
final PsiElement element = location.getPsiElement();
final PsiFile file = element.getContainingFile();
if (!(file instanceof GroovyFile)) {
return null;
}
GroovyFile groovyFile = (GroovyFile) file;
final PsiClass aClass = GroovyRunnerPsiUtil.getRunningClass(location.getPsiElement());
if (aClass instanceof GroovyScriptClass || GroovyRunnerPsiUtil.isRunnable(aClass)) {
final RunnerAndConfigurationSettings settings = createConfiguration(aClass);
if (settings != null) {
mySourceElement = element;
final GroovyScriptRunConfiguration configuration = (GroovyScriptRunConfiguration) settings.getConfiguration();
GroovyScriptUtil.getScriptType(groovyFile).tuneConfiguration(groovyFile, configuration, location);
return settings;
}
}
if (file.getText().contains("@Grab")) {
ApplicationConfigurationProducer producer = new ApplicationConfigurationProducer();
ConfigurationFromContext settings = producer.createConfigurationFromContext(context);
if (settings != null) {
PsiElement src = settings.getSourceElement();
mySourceElement = src;
return createConfiguration(src instanceof PsiMethod ? ((PsiMethod) src).getContainingClass() : (PsiClass) src);
}
return null;
} else {
return null;
}
}
Aggregations