Search in sources :

Example 21 with Diagnostic

use of org.eclipse.lsp4j.Diagnostic in project sts4 by spring-projects.

the class SimpleLanguageServer method validateWith.

/**
 * Convenience method. Subclasses can call this to use a {@link IReconcileEngine} ported
 * from old STS codebase to validate a given {@link TextDocument} and publish Diagnostics.
 */
public void validateWith(TextDocumentIdentifier docId, IReconcileEngine engine) {
    CompletableFuture<Void> reconcileSession = this.busyReconcile = new CompletableFuture<Void>();
    // Log.debug("Reconciling BUSY");
    SimpleTextDocumentService documents = getTextDocumentService();
    int requestedVersion = documents.getDocument(docId.getUri()).getVersion();
    // Avoid running in the same thread as lsp4j as it can result
    // in long "hangs" for slow reconcile providers
    Mono.fromRunnable(() -> {
        TextDocument doc = documents.getDocument(docId.getUri()).copy();
        if (requestedVersion != doc.getVersion()) {
            // Do not bother reconciling if document contents is already stale.
            return;
        }
        if (testListener != null) {
            testListener.reconcileStarted(docId.getUri(), doc.getVersion());
        }
        IProblemCollector problems = new IProblemCollector() {

            private LinkedHashSet<Diagnostic> diagnostics = new LinkedHashSet<>();

            private List<Quickfix> quickfixes = new ArrayList<>();

            @Override
            public void endCollecting() {
                documents.setQuickfixes(docId, quickfixes);
                documents.publishDiagnostics(docId, diagnostics);
            }

            @Override
            public void beginCollecting() {
                diagnostics.clear();
            }

            @Override
            public void checkPointCollecting() {
                // publish what has been collected so far
                documents.publishDiagnostics(docId, diagnostics);
            }

            @Override
            public void accept(ReconcileProblem problem) {
                try {
                    DiagnosticSeverity severity = getDiagnosticSeverity(problem);
                    if (severity != null) {
                        Diagnostic d = new Diagnostic();
                        d.setCode(problem.getCode());
                        d.setMessage(problem.getMessage());
                        Range rng = doc.toRange(problem.getOffset(), problem.getLength());
                        d.setRange(rng);
                        d.setSeverity(severity);
                        List<QuickfixData<?>> fixes = problem.getQuickfixes();
                        if (CollectionUtil.hasElements(fixes)) {
                            for (QuickfixData<?> fix : fixes) {
                                quickfixes.add(new Quickfix<>(CODE_ACTION_COMMAND_ID, d, fix));
                            }
                        }
                        diagnostics.add(d);
                    }
                } catch (BadLocationException e) {
                    Log.warn("Invalid reconcile problem ignored", e);
                }
            }
        };
        // try {
        // Thread.sleep(2000);
        // } catch (InterruptedException e) {
        // }
        engine.reconcile(doc, problems);
    }).onErrorResume(error -> {
        Log.log(error);
        return Mono.empty();
    }).doFinally(ignore -> {
        reconcileSession.complete(null);
    // Log.debug("Reconciler DONE : "+this.busyReconcile.isDone());
    }).subscribeOn(RECONCILER_SCHEDULER).subscribe();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) QuickfixData(org.springframework.ide.vscode.commons.languageserver.quickfix.Quickfix.QuickfixData) JsonObject(com.google.gson.JsonObject) CodeLensOptions(org.eclipse.lsp4j.CodeLensOptions) LanguageClient(org.eclipse.lsp4j.services.LanguageClient) Sts4LanguageServer(org.springframework.ide.vscode.commons.languageserver.Sts4LanguageServer) QuickfixData(org.springframework.ide.vscode.commons.languageserver.quickfix.Quickfix.QuickfixData) LoggerFactory(org.slf4j.LoggerFactory) DiagnosticService(org.springframework.ide.vscode.commons.languageserver.DiagnosticService) MessageType(org.eclipse.lsp4j.MessageType) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) VscodeCompletionEngineAdapter(org.springframework.ide.vscode.commons.languageserver.completion.VscodeCompletionEngineAdapter) Map(java.util.Map) IProblemCollector(org.springframework.ide.vscode.commons.languageserver.reconcile.IProblemCollector) URI(java.net.URI) QuickfixRegistry(org.springframework.ide.vscode.commons.languageserver.quickfix.QuickfixRegistry) ProgressService(org.springframework.ide.vscode.commons.languageserver.ProgressService) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException) Collection(java.util.Collection) UUID(java.util.UUID) ClasspathListenerManager(org.springframework.ide.vscode.commons.languageserver.jdt.ls.ClasspathListenerManager) Assert(org.springframework.ide.vscode.commons.util.Assert) ServerCapabilities(org.eclipse.lsp4j.ServerCapabilities) Quickfix(org.springframework.ide.vscode.commons.languageserver.quickfix.Quickfix) List(java.util.List) ExecuteCommandOptions(org.eclipse.lsp4j.ExecuteCommandOptions) JsonArray(com.google.gson.JsonArray) ClasspathListener(org.springframework.ide.vscode.commons.languageserver.jdt.ls.ClasspathListener) WorkspaceFolder(org.eclipse.lsp4j.WorkspaceFolder) InitializeParams(org.eclipse.lsp4j.InitializeParams) WorkspaceFoldersOptions(org.eclipse.lsp4j.WorkspaceFoldersOptions) LanguageClientAware(org.eclipse.lsp4j.services.LanguageClientAware) Disposable(reactor.core.Disposable) ApplyWorkspaceEditParams(org.eclipse.lsp4j.ApplyWorkspaceEditParams) AsyncRunner(org.springframework.ide.vscode.commons.languageserver.util.AsyncRunner) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) QuickfixResolveParams(org.springframework.ide.vscode.commons.languageserver.quickfix.QuickfixResolveParams) Scheduler(reactor.core.scheduler.Scheduler) ArrayList(java.util.ArrayList) JsonElement(com.google.gson.JsonElement) LazyCompletionResolver(org.springframework.ide.vscode.commons.languageserver.completion.VscodeCompletionEngineAdapter.LazyCompletionResolver) ImmutableList(com.google.common.collect.ImmutableList) ExecuteCommandParams(org.eclipse.lsp4j.ExecuteCommandParams) MessageParams(org.eclipse.lsp4j.MessageParams) Schedulers(reactor.core.scheduler.Schedulers) Registration(org.eclipse.lsp4j.Registration) LinkedHashSet(java.util.LinkedHashSet) TextDocumentSyncKind(org.eclipse.lsp4j.TextDocumentSyncKind) InitializeResult(org.eclipse.lsp4j.InitializeResult) RegistrationParams(org.eclipse.lsp4j.RegistrationParams) QuickfixEdit(org.springframework.ide.vscode.commons.languageserver.quickfix.QuickfixEdit) Logger(org.slf4j.Logger) Log(org.springframework.ide.vscode.commons.util.Log) ProgressParams(org.springframework.ide.vscode.commons.languageserver.ProgressParams) ICompletionEngine(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionEngine) Mono(reactor.core.publisher.Mono) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) Consumer(java.util.function.Consumer) CollectionUtil(org.springframework.ide.vscode.commons.util.CollectionUtil) STS4LanguageClient(org.springframework.ide.vscode.commons.languageserver.STS4LanguageClient) Paths(java.nio.file.Paths) ProblemSeverity(org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemSeverity) CompletionOptions(org.eclipse.lsp4j.CompletionOptions) WorkspaceServerCapabilities(org.eclipse.lsp4j.WorkspaceServerCapabilities) IReconcileEngine(org.springframework.ide.vscode.commons.languageserver.reconcile.IReconcileEngine) ReconcileProblem(org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileProblem) Collections(java.util.Collections) ApplyWorkspaceEditResponse(org.eclipse.lsp4j.ApplyWorkspaceEditResponse) IProblemCollector(org.springframework.ide.vscode.commons.languageserver.reconcile.IProblemCollector) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) ReconcileProblem(org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileProblem) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException)

