Search in sources :

Example 41 with Diagnostic

use of org.eclipse.lsp4j.Diagnostic in project eclipse.jdt.ls by eclipse.

the class DocumentLifeCycleHandlerTest method testDidOpenStandaloneFileWithSyntaxError.

@Test
public void testDidOpenStandaloneFileWithSyntaxError() throws Exception {
    IJavaProject javaProject = newDefaultProject();
    IPackageFragmentRoot sourceFolder = javaProject.getPackageFragmentRoot(javaProject.getProject().getFolder("src"));
    IPackageFragment pack1 = sourceFolder.createPackageFragment("java", false, null);
    // @formatter:off
    String standaloneFileContent = "package java;\n" + "public class Foo extends UnknownType {\n" + "	public void method1(){\n" + "		super.whatever()\n" + "	}\n" + "}";
    // @formatter:on
    ICompilationUnit cu1 = pack1.createCompilationUnit("Foo.java", standaloneFileContent, false, null);
    openDocument(cu1, cu1.getSource(), 1);
    List<PublishDiagnosticsParams> diagnosticReports = getClientRequests("publishDiagnostics");
    assertEquals(1, diagnosticReports.size());
    PublishDiagnosticsParams diagParam = diagnosticReports.get(0);
    assertEquals("Unexpected number of errors " + diagParam.getDiagnostics(), 1, diagParam.getDiagnostics().size());
    Diagnostic d = diagParam.getDiagnostics().get(0);
    assertEquals("Syntax error, insert \";\" to complete BlockStatements", d.getMessage());
    assertRange(3, 17, 18, d.getRange());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJavaProject(org.eclipse.jdt.core.IJavaProject) PublishDiagnosticsParams(org.eclipse.lsp4j.PublishDiagnosticsParams) Diagnostic(org.eclipse.lsp4j.Diagnostic) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 42 with Diagnostic

use of org.eclipse.lsp4j.Diagnostic in project eclipse.jdt.ls by eclipse.

the class WorkspaceDiagnosticsHandlerTest method testToDiagnosticsArray.

@Test
public void testToDiagnosticsArray() throws Exception {
    String msg1 = "Something's wrong Jim";
    IMarker m1 = createMarker(IMarker.SEVERITY_WARNING, msg1, 2, 95, 100);
    String msg2 = "He's dead";
    IMarker m2 = createMarker(IMarker.SEVERITY_ERROR, msg2, 10, 1015, 1025);
    String msg3 = "It's probably time to panic";
    IMarker m3 = createMarker(42, msg3, 100, 10000, 10005);
    IDocument d = mock(IDocument.class);
    when(d.getLineOffset(1)).thenReturn(90);
    when(d.getLineOffset(9)).thenReturn(1000);
    when(d.getLineOffset(99)).thenReturn(10000);
    List<Diagnostic> diags = handler.toDiagnosticsArray(d, new IMarker[] { m1, m2, m3 });
    assertEquals(3, diags.size());
    Range r;
    Diagnostic d1 = diags.get(0);
    assertEquals(msg1, d1.getMessage());
    assertEquals(DiagnosticSeverity.Warning, d1.getSeverity());
    r = d1.getRange();
    assertEquals(1, r.getStart().getLine());
    assertEquals(5, r.getStart().getCharacter());
    assertEquals(1, r.getEnd().getLine());
    assertEquals(10, r.getEnd().getCharacter());
    Diagnostic d2 = diags.get(1);
    assertEquals(msg2, d2.getMessage());
    assertEquals(DiagnosticSeverity.Error, d2.getSeverity());
    r = d2.getRange();
    assertEquals(9, r.getStart().getLine());
    assertEquals(15, r.getStart().getCharacter());
    assertEquals(9, r.getEnd().getLine());
    assertEquals(25, r.getEnd().getCharacter());
    Diagnostic d3 = diags.get(2);
    assertEquals(msg3, d3.getMessage());
    assertEquals(DiagnosticSeverity.Information, d3.getSeverity());
    r = d3.getRange();
    assertEquals(99, r.getStart().getLine());
    assertEquals(0, r.getStart().getCharacter());
    assertEquals(99, r.getEnd().getLine());
    assertEquals(5, r.getEnd().getCharacter());
}
Also used : Diagnostic(org.eclipse.lsp4j.Diagnostic) IMarker(org.eclipse.core.resources.IMarker) Range(org.eclipse.lsp4j.Range) IDocument(org.eclipse.jface.text.IDocument) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 43 with Diagnostic

use of org.eclipse.lsp4j.Diagnostic in project eclipse.jdt.ls by eclipse.

the class CodeActionHandler method getProblemLocations.

private IProblemLocation[] getProblemLocations(ICompilationUnit unit, List<Diagnostic> diagnostics) {
    IProblemLocation[] locations = new IProblemLocation[diagnostics.size()];
    for (int i = 0; i < diagnostics.size(); i++) {
        Diagnostic diagnostic = diagnostics.get(i);
        int problemId = getProblemId(diagnostic);
        int start = DiagnosticsHelper.getStartOffset(unit, diagnostic.getRange());
        int end = DiagnosticsHelper.getEndOffset(unit, diagnostic.getRange());
        boolean isError = diagnostic.getSeverity() == DiagnosticSeverity.Error;
        locations[i] = new ProblemLocation(start, end - start, problemId, isError);
    }
    return locations;
}
Also used : Diagnostic(org.eclipse.lsp4j.Diagnostic) IProblemLocation(org.eclipse.jdt.ls.core.internal.corrections.IProblemLocation) ProblemLocation(org.eclipse.jdt.ls.core.internal.corrections.ProblemLocation) IProblemLocation(org.eclipse.jdt.ls.core.internal.corrections.IProblemLocation)

Example 44 with Diagnostic

use of org.eclipse.lsp4j.Diagnostic in project eclipse.jdt.ls by eclipse.

the class DiagnosticsHandler method toDiagnosticsArray.

public static List<Diagnostic> toDiagnosticsArray(IOpenable openable, List<IProblem> problems) {
    List<Diagnostic> array = new ArrayList<>(problems.size());
    for (IProblem problem : problems) {
        Diagnostic diag = new Diagnostic();
        diag.setSource(JavaLanguageServerPlugin.SERVER_SOURCE_ID);
        diag.setMessage(problem.getMessage());
        diag.setCode(Integer.toString(problem.getID()));
        diag.setSeverity(convertSeverity(problem));
        diag.setRange(convertRange(openable, problem));
        array.add(diag);
    }
    return array;
}
Also used : ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.lsp4j.Diagnostic) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Example 45 with Diagnostic

