Search in sources :

Example 1 with Selection

use of org.eclipse.jdt.ls.core.internal.corext.dom.Selection in project eclipse.jdt.ls by eclipse.

the class StatementAnalyzer method endVisit.

@Override
public void endVisit(CompilationUnit node) {
    if (!hasSelectedNodes()) {
        super.endVisit(node);
        return;
    }
    ASTNode selectedNode = getFirstSelectedNode();
    Selection selection = getSelection();
    if (node != selectedNode) {
        ASTNode parent = selectedNode.getParent();
        fStatus.merge(CommentAnalyzer.perform(selection, fScanner.getScanner(), parent.getStartPosition(), parent.getLength()));
    }
    if (!fStatus.hasFatalError()) {
        checkSelectedNodes();
    }
    super.endVisit(node);
}
Also used : Selection(org.eclipse.jdt.ls.core.internal.corext.dom.Selection) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 2 with Selection

use of org.eclipse.jdt.ls.core.internal.corext.dom.Selection in project eclipse.jdt.ls by eclipse.

the class ExtractMethodAnalyzer method computeOutput.

private void computeOutput(RefactoringStatus status) {
    // First find all writes inside the selection.
    FlowContext flowContext = new FlowContext(0, fMaxVariableId + 1);
    flowContext.setConsiderAccessMode(true);
    flowContext.setComputeMode(FlowContext.RETURN_VALUES);
    FlowInfo returnInfo = new InOutFlowAnalyzer(flowContext).perform(getSelectedNodes());
    IVariableBinding[] returnValues = returnInfo.get(flowContext, FlowInfo.WRITE | FlowInfo.WRITE_POTENTIAL | FlowInfo.UNKNOWN);
    // Compute a selection that exactly covers the selected nodes
    Selection selection = getSelectedNodeRange();
    List<IVariableBinding> localReads = new ArrayList<>();
    flowContext.setComputeMode(FlowContext.ARGUMENTS);
    FlowInfo argInfo = new InputFlowAnalyzer(flowContext, selection, true).perform(fEnclosingBodyDeclaration);
    IVariableBinding[] reads = argInfo.get(flowContext, FlowInfo.READ | FlowInfo.READ_POTENTIAL | FlowInfo.UNKNOWN);
    outer: for (int i = 0; i < returnValues.length && localReads.size() < returnValues.length; i++) {
        IVariableBinding binding = returnValues[i];
        for (int x = 0; x < reads.length; x++) {
            if (reads[x] == binding) {
                localReads.add(binding);
                fReturnValue = binding;
                continue outer;
            }
        }
    }
    switch(localReads.size()) {
        case 0:
            fReturnValue = null;
            break;
        case 1:
            break;
        default:
            fReturnValue = null;
            StringBuffer affectedLocals = new StringBuffer();
            for (int i = 0; i < localReads.size(); i++) {
                IVariableBinding binding = localReads.get(i);
                String bindingName = BindingLabelProvider.getBindingLabel(binding, BindingLabelProvider.DEFAULT_TEXTFLAGS | JavaElementLabels.F_PRE_TYPE_SIGNATURE);
                affectedLocals.append(bindingName);
                if (i != localReads.size() - 1) {
                    affectedLocals.append('\n');
                }
            }
            String message = MessageFormat.format(RefactoringCoreMessages.ExtractMethodAnalyzer_assignments_to_local, new Object[] { affectedLocals.toString() });
            status.addFatalError(message, JavaStatusContext.create(fCUnit, getSelection()));
            return;
    }
    List<IVariableBinding> callerLocals = new ArrayList<>(5);
    FlowInfo localInfo = new InputFlowAnalyzer(flowContext, selection, false).perform(fEnclosingBodyDeclaration);
    IVariableBinding[] writes = localInfo.get(flowContext, FlowInfo.WRITE | FlowInfo.WRITE_POTENTIAL | FlowInfo.UNKNOWN);
    for (int i = 0; i < writes.length; i++) {
        IVariableBinding write = writes[i];
        if (getSelection().covers(ASTNodes.findDeclaration(write, fEnclosingBodyDeclaration))) {
            callerLocals.add(write);
        }
    }
    fCallerLocals = callerLocals.toArray(new IVariableBinding[callerLocals.size()]);
    if (fReturnValue != null && getSelection().covers(ASTNodes.findDeclaration(fReturnValue, fEnclosingBodyDeclaration))) {
        fReturnLocal = fReturnValue;
    }
}
Also used : FlowInfo(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.flow.FlowInfo) Selection(org.eclipse.jdt.ls.core.internal.corext.dom.Selection) InputFlowAnalyzer(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.flow.InputFlowAnalyzer) ArrayList(java.util.ArrayList) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) FlowContext(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.flow.FlowContext) InOutFlowAnalyzer(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.flow.InOutFlowAnalyzer)