Example 22 with Diagnostic

use of org.eclipse.lsp4j.Diagnostic in project sts4 by spring-projects.

the class ConcourseEditorTest method reconcileUnusedResources.

@Test
public void reconcileUnusedResources() throws Exception {
    Editor editor;
    editor = harness.newEditor("resources:\n" + "- name: version\n" + "  type: semver\n" + "- name: source-repo\n" + "  type: git\n" + "jobs:\n" + "- name: build-it\n" + "  plan:\n" + "  - get: version\n");
    Diagnostic p = editor.assertProblems("source-repo|Unused 'Resource'").get(0);
    assertEquals(DiagnosticSeverity.Error, p.getSeverity());
    editor = harness.newEditor("resources:\n" + "- name: not-used\n" + "  type: pool\n" + "- name: version\n" + "  type: semver\n" + "- name: source-repo\n" + "  type: git\n" + "  source:\n" + "    branch: master\n" + "    uri: git@someplace.com:blah/blah.git\n" + "jobs:\n" + "- name: build-it\n" + "  plan:\n" + "  - aggregate:\n" + // <-- This isn't a real use but looks like one!
    "    - get: not-used\n" + "      resource: version\n" + "    - put: source-repo\n");
    editor.assertProblems("not-used|Unused 'Resource'");
}
Also used : Diagnostic(org.eclipse.lsp4j.Diagnostic) Editor(org.springframework.ide.vscode.languageserver.testharness.Editor) Test(org.junit.Test)

