Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: polish SettingBinder #956

Merged
merged 1 commit into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import com.itangcent.intellij.file.BeanBinder
import com.itangcent.intellij.file.CachedBeanBinder

@ImplementedBy(DefaultSettingBinder::class)
interface SettingBinder : BeanBinder<Settings> {
}
interface SettingBinder : BeanBinder<Settings>

fun SettingBinder.update(updater: (Settings) -> Unit) {
fun SettingBinder.update(updater: Settings.() -> Unit) {
this.read().also(updater).let { this.save(it) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ open class DefaultPostmanSettingsHelper : PostmanSettingsHelper {
)
if (postmanPrivateToken.isNullOrBlank()) return null
settingBinder.update {
it.postmanToken = postmanPrivateToken
postmanToken = postmanPrivateToken
}
return postmanPrivateToken
}
Expand All @@ -59,7 +59,7 @@ open class DefaultPostmanSettingsHelper : PostmanSettingsHelper {
postmanWorkspace = selectWorkspace()
if (postmanWorkspace.notNullOrBlank()) {
settingBinder.update {
it.postmanWorkspace = postmanWorkspace
this.postmanWorkspace = postmanWorkspace
}
}
}
Expand Down Expand Up @@ -128,7 +128,7 @@ open class DefaultPostmanSettingsHelper : PostmanSettingsHelper {
}

override fun addCollectionId(module: String, collectionId: String) {
settingBinder.update { it.addPostmanCollections(module, collectionId) }
settingBinder.update { addPostmanCollections(module, collectionId) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ class HttpSettingsHelper {
}

fun addTrustHost(host: String) {
settingBinder.update { settings ->
if (!settings.trustHosts.contains(host)) {
settings.trustHosts = settings.trustHosts + host
settingBinder.update {
if (!trustHosts.contains(host)) {
trustHosts += host
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ class RecommendConfigSettingsHelper {

fun addConfig(code: String) {
settingBinder.update {
it.recommendConfigs = RecommendConfigLoader.addSelectedConfig(it.recommendConfigs, code)
recommendConfigs = RecommendConfigLoader.addSelectedConfig(recommendConfigs, code)
}
}

fun removeConfig(vararg code: String) {
settingBinder.update {
it.recommendConfigs = RecommendConfigLoader.removeSelectedConfig(it.recommendConfigs, *code)
recommendConfigs = RecommendConfigLoader.removeSelectedConfig(recommendConfigs, *code)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class YapiSettingsHelper {
if (yapiServer.isNullOrBlank()) return null
server = yapiServer
settingBinder.update {
it.yapiServer = yapiServer
this.yapiServer = yapiServer
}
return yapiServer
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlin.reflect.full.functions
*/
class MarkdownHtmlGenerator {

private val commonMarkFlavourDescriptor = commonMarkFlavourDescriptorBuilder()
private val commonMarkFlavourDescriptor by lazy { commonMarkFlavourDescriptorBuilder() }

fun render(markdown: String): String? {
if (markdown.isBlank()) {
Expand All @@ -26,7 +26,7 @@ class MarkdownHtmlGenerator {
}

companion object {
val generateHtmlCall: (HtmlGenerator) -> String = findAdaptedGenerateHtmlCall()
val generateHtmlCall: (HtmlGenerator) -> String by lazy { findAdaptedGenerateHtmlCall() }

private fun findAdaptedGenerateHtmlCall(): (HtmlGenerator) -> String {
val functions = HtmlGenerator::class.functions
Expand All @@ -38,8 +38,9 @@ class MarkdownHtmlGenerator {
return { it.generateHtml() }
}

val commonMarkFlavourDescriptorBuilder: () -> CommonMarkFlavourDescriptor =
val commonMarkFlavourDescriptorBuilder: () -> CommonMarkFlavourDescriptor by lazy {
findAdaptedCommonMarkFlavourDescriptor()
}

private fun findAdaptedCommonMarkFlavourDescriptor(): () -> CommonMarkFlavourDescriptor {
val constructors = CommonMarkFlavourDescriptor::class.constructors
Expand Down