Search in sources :

Example 11 with TextDocumentIdentifier

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

the class SpringIndexer method updateDocument.

public CompletableFuture<Void> updateDocument(String docURI, String content) {
    synchronized (this) {
        if (docURI.endsWith(".java") && lastInitializeItem != null) {
            try {
                Optional<IJavaProject> maybeProject = projectFinder.find(new TextDocumentIdentifier(docURI));
                if (maybeProject.isPresent()) {
                    String[] classpathEntries = getClasspathEntries(maybeProject.get());
                    UpdateItem updateItem = new UpdateItem(docURI, content, classpathEntries);
                    updateQueue.put(updateItem);
                    return updateItem.getFuture();
                }
            } catch (Exception e) {
                log.error("{}", e);
            }
        }
    }
    return null;
}
Also used : TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) IJavaProject(org.springframework.ide.vscode.commons.java.IJavaProject)

Example 12 with TextDocumentIdentifier

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

the class SpringIndexer method scanFiles.

private void scanFiles(WorkspaceFolder directory) {
    try {
        Map<Optional<IJavaProject>, List<String>> projects = Files.walk(Paths.get(new URI(directory.getUri()))).filter(path -> path.getFileName().toString().endsWith(".java")).filter(Files::isRegularFile).map(path -> path.toAbsolutePath().toString()).collect(Collectors.groupingBy((javaFile) -> projectFinder.find(new TextDocumentIdentifier(new File(javaFile).toURI().toString()))));
        projects.forEach((maybeProject, files) -> maybeProject.ifPresent(project -> scanProject(project, files.toArray(new String[0]))));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Arrays(java.util.Arrays) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SymbolProvider(org.springframework.ide.vscode.boot.java.handlers.SymbolProvider) LoggerFactory(org.slf4j.LoggerFactory) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) FileASTRequestor(org.eclipse.jdt.core.dom.FileASTRequestor) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Location(org.eclipse.lsp4j.Location) AnnotationHierarchyAwareLookup(org.springframework.ide.vscode.boot.java.annotations.AnnotationHierarchyAwareLookup) URI(java.net.URI) Path(java.nio.file.Path) ExceptionUtil(org.springframework.ide.vscode.commons.util.ExceptionUtil) Predicate(java.util.function.Predicate) SymbolAddOnInformation(org.springframework.ide.vscode.boot.java.handlers.SymbolAddOnInformation) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) EnhancedSymbolInformation(org.springframework.ide.vscode.boot.java.handlers.EnhancedSymbolInformation) List(java.util.List) Stream(java.util.stream.Stream) ASTParser(org.eclipse.jdt.core.dom.ASTParser) WorkspaceFolder(org.eclipse.lsp4j.WorkspaceFolder) AST(org.eclipse.jdt.core.dom.AST) Optional(java.util.Optional) SimpleWorkspaceService(org.springframework.ide.vscode.commons.languageserver.util.SimpleWorkspaceService) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleLanguageServer(org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer) MarkerAnnotation(org.eclipse.jdt.core.dom.MarkerAnnotation) JavaProjectFinder(org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder) SymbolKind(org.eclipse.lsp4j.SymbolKind) BootLanguageServerParams(org.springframework.ide.vscode.boot.BootLanguageServerParams) IJavaProject(org.springframework.ide.vscode.commons.java.IJavaProject) ProjectObserver(org.springframework.ide.vscode.commons.languageserver.java.ProjectObserver) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) StringUtil(org.springframework.ide.vscode.commons.util.StringUtil) ImmutableList(com.google.common.collect.ImmutableList) Annotation(org.eclipse.jdt.core.dom.Annotation) AnnotationHierarchies(org.springframework.ide.vscode.boot.java.annotations.AnnotationHierarchies) UriUtil(org.springframework.ide.vscode.commons.util.UriUtil) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) IClasspath(org.springframework.ide.vscode.commons.java.IClasspath) LanguageId(org.springframework.ide.vscode.commons.util.text.LanguageId) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) Logger(org.slf4j.Logger) Files(java.nio.file.Files) JavaCore(org.eclipse.jdt.core.JavaCore) Futures(org.springframework.ide.vscode.commons.util.Futures) FileUtils(org.apache.commons.io.FileUtils) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) File(java.io.File) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Listener(org.springframework.ide.vscode.commons.languageserver.java.ProjectObserver.Listener) Paths(java.nio.file.Paths) Collections(java.util.Collections) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Optional(java.util.Optional) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Files(java.nio.file.Files) URI(java.net.URI) File(java.io.File)

