Search in sources :

Example 11 with IProblem

use of org.eclipse.jdt.core.compiler.IProblem in project eclipse.jdt.ls by eclipse.

the class LinkedNodeFinder method findByProblems.

public static SimpleName[] findByProblems(ASTNode parent, SimpleName nameNode) {
    ArrayList<SimpleName> res = new ArrayList<>();
    ASTNode astRoot = parent.getRoot();
    if (!(astRoot instanceof CompilationUnit)) {
        return null;
    }
    IProblem[] problems = ((CompilationUnit) astRoot).getProblems();
    int nameNodeKind = getNameNodeProblemKind(problems, nameNode);
    if (nameNodeKind == 0) {
        // no problem on node
        return null;
    }
    int bodyStart = parent.getStartPosition();
    int bodyEnd = bodyStart + parent.getLength();
    String name = nameNode.getIdentifier();
    for (int i = 0; i < problems.length; i++) {
        IProblem curr = problems[i];
        int probStart = curr.getSourceStart();
        int probEnd = curr.getSourceEnd() + 1;
        if (probStart > bodyStart && probEnd < bodyEnd) {
            int currKind = getProblemKind(curr);
            if ((nameNodeKind & currKind) != 0) {
                ASTNode node = NodeFinder.perform(parent, probStart, (probEnd - probStart));
                if (node instanceof SimpleName && name.equals(((SimpleName) node).getIdentifier())) {
                    res.add((SimpleName) node);
                }
            }
        }
    }
    return res.toArray(new SimpleName[res.size()]);
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Example 12 with IProblem

use of org.eclipse.jdt.core.compiler.IProblem in project eclipse.jdt.ls by eclipse.

the class AbstractQuickFixTest method evaluateCodeActions.

protected List<Command> evaluateCodeActions(ICompilationUnit cu) throws JavaModelException {
    CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
    IProblem[] problems = astRoot.getProblems();
    Range range = getRange(cu, problems);
    CodeActionParams parms = new CodeActionParams();
    TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
    textDocument.setUri(JDTUtils.toURI(cu));
    parms.setTextDocument(textDocument);
    parms.setRange(range);
    CodeActionContext context = new CodeActionContext();
    context.setDiagnostics(DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problems)));
    parms.setContext(context);
    List<Command> commands = new CodeActionHandler().getCodeActionCommands(parms, new NullProgressMonitor());
    if (this.ignoredCommands != null) {
        List<Command> filteredList = new ArrayList<>();
        for (Command command : commands) {
            for (String str : this.ignoredCommands) {
                if (command.getTitle().matches(str)) {
                    filteredList.add(command);
                    break;
                }
            }
        }
        commands.removeAll(filteredList);
    }
    return commands;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) ArrayList(java.util.ArrayList) Range(org.eclipse.lsp4j.Range) IProblem(org.eclipse.jdt.core.compiler.IProblem) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) CodeActionHandler(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler) Command(org.eclipse.lsp4j.Command)

Example 13 with IProblem

use of org.eclipse.jdt.core.compiler.IProblem in project eclipse.jdt.ls by eclipse.

the class RefactoringAnalyzeUtil method getIntroducedCompileProblems.

