Search in sources :

Example 6 with SelectionAnalyzer

use of org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer in project che by eclipse.

the class JavaTextSelection method resolveSelectedNodes.

public ASTNode[] resolveSelectedNodes() {
    if (fNodesRequested)
        return fSelectedNodes;
    fNodesRequested = true;
    CompilationUnit root = resolvePartialAstAtOffset();
    if (root == null)
        return null;
    Selection ds = Selection.createFromStartLength(getOffset(), getLength());
    SelectionAnalyzer analyzer = new SelectionAnalyzer(ds, false);
    root.accept(analyzer);
    fSelectedNodes = analyzer.getSelectedNodes();
    fCoveringNode = analyzer.getLastCoveringNode();
    return fSelectedNodes;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SelectionAnalyzer(org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer) Selection(org.eclipse.jdt.internal.corext.dom.Selection) TextSelection(org.eclipse.che.jface.text.TextSelection)

Example 7 with SelectionAnalyzer

use of org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer in project che by eclipse.

the class TypeContextChecker method parseType.

private static Type parseType(String typeString, IJavaProject javaProject, List<String> problemsCollector) {
    if (//speed up for a common case //$NON-NLS-1$
    "".equals(typeString.trim()))
        return null;
    if (!typeString.trim().equals(typeString))
        return null;
    StringBuffer cuBuff = new StringBuffer();
    //$NON-NLS-1$
    cuBuff.append("interface A{");
    int offset = cuBuff.length();
    //$NON-NLS-1$
    cuBuff.append(typeString).append(" m();}");
    ASTParser p = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
    p.setSource(cuBuff.toString().toCharArray());
    p.setProject(javaProject);
    CompilationUnit cu = (CompilationUnit) p.createAST(null);
    Selection selection = Selection.createFromStartLength(offset, typeString.length());
    SelectionAnalyzer analyzer = new SelectionAnalyzer(selection, false);
    cu.accept(analyzer);
    ASTNode selected = analyzer.getFirstSelectedNode();
    if (!(selected instanceof Type))
        return null;
    Type type = (Type) selected;
    if (MethodTypesSyntaxChecker.isVoidArrayType(type))
        return null;
    IProblem[] problems = ASTNodes.getProblems(type, ASTNodes.NODE_ONLY, ASTNodes.PROBLEMS);
    if (problems.length > 0) {
        for (int i = 0; i < problems.length; i++) problemsCollector.add(problems[i].getMessage());
    }
    String typeNodeRange = cuBuff.substring(type.getStartPosition(), ASTNodes.getExclusiveEnd(type));
    if (typeString.equals(typeNodeRange))
        return type;
    else
        return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SelectionAnalyzer(org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) IType(org.eclipse.jdt.core.IType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) Selection(org.eclipse.jdt.internal.corext.dom.Selection) ASTNode(org.eclipse.jdt.core.dom.ASTNode) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Example 8 with SelectionAnalyzer

use of org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer in project che by eclipse.

the class QuickAssistProcessor method getMakeVariableDeclarationFinalProposals.

private static boolean getMakeVariableDeclarationFinalProposals(IInvocationContext context, Collection<ICommandAccess> resultingCollections) {
    SelectionAnalyzer analyzer = new SelectionAnalyzer(Selection.createFromStartLength(context.getSelectionOffset(), context.getSelectionLength()), false);
    context.getASTRoot().accept(analyzer);
    ASTNode[] selectedNodes = analyzer.getSelectedNodes();
    if (selectedNodes.length == 0)
        return false;
    IProposableFix fix = VariableDeclarationFix.createChangeModifierToFinalFix(context.getASTRoot(), selectedNodes);
    if (fix == null)
        return false;
    if (resultingCollections == null)
        return true;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    Map<String, String> options = new Hashtable<String, String>();
    options.put(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL, CleanUpOptions.TRUE);
    options.put(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_LOCAL_VARIABLES, CleanUpOptions.TRUE);
    options.put(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PARAMETERS, CleanUpOptions.TRUE);
    options.put(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PRIVATE_FIELDS, CleanUpOptions.TRUE);
    VariableDeclarationCleanUp cleanUp = new VariableDeclarationCleanUp(options);
    FixCorrectionProposal proposal = new FixCorrectionProposal(fix, cleanUp, IProposalRelevance.MAKE_VARIABLE_DECLARATION_FINAL, image, context);
    resultingCollections.add(proposal);
    return true;
}
Also used : SelectionAnalyzer(org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer) FixCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.FixCorrectionProposal) Hashtable(java.util.Hashtable) IProposableFix(org.eclipse.jdt.internal.corext.fix.IProposableFix) Image(org.eclipse.swt.graphics.Image) VariableDeclarationCleanUp(org.eclipse.jdt.internal.ui.fix.VariableDeclarationCleanUp)

Aggregations

SelectionAnalyzer (org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer)8 ASTNode (org.eclipse.jdt.core.dom.ASTNode)5 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)4 Selection (org.eclipse.jdt.internal.corext.dom.Selection)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 ASTParser (org.eclipse.jdt.core.dom.ASTParser)3 RefactoringASTParser (org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser)3 Expression (org.eclipse.jdt.core.dom.Expression)2 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)2 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)2 Hashtable (java.util.Hashtable)1 TextSelection (org.eclipse.che.jface.text.TextSelection)1 IType (org.eclipse.jdt.core.IType)1 IProblem (org.eclipse.jdt.core.compiler.IProblem)1 ArrayType (org.eclipse.jdt.core.dom.ArrayType)1 Block (org.eclipse.jdt.core.dom.Block)1 ConstructorInvocation (org.eclipse.jdt.core.dom.ConstructorInvocation)1 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)1 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)1 QualifiedType (org.eclipse.jdt.core.dom.QualifiedType)1