Skip to content

Commit

Permalink
[NETBEANS-5229] Fixing mapping of jar:file URI to cache and back; dis…
Browse files Browse the repository at this point in the history
…abling ErrorDescriptions in cases when they should be disabled.
  • Loading branch information
jlahoda authored Jan 10, 2021
1 parent 4d92aa6 commit 2fcee89
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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()) {
Expand All @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<Severity> 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);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, List<Integer>> publishedDiagnostics = new HashMap<>();
FileUtil.refreshFor(getWorkDir());
Launcher<LanguageServer> 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<MessageActionItem> 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<? extends Location> 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<T> {
public void validate(T t) throws Exception;
}
Expand Down

0 comments on commit 2fcee89

Please sign in to comment.