// 
// public static LambdaExpression getLambdaExpression(TextEdit edit, TextChange change, CompilationUnit cuNode) {
// ASTNode decl = RefactoringAnalyzeUtil.findSimpleNameNode(RefactoringAnalyzeUtil.getNewTextRange(edit, change), cuNode);
// return ((LambdaExpression) ASTNodes.getParent(decl, LambdaExpression.class));
// }
// 
// public static MethodDeclaration getMethodDeclaration(TextEdit edit, TextChange change, CompilationUnit cuNode) {
// ASTNode decl = RefactoringAnalyzeUtil.findSimpleNameNode(RefactoringAnalyzeUtil.getNewTextRange(edit, change), cuNode);
// return ((MethodDeclaration) ASTNodes.getParent(decl, MethodDeclaration.class));
// }
// 
// public static Block getBlock(TextEdit edit, TextChange change, CompilationUnit cuNode) {
// ASTNode decl = RefactoringAnalyzeUtil.findSimpleNameNode(RefactoringAnalyzeUtil.getNewTextRange(edit, change), cuNode);
// return ((Block) ASTNodes.getParent(decl, Block.class));
// }
public static IProblem[] getIntroducedCompileProblems(CompilationUnit newCUNode, CompilationUnit oldCuNode) {
    Set<IProblem> subResult = new HashSet<>();
    Set<IProblem> oldProblems = getOldProblems(oldCuNode);
    IProblem[] newProblems = ASTNodes.getProblems(newCUNode, ASTNodes.INCLUDE_ALL_PARENTS, ASTNodes.PROBLEMS);
    for (int i = 0; i < newProblems.length; i++) {
        IProblem correspondingOld = findCorrespondingProblem(oldProblems, newProblems[i]);
        if (correspondingOld == null) {
            subResult.add(newProblems[i]);
        }
    }
    return subResult.toArray(new IProblem[subResult.size()]);
}
Also used : IProblem(org.eclipse.jdt.core.compiler.IProblem) HashSet(java.util.HashSet)

Example 14 with IProblem

use of org.eclipse.jdt.core.compiler.IProblem in project eclipse.jdt.ls by eclipse.

the class DiagnosticHandlerTest method testMultipleLineRange.

@Test
public void testMultipleLineRange() throws Exception {
    IJavaProject javaProject = newEmptyProject();
    IPackageFragmentRoot sourceFolder = javaProject.getPackageFragmentRoot(javaProject.getProject().getFolder("src"));
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public boolean foo(boolean b1) {\n");
    buf.append("        if (false) {\n");
    buf.append("            return true;\n");
    buf.append("        }\n");
    buf.append("        return false;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, monitor);
    IProblem[] problems = astRoot.getProblems();
    List<Diagnostic> diagnostics = DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problems));
    assertEquals(diagnostics.size(), 1);
    Range range = diagnostics.get(0).getRange();
    assertNotEquals(range.getStart().getLine(), range.getEnd().getLine());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJavaProject(org.eclipse.jdt.core.IJavaProject) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) IProblem(org.eclipse.jdt.core.compiler.IProblem) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Test(org.junit.Test) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)

Example 15 with IProblem

use of org.eclipse.jdt.core.compiler.IProblem in project eclipse.jdt.ls by eclipse.

the class DocumentLifeCycleHandlerTest method getCodeActions.

protected List<Command> getCodeActions(ICompilationUnit cu) throws JavaModelException {
    CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
    IProblem[] problems = astRoot.getProblems();
    Range range = getRange(cu, problems);
    CodeActionParams parms = new CodeActionParams();
    TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
    textDocument.setUri(JDTUtils.toURI(cu));
    parms.setTextDocument(textDocument);
    parms.setRange(range);
    CodeActionContext context = new CodeActionContext();
    context.setDiagnostics(DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problems)));
    parms.setContext(context);
    return new CodeActionHandler().getCodeActionCommands(parms, new NullProgressMonitor());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Range(org.eclipse.lsp4j.Range) Lsp4jAssertions.assertRange(org.eclipse.jdt.ls.core.internal.Lsp4jAssertions.assertRange) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Aggregations

IProblem (org.eclipse.jdt.core.compiler.IProblem)62 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)28 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)25 ArrayList (java.util.ArrayList)21 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)10 File (java.io.File)9 ASTParser (org.eclipse.jdt.core.dom.ASTParser)9 ClassFile (org.eclipse.jdt.internal.compiler.ClassFile)7 IProblemLocation (org.eclipse.jdt.ui.text.java.IProblemLocation)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 HashMap (java.util.HashMap)5 ASTNode (org.eclipse.jdt.core.dom.ASTNode)5 RefactoringStatusEntry (org.eclipse.ltk.core.refactoring.RefactoringStatusEntry)5 URI (java.net.URI)4 HashSet (java.util.HashSet)4 StringTokenizer (java.util.StringTokenizer)4 IJavaProject (org.eclipse.jdt.core.IJavaProject)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4