use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GrIntroduceFieldDialog method createUIComponents.
private void createUIComponents() {
final GrExpression expression = myContext.getExpression();
final GrVariable var = myContext.getVar();
final StringPartInfo stringPart = myContext.getStringPart();
List<String> list = new ArrayList<>();
if (var != null) {
list.add(var.getName());
}
ContainerUtil.addAll(list, suggestNames());
myNameField = new NameSuggestionsField(ArrayUtil.toStringArray(list), myContext.getProject(), GroovyFileType.GROOVY_FILE_TYPE);
if (expression != null) {
myTypeComboBox = GrTypeComboBox.createTypeComboBoxFromExpression(expression);
} else if (stringPart != null) {
myTypeComboBox = GrTypeComboBox.createTypeComboBoxFromExpression(stringPart.getLiteral());
} else {
myTypeComboBox = GrTypeComboBox.createTypeComboBoxWithDefType(var.getDeclaredType(), var);
}
GrTypeComboBox.registerUpDownHint(myNameField, myTypeComboBox);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class ImportMavenRepositoriesTask method resolveUriFromSimpleExpression.
@Nullable
private static URI resolveUriFromSimpleExpression(@Nullable GrExpression expression) {
if (expression == null)
return null;
try {
if (expression instanceof PsiLiteral) {
URI uri = new URI(String.valueOf(PsiLiteral.class.cast(expression).getValue()));
if (uri.getScheme() != null && StringUtil.startsWith(uri.getScheme(), "http"))
return uri;
}
} catch (URISyntaxException ignored) {
// ignore it
}
try {
PsiReference reference = expression.getReference();
if (reference == null)
return null;
PsiElement element = reference.resolve();
if (element instanceof GrVariable) {
List<GrLiteral> grLiterals = PsiTreeUtil.getChildrenOfTypeAsList(element, GrLiteral.class);
if (grLiterals.isEmpty())
return null;
URI uri = new URI(String.valueOf(grLiterals.get(0).getValue()));
if (uri.getScheme() != null && StringUtil.startsWith("http", uri.getScheme()))
return uri;
}
} catch (URISyntaxException ignored) {
// ignore it
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GroovyLineMarkerProvider method getGroovyCategory.
private static int getGroovyCategory(@NotNull PsiElement element, @NotNull CharSequence documentChars) {
if (element instanceof GrVariableDeclarationImpl) {
GrVariable[] variables = ((GrVariableDeclarationImpl) element).getVariables();
if (variables.length == 1 && variables[0] instanceof GrField && variables[0].getInitializerGroovy() instanceof GrClosableBlock) {
return 2;
}
}
if (element instanceof GrField || element instanceof GrTypeParameter)
return 1;
if (element instanceof GrTypeDefinition || element instanceof GrClassInitializer)
return 2;
if (element instanceof GrMethod) {
if (((GrMethod) element).hasModifierProperty(PsiModifier.ABSTRACT) && !(((GrMethod) element).getBlock() != null && GrTraitUtil.isTrait(((GrMethod) element).getContainingClass()))) {
return 1;
}
TextRange textRange = element.getTextRange();
int start = textRange.getStartOffset();
int end = Math.min(documentChars.length(), textRange.getEndOffset());
int crlf = StringUtil.getLineBreakCount(documentChars.subSequence(start, end));
return crlf == 0 ? 1 : 2;
}
return 0;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class ExtractUtil method collectUsedLocalVarsOrParamsDeclaredOutside.
private static Collection<GrVariable> collectUsedLocalVarsOrParamsDeclaredOutside(ExtractInfoHelper helper) {
final Collection<GrVariable> result = new HashSet<>();
final TextRange range = getRangeOfRefactoring(helper);
final int start = range.getStartOffset();
final int end = range.getEndOffset();
final GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression ref) {
final PsiElement resolved = ref.resolve();
if ((resolved instanceof GrParameter || PsiUtil.isLocalVariable(resolved)) && resolved.isPhysical()) {
final int offset = resolved.getTextRange().getStartOffset();
//var is declared outside of selected code
if (offset < start || end <= offset) {
result.add((GrVariable) resolved);
}
}
}
};
final GrStatement[] statements = helper.getStatements();
for (GrStatement statement : statements) {
statement.accept(visitor);
}
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class ExtractUtil method mustAddVariableDeclaration.
/*
To declare or not a variable to which method call result will be assigned.
*/
private static List<VariableInfo> mustAddVariableDeclaration(@NotNull GrStatement[] statements, @NotNull VariableInfo[] vars) {
Map<String, VariableInfo> names = new HashMap<>();
for (VariableInfo var : vars) {
names.put(var.getName(), var);
}
List<VariableInfo> result = new ArrayList<>();
for (GrStatement statement : statements) {
if (statement instanceof GrVariableDeclaration) {
GrVariableDeclaration declaration = (GrVariableDeclaration) statement;
for (GrVariable variable : declaration.getVariables()) {
final VariableInfo removed = names.remove(variable.getName());
if (removed != null) {
result.add(removed);
}
}
}
}
for (String varName : names.keySet()) {
if (ResolveUtil.resolveProperty(statements[statements.length - 1], varName) == null) {
result.add(names.get(varName));
}
}
return result;
}
Aggregations