Search in sources :

Example 11 with BadLocationException

use of org.springframework.ide.vscode.commons.util.BadLocationException 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 12 with BadLocationException

use of org.springframework.ide.vscode.commons.util.BadLocationException in project sts4 by spring-projects.

the class WebfluxAcceptTypeFinder method visit.

@Override
public boolean visit(MethodInvocation node) {
    IMethodBinding methodBinding = node.resolveMethodBinding();
    try {
        if (WebfluxUtils.REQUEST_PREDICATES_TYPE.equals(methodBinding.getDeclaringClass().getBinaryName())) {
            String name = methodBinding.getName();
            if (name != null && WebfluxUtils.REQUEST_PREDICATE_ACCEPT_TYPE_METHOD.equals(name)) {
                SimpleName nameArgument = WebfluxUtils.extractSimpleNameArgument(node);
                if (nameArgument != null && nameArgument.getFullyQualifiedName() != null) {
                    Range range = doc.toRange(nameArgument.getStartPosition(), nameArgument.getLength());
                    acceptTypes.add(new WebfluxRouteElement(nameArgument.getFullyQualifiedName().toString(), range));
                }
            }
        }
    } catch (BadLocationException e) {
    // ignore
    }
    return !WebfluxUtils.isRouteMethodInvocation(methodBinding);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Range(org.eclipse.lsp4j.Range) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException)

Example 13 with BadLocationException

use of org.springframework.ide.vscode.commons.util.BadLocationException in project sts4 by spring-projects.

the class BeansSymbolProvider method getSymbols.

@Override
public Collection<EnhancedSymbolInformation> getSymbols(Annotation node, ITypeBinding annotationType, Collection<ITypeBinding> metaAnnotations, TextDocument doc) {
    if (isMethodAbstract(node))
        return null;
    ImmutableList.Builder<EnhancedSymbolInformation> symbols = ImmutableList.builder();
    boolean isFunction = isFunctionBean(node);
    String beanType = getBeanType(node);
    for (Tuple2<String, DocumentRegion> nameAndRegion : getBeanNames(node, doc)) {
        try {
            symbols.add(new EnhancedSymbolInformation(new SymbolInformation(beanLabel(isFunction, nameAndRegion.getT1(), beanType, "@Bean"), SymbolKind.Interface, new Location(doc.getUri(), doc.toRange(nameAndRegion.getT2()))), null));
        } catch (BadLocationException e) {
            Log.log(e);
        }
    }
    return symbols.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) EnhancedSymbolInformation(org.springframework.ide.vscode.boot.java.handlers.EnhancedSymbolInformation) EnhancedSymbolInformation(org.springframework.ide.vscode.boot.java.handlers.EnhancedSymbolInformation) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException) Location(org.eclipse.lsp4j.Location)

Example 14 with BadLocationException

use of org.springframework.ide.vscode.commons.util.BadLocationException in project sts4 by spring-projects.

the class WebfluxHandlerCodeLensProvider method provideCodeLens.

protected void provideCodeLens(MethodDeclaration node, TextDocument document, List<CodeLens> resultAccumulator) {
    IMethodBinding methodBinding = node.resolveBinding();
    if (methodBinding != null && methodBinding.getDeclaringClass() != null && methodBinding.getMethodDeclaration() != null && methodBinding.getDeclaringClass().getBinaryName() != null && methodBinding.getMethodDeclaration().toString() != null) {
        final String handlerClass = methodBinding.getDeclaringClass().getBinaryName().trim();
        final String handlerMethod = methodBinding.getMethodDeclaration().toString().trim();
        List<SymbolAddOnInformation> handlerInfos = this.springIndexer.getAllAdditionalInformation((addon) -> {
            if (addon instanceof WebfluxHandlerInformation) {
                WebfluxHandlerInformation handlerInfo = (WebfluxHandlerInformation) addon;
                return handlerInfo.getHandlerClass() != null && handlerInfo.getHandlerClass().equals(handlerClass) && handlerInfo.getHandlerMethod() != null && handlerInfo.getHandlerMethod().equals(handlerMethod);
            }
            return false;
        });
        if (handlerInfos != null && handlerInfos.size() > 0) {
            for (Object object : handlerInfos) {
                try {
                    WebfluxHandlerInformation handlerInfo = (WebfluxHandlerInformation) object;
                    CodeLens codeLens = new CodeLens();
                    codeLens.setRange(document.toRange(node.getName().getStartPosition(), node.getName().getLength()));
                    String httpMethod = WebfluxUtils.getStringRep(handlerInfo.getHttpMethods(), string -> string);
                    String codeLensCommand = httpMethod != null ? httpMethod + " " : "";
                    codeLensCommand += handlerInfo.getPath();
                    String acceptType = WebfluxUtils.getStringRep(handlerInfo.getAcceptTypes(), WebfluxUtils::getMediaType);
                    codeLensCommand += acceptType != null ? " - Accept: " + acceptType : "";
                    String contentType = WebfluxUtils.getStringRep(handlerInfo.getContentTypes(), WebfluxUtils::getMediaType);
                    codeLensCommand += contentType != null ? " - Content-Type: " + contentType : "";
                    codeLens.setCommand(new Command(codeLensCommand, null));
                    resultAccumulator.add(codeLens);
                } catch (BadLocationException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) CodeLens(org.eclipse.lsp4j.CodeLens) Command(org.eclipse.lsp4j.Command) SymbolAddOnInformation(org.springframework.ide.vscode.boot.java.handlers.SymbolAddOnInformation) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException)

Example 15 with BadLocationException

use of org.springframework.ide.vscode.commons.util.BadLocationException in project sts4 by spring-projects.

the class WebfluxMethodFinder method visit.

@Override
public boolean visit(MethodInvocation node) {
    boolean visitChildren = true;
    if (node != this.root) {
        IMethodBinding methodBinding = node.resolveMethodBinding();
        try {
            if (WebfluxUtils.REQUEST_PREDICATES_TYPE.equals(methodBinding.getDeclaringClass().getBinaryName())) {
                String name = methodBinding.getName();
                if (name != null && WebfluxUtils.REQUEST_PREDICATE_HTTPMETHOD_METHODS.contains(name)) {
                    Range range = doc.toRange(node.getStartPosition(), node.getLength());
                    methods.add(new WebfluxRouteElement(name, range));
                } else if (name != null && WebfluxUtils.REQUEST_PREDICATE_METHOD_METHOD.equals(name)) {
                    QualifiedName qualifiedName = WebfluxUtils.extractQualifiedNameArgument(node);
                    if (qualifiedName.getName() != null) {
                        Range range = doc.toRange(qualifiedName.getStartPosition(), qualifiedName.getLength());
                        methods.add(new WebfluxRouteElement(qualifiedName.getName().toString(), range));
                    }
                }
            }
        } catch (BadLocationException e) {
        // ignore
        }
        if (WebfluxUtils.isRouteMethodInvocation(methodBinding)) {
            visitChildren = false;
        }
    }
    return visitChildren;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Range(org.eclipse.lsp4j.Range) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException)

Aggregations

BadLocationException (org.springframework.ide.vscode.commons.util.BadLocationException)23 Range (org.eclipse.lsp4j.Range)10 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)9 Location (org.eclipse.lsp4j.Location)5 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 SimpleName (org.eclipse.jdt.core.dom.SimpleName)4 TextDocument (org.springframework.ide.vscode.commons.util.text.TextDocument)3 ImmutableList (com.google.common.collect.ImmutableList)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)2 StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 File (java.io.File)1 Paths (java.nio.file.Paths)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1