From 125a2f26804843ab01aba9498a5a4a34d0dc943e Mon Sep 17 00:00:00 2001 From: "Andrew J. Mauer" Date: Fri, 27 Oct 2023 21:33:49 +0100 Subject: [PATCH 1/2] fix missing package name in snippets on vscode --- .../lsp4e/JakartaLSConnection.java | 4 +- .../lsp4e/JakartaLanguageClient.java | 2 +- .../commons/utils/JSONUtility.java | 121 +++++++++--------- .../jdt/core/PropertiesManagerForJava.java | 2 +- .../JakartaDelegateCommandHandlerForJava.java | 48 +++++++ 5 files changed, 112 insertions(+), 65 deletions(-) diff --git a/jakarta.eclipse/org.eclipse.lsp4jakarta.lsp4e.core/src/main/java/org/eclipse/lsp4jakarta/lsp4e/JakartaLSConnection.java b/jakarta.eclipse/org.eclipse.lsp4jakarta.lsp4e.core/src/main/java/org/eclipse/lsp4jakarta/lsp4e/JakartaLSConnection.java index 27b7618c..6da5e288 100644 --- a/jakarta.eclipse/org.eclipse.lsp4jakarta.lsp4e.core/src/main/java/org/eclipse/lsp4jakarta/lsp4e/JakartaLSConnection.java +++ b/jakarta.eclipse/org.eclipse.lsp4jakarta.lsp4e.core/src/main/java/org/eclipse/lsp4jakarta/lsp4e/JakartaLSConnection.java @@ -49,8 +49,8 @@ public JakartaLSConnection() { String debugPortString = System.getProperty(getClass().getName() + ".debugPort"); if (debugPortString != null) { - commands.add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=" - + debugPortString); + //commands.add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=" + // + debugPortString); } commands.add("-classpath"); diff --git a/jakarta.eclipse/org.eclipse.lsp4jakarta.lsp4e.core/src/main/java/org/eclipse/lsp4jakarta/lsp4e/JakartaLanguageClient.java b/jakarta.eclipse/org.eclipse.lsp4jakarta.lsp4e.core/src/main/java/org/eclipse/lsp4jakarta/lsp4e/JakartaLanguageClient.java index da9bd5fd..5d65cfbe 100755 --- a/jakarta.eclipse/org.eclipse.lsp4jakarta.lsp4e.core/src/main/java/org/eclipse/lsp4jakarta/lsp4e/JakartaLanguageClient.java +++ b/jakarta.eclipse/org.eclipse.lsp4jakarta.lsp4e.core/src/main/java/org/eclipse/lsp4jakarta/lsp4e/JakartaLanguageClient.java @@ -95,7 +95,7 @@ public CompletableFuture getJavaProjectLabels(JakartaJava public CompletableFuture getJavaFileInfo(JakartaJavaFileInfoParams javaParams) { return CompletableFutures.computeAsync(cancelChecker -> { IProgressMonitor monitor = getProgressMonitor(cancelChecker); - return PropertiesManagerForJava.getInstance().fileInfo(javaParams, JDTUtilsLSImpl.getInstance(), monitor); + return PropertiesManagerForJava.getInstance().fileInfo(javaParams, JDTUtilsLSImpl.getInstance()); }); } diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/commons/utils/JSONUtility.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/commons/utils/JSONUtility.java index 8f1a5da0..7ac6b334 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/commons/utils/JSONUtility.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/commons/utils/JSONUtility.java @@ -27,71 +27,70 @@ */ public class JSONUtility { - private JSONUtility() { - } + private JSONUtility() {} - private static final Gson LSP4J_GSON = new MessageJsonHandler(new HashMap<>()).getGson(); + private static final Gson LSP4J_GSON = new MessageJsonHandler(new HashMap<>()).getGson(); - private static final Gson EITHER_GSON = new GsonBuilder() // - .registerTypeAdapterFactory(new EitherTypeAdapter.Factory()).create(); + private static final Gson EITHER_GSON = new GsonBuilder() // + .registerTypeAdapterFactory(new EitherTypeAdapter.Factory()).create(); - /** - * Converts the given Object to the given class using lsp4j's GSON logic. - * - * @param the class to convert the Object to - * @param object the object to convert - * @param clazz the class to convert the Object to - * @return the given Object converted to the given class using lsp4j's GSON - * logic - */ - public static T toModel(Object object, Class clazz) { - return toModel(getLsp4jGson(), object, clazz); - } + /** + * Converts the given Object to the given class using lsp4j's GSON logic. + * + * @param the class to convert the Object to + * @param object the object to convert + * @param clazz the class to convert the Object to + * @return the given Object converted to the given class using lsp4j's GSON + * logic + */ + public static T toModel(Object object, Class clazz) { + return toModel(getLsp4jGson(), object, clazz); + } - /** - * Converts the given Object to the given class using the given GSON instance. - * - * @param the class to convert the Object to - * @param gson the gson instance to use to perform the conversion - * @param object the object to convert - * @param clazz the class to convert the Object to - * @return the given Object converted to the given class using the given GSON - * instance - */ - public static T toModel(Gson gson, Object object, Class clazz) { - if (object == null) { - return null; - } - if (clazz == null) { - throw new IllegalArgumentException("Class can not be null"); - } - if (object instanceof JsonElement) { - return gson.fromJson((JsonElement) object, clazz); - } - if (clazz.isInstance(object)) { - return clazz.cast(object); - } - // if nothing else works, try serializing and deserializing again - return gson.fromJson(gson.toJson(object), clazz); - } + /** + * Converts the given Object to the given class using the given GSON instance. + * + * @param the class to convert the Object to + * @param gson the gson instance to use to perform the conversion + * @param object the object to convert + * @param clazz the class to convert the Object to + * @return the given Object converted to the given class using the given GSON + * instance + */ + public static T toModel(Gson gson, Object object, Class clazz) { + if (object == null) { + return null; + } + if (clazz == null) { + throw new IllegalArgumentException("Class can not be null"); + } + if (object instanceof JsonElement) { + return gson.fromJson((JsonElement) object, clazz); + } + if (clazz.isInstance(object)) { + return clazz.cast(object); + } + // if nothing else works, try serializing and deserializing again + return gson.fromJson(gson.toJson(object), clazz); + } - /** - * Returns a Gson instance configured similarly to the instance lsp4j uses. - * - * @return a Gson instance configured similarly to the instance lsp4j uses - */ - public static Gson getLsp4jGson() { - return LSP4J_GSON; - } + /** + * Returns a Gson instance configured similarly to the instance lsp4j uses. + * + * @return a Gson instance configured similarly to the instance lsp4j uses + */ + public static Gson getLsp4jGson() { + return LSP4J_GSON; + } - /** - * Returns a Gson instance with most of the default options, but with the - * ability to parse {@code org.eclipse.lsp4j.Either}. - * - * @return a Gson instance with most of the default options, but with the - * ability to parse {@code org.eclipse.lsp4j.Either} - */ - public static Gson getEitherGson() { - return EITHER_GSON; - } + /** + * Returns a Gson instance with most of the default options, but with the + * ability to parse {@code org.eclipse.lsp4j.Either}. + * + * @return a Gson instance with most of the default options, but with the + * ability to parse {@code org.eclipse.lsp4j.Either} + */ + public static Gson getEitherGson() { + return EITHER_GSON; + } } \ No newline at end of file diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/PropertiesManagerForJava.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/PropertiesManagerForJava.java index fd925245..d04fa8ff 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/PropertiesManagerForJava.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/PropertiesManagerForJava.java @@ -538,7 +538,7 @@ private static boolean isGenerated(ASTNode node) { * @return the Java file information (ex : package name) from the given file URI * and null otherwise. */ - public JakartaJavaFileInfo fileInfo(JakartaJavaFileInfoParams params, IJDTUtils utils, IProgressMonitor monitor) { + public JakartaJavaFileInfo fileInfo(JakartaJavaFileInfoParams params, IJDTUtils utils) { String uri = params.getUri(); final ICompilationUnit unit = utils.resolveCompilationUnit(uri); if (unit != null && unit.exists()) { diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/JakartaDelegateCommandHandlerForJava.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/JakartaDelegateCommandHandlerForJava.java index f66850c4..f458af76 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/JakartaDelegateCommandHandlerForJava.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/JakartaDelegateCommandHandlerForJava.java @@ -44,6 +44,8 @@ import org.eclipse.lsp4jakarta.commons.JakartaJavaCompletionResult; import org.eclipse.lsp4jakarta.commons.JakartaJavaDiagnosticsParams; import org.eclipse.lsp4jakarta.commons.JakartaJavaDiagnosticsSettings; +import org.eclipse.lsp4jakarta.commons.JakartaJavaFileInfo; +import org.eclipse.lsp4jakarta.commons.JakartaJavaFileInfoParams; import org.eclipse.lsp4jakarta.commons.JavaCursorContextResult; import org.eclipse.lsp4jakarta.commons.codeaction.CodeActionResolveData; import org.eclipse.lsp4jakarta.commons.utils.JSONUtility; @@ -54,6 +56,7 @@ */ public class JakartaDelegateCommandHandlerForJava extends AbstractJakartaDelegateCommandHandler { + private static final String FILE_INFO_COMMAND_ID = "jakarta/java/fileInfo"; private static final String JAVA_CODEACTION_COMMAND_ID = "jakarta/java/codeAction"; private static final String JAVA_CODEACTION_RESOLVE_COMMAND_ID = "jakarta/java/codeActionResolve"; private static final String JAVA_COMPLETION_COMMAND_ID = "jakarta/java/completion"; @@ -74,6 +77,8 @@ public JakartaDelegateCommandHandlerForJava() {} public Object executeCommand(String commandId, List arguments, IProgressMonitor monitor) throws Exception { JavaLanguageServerPlugin.logInfo(String.format("Executing command '%s' in LSP4Jakarta JDT LS extension", commandId)); switch (commandId) { + case FILE_INFO_COMMAND_ID: + return getFileInfo(arguments, commandId); case JAVA_CODEACTION_COMMAND_ID: return getCodeActionForJava(arguments, commandId, monitor); case JAVA_CODEACTION_RESOLVE_COMMAND_ID: @@ -87,6 +92,49 @@ public Object executeCommand(String commandId, List arguments, IProgress } } + /** + * Returns the file information (package name, etc) for the given Java file. + * + * @param arguments + * @param commandId + * @param monitor + * @return the file information (package name, etc) for the given Java file. + * @throws CoreException + * @throws JavaModelException + */ + private static JakartaJavaFileInfo getFileInfo(List arguments, String commandId) throws JavaModelException, CoreException { + // Create java file information parameter + JakartaJavaFileInfoParams params = createJavaFileInfoParams(arguments, commandId); + // Return file information from the parameter + return PropertiesManagerForJava.getInstance().fileInfo(params, JDTUtilsLSImpl.getInstance()); + } + + /** + * Create the Java file information parameter from the given arguments map. + * + * @param arguments + * @param commandId + * + * @return the Java file information parameter. + */ + private static JakartaJavaFileInfoParams createJavaFileInfoParams(List arguments, String commandId) { + Map obj = getFirst(arguments); + if (obj == null) { + throw new UnsupportedOperationException(String.format( + "Command '%s' must be called with one MicroProfileJavaFileInfoParams argument!", commandId)); + } + // Get project name from the java file URI + String javaFileUri = getString(obj, "uri"); + if (javaFileUri == null) { + throw new UnsupportedOperationException(String.format( + "Command '%s' must be called with required MicroProfileJavaFileInfoParams.uri (java file URI)!", + commandId)); + } + JakartaJavaFileInfoParams params = new JakartaJavaFileInfoParams(); + params.setUri(javaFileUri); + return params; + } + /** * Return the completion result for the given arguments * From 80a87a0f5abf1a58bbf7f510d3c640bf58dcb3fe Mon Sep 17 00:00:00 2001 From: "Andrew J. Mauer" Date: Fri, 27 Oct 2023 22:43:41 +0100 Subject: [PATCH 2/2] add fileInfo msg to plugin.xml --- .../org/eclipse/lsp4jakarta/lsp4e/JakartaLSConnection.java | 4 ++-- jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/plugin.xml | 1 + .../core/ls/JakartaDelegateCommandHandlerForJava.java | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/jakarta.eclipse/org.eclipse.lsp4jakarta.lsp4e.core/src/main/java/org/eclipse/lsp4jakarta/lsp4e/JakartaLSConnection.java b/jakarta.eclipse/org.eclipse.lsp4jakarta.lsp4e.core/src/main/java/org/eclipse/lsp4jakarta/lsp4e/JakartaLSConnection.java index 6da5e288..27b7618c 100644 --- a/jakarta.eclipse/org.eclipse.lsp4jakarta.lsp4e.core/src/main/java/org/eclipse/lsp4jakarta/lsp4e/JakartaLSConnection.java +++ b/jakarta.eclipse/org.eclipse.lsp4jakarta.lsp4e.core/src/main/java/org/eclipse/lsp4jakarta/lsp4e/JakartaLSConnection.java @@ -49,8 +49,8 @@ public JakartaLSConnection() { String debugPortString = System.getProperty(getClass().getName() + ".debugPort"); if (debugPortString != null) { - //commands.add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=" - // + debugPortString); + commands.add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=" + + debugPortString); } commands.add("-classpath"); diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/plugin.xml b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/plugin.xml index 26bbde03..e4b65a76 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/plugin.xml +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/plugin.xml @@ -16,6 +16,7 @@ + diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/JakartaDelegateCommandHandlerForJava.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/JakartaDelegateCommandHandlerForJava.java index f458af76..90b32842 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/JakartaDelegateCommandHandlerForJava.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/JakartaDelegateCommandHandlerForJava.java @@ -121,13 +121,13 @@ private static JakartaJavaFileInfoParams createJavaFileInfoParams(List a Map obj = getFirst(arguments); if (obj == null) { throw new UnsupportedOperationException(String.format( - "Command '%s' must be called with one MicroProfileJavaFileInfoParams argument!", commandId)); + "Command '%s' must be called with one JakartaJavaFileInfoParams argument!", commandId)); } // Get project name from the java file URI String javaFileUri = getString(obj, "uri"); if (javaFileUri == null) { throw new UnsupportedOperationException(String.format( - "Command '%s' must be called with required MicroProfileJavaFileInfoParams.uri (java file URI)!", + "Command '%s' must be called with required JakartaJavaFileInfoParams.uri (java file URI)!", commandId)); } JakartaJavaFileInfoParams params = new JakartaJavaFileInfoParams();