Example 23 with Diagnostic

use of org.eclipse.lsp4j.Diagnostic in project sts4 by spring-projects.

the class ConcourseEditorTest method addSingleRequiredPropertiesQuickfix.

@Test
public void addSingleRequiredPropertiesQuickfix() throws Exception {
    Editor editor = harness.newEditor("resources:\n" + "- name: foo\n" + "  source:\n" + "    username: someone\n" + "# Confuse");
    Diagnostic problem = editor.assertProblems("-|'type' is required", "foo|Unused").get(0);
    CodeAction quickfix = editor.assertCodeAction(problem);
    assertEquals("Add property 'type'", quickfix.getLabel());
    quickfix.perform();
    editor.assertText("resources:\n" + "- name: foo\n" + "  source:\n" + "    username: someone\n" + "  type: <*>\n" + "# Confuse");
}
Also used : CodeAction(org.springframework.ide.vscode.languageserver.testharness.CodeAction) Diagnostic(org.eclipse.lsp4j.Diagnostic) Editor(org.springframework.ide.vscode.languageserver.testharness.Editor) Test(org.junit.Test)

Example 24 with Diagnostic

use of org.eclipse.lsp4j.Diagnostic in project sts4 by spring-projects.

the class ConcourseEditorTest method addMultipleRequiredPropertiesQuickfix.

@Test
public void addMultipleRequiredPropertiesQuickfix() throws Exception {
    Editor editor = harness.newEditor("resources:\n" + "- name: foo\n" + "  type: pool\n" + "  source:\n" + "    username: someone\n");
    Diagnostic problem = editor.assertProblems("foo|Unused", "source|[branch, pool, uri] are required").get(1);
    CodeAction quickfix = editor.assertCodeAction(problem);
    assertEquals("Add properties: [branch, pool, uri]", quickfix.getLabel());
    quickfix.perform();
    editor.assertText("resources:\n" + "- name: foo\n" + "  type: pool\n" + "  source:\n" + "    username: someone\n" + "    uri: <*>\n" + "    branch: \n" + "    pool: \n");
}
Also used : CodeAction(org.springframework.ide.vscode.languageserver.testharness.CodeAction) Diagnostic(org.eclipse.lsp4j.Diagnostic) Editor(org.springframework.ide.vscode.languageserver.testharness.Editor) Test(org.junit.Test)

Example 25 with Diagnostic

use of org.eclipse.lsp4j.Diagnostic in project sts4 by spring-projects.

the class ConcourseEditorTest method resourceInEmbeddedTaskConfigDeprecated.

@Test
public void resourceInEmbeddedTaskConfigDeprecated() throws Exception {
    Editor editor = harness.newEditor("resources:\n" + "- name: my-docker-image\n" + "  type: docker-image\n" + "  source:\n" + "    username: {{docker_hub_username}}\n" + "    password: {{docker_hub_password}}\n" + "    repository: kdvolder/sts3-build-env\n" + "jobs:\n" + "- name: build-commons-update-site\n" + "  plan:\n" + "  - task: hello-world\n" + "    image: my-docker-image\n" + "    config:\n" + "      rootfs_uri: blah\n" + "      image_resource:\n" + "        type: docker-image\n" + "      inputs:\n" + "      - name: commons-git\n" + "      platform: linux\n" + "      run:\n" + "        path: which\n" + "        args:\n" + "        - mvn");
    List<Diagnostic> problems = editor.assertProblems("rootfs_uri|Deprecated", "image_resource|Deprecated");
    for (Diagnostic d : problems) {
        assertEquals(DiagnosticSeverity.Warning, d.getSeverity());
    }
}
Also used : Diagnostic(org.eclipse.lsp4j.Diagnostic) Editor(org.springframework.ide.vscode.languageserver.testharness.Editor) Test(org.junit.Test)

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