|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Broadcom |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Broadcom - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | +package org.springframework.ide.vscode.boot.java.utils; |
| 12 | + |
| 13 | +import java.io.File; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.HashSet; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.stream.Collectors; |
| 23 | + |
| 24 | +import org.eclipse.lsp4j.Diagnostic; |
| 25 | +import org.eclipse.lsp4j.WorkspaceSymbol; |
| 26 | +import org.springframework.ide.vscode.boot.java.beans.CachedBean; |
| 27 | +import org.springframework.ide.vscode.boot.java.reconcilers.CachedDiagnostics; |
| 28 | +import org.springframework.ide.vscode.commons.java.IJavaProject; |
| 29 | +import org.springframework.ide.vscode.commons.protocol.spring.SpringIndexElement; |
| 30 | +import org.springframework.ide.vscode.commons.util.UriUtil; |
| 31 | + |
| 32 | +/** |
| 33 | + * Class to capture the major results of scanning one or more files |
| 34 | + * for Spring symbol creation, indexing, and reconciling |
| 35 | + * |
| 36 | + * @author Martin Lippert |
| 37 | + */ |
| 38 | +public class SpringIndexerJavaScanResult { |
| 39 | + |
| 40 | + private final Map<String, Long> markedForReconciling; |
| 41 | + |
| 42 | + private final List<CachedSymbol> generatedSymbols; |
| 43 | + private final List<CachedBean> generatedBeans; |
| 44 | + private final List<CachedDiagnostics> generatedDiagnostics; |
| 45 | + |
| 46 | + private final IJavaProject project; |
| 47 | + private final String[] javaFiles; |
| 48 | + |
| 49 | + public SpringIndexerJavaScanResult(IJavaProject project, String[] javaFiles) { |
| 50 | + this.project = project; |
| 51 | + this.javaFiles = javaFiles; |
| 52 | + |
| 53 | + this.markedForReconciling = new HashMap<>(); |
| 54 | + this.generatedSymbols = new ArrayList<CachedSymbol>(); |
| 55 | + this.generatedBeans = new ArrayList<CachedBean>(); |
| 56 | + this.generatedDiagnostics = new ArrayList<CachedDiagnostics>(); |
| 57 | + } |
| 58 | + |
| 59 | + public SpringIndexerJavaScanResult(IJavaProject project, String[] javaFiles, SymbolHandler symbolHandler, |
| 60 | + CachedSymbol[] symbols, CachedBean[] beans, CachedDiagnostics[] diagnostics) { |
| 61 | + |
| 62 | + this.project = project; |
| 63 | + this.javaFiles = javaFiles; |
| 64 | + |
| 65 | + this.markedForReconciling = new HashMap<>(); |
| 66 | + this.generatedSymbols = Arrays.asList(symbols); |
| 67 | + this.generatedBeans = Arrays.asList(beans); |
| 68 | + this.generatedDiagnostics = Arrays.asList(diagnostics); |
| 69 | + } |
| 70 | + |
| 71 | + public Map<String, Long> getMarkedForReconcilingWithCompleteIndex() { |
| 72 | + return markedForReconciling; |
| 73 | + } |
| 74 | + |
| 75 | + public void markForReconcilingWithCompleteIndex(String file, long lastModified) { |
| 76 | + this.markedForReconciling.put(file, lastModified); |
| 77 | + } |
| 78 | + |
| 79 | + public List<CachedBean> getGeneratedBeans() { |
| 80 | + return generatedBeans; |
| 81 | + } |
| 82 | + |
| 83 | + public List<CachedSymbol> getGeneratedSymbols() { |
| 84 | + return generatedSymbols; |
| 85 | + } |
| 86 | + |
| 87 | + public List<CachedDiagnostics> getGeneratedDiagnostics() { |
| 88 | + return generatedDiagnostics; |
| 89 | + } |
| 90 | + |
| 91 | + public void publishResults(SymbolHandler symbolHandler) { |
| 92 | + WorkspaceSymbol[] enhancedSymbols = generatedSymbols.stream().map(cachedSymbol -> cachedSymbol.getEnhancedSymbol()).toArray(WorkspaceSymbol[]::new); |
| 93 | + Map<String, List<SpringIndexElement>> allBeans = generatedBeans.stream().filter(cachedBean -> cachedBean.getBean() != null).collect(Collectors.groupingBy(CachedBean::getDocURI, Collectors.mapping(CachedBean::getBean, Collectors.toList()))); |
| 94 | + Map<String, List<Diagnostic>> diagnosticsByDoc = generatedDiagnostics.stream().filter(cachedDiagnostic -> cachedDiagnostic.getDiagnostic() != null).collect(Collectors.groupingBy(CachedDiagnostics::getDocURI, Collectors.mapping(CachedDiagnostics::getDiagnostic, Collectors.toList()))); |
| 95 | + |
| 96 | + addEmptyDiagnostics(diagnosticsByDoc, javaFiles); // to make sure that files without diagnostics publish an empty array of diagnostics |
| 97 | + |
| 98 | + symbolHandler.addSymbols(this.project, enhancedSymbols, allBeans, diagnosticsByDoc); |
| 99 | + } |
| 100 | + |
| 101 | + private void addEmptyDiagnostics(Map<String, List<Diagnostic>> diagnosticsByDoc, String[] javaFiles) { |
| 102 | + for (int i = 0; i < javaFiles.length; i++) { |
| 103 | + File file = new File(javaFiles[i]); |
| 104 | + String docURI = UriUtil.toUri(file).toASCIIString(); |
| 105 | + |
| 106 | + if (!diagnosticsByDoc.containsKey(docURI)) { |
| 107 | + diagnosticsByDoc.put(docURI, Collections.emptyList()); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments