use of org.eclipse.jdt.ls.core.internal.corext.fix.LinkedProposalPositionGroup in project eclipse.jdt.ls by eclipse.
the class ExtractConstantRefactoring method createConstantDeclaration.
private void createConstantDeclaration() throws CoreException {
Type type = getConstantType();
IExpressionFragment fragment = getSelectedExpression();
Expression initializer = getSelectedExpression().createCopyTarget(fCuRewrite.getASTRewrite(), true);
AST ast = fCuRewrite.getAST();
VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
variableDeclarationFragment.setName(ast.newSimpleName(fConstantName));
variableDeclarationFragment.setInitializer(initializer);
FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(variableDeclarationFragment);
fieldDeclaration.setType(type);
Modifier.ModifierKeyword accessModifier = Modifier.ModifierKeyword.toKeyword(fVisibility);
if (accessModifier != null) {
fieldDeclaration.modifiers().add(ast.newModifier(accessModifier));
}
fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));
// boolean createComments = JavaPreferencesSettings.getCodeGenerationSettings(fCu.getJavaProject()).createComments;
// if (createComments) {
String comment = CodeGeneration.getFieldComment(fCu, getConstantTypeName(), fConstantName, StubUtility.getLineDelimiterUsed(fCu));
if (comment != null && comment.length() > 0 && !"/**\n *\n */\n".equals(comment)) {
Javadoc doc = (Javadoc) fCuRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JAVADOC);
fieldDeclaration.setJavadoc(doc);
}
// }
AbstractTypeDeclaration parent = getContainingTypeDeclarationNode();
ListRewrite listRewrite = fCuRewrite.getASTRewrite().getListRewrite(parent, parent.getBodyDeclarationsProperty());
TextEditGroup msg = fCuRewrite.createGroupDescription(RefactoringCoreMessages.ExtractConstantRefactoring_declare_constant);
if (insertFirst()) {
listRewrite.insertFirst(fieldDeclaration, msg);
} else {
listRewrite.insertAfter(fieldDeclaration, getNodeToInsertConstantDeclarationAfter(), msg);
}
if (fLinkedProposalModel != null) {
ASTRewrite rewrite = fCuRewrite.getASTRewrite();
LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
nameGroup.addPosition(rewrite.track(variableDeclarationFragment.getName()), true);
String[] nameSuggestions = guessConstantNames();
if (nameSuggestions.length > 0 && !nameSuggestions[0].equals(fConstantName)) {
nameGroup.addProposal(fConstantName, nameSuggestions.length + 1);
}
for (int i = 0; i < nameSuggestions.length; i++) {
nameGroup.addProposal(nameSuggestions[i], nameSuggestions.length - i);
}
LinkedProposalPositionGroup typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true);
typeGroup.addPosition(rewrite.track(type), true);
ITypeBinding typeBinding = guessBindingForReference(fragment.getAssociatedExpression());
if (typeBinding != null) {
ITypeBinding[] relaxingTypes = ASTResolving.getNarrowingTypes(ast, typeBinding);
for (int i = 0; i < relaxingTypes.length; i++) {
typeGroup.addProposal(relaxingTypes[i], fCuRewrite.getCu(), relaxingTypes.length - i);
}
}
boolean isInterface = parent.resolveBinding() != null && parent.resolveBinding().isInterface();
ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(fLinkedProposalModel, rewrite, fieldDeclaration.modifiers(), isInterface);
}
}
use of org.eclipse.jdt.ls.core.internal.corext.fix.LinkedProposalPositionGroup in project eclipse.jdt.ls by eclipse.
the class ExtractMethodRefactoring method createCallNodes.
// ---- Code generation -----------------------------------------------------------------------
private ASTNode[] createCallNodes(SnippetFinder.Match duplicate, int modifiers) {
List<ASTNode> result = new ArrayList<>(2);
IVariableBinding[] locals = fAnalyzer.getCallerLocals();
for (int i = 0; i < locals.length; i++) {
result.add(createDeclaration(locals[i], null));
}
MethodInvocation invocation = fAST.newMethodInvocation();
invocation.setName(fAST.newSimpleName(fMethodName));
ASTNode typeNode = ASTResolving.findParentType(fAnalyzer.getEnclosingBodyDeclaration());
RefactoringStatus status = new RefactoringStatus();
while (fDestination != typeNode) {
fAnalyzer.checkInput(status, fMethodName, typeNode);
if (!status.isOK()) {
SimpleName destinationTypeName = fAST.newSimpleName(ASTNodes.getEnclosingType(fDestination).getName());
if ((modifiers & Modifier.STATIC) == 0) {
ThisExpression thisExpression = fAST.newThisExpression();
thisExpression.setQualifier(destinationTypeName);
invocation.setExpression(thisExpression);
} else {
invocation.setExpression(destinationTypeName);
}
break;
}
typeNode = typeNode.getParent();
}
List<Expression> arguments = invocation.arguments();
for (int i = 0; i < fParameterInfos.size(); i++) {
ParameterInfo parameter = fParameterInfos.get(i);
arguments.add(ASTNodeFactory.newName(fAST, getMappedName(duplicate, parameter)));
}
if (fLinkedProposalModel != null) {
LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
nameGroup.addPosition(fRewriter.track(invocation.getName()), false);
}
ASTNode call;
int returnKind = fAnalyzer.getReturnKind();
switch(returnKind) {
case ExtractMethodAnalyzer.ACCESS_TO_LOCAL:
IVariableBinding binding = fAnalyzer.getReturnLocal();
if (binding != null) {
VariableDeclarationStatement decl = createDeclaration(getMappedBinding(duplicate, binding), invocation);
call = decl;
} else {
Assignment assignment = fAST.newAssignment();
assignment.setLeftHandSide(ASTNodeFactory.newName(fAST, getMappedBinding(duplicate, fAnalyzer.getReturnValue()).getName()));
assignment.setRightHandSide(invocation);
call = assignment;
}
break;
case ExtractMethodAnalyzer.RETURN_STATEMENT_VALUE:
ReturnStatement rs = fAST.newReturnStatement();
rs.setExpression(invocation);
call = rs;
break;
default:
call = invocation;
}
if (call instanceof Expression && !fAnalyzer.isExpressionSelected()) {
call = fAST.newExpressionStatement((Expression) call);
}
result.add(call);
// return;
if (returnKind == ExtractMethodAnalyzer.RETURN_STATEMENT_VOID && !fAnalyzer.isLastStatementSelected()) {
result.add(fAST.newReturnStatement());
}
return result.toArray(new ASTNode[result.size()]);
}
use of org.eclipse.jdt.ls.core.internal.corext.fix.LinkedProposalPositionGroup in project eclipse.jdt.ls by eclipse.
the class ExtractMethodRefactoring method createChange.
@Override
public Change createChange(IProgressMonitor pm) throws CoreException {
if (fMethodName == null) {
return null;
}
// $NON-NLS-1$
pm.beginTask("", 2);
try {
fAnalyzer.aboutToCreateChange();
BodyDeclaration declaration = fAnalyzer.getEnclosingBodyDeclaration();
fRewriter = ASTRewrite.create(declaration.getAST());
final CompilationUnitChange result = new CompilationUnitChange(RefactoringCoreMessages.ExtractMethodRefactoring_change_name, fCUnit);
result.setSaveMode(TextFileChange.KEEP_SAVE_STATE);
result.setDescriptor(new RefactoringChangeDescriptor(getRefactoringDescriptor()));
MultiTextEdit root = new MultiTextEdit();
result.setEdit(root);
ASTNode[] selectedNodes = fAnalyzer.getSelectedNodes();
fRewriter.setTargetSourceRangeComputer(new SelectionAwareSourceRangeComputer(selectedNodes, fCUnit.getBuffer(), fSelectionStart, fSelectionLength));
TextEditGroup substituteDesc = new TextEditGroup(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_substitute_with_call, BasicElementLabels.getJavaElementName(fMethodName)));
result.addTextEditGroup(substituteDesc);
MethodDeclaration mm = createNewMethod(selectedNodes, fCUnit.findRecommendedLineSeparator(), substituteDesc);
if (fLinkedProposalModel != null) {
LinkedProposalPositionGroup typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true);
typeGroup.addPosition(fRewriter.track(mm.getReturnType2()), false);
ITypeBinding typeBinding = fAnalyzer.getReturnTypeBinding();
if (typeBinding != null) {
ITypeBinding[] relaxingTypes = ASTResolving.getNarrowingTypes(fAST, typeBinding);
for (int i = 0; i < relaxingTypes.length; i++) {
typeGroup.addProposal(relaxingTypes[i], fCUnit, relaxingTypes.length - i);
}
}
LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
nameGroup.addPosition(fRewriter.track(mm.getName()), false);
ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(fLinkedProposalModel, fRewriter, mm.modifiers(), false);
}
TextEditGroup insertDesc = new TextEditGroup(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_add_method, BasicElementLabels.getJavaElementName(fMethodName)));
result.addTextEditGroup(insertDesc);
if (fDestination == ASTResolving.findParentType(declaration.getParent())) {
ChildListPropertyDescriptor desc = (ChildListPropertyDescriptor) declaration.getLocationInParent();
ListRewrite container = fRewriter.getListRewrite(declaration.getParent(), desc);
container.insertAfter(mm, declaration, insertDesc);
} else {
BodyDeclarationRewrite container = BodyDeclarationRewrite.create(fRewriter, fDestination);
container.insert(mm, insertDesc);
}
replaceDuplicates(result, mm.getModifiers());
replaceBranches(result);
if (fImportRewriter.hasRecordedChanges()) {
TextEdit edit = fImportRewriter.rewriteImports(null);
root.addChild(edit);
result.addTextEditGroup(new TextEditGroup(RefactoringCoreMessages.ExtractMethodRefactoring_organize_imports, new TextEdit[] { edit }));
}
root.addChild(fRewriter.rewriteAST());
return result;
} finally {
pm.done();
}
}
use of org.eclipse.jdt.ls.core.internal.corext.fix.LinkedProposalPositionGroup in project eclipse.jdt.ls by eclipse.
the class ModifierCorrectionSubProcessor method installLinkedVisibilityProposals.
public static void installLinkedVisibilityProposals(LinkedProposalModel linkedProposalModel, ASTRewrite rewrite, List<IExtendedModifier> modifiers, boolean inInterface, String groupId) {
ASTNode modifier = findVisibilityModifier(modifiers);
if (modifier != null) {
int selected = ((Modifier) modifier).getKeyword().toFlagValue();
LinkedProposalPositionGroup positionGroup = linkedProposalModel.getPositionGroup(groupId, true);
positionGroup.addPosition(rewrite.track(modifier), false);
positionGroup.addProposal(new ModifierLinkedModeProposal(selected, 10));
// add all others
int[] flagValues = inInterface ? new int[] { Modifier.PUBLIC, 0 } : new int[] { Modifier.PUBLIC, 0, Modifier.PROTECTED, Modifier.PRIVATE };
for (int i = 0; i < flagValues.length; i++) {
if (flagValues[i] != selected) {
positionGroup.addProposal(new ModifierLinkedModeProposal(flagValues[i], 9 - i));
}
}
}
}
use of org.eclipse.jdt.ls.core.internal.corext.fix.LinkedProposalPositionGroup in project eclipse.jdt.ls by eclipse.
the class ExtractTempRefactoring method createTempType.
private Type createTempType() throws CoreException {
Expression expression = getSelectedExpression().getAssociatedExpression();
Type resultingType = null;
ITypeBinding typeBinding = expression.resolveTypeBinding();
ASTRewrite rewrite = fCURewrite.getASTRewrite();
AST ast = rewrite.getAST();
if (expression instanceof ClassInstanceCreation && (typeBinding == null || typeBinding.getTypeArguments().length == 0)) {
resultingType = (Type) rewrite.createCopyTarget(((ClassInstanceCreation) expression).getType());
} else if (expression instanceof CastExpression) {
resultingType = (Type) rewrite.createCopyTarget(((CastExpression) expression).getType());
} else {
if (typeBinding == null) {
typeBinding = ASTResolving.guessBindingForReference(expression);
}
if (typeBinding != null) {
typeBinding = Bindings.normalizeForDeclarationUse(typeBinding, ast);
ImportRewrite importRewrite = fCURewrite.getImportRewrite();
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(expression, importRewrite);
resultingType = importRewrite.addImport(typeBinding, ast, context, TypeLocation.LOCAL_VARIABLE);
} else {
// $NON-NLS-1$
resultingType = ast.newSimpleType(ast.newSimpleName("Object"));
}
}
if (fLinkedProposalModel != null) {
LinkedProposalPositionGroup typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true);
typeGroup.addPosition(rewrite.track(resultingType), false);
if (typeBinding != null) {
ITypeBinding[] relaxingTypes = ASTResolving.getNarrowingTypes(ast, typeBinding);
for (int i = 0; i < relaxingTypes.length; i++) {
typeGroup.addProposal(relaxingTypes[i], fCURewrite.getCu(), relaxingTypes.length - i);
}
}
}
return resultingType;
}
Aggregations