generated from redhat-developer/new-project-template
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Allow requesting scalar values in workspace/configuration (#649)
fix: Allow requesting scalar values for workspace/configuration
- Loading branch information
1 parent
fa59307
commit cb09c16
Showing
3 changed files
with
137 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/com/redhat/devtools/lsp4ij/client/SettingsHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
src/test/java/com/redhat/devtools/lsp4ij/client/SettingsHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |