use of org.eclipse.jdt.internal.corext.dom.TokenScanner in project che by eclipse.
the class QuickAssistProcessor method getAddBlockProposals.
private static boolean getAddBlockProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
if (!(node instanceof Statement)) {
return false;
}
/*
* only show the quick assist when the selection is of the control statement keywords (if, else, while,...)
* but not inside the statement or the if expression.
*/
if (!isControlStatementWithBlock(node) && isControlStatementWithBlock(node.getParent())) {
int statementStart = node.getStartPosition();
int statementEnd = statementStart + node.getLength();
int offset = context.getSelectionOffset();
int length = context.getSelectionLength();
if (length == 0) {
if (offset != statementEnd) {
// cursor at end
return false;
}
} else {
if (offset > statementStart || offset + length < statementEnd) {
// statement selected
return false;
}
}
node = node.getParent();
}
StructuralPropertyDescriptor childProperty = null;
ASTNode child = null;
switch(node.getNodeType()) {
case ASTNode.IF_STATEMENT:
ASTNode then = ((IfStatement) node).getThenStatement();
ASTNode elseStatement = ((IfStatement) node).getElseStatement();
if ((then instanceof Block) && (elseStatement instanceof Block || elseStatement == null)) {
break;
}
int thenEnd = then.getStartPosition() + then.getLength();
int selectionEnd = context.getSelectionOffset() + context.getSelectionLength();
if (!(then instanceof Block)) {
if (selectionEnd <= thenEnd) {
childProperty = IfStatement.THEN_STATEMENT_PROPERTY;
child = then;
break;
} else if (elseStatement != null && selectionEnd < elseStatement.getStartPosition()) {
// find out if we are before or after the 'else' keyword
try {
TokenScanner scanner = new TokenScanner(context.getCompilationUnit());
int elseTokenStart = scanner.getNextStartOffset(thenEnd, true);
if (selectionEnd < elseTokenStart) {
childProperty = IfStatement.THEN_STATEMENT_PROPERTY;
child = then;
break;
}
} catch (CoreException e) {
// ignore
}
}
}
if (elseStatement != null && !(elseStatement instanceof Block) && context.getSelectionOffset() >= thenEnd) {
childProperty = IfStatement.ELSE_STATEMENT_PROPERTY;
child = elseStatement;
}
break;
case ASTNode.WHILE_STATEMENT:
ASTNode whileBody = ((WhileStatement) node).getBody();
if (!(whileBody instanceof Block)) {
childProperty = WhileStatement.BODY_PROPERTY;
child = whileBody;
}
break;
case ASTNode.FOR_STATEMENT:
ASTNode forBody = ((ForStatement) node).getBody();
if (!(forBody instanceof Block)) {
childProperty = ForStatement.BODY_PROPERTY;
child = forBody;
}
break;
case ASTNode.ENHANCED_FOR_STATEMENT:
ASTNode enhancedForBody = ((EnhancedForStatement) node).getBody();
if (!(enhancedForBody instanceof Block)) {
childProperty = EnhancedForStatement.BODY_PROPERTY;
child = enhancedForBody;
}
break;
case ASTNode.DO_STATEMENT:
ASTNode doBody = ((DoStatement) node).getBody();
if (!(doBody instanceof Block)) {
childProperty = DoStatement.BODY_PROPERTY;
child = doBody;
}
break;
default:
}
if (child == null) {
return false;
}
if (resultingCollections == null) {
return true;
}
AST ast = node.getAST();
{
ASTRewrite rewrite = ASTRewrite.create(ast);
ASTNode childPlaceholder = rewrite.createMoveTarget(child);
Block replacingBody = ast.newBlock();
replacingBody.statements().add(childPlaceholder);
rewrite.set(node, childProperty, replacingBody, null);
String label;
if (childProperty == IfStatement.THEN_STATEMENT_PROPERTY) {
label = CorrectionMessages.QuickAssistProcessor_replacethenwithblock_description;
} else if (childProperty == IfStatement.ELSE_STATEMENT_PROPERTY) {
label = CorrectionMessages.QuickAssistProcessor_replaceelsewithblock_description;
} else {
label = CorrectionMessages.QuickAssistProcessor_replacebodywithblock_description;
}
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.ADD_BLOCK, image);
proposal.setCommandId(ADD_BLOCK_ID);
proposal.setEndPosition(rewrite.track(child));
resultingCollections.add(proposal);
}
if (node.getNodeType() == ASTNode.IF_STATEMENT) {
ASTRewrite rewrite = ASTRewrite.create(ast);
while (node.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
node = node.getParent();
}
boolean missingBlockFound = false;
boolean foundElse = false;
IfStatement ifStatement;
Statement thenStatment;
Statement elseStatment;
do {
ifStatement = (IfStatement) node;
thenStatment = ifStatement.getThenStatement();
elseStatment = ifStatement.getElseStatement();
if (!(thenStatment instanceof Block)) {
ASTNode childPlaceholder1 = rewrite.createMoveTarget(thenStatment);
Block replacingBody1 = ast.newBlock();
replacingBody1.statements().add(childPlaceholder1);
rewrite.set(ifStatement, IfStatement.THEN_STATEMENT_PROPERTY, replacingBody1, null);
if (thenStatment != child) {
missingBlockFound = true;
}
}
if (elseStatment != null) {
foundElse = true;
}
node = elseStatment;
} while (elseStatment instanceof IfStatement);
if (elseStatment != null && !(elseStatment instanceof Block)) {
ASTNode childPlaceholder2 = rewrite.createMoveTarget(elseStatment);
Block replacingBody2 = ast.newBlock();
replacingBody2.statements().add(childPlaceholder2);
rewrite.set(ifStatement, IfStatement.ELSE_STATEMENT_PROPERTY, replacingBody2, null);
if (elseStatment != child) {
missingBlockFound = true;
}
}
if (missingBlockFound && foundElse) {
String label = CorrectionMessages.QuickAssistProcessor_replacethenelsewithblock_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CHANGE_IF_ELSE_TO_BLOCK, image);
resultingCollections.add(proposal);
}
}
return true;
}
use of org.eclipse.jdt.internal.corext.dom.TokenScanner in project che by eclipse.
the class SelectionAwareSourceRangeComputer method initializeRanges.
private void initializeRanges() throws CoreException {
fRanges = new HashMap<ASTNode, SourceRange>();
if (fSelectedNodes.length == 0)
return;
fRanges.put(fSelectedNodes[0], super.computeSourceRange(fSelectedNodes[0]));
int last = fSelectedNodes.length - 1;
fRanges.put(fSelectedNodes[last], super.computeSourceRange(fSelectedNodes[last]));
IScanner scanner = ToolFactory.createScanner(true, false, false, false);
char[] source = fDocumentPortionToScan.toCharArray();
scanner.setSource(source);
// initializeRanges() is only called once
fDocumentPortionToScan = null;
TokenScanner tokenizer = new TokenScanner(scanner);
int pos = tokenizer.getNextStartOffset(0, false);
ASTNode currentNode = fSelectedNodes[0];
int newStart = Math.min(fSelectionStart + pos, currentNode.getStartPosition());
SourceRange range = fRanges.get(currentNode);
fRanges.put(currentNode, new SourceRange(newStart, range.getLength() + range.getStartPosition() - newStart));
currentNode = fSelectedNodes[last];
int scannerStart = currentNode.getStartPosition() + currentNode.getLength() - fSelectionStart;
tokenizer.setOffset(scannerStart);
pos = scannerStart;
int token = -1;
try {
while (true) {
token = tokenizer.readNext(false);
pos = tokenizer.getCurrentEndOffset();
}
} catch (CoreException e) {
}
if (token == ITerminalSymbols.TokenNameCOMMENT_LINE) {
int index = pos - 1;
while (index >= 0 && IndentManipulation.isLineDelimiterChar(source[index])) {
pos--;
index--;
}
}
int newEnd = Math.max(fSelectionStart + pos, currentNode.getStartPosition() + currentNode.getLength());
range = fRanges.get(currentNode);
fRanges.put(currentNode, new SourceRange(range.getStartPosition(), newEnd - range.getStartPosition()));
}
use of org.eclipse.jdt.internal.corext.dom.TokenScanner in project che by eclipse.
the class ExtractMethodAnalyzer method visit.
@Override
public boolean visit(LambdaExpression node) {
Selection selection = getSelection();
int selectionStart = selection.getOffset();
int selectionExclusiveEnd = selection.getExclusiveEnd();
int lambdaStart = node.getStartPosition();
int lambdaExclusiveEnd = lambdaStart + node.getLength();
ASTNode body = node.getBody();
int bodyStart = body.getStartPosition();
int bodyExclusiveEnd = bodyStart + body.getLength();
boolean isValidSelection = false;
if ((body instanceof Block) && (bodyStart < selectionStart && selectionExclusiveEnd <= bodyExclusiveEnd)) {
// if selection is inside lambda body's block
isValidSelection = true;
} else if (body instanceof Expression) {
try {
TokenScanner scanner = new TokenScanner(fCUnit);
int arrowExclusiveEnd = scanner.getTokenEndOffset(ITerminalSymbols.TokenNameARROW, lambdaStart);
if (selectionStart >= arrowExclusiveEnd) {
isValidSelection = true;
}
} catch (CoreException e) {
// ignore
}
}
if (selectionStart <= lambdaStart && selectionExclusiveEnd >= lambdaExclusiveEnd) {
// if selection covers the lambda node
isValidSelection = true;
}
if (!isValidSelection) {
return false;
}
return super.visit(node);
}
use of org.eclipse.jdt.internal.corext.dom.TokenScanner in project eclipse.jdt.ls by eclipse.
the class SelectionAwareSourceRangeComputer method initializeRanges.
private void initializeRanges() throws CoreException {
fRanges = new HashMap<>();
if (fSelectedNodes.length == 0) {
return;
}
fRanges.put(fSelectedNodes[0], super.computeSourceRange(fSelectedNodes[0]));
int last = fSelectedNodes.length - 1;
fRanges.put(fSelectedNodes[last], super.computeSourceRange(fSelectedNodes[last]));
IJavaProject javaProject = ((CompilationUnit) fSelectedNodes[0].getRoot()).getTypeRoot().getJavaProject();
String sourceLevel = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
String complianceLevel = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
IScanner scanner = ToolFactory.createScanner(true, false, false, sourceLevel, complianceLevel);
char[] source = fDocumentPortionToScan.toCharArray();
scanner.setSource(source);
// initializeRanges() is only called once
fDocumentPortionToScan = null;
TokenScanner tokenizer = new TokenScanner(scanner);
int pos = tokenizer.getNextStartOffset(0, false);
ASTNode currentNode = fSelectedNodes[0];
int newStart = Math.min(fSelectionStart + pos, currentNode.getStartPosition());
SourceRange range = fRanges.get(currentNode);
fRanges.put(currentNode, new SourceRange(newStart, range.getLength() + range.getStartPosition() - newStart));
currentNode = fSelectedNodes[last];
int scannerStart = currentNode.getStartPosition() + currentNode.getLength() - fSelectionStart;
tokenizer.setOffset(scannerStart);
pos = scannerStart;
int token = -1;
try {
while (true) {
token = tokenizer.readNext(false);
pos = tokenizer.getCurrentEndOffset();
}
} catch (CoreException e) {
}
if (token == ITerminalSymbols.TokenNameCOMMENT_LINE) {
int index = pos - 1;
while (index >= 0 && IndentManipulation.isLineDelimiterChar(source[index])) {
pos--;
index--;
}
}
int newEnd = Math.max(fSelectionStart + pos, currentNode.getStartPosition() + currentNode.getLength());
range = fRanges.get(currentNode);
fRanges.put(currentNode, new SourceRange(range.getStartPosition(), newEnd - range.getStartPosition()));
// Selection is between /*]*/ and /*[*/, but the extended range of the "transform" node actually includes /*[*/ as well.
while (true) {
List<ASTNode> children = ASTNodes.getChildren(currentNode);
if (!children.isEmpty()) {
ASTNode lastChild = children.get(children.size() - 1);
SourceRange extRange = super.computeSourceRange(lastChild);
if (extRange.getStartPosition() + extRange.getLength() > newEnd) {
fRanges.put(lastChild, new SourceRange(extRange.getStartPosition(), newEnd - extRange.getStartPosition()));
currentNode = lastChild;
continue;
}
}
break;
}
}
use of org.eclipse.jdt.internal.corext.dom.TokenScanner in project flux by eclipse.
the class QuickAssistProcessor method getAddBlockProposals.
private static boolean getAddBlockProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
if (!(node instanceof Statement)) {
return false;
}
/*
* only show the quick assist when the selection is of the control statement keywords (if, else, while,...)
* but not inside the statement or the if expression.
*/
if (!isControlStatementWithBlock(node) && isControlStatementWithBlock(node.getParent())) {
int statementStart = node.getStartPosition();
int statementEnd = statementStart + node.getLength();
int offset = context.getSelectionOffset();
int length = context.getSelectionLength();
if (length == 0) {
if (offset != statementEnd) {
// cursor at end
return false;
}
} else {
if (offset > statementStart || offset + length < statementEnd) {
// statement selected
return false;
}
}
node = node.getParent();
}
StructuralPropertyDescriptor childProperty = null;
ASTNode child = null;
switch(node.getNodeType()) {
case ASTNode.IF_STATEMENT:
ASTNode then = ((IfStatement) node).getThenStatement();
ASTNode elseStatement = ((IfStatement) node).getElseStatement();
if ((then instanceof Block) && (elseStatement instanceof Block || elseStatement == null)) {
break;
}
int thenEnd = then.getStartPosition() + then.getLength();
int selectionEnd = context.getSelectionOffset() + context.getSelectionLength();
if (!(then instanceof Block)) {
if (selectionEnd <= thenEnd) {
childProperty = IfStatement.THEN_STATEMENT_PROPERTY;
child = then;
break;
} else if (elseStatement != null && selectionEnd < elseStatement.getStartPosition()) {
// find out if we are before or after the 'else' keyword
try {
TokenScanner scanner = new TokenScanner(context.getCompilationUnit());
int elseTokenStart = scanner.getNextStartOffset(thenEnd, true);
if (selectionEnd < elseTokenStart) {
childProperty = IfStatement.THEN_STATEMENT_PROPERTY;
child = then;
break;
}
} catch (CoreException e) {
// ignore
}
}
}
if (elseStatement != null && !(elseStatement instanceof Block) && context.getSelectionOffset() >= thenEnd) {
childProperty = IfStatement.ELSE_STATEMENT_PROPERTY;
child = elseStatement;
}
break;
case ASTNode.WHILE_STATEMENT:
ASTNode whileBody = ((WhileStatement) node).getBody();
if (!(whileBody instanceof Block)) {
childProperty = WhileStatement.BODY_PROPERTY;
child = whileBody;
}
break;
case ASTNode.FOR_STATEMENT:
ASTNode forBody = ((ForStatement) node).getBody();
if (!(forBody instanceof Block)) {
childProperty = ForStatement.BODY_PROPERTY;
child = forBody;
}
break;
case ASTNode.ENHANCED_FOR_STATEMENT:
ASTNode enhancedForBody = ((EnhancedForStatement) node).getBody();
if (!(enhancedForBody instanceof Block)) {
childProperty = EnhancedForStatement.BODY_PROPERTY;
child = enhancedForBody;
}
break;
case ASTNode.DO_STATEMENT:
ASTNode doBody = ((DoStatement) node).getBody();
if (!(doBody instanceof Block)) {
childProperty = DoStatement.BODY_PROPERTY;
child = doBody;
}
break;
default:
}
if (child == null) {
return false;
}
if (resultingCollections == null) {
return true;
}
AST ast = node.getAST();
{
ASTRewrite rewrite = ASTRewrite.create(ast);
ASTNode childPlaceholder = rewrite.createMoveTarget(child);
Block replacingBody = ast.newBlock();
replacingBody.statements().add(childPlaceholder);
rewrite.set(node, childProperty, replacingBody, null);
String label;
if (childProperty == IfStatement.THEN_STATEMENT_PROPERTY) {
label = CorrectionMessages.QuickAssistProcessor_replacethenwithblock_description;
} else if (childProperty == IfStatement.ELSE_STATEMENT_PROPERTY) {
label = CorrectionMessages.QuickAssistProcessor_replaceelsewithblock_description;
} else {
label = CorrectionMessages.QuickAssistProcessor_replacebodywithblock_description;
}
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.ADD_BLOCK);
proposal.setCommandId(ADD_BLOCK_ID);
proposal.setEndPosition(rewrite.track(child));
resultingCollections.add(proposal);
}
if (node.getNodeType() == ASTNode.IF_STATEMENT) {
ASTRewrite rewrite = ASTRewrite.create(ast);
while (node.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
node = node.getParent();
}
boolean missingBlockFound = false;
boolean foundElse = false;
IfStatement ifStatement;
Statement thenStatment;
Statement elseStatment;
do {
ifStatement = (IfStatement) node;
thenStatment = ifStatement.getThenStatement();
elseStatment = ifStatement.getElseStatement();
if (!(thenStatment instanceof Block)) {
ASTNode childPlaceholder1 = rewrite.createMoveTarget(thenStatment);
Block replacingBody1 = ast.newBlock();
replacingBody1.statements().add(childPlaceholder1);
rewrite.set(ifStatement, IfStatement.THEN_STATEMENT_PROPERTY, replacingBody1, null);
if (thenStatment != child) {
missingBlockFound = true;
}
}
if (elseStatment != null) {
foundElse = true;
}
node = elseStatment;
} while (elseStatment instanceof IfStatement);
if (elseStatment != null && !(elseStatment instanceof Block)) {
ASTNode childPlaceholder2 = rewrite.createMoveTarget(elseStatment);
Block replacingBody2 = ast.newBlock();
replacingBody2.statements().add(childPlaceholder2);
rewrite.set(ifStatement, IfStatement.ELSE_STATEMENT_PROPERTY, replacingBody2, null);
if (elseStatment != child) {
missingBlockFound = true;
}
}
if (missingBlockFound && foundElse) {
String label = CorrectionMessages.QuickAssistProcessor_replacethenelsewithblock_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CHANGE_IF_ELSE_TO_BLOCK);
resultingCollections.add(proposal);
}
}
return true;
}
Aggregations