diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java index 93d22743..b806db19 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (c) 2019, 2023 Red Hat Inc. and others. +* Copyright (c) 2019, 2024 Red Hat Inc. and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at @@ -15,6 +15,7 @@ import java.util.List; import java.util.Map; +import java.util.logging.Logger; import java.util.stream.Collectors; import org.eclipse.lsp4j.CodeActionContext; @@ -24,7 +25,7 @@ import org.eclipse.lsp4j.TextDocumentIdentifier; import com.google.gson.Gson; -import com.google.gson.JsonObject; +import com.google.gson.JsonArray; /** * Arguments utilities. @@ -49,6 +50,8 @@ public class ArgumentUtils { private static final String LINE_PROPERTY = "line"; private static final String URI_PROPERTY = "uri"; + private static final Logger LOGGER = Logger.getLogger(ArgumentUtils.class.getName()); + public static Map getFirst(List arguments) { return arguments.isEmpty() ? null : (Map) arguments.get(0); } @@ -108,15 +111,20 @@ public static CodeActionContext getCodeActionContext(Map obj, St } List> diagnosticsObj = (List>) contextObj.get(DIAGNOSTICS_PROPERTY); List diagnostics = diagnosticsObj.stream().map(diagnosticObj -> { + LOGGER.info("Received diagnostic data" + diagnosticObj.toString()); Diagnostic diagnostic = new Diagnostic(); diagnostic.setRange(getRange(diagnosticObj, RANGE_PROPERTY)); diagnostic.setCode(getString(diagnosticObj, CODE_PROPERTY)); diagnostic.setMessage(getString(diagnosticObj, MESSAGE_PROPERTY)); diagnostic.setSource(getString(diagnosticObj, SOURCE_PROPERTY)); // In Eclipse IDE (LSP client), the data is JsonObject, and in JDT-LS (ex : - // vscode as LSP client) the data is a Map, we - // convert the Map to a JsonObject to be consistent with any LSP clients. - diagnostic.setData(getObjectAsJson(diagnosticObj, DATA_PROPERTY)); + // vscode as LSP client) the data is a Map. + + // In Vscode we are sending data in two different formats either as a string or + // as an array of strings. eg: data = “AssertTrue” or data =[“ApplicationScoped", "RequestScoped”] + // if it is a string -> set data as an Object. + // if ite is an array of strings - > set data as an JsonArray + diagnostic.setData(getValueFromDataParameter(diagnosticObj, DATA_PROPERTY)); return diagnostic; }).collect(Collectors.toList()); List only = null; @@ -139,20 +147,32 @@ public static Map getObject(Map obj, String key) } /** - * Returns the child as a JsonObject if it exists and is an object, and null - * otherwise + * Returns the child as a JSON array if the data parameter contains an array of strings; otherwise, + * returns it as an object if present, or null if not. * - * @param obj the object to get the child of + * + * @param data the object to get the child of * @param key the key of the child - * @return the child as a JsonObject if it exists and is an object, and null - * otherwise + * @return Returns the child as a JSON array if the data parameter contains an array of strings; otherwise, + * returns null. */ - public static JsonObject getObjectAsJson(Map obj, String key) { - Object child = obj.get(key); - if (child != null && child instanceof Map) { + public static Object getValueFromDataParameter(Map data, String key) { + + Object child = data.get(key); + if (child instanceof String) { + // if the value in the 'data' is a string, we string the object. + // eg: data = “AssertTrue” + return child; + } else if (child instanceof List) { + // if the value in the 'data' is an array, we will convert it to an JsonArray. + // eg: data =[“ApplicationScoped", "RequestScoped”] Gson gson = new Gson(); - return (JsonObject) gson.toJsonTree(child); + JsonArray jsonArray = gson.toJsonTree(child).getAsJsonArray(); + return jsonArray; + } else { + // Returns null if it is in any other format. + return null; } - return null; + } }