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/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/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..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 @@ -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 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 JakartaJavaFileInfoParams.uri (java file URI)!", + commandId)); + } + JakartaJavaFileInfoParams params = new JakartaJavaFileInfoParams(); + params.setUri(javaFileUri); + return params; + } + /** * Return the completion result for the given arguments *