use of org.eclipse.lsp4j.Diagnostic in project ballerina by ballerina-lang.

the class BallerinaTextDocumentService method codeAction.

@Override
public CompletableFuture<List<? extends Command>> codeAction(CodeActionParams params) {
    return CompletableFuture.supplyAsync(() -> {
        List<Command> commands = new ArrayList<>();
        String topLevelNodeType = CommonUtil.topLevelNodeTypeInLine(params.getTextDocument(), params.getRange().getStart(), documentManager);
        if (topLevelNodeType != null) {
            commands.add(CommandUtil.getDocGenerationCommand(topLevelNodeType, params.getTextDocument().getUri(), params.getRange().getStart().getLine()));
            commands.add(CommandUtil.getAllDocGenerationCommand(params.getTextDocument().getUri()));
        } else if (!params.getContext().getDiagnostics().isEmpty()) {
            params.getContext().getDiagnostics().forEach(diagnostic -> {
                commands.addAll(CommandUtil.getCommandsByDiagnostic(diagnostic, params, lSPackageCache));
            });
        }
        return commands;
    });
}
Also used : CommonUtil(org.ballerinalang.langserver.common.utils.CommonUtil) JsonObject(com.google.gson.JsonObject) RenameUtil(org.ballerinalang.langserver.rename.RenameUtil) HoverUtil(org.ballerinalang.langserver.hover.util.HoverUtil) DidChangeTextDocumentParams(org.eclipse.lsp4j.DidChangeTextDocumentParams) NodeContextKeys(org.ballerinalang.langserver.common.constants.NodeContextKeys) DidSaveTextDocumentParams(org.eclipse.lsp4j.DidSaveTextDocumentParams) TreeVisitor(org.ballerinalang.langserver.completions.TreeVisitor) LSDocument(org.ballerinalang.langserver.common.LSDocument) PublishDiagnosticsParams(org.eclipse.lsp4j.PublishDiagnosticsParams) CodeLens(org.eclipse.lsp4j.CodeLens) Map(java.util.Map) Location(org.eclipse.lsp4j.Location) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) URI(java.net.URI) DidCloseTextDocumentParams(org.eclipse.lsp4j.DidCloseTextDocumentParams) RenameParams(org.eclipse.lsp4j.RenameParams) Path(java.nio.file.Path) SignatureTreeVisitor(org.ballerinalang.langserver.signature.SignatureTreeVisitor) ReferenceUtil(org.ballerinalang.langserver.references.util.ReferenceUtil) TextDocumentService(org.eclipse.lsp4j.services.TextDocumentService) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) DiagnosticListener(org.ballerinalang.util.diagnostic.DiagnosticListener) MarkedString(org.eclipse.lsp4j.MarkedString) DocumentOnTypeFormattingParams(org.eclipse.lsp4j.DocumentOnTypeFormattingParams) CompletionItem(org.eclipse.lsp4j.CompletionItem) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) List(java.util.List) Command(org.eclipse.lsp4j.Command) DidOpenTextDocumentParams(org.eclipse.lsp4j.DidOpenTextDocumentParams) WorkspacePackageRepository(org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository) Optional(java.util.Optional) Debouncer(org.ballerinalang.langserver.util.Debouncer) WorkspaceDocumentManager(org.ballerinalang.langserver.workspace.WorkspaceDocumentManager) DocumentSymbolParams(org.eclipse.lsp4j.DocumentSymbolParams) WorkspaceDocumentManagerImpl(org.ballerinalang.langserver.workspace.WorkspaceDocumentManagerImpl) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) DocumentRangeFormattingParams(org.eclipse.lsp4j.DocumentRangeFormattingParams) Hover(org.eclipse.lsp4j.Hover) Compiler(org.wso2.ballerinalang.compiler.Compiler) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) ArrayList(java.util.ArrayList) CommandUtil(org.ballerinalang.langserver.command.CommandUtil) SignatureHelpUtil(org.ballerinalang.langserver.signature.SignatureHelpUtil) PositionTreeVisitor(org.ballerinalang.langserver.common.position.PositionTreeVisitor) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) TextEdit(org.eclipse.lsp4j.TextEdit) PackageRepository(org.ballerinalang.repository.PackageRepository) DocumentFormattingParams(org.eclipse.lsp4j.DocumentFormattingParams) Position(org.eclipse.lsp4j.Position) CompletionKeys(org.ballerinalang.langserver.completions.CompletionKeys) CodeLensParams(org.eclipse.lsp4j.CodeLensParams) CompletionList(org.eclipse.lsp4j.CompletionList) SymbolFindingVisitor(org.ballerinalang.langserver.symbols.SymbolFindingVisitor) CompletionItemResolver(org.ballerinalang.langserver.completions.util.CompletionItemResolver) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) DocumentHighlight(org.eclipse.lsp4j.DocumentHighlight) DefinitionUtil(org.ballerinalang.langserver.definition.util.DefinitionUtil) SignatureHelp(org.eclipse.lsp4j.SignatureHelp) LSCustomErrorStrategy(org.ballerinalang.langserver.common.LSCustomErrorStrategy) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) Paths(java.nio.file.Paths) CompletionCustomErrorStrategy(org.ballerinalang.langserver.completions.CompletionCustomErrorStrategy) Collections(java.util.Collections) TopLevelResolver(org.ballerinalang.langserver.completions.resolvers.TopLevelResolver) TextDocumentFormatUtil(org.ballerinalang.langserver.format.TextDocumentFormatUtil) ReferenceParams(org.eclipse.lsp4j.ReferenceParams) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) Command(org.eclipse.lsp4j.Command) ArrayList(java.util.ArrayList) MarkedString(org.eclipse.lsp4j.MarkedString)

Aggregations

Diagnostic (org.eclipse.lsp4j.Diagnostic)56 Test (org.junit.Test)31 Editor (org.springframework.ide.vscode.languageserver.testharness.Editor)22 Range (org.eclipse.lsp4j.Range)13 ArrayList (java.util.ArrayList)12 PublishDiagnosticsParams (org.eclipse.lsp4j.PublishDiagnosticsParams)11 List (java.util.List)8 Position (org.eclipse.lsp4j.Position)8 DiagnosticSeverity (org.eclipse.lsp4j.DiagnosticSeverity)7 URI (java.net.URI)5 Path (java.nio.file.Path)5 Map (java.util.Map)5 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)5 Command (org.eclipse.lsp4j.Command)5 MarkedString (org.eclipse.lsp4j.MarkedString)5 CodeAction (org.springframework.ide.vscode.languageserver.testharness.CodeAction)5 IOException (java.io.IOException)4 Collections (java.util.Collections)4 HashMap (java.util.HashMap)4 ImmutableList (com.google.common.collect.ImmutableList)3