From d21db31b747f5f8d8cc4d5f9a88c97702e322b87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kukie=C5=82ka?= Date: Fri, 1 Nov 2024 20:18:27 +0100 Subject: [PATCH] Send cody custom config as json, add support for dotted names (#2552) ## Changes: See: https://github.com/sourcegraph/cody/pull/6027 ## Test plan See: https://github.com/sourcegraph/cody/pull/6027 --- build.gradle.kts | 1 + gradle.properties | 2 +- .../com/sourcegraph/config/ConfigUtil.kt | 29 ++++++++++--------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index f85c1fb88..df20e94aa 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -133,6 +133,7 @@ dependencies { } } + implementation("com.typesafe:config:1.4.3") implementation("org.commonmark:commonmark:0.22.0") implementation("org.commonmark:commonmark-ext-gfm-tables:0.22.0") implementation("org.eclipse.lsp4j:org.eclipse.lsp4j.jsonrpc:0.23.1") diff --git a/gradle.properties b/gradle.properties index 190fc2e08..eaebd2ffc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -25,4 +25,4 @@ org.gradle.jvmargs=-Xmx4g -Xms500m nodeBinaries.commit=8755ae4c05fd476cd23f2972049111ba436c86d4 nodeBinaries.version=v20.12.2 cody.autocomplete.enableFormatting=true -cody.commit=23af99f690659084d820461a73a6fb094e52e998 +cody.commit=5dccf8dc97191083cc3fe2209b11edb0ebea0017 diff --git a/src/main/kotlin/com/sourcegraph/config/ConfigUtil.kt b/src/main/kotlin/com/sourcegraph/config/ConfigUtil.kt index 5005a34c1..7660d2d9b 100644 --- a/src/main/kotlin/com/sourcegraph/config/ConfigUtil.kt +++ b/src/main/kotlin/com/sourcegraph/config/ConfigUtil.kt @@ -1,7 +1,6 @@ package com.sourcegraph.config import com.google.gson.JsonObject -import com.google.gson.JsonParser import com.intellij.ide.plugins.PluginManagerCore import com.intellij.openapi.diagnostic.Logger import com.intellij.openapi.editor.Editor @@ -17,6 +16,9 @@ import com.sourcegraph.cody.config.CodyAuthenticationManager import com.sourcegraph.cody.config.ServerAuthLoader import com.sourcegraph.cody.config.SourcegraphServerPath import com.sourcegraph.cody.config.SourcegraphServerPath.Companion.from +import com.typesafe.config.ConfigFactory +import com.typesafe.config.ConfigRenderOptions +import com.typesafe.config.ConfigValueFactory import java.nio.file.Path import java.nio.file.Paths import kotlin.io.path.readText @@ -80,7 +82,7 @@ object ConfigUtil { UserLevelConfig.getAutocompleteProviderType()?.vscodeSettingString(), debug = isCodyDebugEnabled(), verboseDebug = isCodyVerboseDebugEnabled(), - customConfiguration = getCustomConfiguration(project, customConfigContent), + customConfigurationJson = getCustomConfiguration(project, customConfigContent), ) } @@ -131,22 +133,23 @@ object ConfigUtil { } @JvmStatic - fun getCustomConfiguration(project: Project, customConfigContent: String?): Map { - val settingsProperties = - try { - val text = customConfigContent ?: getSettingsFile(project).readText() - val jsonObj = JsonParser.parseString(text).asJsonObject - jsonObj.keySet().associateWith { jsonObj[it] } - } catch (e: Exception) { - logger.info("No user defined settings file found. Proceeding with empty custom config") - emptyMap() - } + fun getCustomConfiguration(project: Project, customConfigContent: String?): String { // Needed by Edit commands to trigger smart-selection; without it things break. // So it isn't optional in JetBrains clients, which do not offer language-neutral solutions // to this problem; instead we hardwire it to use the indentation-based provider. val additionalProperties = mapOf("cody.experimental.foldingRanges" to "indentation-based") - return settingsProperties + additionalProperties + return try { + val text = customConfigContent ?: getSettingsFile(project).readText() + val config = ConfigFactory.parseString(text).resolve() + additionalProperties.forEach { (key, value) -> + config.withValue(key, ConfigValueFactory.fromAnyRef(value)) + } + config.root().render(ConfigRenderOptions.defaults().setOriginComments(false)) + } catch (e: Exception) { + logger.info("No user defined settings file found. Proceeding with empty custom config") + "" + } } @JvmStatic