Java Code Examples for org.eclipse.jdt.core.dom.rewrite.ASTRewrite#setTargetSourceRangeComputer()
The following examples show how to use
org.eclipse.jdt.core.dom.rewrite.ASTRewrite#setTargetSourceRangeComputer() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocalCorrectionsSubProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
public static void addCasesOmittedProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) { ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot()); if (selectedNode instanceof Expression && selectedNode.getLocationInParent() == SwitchStatement.EXPRESSION_PROPERTY) { AST ast = selectedNode.getAST(); SwitchStatement parent = (SwitchStatement) selectedNode.getParent(); for (Statement statement : (List<Statement>) parent.statements()) { if (statement instanceof SwitchCase && ((SwitchCase) statement).isDefault()) { // insert //$CASES-OMITTED$: ASTRewrite rewrite = ASTRewrite.create(ast); rewrite.setTargetSourceRangeComputer(new NoCommentSourceRangeComputer()); ListRewrite listRewrite = rewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY); ASTNode casesOmittedComment = rewrite.createStringPlaceholder("//$CASES-OMITTED$", ASTNode.EMPTY_STATEMENT); //$NON-NLS-1$ listRewrite.insertBefore(casesOmittedComment, statement, null); String label = CorrectionMessages.LocalCorrectionsSubProcessor_insert_cases_omitted; ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_CASES_OMITTED); proposals.add(proposal); break; } } } }
Example 2
Source File: ChangeTypeRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private void updateCu(CompilationUnit unit, Set<ConstraintVariable> vars, CompilationUnitChange unitChange, ASTRewrite unitRewriter, String typeName) throws JavaModelException { // use custom SourceRangeComputer to avoid losing comments unitRewriter.setTargetSourceRangeComputer(new SourceRangeComputer()); for (Iterator<ConstraintVariable> it=vars.iterator(); it.hasNext(); ){ ConstraintVariable cv = it.next(); ASTNode decl= findDeclaration(unit, cv); if ((decl instanceof SimpleName || decl instanceof QualifiedName) && cv instanceof ExpressionVariable) { ASTNode gp= decl.getParent().getParent(); updateType(unit, getType(gp), unitChange, unitRewriter, typeName); // local variable or parameter } else if (decl instanceof MethodDeclaration || decl instanceof FieldDeclaration) { updateType(unit, getType(decl), unitChange, unitRewriter, typeName); // method return or field type } else if (decl instanceof ParameterizedType){ updateType(unit, getType(decl), unitChange, unitRewriter, typeName); } } }
Example 3
Source File: ConvertIterableLoopOperation.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * {@inheritDoc} */ @Override public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel positionGroups) throws CoreException { final TextEditGroup group= createTextEditGroup(FixMessages.Java50Fix_ConvertToEnhancedForLoop_description, cuRewrite); final ASTRewrite astRewrite= cuRewrite.getASTRewrite(); TightSourceRangeComputer rangeComputer; if (astRewrite.getExtendedSourceRangeComputer() instanceof TightSourceRangeComputer) { rangeComputer= (TightSourceRangeComputer)astRewrite.getExtendedSourceRangeComputer(); } else { rangeComputer= new TightSourceRangeComputer(); } rangeComputer.addTightSourceNode(getForStatement()); astRewrite.setTargetSourceRangeComputer(rangeComputer); Statement statement= convert(cuRewrite, group, positionGroups); astRewrite.replace(getForStatement(), statement, group); }
Example 4
Source File: ConvertForLoopOperation.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * {@inheritDoc} */ @Override public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel positionGroups) throws CoreException { TextEditGroup group= createTextEditGroup(FixMessages.Java50Fix_ConvertToEnhancedForLoop_description, cuRewrite); ASTRewrite rewrite= cuRewrite.getASTRewrite(); TightSourceRangeComputer rangeComputer; if (rewrite.getExtendedSourceRangeComputer() instanceof TightSourceRangeComputer) { rangeComputer= (TightSourceRangeComputer)rewrite.getExtendedSourceRangeComputer(); } else { rangeComputer= new TightSourceRangeComputer(); } rangeComputer.addTightSourceNode(getForStatement()); rewrite.setTargetSourceRangeComputer(rangeComputer); Statement statement= convert(cuRewrite, group, positionGroups); rewrite.replace(getForStatement(), statement, group); }
Example 5
Source File: LocalCorrectionsSubProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
public static void addCasesOmittedProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) { ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot()); if (selectedNode instanceof Expression && selectedNode.getLocationInParent() == SwitchStatement.EXPRESSION_PROPERTY) { AST ast= selectedNode.getAST(); SwitchStatement parent= (SwitchStatement) selectedNode.getParent(); for (Statement statement : (List<Statement>) parent.statements()) { if (statement instanceof SwitchCase && ((SwitchCase) statement).isDefault()) { // insert //$CASES-OMITTED$: ASTRewrite rewrite= ASTRewrite.create(ast); rewrite.setTargetSourceRangeComputer(new NoCommentSourceRangeComputer()); ListRewrite listRewrite= rewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY); ASTNode casesOmittedComment= rewrite.createStringPlaceholder("//$CASES-OMITTED$", ASTNode.EMPTY_STATEMENT); //$NON-NLS-1$ listRewrite.insertBefore(casesOmittedComment, statement, null); String label= CorrectionMessages.LocalCorrectionsSubProcessor_insert_cases_omitted; Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_CASES_OMITTED, image); proposals.add(proposal); break; } } } }
Example 6
Source File: InlineTempRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void removeTemp(CompilationUnitRewrite cuRewrite) { VariableDeclaration variableDeclaration= getVariableDeclaration(); TextEditGroup groupDesc= cuRewrite.createGroupDescription(RefactoringCoreMessages.InlineTempRefactoring_remove_edit_name); ASTNode parent= variableDeclaration.getParent(); ASTRewrite rewrite= cuRewrite.getASTRewrite(); TightSourceRangeComputer sourceRangeComputer= new TightSourceRangeComputer(); rewrite.setTargetSourceRangeComputer(sourceRangeComputer); if (parent instanceof VariableDeclarationStatement && ((VariableDeclarationStatement) parent).fragments().size() == 1) { sourceRangeComputer.addTightSourceNode(parent); rewrite.remove(parent, groupDesc); } else { sourceRangeComputer.addTightSourceNode(variableDeclaration); rewrite.remove(variableDeclaration, groupDesc); } }
Example 7
Source File: TypeParametersFix.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * {@inheritDoc} */ @Override public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException { TextEditGroup group= createTextEditGroup(FixMessages.TypeParametersFix_remove_redundant_type_arguments_description, cuRewrite); ASTRewrite rewrite= cuRewrite.getASTRewrite(); rewrite.setTargetSourceRangeComputer(new NoCommentSourceRangeComputer()); ListRewrite listRewrite= rewrite.getListRewrite(fParameterizedType, ParameterizedType.TYPE_ARGUMENTS_PROPERTY); List<Type> typeArguments= fParameterizedType.typeArguments(); for (Iterator<Type> iterator= typeArguments.iterator(); iterator.hasNext();) { listRewrite.remove(iterator.next(), group); } }
Example 8
Source File: LocalCorrectionsSubProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public static void addFallThroughProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) { ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot()); if (selectedNode instanceof SwitchCase && selectedNode.getLocationInParent() == SwitchStatement.STATEMENTS_PROPERTY) { AST ast= selectedNode.getAST(); ASTNode parent= selectedNode.getParent(); // insert break: ASTRewrite rewrite= ASTRewrite.create(ast); ListRewrite listRewrite= rewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY); listRewrite.insertBefore(ast.newBreakStatement(), selectedNode, null); String label= CorrectionMessages.LocalCorrectionsSubProcessor_insert_break_statement; Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_BREAK_STATEMENT, image); proposals.add(proposal); // insert //$FALL-THROUGH$: rewrite= ASTRewrite.create(ast); rewrite.setTargetSourceRangeComputer(new NoCommentSourceRangeComputer()); listRewrite= rewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY); ASTNode fallThroughComment= rewrite.createStringPlaceholder("//$FALL-THROUGH$", ASTNode.EMPTY_STATEMENT); //$NON-NLS-1$ listRewrite.insertBefore(fallThroughComment, selectedNode, null); label= CorrectionMessages.LocalCorrectionsSubProcessor_insert_fall_through; image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_FALL_THROUGH, image); proposals.add(proposal); } }