From 2fcee892e18b7a7d27805b7f875c8cc1e5d6401c Mon Sep 17 00:00:00 2001 From: Jan Lahoda Date: Sun, 10 Jan 2021 10:02:39 +0100 Subject: [PATCH] [NETBEANS-5229] Fixing mapping of jar:file URI to cache and back; disabling ErrorDescriptions in cases when they should be disabled. --- .../modules/java/lsp/server/Utils.java | 23 +++--- .../protocol/TextDocumentServiceImpl.java | 11 ++- .../java/lsp/server/protocol/ServerTest.java | 70 +++++++++++++++++++ 3 files changed, 89 insertions(+), 15 deletions(-) diff --git a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/Utils.java b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/Utils.java index 7ec9bb023a1f..f79bcb8bbcc8 100644 --- a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/Utils.java +++ b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/Utils.java @@ -27,10 +27,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.io.UncheckedIOException; import java.net.MalformedURLException; import java.net.URI; -import java.nio.file.Files; import java.util.Properties; import javax.lang.model.element.ElementKind; import javax.swing.text.Document; @@ -131,18 +129,11 @@ public static int getOffset(Document doc, Position pos) { return LineDocumentUtils.getLineStartFromIndex((LineDocument) doc, pos.getLine()) + pos.getCharacter(); } - public static String toUri(FileObject file) { + public static synchronized String toUri(FileObject file) { if (FileUtil.isArchiveArtifact(file)) { //VS code cannot open jar:file: URLs, workaround: - //another workaround, should be: - //File cacheDir = Places.getCacheSubfile("java-server"); - //but that locks up VS Code, using a temp directory: - File cacheDir; - try { - cacheDir = Files.createTempDirectory("nbcode").toFile(); - } catch (IOException ex) { - throw new UncheckedIOException(ex); - } + File cacheDir = getCacheDir(); + cacheDir.mkdirs(); File segments = new File(cacheDir, "segments"); Properties props = new Properties(); @@ -184,8 +175,8 @@ public static String toUri(FileObject file) { return file.toURI().toString(); } - public static FileObject fromUri(String uri) throws MalformedURLException { - File cacheDir = Places.getCacheSubfile("java-server"); + public static synchronized FileObject fromUri(String uri) throws MalformedURLException { + File cacheDir = getCacheDir(); URI uriUri = URI.create(uri); URI relative = cacheDir.toURI().relativize(uriUri); if (relative != null && new File(cacheDir, relative.toString()).canRead()) { @@ -211,4 +202,8 @@ public static FileObject fromUri(String uri) throws MalformedURLException { } return URLMapper.findFileObject(URI.create(uri).toURL()); } + + private static File getCacheDir() { + return Places.getCacheSubfile("java-server"); + } } diff --git a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/TextDocumentServiceImpl.java b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/TextDocumentServiceImpl.java index 52085f0cd285..608c78902f5c 100644 --- a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/TextDocumentServiceImpl.java +++ b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/TextDocumentServiceImpl.java @@ -55,6 +55,7 @@ import java.util.function.BiConsumer; import java.util.function.IntFunction; import java.util.prefs.Preferences; +import java.util.stream.Collectors; import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; @@ -192,6 +193,7 @@ import org.netbeans.spi.editor.hints.ErrorDescription; import org.netbeans.spi.editor.hints.Fix; import org.netbeans.spi.editor.hints.LazyFixList; +import org.netbeans.spi.editor.hints.Severity; import org.netbeans.spi.java.classpath.support.ClassPathSupport; import org.netbeans.spi.java.hints.JavaFix; import org.openide.cookies.EditorCookie; @@ -1601,7 +1603,14 @@ private void runDiagnoticTasks(String uri) { }, "errors", false); BACKGROUND_TASKS.create(() -> { computeDiags(u, (info, doc) -> { - return new HintsInvoker(HintsSettings.getGlobalSettings(), new AtomicBoolean()).computeHints(info); + Set disabled = org.netbeans.modules.java.hints.spiimpl.Utilities.disableErrors(info.getFileObject()); + if (disabled.size() == Severity.values().length) { + return Collections.emptyList(); + } + return new HintsInvoker(HintsSettings.getGlobalSettings(), new AtomicBoolean()).computeHints(info) + .stream() + .filter(ed -> !disabled.contains(ed.getSeverity())) + .collect(Collectors.toList()); }, "hints", true); }).schedule(DELAY); }); diff --git a/java/java.lsp.server/test/unit/src/org/netbeans/modules/java/lsp/server/protocol/ServerTest.java b/java/java.lsp.server/test/unit/src/org/netbeans/modules/java/lsp/server/protocol/ServerTest.java index fabaa9b99c7c..c80aee817293 100644 --- a/java/java.lsp.server/test/unit/src/org/netbeans/modules/java/lsp/server/protocol/ServerTest.java +++ b/java/java.lsp.server/test/unit/src/org/netbeans/modules/java/lsp/server/protocol/ServerTest.java @@ -29,14 +29,17 @@ import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; +import java.net.URL; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.EnumSet; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.concurrent.CompletableFuture; @@ -123,6 +126,7 @@ import org.openide.cookies.LineCookie; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; +import org.openide.filesystems.URLMapper; import org.openide.modules.ModuleInfo; import org.openide.modules.Places; import org.openide.text.Line; @@ -3109,6 +3113,72 @@ public void logMessage(MessageParams arg0) { } + public void testNoErrorAndHintsFor() throws Exception { + File src = new File(getWorkDir(), "Test.java"); + src.getParentFile().mkdirs(); + String code = "public class Test {\n" + + " private String field;\n" + + "}\n"; + try (Writer w = new FileWriter(src)) { + w.write(code); + } + File otherSrc = new File(getWorkDir(), "Other.java"); + try (Writer w = new FileWriter(otherSrc)) { + w.write("/**Some source*/\n" + + "public class Other {\n" + + " public void test() { }\n" + + "}"); + } + Map> publishedDiagnostics = new HashMap<>(); + FileUtil.refreshFor(getWorkDir()); + Launcher serverLauncher = LSPLauncher.createClientLauncher(new LanguageClient() { + @Override + public void telemetryEvent(Object arg0) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void publishDiagnostics(PublishDiagnosticsParams params) { + synchronized (publishedDiagnostics) { + publishedDiagnostics.computeIfAbsent(params.getUri(), uri -> new ArrayList<>()) + .add(params.getDiagnostics().size()); + publishedDiagnostics.notifyAll(); + } + } + + @Override + public void showMessage(MessageParams arg0) { + } + + @Override + public CompletableFuture showMessageRequest(ShowMessageRequestParams arg0) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void logMessage(MessageParams arg0) { + throw new UnsupportedOperationException("Not supported yet."); + } + }, client.getInputStream(), client.getOutputStream()); + serverLauncher.startListening(); + LanguageServer server = serverLauncher.getRemoteProxy(); + InitializeResult result = server.initialize(new InitializeParams()).get(); + server.getTextDocumentService().didOpen(new DidOpenTextDocumentParams(new TextDocumentItem(toURI(src), "java", 0, code))); + Position pos = new Position(1, 14); + List definition = server.getTextDocumentService().definition(new DefinitionParams(new TextDocumentIdentifier(toURI(src)), pos)).get().getLeft(); + assertEquals(1, definition.size()); + String jlStringURI = definition.get(0).getUri(); + server.getTextDocumentService().didOpen(new DidOpenTextDocumentParams(new TextDocumentItem(jlStringURI, "java", 0, URLMapper.findFileObject(new URL(jlStringURI)).asText()))); + String otherSrcURI = toURI(otherSrc); + server.getTextDocumentService().didOpen(new DidOpenTextDocumentParams(new TextDocumentItem(otherSrcURI, "java", 0, FileUtil.toFileObject(otherSrc).asText()))); + synchronized (publishedDiagnostics) { + while (publishedDiagnostics.get(otherSrcURI) == null || publishedDiagnostics.get(otherSrcURI).size() != 2) { + publishedDiagnostics.wait(); + } + } + assertEquals(Arrays.asList(0, 0), publishedDiagnostics.get(jlStringURI)); + } + interface Validator { public void validate(T t) throws Exception; }