Java Code Examples for org.eclipse.jdt.ui.text.java.IInvocationContext#getSelectionLength()
The following examples show how to use
org.eclipse.jdt.ui.text.java.IInvocationContext#getSelectionLength() .
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: AdvancedQuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private static boolean getRemoveExtraParenthesesProposals(IInvocationContext context, ASTNode covering, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) { ArrayList<ASTNode> nodes; if (context.getSelectionLength() == 0 && covering instanceof ParenthesizedExpression) { nodes= new ArrayList<ASTNode>(); nodes.add(covering); } else { nodes= coveredNodes; } if (nodes.isEmpty()) return false; IProposableFix fix= ExpressionsFix.createRemoveUnnecessaryParenthesisFix(context.getASTRoot(), nodes.toArray(new ASTNode[nodes.size()])); if (fix == null) return false; if (resultingCollections == null) return true; Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_REMOVE); Map<String, String> options= new Hashtable<String, String>(); options.put(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES, CleanUpOptions.TRUE); options.put(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER, CleanUpOptions.TRUE); FixCorrectionProposal proposal= new FixCorrectionProposal(fix, new ExpressionsCleanUp(options), IProposalRelevance.REMOVE_EXTRA_PARENTHESES, image, context); resultingCollections.add(proposal); return true; }
Example 2
Source File: QuickTemplateProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
public boolean hasAssists(IInvocationContext context) throws CoreException { ICompilationUnit cu= context.getCompilationUnit(); IDocument document= getDocument(cu); int offset= context.getSelectionOffset(); int length= context.getSelectionLength(); if (length == 0) { return false; } try { int startLine= document.getLineOfOffset(offset); int endLine= document.getLineOfOffset(offset + length); IRegion region= document.getLineInformation(endLine); return startLine < endLine || length > 0 && offset == region.getOffset() && length == region.getLength(); } catch (BadLocationException e) { return false; } }
Example 3
Source File: QuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static boolean getExtractMethodProposal(IInvocationContext context, ASTNode coveringNode, boolean problemsAtLocation, Collection<ICommandAccess> proposals) throws CoreException { if (!(coveringNode instanceof Expression) && !(coveringNode instanceof Statement) && !(coveringNode instanceof Block)) { return false; } if (coveringNode instanceof Block) { List<Statement> statements= ((Block) coveringNode).statements(); int startIndex= getIndex(context.getSelectionOffset(), statements); if (startIndex == -1) return false; int endIndex= getIndex(context.getSelectionOffset() + context.getSelectionLength(), statements); if (endIndex == -1 || endIndex <= startIndex) return false; } if (proposals == null) { return true; } final ICompilationUnit cu= context.getCompilationUnit(); final ExtractMethodRefactoring extractMethodRefactoring= new ExtractMethodRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength()); extractMethodRefactoring.setMethodName("extracted"); //$NON-NLS-1$ if (extractMethodRefactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) { String label= CorrectionMessages.QuickAssistProcessor_extractmethod_description; LinkedProposalModel linkedProposalModel= new LinkedProposalModel(); extractMethodRefactoring.setLinkedProposalModel(linkedProposalModel); Image image= JavaPluginImages.get(JavaPluginImages.IMG_MISC_PUBLIC); int relevance= problemsAtLocation ? IProposalRelevance.EXTRACT_METHOD_ERROR : IProposalRelevance.EXTRACT_METHOD; RefactoringCorrectionProposal proposal= new RefactoringCorrectionProposal(label, cu, extractMethodRefactoring, relevance, image); proposal.setCommandId(EXTRACT_METHOD_INPLACE_ID); proposal.setLinkedProposalModel(linkedProposalModel); proposals.add(proposal); } return true; }
Example 4
Source File: AdvancedQuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private static boolean getAddParenthesesForExpressionProposals(IInvocationContext context, ASTNode coveringNode, Collection<ICommandAccess> resultingCollections) { ASTNode node; if (context.getSelectionLength() == 0) { node= coveringNode; while (node != null && !(node instanceof CastExpression) && !(node instanceof InfixExpression) && !(node instanceof InstanceofExpression) && !(node instanceof ConditionalExpression)) { node= node.getParent(); } } else { node= context.getCoveredNode(); } String label= null; if (node instanceof CastExpression) { label= CorrectionMessages.UnresolvedElementsSubProcessor_missingcastbrackets_description; } else if (node instanceof InstanceofExpression) { label= CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_instanceof_description; } else if (node instanceof InfixExpression) { InfixExpression infixExpression= (InfixExpression)node; label= Messages.format(CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_description, infixExpression.getOperator().toString()); } else if (node instanceof ConditionalExpression) { label= CorrectionMessages.AdvancedQuickAssistProcessor_putConditionalExpressionInParentheses; } else { return false; } if (node.getParent() instanceof ParenthesizedExpression) return false; if (resultingCollections == null) return true; AST ast= node.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); ParenthesizedExpression parenthesizedExpression= ast.newParenthesizedExpression(); parenthesizedExpression.setExpression((Expression)rewrite.createCopyTarget(node)); rewrite.replace(node, parenthesizedExpression, null); Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.ADD_PARENTHESES_FOR_EXPRESSION, image); resultingCollections.add(proposal); return true; }
Example 5
Source File: AdvancedQuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private static boolean getPickOutStringProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) { // we work with String's if (!(node instanceof StringLiteral)) { return false; } // user should select part of String int selectionPos= context.getSelectionOffset(); int selectionLen= context.getSelectionLength(); if (selectionLen == 0) { return false; } int valueStart= node.getStartPosition() + 1; int valueEnd= node.getStartPosition() + node.getLength() - 1; // selection must be inside node and the quotes and not contain the full value if (selectionPos < valueStart || selectionPos + selectionLen > valueEnd || valueEnd - valueStart == selectionLen) { return false; } // prepare string parts positions StringLiteral stringLiteral= (StringLiteral) node; String stringValue= stringLiteral.getEscapedValue(); int firstPos= selectionPos - node.getStartPosition(); int secondPos= firstPos + selectionLen; // prepare new string literals AST ast= node.getAST(); StringLiteral leftLiteral= ast.newStringLiteral(); StringLiteral centerLiteral= ast.newStringLiteral(); StringLiteral rightLiteral= ast.newStringLiteral(); try { leftLiteral.setEscapedValue('"' + stringValue.substring(1, firstPos) + '"'); centerLiteral.setEscapedValue('"' + stringValue.substring(firstPos, secondPos) + '"'); rightLiteral.setEscapedValue('"' + stringValue.substring(secondPos, stringValue.length() - 1) + '"'); } catch (IllegalArgumentException e) { return false; } if (resultingCollections == null) { return true; } ASTRewrite rewrite= ASTRewrite.create(ast); // prepare new expression instead of StringLiteral InfixExpression expression= ast.newInfixExpression(); expression.setOperator(InfixExpression.Operator.PLUS); if (firstPos != 1) { expression.setLeftOperand(leftLiteral); } if (firstPos == 1) { expression.setLeftOperand(centerLiteral); } else { expression.setRightOperand(centerLiteral); } if (secondPos < stringValue.length() - 1) { if (firstPos == 1) { expression.setRightOperand(rightLiteral); } else { expression.extendedOperands().add(rightLiteral); } } // use new expression instead of old StirngLiteral rewrite.replace(stringLiteral, expression, null); // add correction proposal String label= CorrectionMessages.AdvancedQuickAssistProcessor_pickSelectedString; Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.PICK_SELECTED_STRING, image); proposal.addLinkedPosition(rewrite.track(centerLiteral), true, "CENTER_STRING"); //$NON-NLS-1$ resultingCollections.add(proposal); return true; }