Example 3 with Selection

use of org.eclipse.jdt.ls.core.internal.corext.dom.Selection in project eclipse.jdt.ls 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);
}
Also used : TokenScanner(org.eclipse.jdt.internal.corext.dom.TokenScanner) CoreException(org.eclipse.core.runtime.CoreException) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) Selection(org.eclipse.jdt.ls.core.internal.corext.dom.Selection) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block)

Example 4 with Selection

use of org.eclipse.jdt.ls.core.internal.corext.dom.Selection in project eclipse.jdt.ls by eclipse.

the class ExtractMethodAnalyzer method computeTypeVariables.

private ITypeBinding[] computeTypeVariables(ITypeBinding[] bindings) {
    Selection selection = getSelection();
    Set<ITypeBinding> result = new HashSet<>();
    // first remove all type variables that come from outside of the method
    // or are covered by the selection
    CompilationUnit compilationUnit = (CompilationUnit) fEnclosingBodyDeclaration.getRoot();
    for (int i = 0; i < bindings.length; i++) {
        ASTNode decl = compilationUnit.findDeclaringNode(bindings[i]);
        if (decl == null || (!selection.covers(decl) && decl.getParent() instanceof MethodDeclaration)) {
            result.add(bindings[i]);
        }
    }
    // all all type variables which are needed since a local variable uses it
    for (int i = 0; i < fArguments.length; i++) {
        IVariableBinding arg = fArguments[i];
        ITypeBinding type = arg.getType();
        if (type != null && type.isTypeVariable()) {
            ASTNode decl = compilationUnit.findDeclaringNode(type);
            if (decl == null || (!selection.covers(decl) && decl.getParent() instanceof MethodDeclaration)) {
                result.add(type);
            }
        }
    }
    return result.toArray(new ITypeBinding[result.size()]);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Selection(org.eclipse.jdt.ls.core.internal.corext.dom.Selection) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) HashSet(java.util.HashSet)

Example 5 with Selection

use of org.eclipse.jdt.ls.core.internal.corext.dom.Selection in project eclipse.jdt.ls by eclipse.

the class ExtractMethodAnalyzer method removeSelectedDeclarations.

private IVariableBinding[] removeSelectedDeclarations(IVariableBinding[] bindings) {
    List<IVariableBinding> result = new ArrayList<>(bindings.length);
    Selection selection = getSelection();
    for (int i = 0; i < bindings.length; i++) {
        ASTNode decl = ((CompilationUnit) fEnclosingBodyDeclaration.getRoot()).findDeclaringNode(bindings[i]);
        if (!selection.covers(decl)) {
            result.add(bindings[i]);
        }
    }
    return result.toArray(new IVariableBinding[result.size()]);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Selection(org.eclipse.jdt.ls.core.internal.corext.dom.Selection) ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Aggregations

Selection (org.eclipse.jdt.ls.core.internal.corext.dom.Selection)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)5 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)3 ArrayList (java.util.ArrayList)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 Block (org.eclipse.jdt.core.dom.Block)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2 HashSet (java.util.HashSet)1 CoreException (org.eclipse.core.runtime.CoreException)1 Expression (org.eclipse.jdt.core.dom.Expression)1 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)1 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)1 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)1 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)1 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)1 TokenScanner (org.eclipse.jdt.internal.corext.dom.TokenScanner)1 FlowContext (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.flow.FlowContext)1 FlowInfo (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.flow.FlowInfo)1 InOutFlowAnalyzer (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.flow.InOutFlowAnalyzer)1 InputFlowAnalyzer (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.flow.InputFlowAnalyzer)1