use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GroovyExtractChooser method invoke.
public static InitialInfo invoke(Project project, Editor editor, PsiFile file, int start, int end, boolean forceStatements) throws GrRefactoringError {
PsiDocumentManager.getInstance(project).commitAllDocuments();
if (!(file instanceof GroovyFileBase)) {
throw new GrRefactoringError(GroovyRefactoringBundle.message("only.in.groovy.files"));
}
if (!CommonRefactoringUtil.checkReadOnlyStatus(project, file)) {
throw new GrRefactoringError(RefactoringBundle.message("readonly.occurences.found"));
}
PsiDocumentManager.getInstance(project).commitAllDocuments();
final StringPartInfo stringPart = StringPartInfo.findStringPart(file, start, end);
if (stringPart != null) {
return new InitialInfo(new VariableInfo[0], new VariableInfo[0], PsiElement.EMPTY_ARRAY, GrStatement.EMPTY_ARRAY, new ArrayList<>(), stringPart, project, null);
}
final SelectionModel selectionModel = editor.getSelectionModel();
if (!forceStatements) {
GrVariable variable = GrIntroduceHandlerBase.findVariable(file, start, end);
if (variable != null) {
GrExpression initializer = variable.getInitializerGroovy();
if (initializer != null) {
TextRange range = initializer.getTextRange();
return buildInfo(project, file, range.getStartOffset(), range.getEndOffset(), forceStatements, selectionModel, variable);
}
}
}
return buildInfo(project, file, start, end, forceStatements, selectionModel, null);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GroovyNameSuggestionProvider method getSuggestedNames.
@Override
public SuggestedNameInfo getSuggestedNames(final PsiElement element, @Nullable PsiElement nameSuggestionContext, Set<String> result) {
if (nameSuggestionContext == null)
nameSuggestionContext = element;
if (element instanceof GrVariable && nameSuggestionContext instanceof GroovyPsiElement) {
final PsiType type = ((GrVariable) element).getTypeGroovy();
if (type != null) {
final String[] names = GroovyNameSuggestionUtil.suggestVariableNameByType(type, new DefaultGroovyVariableNameValidator((GroovyPsiElement) nameSuggestionContext));
result.addAll(Arrays.asList(names));
return new SuggestedNameInfo(names) {
@Override
public void nameChosen(String name) {
JavaStatisticsManager.incVariableNameUseCount(name, JavaCodeStyleManager.getInstance(element.getProject()).getVariableKind((GrVariable) element), ((GrVariable) element).getName(), type);
}
};
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class ClassItemGeneratorImpl method writeVariableDeclarations.
@Override
public void writeVariableDeclarations(StringBuilder mainBuilder, GrVariableDeclaration variableDeclaration) {
ExpressionContext extended = context.extend();
GrVariable[] variables = variableDeclaration.getVariables();
if (variables.length > 0 && variables[0] instanceof PsiField) {
GenerationUtil.writeDocComment(mainBuilder, ((PsiField) variables[0]), true);
}
StringBuilder builder = new StringBuilder();
StringBuilder initBuilder = new StringBuilder();
initBuilder.append("{\n");
for (GrVariable variable : variables) {
PsiType type = extended.typeProvider.getVarType(variable);
ModifierListGenerator.writeModifiers(builder, variable.getModifierList());
TypeWriter.writeType(builder, type, variable);
builder.append(' ');
builder.append(variable.getName());
final GrExpression initializer = variable.getInitializerGroovy();
if (initializer != null) {
int count = extended.myStatements.size();
StringBuilder initializerBuilder = new StringBuilder();
extended.searchForLocalVarsToWrap(initializer);
initializer.accept(new ExpressionGenerator(initializerBuilder, extended));
if (extended.myStatements.size() == count) {
//didn't use extra statements
builder.append(" = ").append(initializerBuilder);
} else {
StringBuilder assignment = new StringBuilder().append(variable.getName()).append(" = ").append(initializerBuilder).append(';');
GenerationUtil.writeStatement(initBuilder, assignment, null, extended);
}
}
builder.append(";\n");
}
if (!extended.myStatements.isEmpty()) {
initBuilder.append("}\n");
mainBuilder.append(initBuilder);
}
mainBuilder.append(builder);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class ExpressionGenerator method visitReferenceExpression.
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
final GrExpression qualifier = referenceExpression.getQualifier();
final GroovyResolveResult resolveResult = referenceExpression.advancedResolve();
final PsiElement resolved = resolveResult.getElement();
final String referenceName = referenceExpression.getReferenceName();
if (PsiUtil.isThisOrSuperRef(referenceExpression)) {
writeThisOrSuperRef(referenceExpression, qualifier, referenceName);
return;
}
if (ResolveUtil.isClassReference(referenceExpression)) {
// just delegate to qualifier
LOG.assertTrue(qualifier != null);
qualifier.accept(this);
return;
}
if (resolved instanceof PsiClass) {
builder.append(((PsiClass) resolved).getQualifiedName());
if (PsiUtil.isExpressionUsed(referenceExpression)) {
builder.append(".class");
}
return;
}
//don't try to resolve local vars that are provided my this generator (they are listed in myUsedVarNames)
if (resolved == null && qualifier == null && context.myUsedVarNames.contains(referenceName)) {
builder.append(referenceName);
return;
}
//all refs in script that are not resolved are saved in 'binding' of the script
if (qualifier == null && (resolved == null || resolved instanceof GrBindingVariable || resolved instanceof LightElement && !(resolved instanceof ClosureSyntheticParameter)) && (referenceExpression.getParent() instanceof GrIndexProperty || !(referenceExpression.getParent() instanceof GrCall)) && PsiUtil.getContextClass(referenceExpression) instanceof GroovyScriptClass) {
final GrExpression thisExpr = factory.createExpressionFromText("this", referenceExpression);
thisExpr.accept(this);
builder.append(".getBinding().getProperty(\"").append(referenceExpression.getReferenceName()).append("\")");
return;
}
final IElementType type = referenceExpression.getDotTokenType();
GrExpression qualifierToUse = qualifier;
if (type == GroovyTokenTypes.mMEMBER_POINTER) {
LOG.assertTrue(qualifier != null);
builder.append("new ").append(GroovyCommonClassNames.ORG_CODEHAUS_GROOVY_RUNTIME_METHOD_CLOSURE).append('(');
qualifier.accept(this);
builder.append(", \"").append(referenceName).append("\")");
return;
}
if (type == GroovyTokenTypes.mOPTIONAL_DOT) {
LOG.assertTrue(qualifier != null);
String qualifierName = createVarByInitializer(qualifier);
builder.append('(').append(qualifierName).append(" == null ? null : ");
qualifierToUse = factory.createReferenceExpressionFromText(qualifierName, referenceExpression);
}
if (resolveResult.isInvokedOnProperty()) {
//property-style access to accessor (e.g. qual.prop should be translated to qual.getProp())
LOG.assertTrue(resolved instanceof PsiMethod);
LOG.assertTrue(GroovyPropertyUtils.isSimplePropertyGetter((PsiMethod) resolved));
invokeMethodOn(((PsiMethod) resolved), qualifierToUse, GrExpression.EMPTY_ARRAY, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), referenceExpression);
} else {
if (qualifierToUse != null) {
qualifierToUse.accept(this);
builder.append('.');
}
if (resolved instanceof PsiNamedElement && !(resolved instanceof GrBindingVariable)) {
final String refName = ((PsiNamedElement) resolved).getName();
if (resolved instanceof GrVariable && context.analyzedVars.toWrap((GrVariable) resolved)) {
//this var should be wrapped by groovy.lang.Reference. so we add .get() tail.
builder.append(context.analyzedVars.toVarName((GrVariable) resolved));
if (!PsiUtil.isAccessedForWriting(referenceExpression)) {
builder.append(".get()");
}
} else {
builder.append(refName);
}
} else {
//unresolved reference
if (referenceName != null) {
if (PsiUtil.isAccessedForWriting(referenceExpression)) {
builder.append(referenceName);
} else {
PsiType stringType = PsiType.getJavaLangString(referenceExpression.getManager(), referenceExpression.getResolveScope());
PsiType qualifierType = PsiImplUtil.getQualifierType(referenceExpression);
GroovyResolveResult[] candidates = qualifierType != null ? ResolveUtil.getMethodCandidates(qualifierType, "getProperty", referenceExpression, stringType) : GroovyResolveResult.EMPTY_ARRAY;
final PsiElement method = PsiImplUtil.extractUniqueElement(candidates);
if (method != null) {
builder.append("getProperty(\"").append(referenceName).append("\")");
} else {
builder.append(referenceName);
}
}
} else {
final PsiElement nameElement = referenceExpression.getReferenceNameElement();
if (nameElement instanceof GrExpression) {
((GrExpression) nameElement).accept(this);
} else if (nameElement != null) {
builder.append(nameElement.toString());
}
}
}
}
if (type == GroovyTokenTypes.mOPTIONAL_DOT) {
builder.append(')');
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GenerationUtil method getVarTypes.
static Set<String> getVarTypes(GrVariableDeclaration variableDeclaration) {
GrVariable[] variables = variableDeclaration.getVariables();
final GrTypeElement typeElement = variableDeclaration.getTypeElementGroovy();
Set<String> types = new HashSet<>(variables.length);
if (typeElement == null) {
if (variables.length > 1) {
for (GrVariable variable : variables) {
final GrExpression initializer = variable.getInitializerGroovy();
if (initializer != null) {
final PsiType varType = initializer.getType();
if (varType != null) {
types.add(getTypeText(varType, variableDeclaration));
}
}
}
}
}
return types;
}
Aggregations