Skip to content

Commit

Permalink
fix: Allow requesting scalar values in workspace/configuration (#649)
Browse files Browse the repository at this point in the history
fix: Allow requesting scalar values for workspace/configuration
  • Loading branch information
JanThomas118 authored Dec 5, 2024
1 parent fa59307 commit cb09c16
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
******************************************************************************/
package com.redhat.devtools.lsp4ij.client;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
import com.intellij.openapi.Disposable;
Expand Down Expand Up @@ -250,15 +251,7 @@ protected Object findSettings(@Nullable String section) {
}

protected static Object findSettings(String[] sections, JsonObject jsonObject) {
JsonObject current = jsonObject;
for (String section : sections) {
Object result = current.get(section);
if (!(result instanceof JsonObject json)) {
return null;
}
current = json;
}
return current;
return SettingsHelper.findSettings(sections, jsonObject);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*******************************************************************************
* Copyright (c) 2023 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
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package com.redhat.devtools.lsp4ij.client;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Helpers for extracting nested settings from json
*/
public class SettingsHelper {

private SettingsHelper() {
}

/**
* Extract nested settings from json.
* @param sections path to the json element to retrieve
* @param parent Json to look for settings in
* @return the settings retrieved in the specified section and null if the section was not found.
*/
public static @Nullable JsonElement findSettings(@NotNull String[] sections, @Nullable JsonObject parent) {
JsonElement current = parent;
for (String section : sections) {
if (current instanceof JsonObject currentObject) {
current = currentObject.get(section);
if (current == null) {
return null;
}
}
}
return current;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*******************************************************************************
* Copyright (c) 2023 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
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package com.redhat.devtools.lsp4ij.client;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.intellij.testFramework.fixtures.BasePlatformTestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.StringReader;

/**
* Tests for LSP {@link SettingsHelper}.
*/
public class SettingsHelperTest extends BasePlatformTestCase {
// language=json
private final String testJson = """
{
"mylsp": {
"myscalarsetting": "value",
"myobjectsettings": {
"subsettingA": 1,
"subsettingB": 2
}
}
}
""";

private static void assertFindSettings(@NotNull String json, @NotNull String[] sections, @Nullable String expectedJsonText) {
JsonObject jsonObject = JsonParser.parseReader(new StringReader(json)).getAsJsonObject();
JsonElement expectedJson = expectedJsonText == null ? null : JsonParser.parseReader(new StringReader(expectedJsonText));

JsonElement result = SettingsHelper.findSettings(sections, jsonObject);

assertEquals(result, expectedJson);
}

public void testGetSettingsIdentityOnEmptySections() {
String[] requestedPath = new String[0];
assertFindSettings(testJson, requestedPath, testJson);
}

public void testGetSettingsObjectValue() {
String[] requestedPath = new String[]{"mylsp"};
assertFindSettings(testJson, requestedPath, """
{
"myscalarsetting": "value",
"myobjectsettings": {
"subsettingA": 1,
"subsettingB": 2
}
}
""");
}

public void testGetSettingsPrimitiveValue() {
String[] requestedPath = new String[]{"mylsp", "myscalarsetting"};
assertFindSettings(testJson, requestedPath, "\"value\"");
}

public void testGetSettingsDeepPrimitiveValue() {
String[] requestedPath = new String[]{"mylsp", "myobjectsettings", "subsettingA"};
assertFindSettings(testJson, requestedPath, "1");
}

public void testGetSettingsNonExistingValue() {
String[] requestedPath = new String[]{"mylsp", "nonexistant"};
assertFindSettings(testJson, requestedPath, null);
}

public void testGetSettingsEmptyJson() {
String[] requestedPath = new String[]{"mylsp", "myobjectsettings", "subsettingA"};
assertFindSettings("{}", requestedPath, null);
}
}

0 comments on commit cb09c16

Please sign in to comment.