use of org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo in project intellij-community by JetBrains.
the class GrIntroduceFieldDialog method canBeInitializedOutsideBlock.
private static boolean canBeInitializedOutsideBlock(@NotNull GrIntroduceContext context, @NotNull PsiClass clazz) {
final StringPartInfo part = context.getStringPart();
GrExpression expression = context.getExpression();
if (expression != null) {
expression = (GrExpression) PsiUtil.skipParentheses(expression, false);
if (expression == null)
return false;
if (expression instanceof GrReferenceExpression) {
final PsiElement resolved = ((GrReferenceExpression) expression).resolve();
if (PsiUtil.isLocalVariable(resolved)) {
expression = ((GrVariable) resolved).getInitializerGroovy();
if (expression == null)
return false;
}
}
ExpressionChecker visitor = new ExpressionChecker(clazz, expression);
expression.accept(visitor);
return visitor.isResult();
}
if (part != null) {
for (GrStringInjection injection : part.getInjections()) {
GroovyPsiElement scope = injection.getExpression() != null ? injection.getExpression() : injection.getClosableBlock();
assert scope != null;
ExpressionChecker visitor = new ExpressionChecker(clazz, scope);
scope.accept(visitor);
if (!visitor.isResult()) {
return visitor.isResult();
}
}
return true;
} else {
return false;
}
}
use of org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo in project intellij-community by JetBrains.
the class ExtractUtil method getRangeOfRefactoring.
public static TextRange getRangeOfRefactoring(ExtractInfoHelper helper) {
final StringPartInfo stringPartInfo = helper.getStringPartInfo();
if (stringPartInfo != null) {
return stringPartInfo.getRange();
} else {
final GrStatement[] statements = helper.getStatements();
int start = statements[0].getTextRange().getStartOffset();
int end = statements[statements.length - 1].getTextRange().getEndOffset();
return new TextRange(start, end);
}
}
use of org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo 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.refactoring.introduce.StringPartInfo in project intellij-community by JetBrains.
the class GrInplaceVariableIntroducer method getInitialSettingsForInplace.
@Nullable
@Override
protected GroovyIntroduceVariableSettings getInitialSettingsForInplace(@NotNull final GrIntroduceContext context, @NotNull final OccurrencesChooser.ReplaceChoice choice, final String[] names) {
return new GroovyIntroduceVariableSettings() {
private final CanonicalTypes.Type myType;
{
GrExpression expression = context.getExpression();
StringPartInfo stringPart = context.getStringPart();
GrVariable var = context.getVar();
PsiType type = expression != null ? expression.getType() : var != null ? var.getType() : stringPart != null ? stringPart.getLiteral().getType() : null;
myType = type != null && !PsiType.NULL.equals(type) ? CanonicalTypes.createTypeWrapper(type) : null;
}
@Override
public boolean isDeclareFinal() {
return myCanBeFinalCb != null ? myCanBeFinalCb.isSelected() : false;
}
@Nullable
@Override
public String getName() {
return names[0];
}
@Override
public boolean replaceAllOccurrences() {
return choice == OccurrencesChooser.ReplaceChoice.ALL;
}
@Nullable
@Override
public PsiType getSelectedType() {
return myType != null ? myType.getType(context.getPlace()) : null;
}
};
}
use of org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo in project intellij-community by JetBrains.
the class GrInplaceConstantIntroducer method getInitialSettingsForInplace.
@Nullable
@Override
protected GrIntroduceConstantSettings getInitialSettingsForInplace(@NotNull final GrIntroduceContext context, @NotNull final OccurrencesChooser.ReplaceChoice choice, final String[] names) {
return new GrIntroduceConstantSettings() {
@Override
public String getVisibilityModifier() {
return PsiModifier.PUBLIC;
}
@Nullable
@Override
public PsiClass getTargetClass() {
return (PsiClass) context.getScope();
}
@Nullable
@Override
public String getName() {
return names[0];
}
@Override
public boolean replaceAllOccurrences() {
return isReplaceAllOccurrences();
}
@Nullable
@Override
public PsiType getSelectedType() {
GrExpression expression = context.getExpression();
GrVariable var = context.getVar();
StringPartInfo stringPart = context.getStringPart();
return var != null ? var.getDeclaredType() : expression != null ? expression.getType() : stringPart != null ? stringPart.getLiteral().getType() : null;
}
};
}
Aggregations