Example 13 with TextDocumentIdentifier

use of org.eclipse.lsp4j.TextDocumentIdentifier 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 14 with TextDocumentIdentifier

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

the class SimpleTextDocumentService method didSave.

@Override
public void didSave(DidSaveTextDocumentParams params) {
    // Workaround for PT 147263283, where error markers in STS are lost on document save.
    // STS 3.9.0 does not use the LSP4E editor for edit manifest.yml, which correctly retains error markers after save.
    // Instead, because the LSP4E editor is missing support for hovers and completions, STS 3.9.0 uses its own manifest editor
    // which extends the YEdit editor. This YEdit editor has a problem, where on save, all error markers are deleted.
    // When STS uses the LSP4E editor and no longer needs its own YEdit-based editor, the issue with error markers disappearing
    // on save should not be a problem anymore, and the workaround below will no longer be needed.
    async.execute(() -> {
        if (documentSaveListener != null) {
            TextDocumentIdentifier docId = params.getTextDocument();
            String url = docId.getUri();
            Log.debug("didSave: " + url);
            if (url != null) {
                TextDocument doc = getDocument(url);
                documentSaveListener.accept(new TextDocumentSaveChange(doc));
            }
        }
    });
}
Also used : TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument)

Example 15 with TextDocumentIdentifier

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

the class BootLanguageServerParams method createFallbackProjectCache.

private static JavaProjectsService createFallbackProjectCache(SimpleLanguageServer server) {
    CompositeJavaProjectFinder javaProjectFinder = new CompositeJavaProjectFinder();
    MavenProjectCache mavenProjectCache = new MavenProjectCache(server, MavenCore.getDefault(), true, Paths.get(IJavaProject.PROJECT_CACHE_FOLDER));
    javaProjectFinder.addJavaProjectFinder(new MavenProjectFinder(mavenProjectCache));
    GradleProjectCache gradleProjectCache = new GradleProjectCache(server, GradleCore.getDefault(), true, Paths.get(IJavaProject.PROJECT_CACHE_FOLDER));
    javaProjectFinder.addJavaProjectFinder(new GradleProjectFinder(gradleProjectCache));
    CompositeProjectOvserver projectObserver = new CompositeProjectOvserver(Arrays.asList(mavenProjectCache, gradleProjectCache));
    return new JavaProjectsService() {

        @Override
        public void removeListener(Listener listener) {
            projectObserver.removeListener(listener);
        }

        @Override
        public void addListener(Listener listener) {
            projectObserver.addListener(listener);
        }

        @Override
        public Optional<IJavaProject> find(TextDocumentIdentifier doc) {
            return javaProjectFinder.find(doc);
        }
    };
}
Also used : GradleProjectCache(org.springframework.ide.vscode.commons.gradle.GradleProjectCache) MavenProjectCache(org.springframework.ide.vscode.commons.maven.java.MavenProjectCache) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CompositeJavaProjectFinder(org.springframework.ide.vscode.commons.languageserver.java.CompositeJavaProjectFinder) Listener(org.springframework.ide.vscode.commons.languageserver.java.ProjectObserver.Listener) IJavaProject(org.springframework.ide.vscode.commons.java.IJavaProject) CompositeProjectOvserver(org.springframework.ide.vscode.commons.languageserver.java.CompositeProjectOvserver) MavenProjectFinder(org.springframework.ide.vscode.commons.maven.java.MavenProjectFinder) JavaProjectsService(org.springframework.ide.vscode.boot.jdt.ls.JavaProjectsService) GradleProjectFinder(org.springframework.ide.vscode.commons.gradle.GradleProjectFinder)

Aggregations

TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)172 Test (org.junit.Test)113 Position (org.eclipse.lsp4j.Position)102 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)56 Range (org.eclipse.lsp4j.Range)47 TextDocumentPositionParams (org.eclipse.lsp4j.TextDocumentPositionParams)37 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)35 URI (java.net.URI)34 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)33 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)32 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)32 List (java.util.List)26 VersionedTextDocumentIdentifier (org.eclipse.lsp4j.VersionedTextDocumentIdentifier)25 Location (org.eclipse.lsp4j.Location)23 AbstractQuickFixTest (org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)22 Command (org.eclipse.lsp4j.Command)22 PrepareRenameParams (org.eclipse.lsp4j.PrepareRenameParams)20 FormattingOptions (org.eclipse.lsp4j.FormattingOptions)19 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)18 CodeAction (org.eclipse.lsp4j.CodeAction)18