Apply quick fix to project fixed

This commit is contained in:
aboyko
2022-09-27 14:08:27 -04:00
parent ec8a5a87ba
commit 7d4c7df7ef
6 changed files with 69 additions and 52 deletions

View File

@@ -289,7 +289,7 @@ public class BootLanguageServerInitializer implements InitializingBean {
components.getReconcileEngine().ifPresent(reconcileEngine -> {
Map<IJavaProject, List<TextDocumentIdentifier>> projectsToDocs = new HashMap<>();
for (String f : files) {
URI uri = Path.of(f).toUri();
URI uri = URI.create(f);
TextDocumentIdentifier docId = new TextDocumentIdentifier(uri.toString());
TextDocument doc = server.getTextDocumentService().getLatestSnapshot(docId.getUri());
if (doc == null) {

View File

@@ -11,13 +11,8 @@
package org.springframework.ide.vscode.boot.java.rewrite;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
@@ -27,8 +22,6 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.io.IOUtils;
import org.eclipse.lsp4j.TextDocumentIdentifier;
@@ -38,8 +31,6 @@ import org.openrewrite.java.tree.J.CompilationUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ide.vscode.boot.java.utils.DocumentContentProvider;
import org.springframework.ide.vscode.commons.java.IClasspath;
import org.springframework.ide.vscode.commons.java.IClasspathUtil;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
import org.springframework.ide.vscode.commons.languageserver.java.ProjectObserver;
@@ -161,40 +152,15 @@ public class RewriteCompilationUnitCache implements DocumentContentProvider, Dis
}
}
public static JavaParser createJavaParser(IJavaProject project) {
try {
List<Path> classpath = getClasspathEntries(project).stream().map(s -> new File(s).toPath()).collect(Collectors.toList());
JavaParser jp = JavaParser.fromJavaVersion().build();
jp.setClasspath(classpath);
return jp;
} catch (Exception e) {
logger.error("{}", e);
return null;
}
}
private JavaParser loadJavaParser(IJavaProject project) {
try {
return javaParsers.get(project, () -> createJavaParser(project));
return javaParsers.get(project, () -> ORAstUtils.createJavaParser(project));
} catch (ExecutionException e) {
logger.error("{}", e);
return null;
}
}
private static Set<String> getClasspathEntries(IJavaProject project) throws Exception {
if (project == null) {
return Collections.emptySet();
} else {
IClasspath classpath = project.getClasspath();
Stream<File> classpathEntries = IClasspathUtil.getAllBinaryRoots(classpath).stream();
return classpathEntries
.filter(file -> file.exists())
.map(file -> file.getAbsolutePath()).collect(Collectors.toSet());
}
}
private void invalidateCuForJavaFile(String uriStr) {
URI uri = URI.create(uriStr);
uriToCu.invalidate(uri);
@@ -304,17 +270,4 @@ public class RewriteCompilationUnitCache implements DocumentContentProvider, Dis
return requestor.apply(null);
}
public List<CompilationUnit> getCompiulationUnits(IJavaProject project) {
List<Path> javaFiles = IClasspathUtil.getProjectJavaSourceFolders(project.getClasspath()).flatMap(folder -> {
try {
return Files.walk(folder.toPath());
} catch (IOException e) {
logger.error("", e);
}
return Stream.empty();
}).filter(Files::isRegularFile).filter(p -> p.getFileName().toString().endsWith(".java")).collect(Collectors.toList());
JavaParser javaParser = loadJavaParser(project);
return ORAstUtils.parse(javaParser, javaFiles);
}
}

View File

@@ -136,7 +136,7 @@ public class RewriteReconciler implements JavaReconciler {
try {
List<RecipeSpringJavaProblemDescriptor> descriptors = getProblemRecipeDescriptors(project);
JavaParser javaParser = RewriteCompilationUnitCache.createJavaParser(project);
JavaParser javaParser = ORAstUtils.createJavaParser(project);
List<CompilationUnit> cus = ORAstUtils.parseInputs(javaParser, docs.stream().map(d -> new Parser.Input(Path.of(d.getUri()), () -> {
return new ByteArrayInputStream(d.get().getBytes());
})).collect(Collectors.toList()));

View File

@@ -139,7 +139,7 @@ public class RewriteRefactorings implements CodeActionResolver, QuickfixHandler
boolean projectWide = data.recipeScope == RecipeScope.PROJECT;
Recipe r = createRecipe(data);
if (projectWide) {
return applyRecipe(r, project.get(), cuCache.getCompiulationUnits(project.get()));
return applyRecipe(r, project.get(), ORAstUtils.parse(documents, project.get()));
} else {
CompilationUnit cu = cuCache.getCU(project.get(), URI.create(data.docUri));
if (cu == null) {
@@ -150,6 +150,7 @@ public class RewriteRefactorings implements CodeActionResolver, QuickfixHandler
}
return null;
}
private Recipe createRecipe(Data d) {
Recipe r = recipeRepo.getRecipe(d.id).orElse(null);