This is an automated email from the ASF dual-hosted git repository.
dbalek pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/master by this push:
new 3fd09f3484 LSP: Open sources from Jar files as read-only from their
true locations.
new 00bf9629ec Merge pull request #6286 from
dbalek/dbalek/lsp-src-from-jars
3fd09f3484 is described below
commit 3fd09f3484ea743361479f57c2baefc4def35b55
Author: Dusan Balek <[email protected]>
AuthorDate: Wed Aug 2 12:20:56 2023 +0200
LSP: Open sources from Jar files as read-only from their true locations.
---
.../modules/java/lsp/server/URITranslator.java | 91 +++-------------------
.../modules/java/lsp/server/protocol/Server.java | 2 +
.../lsp/server/protocol/WorkspaceServiceImpl.java | 14 ++++
java/java.lsp.server/vscode/src/extension.ts | 8 ++
4 files changed, 33 insertions(+), 82 deletions(-)
diff --git
a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/URITranslator.java
b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/URITranslator.java
index c36346eead..00f4a91ea5 100644
---
a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/URITranslator.java
+++
b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/URITranslator.java
@@ -19,19 +19,17 @@
package org.netbeans.modules.java.lsp.server;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
import java.io.IOException;
-import java.io.InputStream;
import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
+import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;
-import java.util.Properties;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
@@ -69,60 +67,10 @@ public final class URITranslator {
if (file == null) {
return uri;
}
- File cacheDir = getCacheDir();
- cacheDir.mkdirs();
- File segments = new File(cacheDir, "segments"); // NOI18N
- Properties props = new Properties();
-
- try (InputStream in = new FileInputStream(segments)) {
- props.load(in);
- } catch (IOException ex) {
- //OK, may not exist yet
- }
- FileObject archive = FileUtil.getArchiveFile(file);
- String archiveString = archive.toURL().toString();
- File foundSegment = null;
- for (String segment : props.stringPropertyNames()) {
- if (archiveString.equals(props.getProperty(segment))) {
- foundSegment = new File(cacheDir, segment);
- break;
- }
- }
- if (foundSegment == null) {
- int i = 0;
- while (props.getProperty("s" + i) != null) { // NOI18N
- i++;
- }
- foundSegment = new File(cacheDir, "s" + i); // NOI18N
- props.put("s" + i, archiveString); // NOI18N
- try (OutputStream in = new FileOutputStream(segments)) {
- props.store(in, "");
- } catch (IOException ex) {
- Exceptions.printStackTrace(ex);
- }
- }
- File cache = new File(foundSegment,
FileUtil.getRelativePath(FileUtil.getArchiveRoot(archive), file));
- cache.getParentFile().mkdirs();
- if (file.isFolder()) {
- if (cache.exists() && cache.isFile()) {
- if (!cache.delete()) {
- return uri;
- }
- }
- cache.mkdir();
- return cache.toURI().toString();
- } else if (file.isData()) {
- try {
- if (cache.exists() && cache.isDirectory()) {
- FileUtil.toFileObject(cache).delete();
- }
- try (OutputStream out = new FileOutputStream(cache)) {
- out.write(file.asBytes());
- return cache.toURI().toString();
- }
- } catch (IOException ex) {
- Exceptions.printStackTrace(ex);
- }
+ try {
+ return URLMapper.findURL(file,
URLMapper.EXTERNAL).toURI().toString();
+ } catch (URISyntaxException ex) {
+ Exceptions.printStackTrace(ex);
}
}
if (uriUri.getScheme().equals("nbfs")) { // NOI18N
@@ -150,30 +98,9 @@ public final class URITranslator {
public synchronized String uriFromLSP(String nbUri) {
return uriToCacheMap.computeIfAbsent(nbUri, uri -> {
- URI uriUri = URI.create(uri);
- File cacheDir = getCacheDir();
- URI relative = cacheDir.toURI().relativize(uriUri);
- if (relative != null && new File(cacheDir,
relative.toString()).canRead()) {
- String segmentAndPath = relative.toString();
- int slash = segmentAndPath.indexOf('/');
- String segment = segmentAndPath.substring(0, slash);
- String path = segmentAndPath.substring(slash + 1);
- File segments = new File(cacheDir, "segments"); // NOI18N
- Properties props = new Properties();
-
- try (InputStream in = new FileInputStream(segments)) {
- props.load(in);
- String archiveUri = props.getProperty(segment);
- FileObject archive =
URLMapper.findFileObject(URI.create(archiveUri).toURL());
- archive = archive != null ?
FileUtil.getArchiveRoot(archive) : null;
- FileObject file = archive != null ?
archive.getFileObject(path) : null;
- if (file != null) {
- return file.toURI().toString();
- }
- } catch (IOException ex) {
- Exceptions.printStackTrace(ex);
- }
- }
+ try {
+ return URLDecoder.decode(nbUri, StandardCharsets.UTF_8.name());
+ } catch (UnsupportedEncodingException ex) {}
return uri;
});
}
diff --git
a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/Server.java
b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/Server.java
index 9323fb43d3..5a74cde65f 100644
---
a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/Server.java
+++
b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/Server.java
@@ -792,6 +792,7 @@ public final class Server {
Set<String> commands = new
LinkedHashSet<>(Arrays.asList(GRAALVM_PAUSE_SCRIPT,
NBLS_BUILD_WORKSPACE,
NBLS_CLEAN_WORKSPACE,
+ NBLS_GET_ARCHIVE_FILE_CONTENT,
JAVA_RUN_PROJECT_ACTION,
JAVA_FIND_DEBUG_ATTACH_CONFIGURATIONS,
JAVA_FIND_DEBUG_PROCESS_TO_ATTACH,
@@ -989,6 +990,7 @@ public final class Server {
public static final String JAVA_SUPER_IMPLEMENTATION =
"java.super.implementation";
public static final String GRAALVM_PAUSE_SCRIPT = "graalvm.pause.script";
public static final String JAVA_RUN_PROJECT_ACTION =
"java.project.run.action";
+ public static final String NBLS_GET_ARCHIVE_FILE_CONTENT =
"nbls.get.archive.file.content";
/**
* Enumerates project configurations.
diff --git
a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/WorkspaceServiceImpl.java
b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/WorkspaceServiceImpl.java
index 628d3d86d2..5e119a9230 100644
---
a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/WorkspaceServiceImpl.java
+++
b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/WorkspaceServiceImpl.java
@@ -272,6 +272,20 @@ public final class WorkspaceServiceImpl implements
WorkspaceService, LanguageCli
progressOfCompilation.checkStatus();
return progressOfCompilation.getFinishFuture();
}
+ case Server.NBLS_GET_ARCHIVE_FILE_CONTENT: {
+ CompletableFuture<Object> future = new CompletableFuture<>();
+ try {
+ String uri = ((JsonPrimitive)
params.getArguments().get(0)).getAsString();
+ FileObject file = Utils.fromUri(uri);
+ if (file != null) {
+ future.complete(file.asText("UTF-8"));
+ }
+ future.complete(null);
+ } catch (IOException ioe) {
+ future.completeExceptionally(ioe);
+ }
+ return future;
+ }
case Server.JAVA_GET_PROJECT_SOURCE_ROOTS: {
String uri = ((JsonPrimitive)
params.getArguments().get(0)).getAsString();
String type = params.getArguments().size() > 1 ?
((JsonPrimitive) params.getArguments().get(1)).getAsString() :
JavaProjectConstants.SOURCES_TYPE_JAVA;
diff --git a/java/java.lsp.server/vscode/src/extension.ts
b/java/java.lsp.server/vscode/src/extension.ts
index 8ce5fe55b4..ab9e250ed2 100644
--- a/java/java.lsp.server/vscode/src/extension.ts
+++ b/java/java.lsp.server/vscode/src/extension.ts
@@ -652,6 +652,14 @@ export function activate(context: ExtensionContext):
VSNetBeansAPI {
context.subscriptions.push(commands.registerCommand('nbls.node.properties.edit',
async (node) => await PropertiesView.createOrShow(context, node,
(await client).findTreeViewService())));
+ const archiveFileProvider = <vscode.TextDocumentContentProvider> {
+ provideTextDocumentContent: async (uri: vscode.Uri, token:
vscode.CancellationToken): Promise<string> => {
+ return await
commands.executeCommand('nbls.get.archive.file.content', uri.toString());
+ }
+ };
+
context.subscriptions.push(workspace.registerTextDocumentContentProvider('jar',
archiveFileProvider));
+
context.subscriptions.push(workspace.registerTextDocumentContentProvider('nbjrt',
archiveFileProvider));
+
launchConfigurations.updateLaunchConfig();
// register completions:
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists