Java Code Examples for org.eclipse.jdt.ui.text.java.IInvocationContext#getSelectionOffset()
The following examples show how to use
org.eclipse.jdt.ui.text.java.IInvocationContext#getSelectionOffset() .
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: 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 2
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 3
Source File: AdvancedQuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private static boolean getExchangeOperandsProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) { // check that user invokes quick assist on infix expression if (!(node instanceof InfixExpression)) { return false; } InfixExpression infixExpression= (InfixExpression)node; Operator operator= infixExpression.getOperator(); if (operator != InfixExpression.Operator.CONDITIONAL_AND && operator != InfixExpression.Operator.AND && operator != InfixExpression.Operator.CONDITIONAL_OR && operator != InfixExpression.Operator.OR && operator != InfixExpression.Operator.EQUALS && operator != InfixExpression.Operator.NOT_EQUALS && operator != InfixExpression.Operator.LESS && operator != InfixExpression.Operator.LESS_EQUALS && operator != InfixExpression.Operator.GREATER && operator != InfixExpression.Operator.GREATER_EQUALS && operator != InfixExpression.Operator.PLUS && operator != InfixExpression.Operator.TIMES && operator != InfixExpression.Operator.XOR) { return false; } int offset= isOperatorSelected(infixExpression, context.getSelectionOffset(), context.getSelectionLength()); if (offset == -1) { return false; } // we could produce quick assist if (resultingCollections == null) { return true; } AST ast= infixExpression.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); // prepare left and right expressions Expression leftExpression= null; Expression rightExpression= null; InfixExpression currentExpression= infixExpression; leftExpression= combineOperands(rewrite, leftExpression, infixExpression.getLeftOperand(), false, operator); if (infixExpression.getRightOperand().getStartPosition() <= context.getSelectionOffset()) { leftExpression= combineOperands(rewrite, leftExpression, infixExpression.getRightOperand(), false, operator); } else { rightExpression= combineOperands(rewrite, rightExpression, infixExpression.getRightOperand(), false, operator); } for (Iterator<Expression> iter= currentExpression.extendedOperands().iterator(); iter.hasNext();) { Expression extendedOperand= iter.next(); if (extendedOperand.getStartPosition() <= context.getSelectionOffset()) { leftExpression= combineOperands(rewrite, leftExpression, extendedOperand, false, operator); } else { rightExpression= combineOperands(rewrite, rightExpression, extendedOperand, false, operator); } } if (NecessaryParenthesesChecker.needsParentheses(leftExpression, infixExpression, InfixExpression.RIGHT_OPERAND_PROPERTY)) { leftExpression= getParenthesizedExpression(ast, leftExpression); } if (NecessaryParenthesesChecker.needsParentheses(rightExpression, infixExpression, InfixExpression.LEFT_OPERAND_PROPERTY)) { rightExpression= getParenthesizedExpression(ast, rightExpression); } if (operator == InfixExpression.Operator.LESS) { operator= InfixExpression.Operator.GREATER; } else if (operator == InfixExpression.Operator.LESS_EQUALS) { operator= InfixExpression.Operator.GREATER_EQUALS; } else if (operator == InfixExpression.Operator.GREATER) { operator= InfixExpression.Operator.LESS; } else if (operator == InfixExpression.Operator.GREATER_EQUALS) { operator= InfixExpression.Operator.LESS_EQUALS; } // create new infix expression InfixExpression newInfix= ast.newInfixExpression(); newInfix.setOperator(operator); newInfix.setLeftOperand(rightExpression); newInfix.setRightOperand(leftExpression); rewrite.replace(infixExpression, newInfix, null); // add correction proposal String label= CorrectionMessages.AdvancedQuickAssistProcessor_exchangeOperands_description; Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.EXCHANGE_OPERANDS, image); resultingCollections.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 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; }