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

support setting charset for export markdown #211

Merged
merged 1 commit into from
Dec 17, 2019
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 @@ -87,7 +87,7 @@ object EntityUtils {
@JvmOverloads
fun toString(entity: HttpEntity, defaultCharset: Charset? = null): String? {
Args.notNull(entity, "Entity")
return entity.content?.use { instream ->
return entity.content?.use { inStream ->
Args.check(entity.contentLength <= 2147483647L, "HTTP entity too large to be buffered in memory")
var i = entity.contentLength.toInt()
if (i < 0) {
Expand Down Expand Up @@ -115,7 +115,7 @@ object EntityUtils {
charset = HTTP.DEF_CONTENT_CHARSET
}

val reader = InputStreamReader(instream, charset!!)
val reader = InputStreamReader(inStream, charset!!)
val buffer = CharArrayBuffer(i)
val tmp = CharArray(1024)

Expand Down
4 changes: 2 additions & 2 deletions idea-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ dependencies {
// compile fileTree(dir: 'libs', include: ['*.jar'])


compile('com.itangcent:intellij-idea:0.1.8-SNAPSHOT') {
compile('com.itangcent:intellij-idea:0.1.9-SNAPSHOT') {
exclude group: 'com.google.inject'
exclude group: 'com.google.code.gson'
}

compile('com.itangcent:intellij-kotlin-support:0.1.8-SNAPSHOT') {
compile('com.itangcent:intellij-kotlin-support:0.1.9-SNAPSHOT') {
exclude group: 'com.google.inject'
exclude group: 'com.google.code.gson'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import com.intellij.openapi.ui.Messages
import com.itangcent.common.model.Doc
import com.itangcent.idea.plugin.Worker
import com.itangcent.idea.plugin.api.export.ClassExporter
import com.itangcent.idea.plugin.api.export.RequestHelper
import com.itangcent.idea.plugin.settings.SettingBinder
import com.itangcent.idea.utils.Charsets
import com.itangcent.idea.utils.FileSaveHelper
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.logger.Logger
import com.itangcent.intellij.logger.traceError
import com.itangcent.intellij.psi.SelectedHelper
import com.itangcent.intellij.util.ActionUtils
import com.itangcent.intellij.logger.traceError
import java.util.*

@Singleton
Expand All @@ -34,6 +35,9 @@ class MarkdownApiExporter {
@Inject
private val markdownFormatter: MarkdownFormatter? = null

@Inject
private val settingBinder: SettingBinder? = null

fun export() {

logger!!.info("Start find apis...")
Expand Down Expand Up @@ -78,20 +82,21 @@ class MarkdownApiExporter {
docs.clear()
actionContext!!.runAsync {
try {
fileSaveHelper!!.saveOrCopy(apiInfo, {
fileSaveHelper!!.saveOrCopy(apiInfo, Charsets.forName(settingBinder!!.read().outputCharset)?.charset()
?: kotlin.text.Charsets.UTF_8, {
logger.info("Exported data are copied to clipboard,you can paste to a md file now")
}, {
logger.info("Apis save success")
}) {
logger.info("Apis save failed")
}
} catch (e: Exception) {
logger.traceError("Apis save failed",e)
logger.traceError("Apis save failed", e)

}
}
} catch (e: Exception) {
logger.traceError("Apis save failed",e)
logger.traceError("Apis save failed", e)

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.itangcent.idea.plugin.script.GroovyActionExtLoader
import com.itangcent.idea.plugin.script.LoggerBuffer
import com.itangcent.idea.plugin.settings.SettingBinder
import com.itangcent.idea.psi.PsiResource
import com.itangcent.idea.utils.Charsets
import com.itangcent.idea.utils.CustomizedPsiClassHelper
import com.itangcent.idea.utils.FileSaveHelper
import com.itangcent.intellij.config.ConfigReader
Expand Down Expand Up @@ -63,7 +64,6 @@ class SuvApiExporter {
@Inject
private val classExporter: ClassExporter? = null


@Suppress("UNCHECKED_CAST")
fun showExportWindow() {

Expand Down Expand Up @@ -447,6 +447,9 @@ class SuvApiExporter {
@Inject
private val markdownFormatter: MarkdownFormatter? = null

@Inject
private val settingBinder: SettingBinder? = null

override fun actionName(): String {
return "MarkdownExportAction"
}
Expand Down Expand Up @@ -481,7 +484,8 @@ class SuvApiExporter {
docs.clear()
actionContext!!.runAsync {
try {
fileSaveHelper!!.saveOrCopy(apiInfo, {
fileSaveHelper!!.saveOrCopy(apiInfo, Charsets.forName(settingBinder!!.read().outputCharset)?.charset()
?: kotlin.text.Charsets.UTF_8, {
logger!!.info("Exported data are copied to clipboard,you can paste to a md file now")
}, {
logger!!.info("Apis save success")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class RecommendConfigReader : ConfigReader {

private const val config_name = ".recommend.easy.api.config"
// private const val config_version = ".recommend.easy.api.config.version"
private const val curr_version = "0.0.7"
private const val curr_version = "0.0.8"
//$version$content

private fun loadRecommendConfig(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,52 @@
<properties/>
<border type="none" title="Markdown"/>
<children>
<component id="f9301" class="javax.swing.JCheckBox" binding="outputDemoCheckBox">
<grid id="b034b" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="outputDemo"/>
</properties>
</component>
<hspacer id="cf0ae">
<properties/>
<border type="none"/>
<children>
<component id="f9301" class="javax.swing.JCheckBox" binding="outputDemoCheckBox">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="outputDemo"/>
</properties>
</component>
</children>
</grid>
<grid id="2d3b9" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<properties/>
<border type="none"/>
<children>
<component id="331b3" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="charset:"/>
</properties>
</component>
<component id="f9571" class="javax.swing.JComboBox" binding="outputCharsetComboBox">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="80" height="-1"/>
<preferred-size width="130" height="-1"/>
<maximum-size width="200" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
</children>
</grid>
</children>
</grid>
<grid id="6d629" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.itangcent.idea.icons.EasyIcons
import com.itangcent.idea.icons.iconOnly
import com.itangcent.idea.plugin.config.RecommendConfigReader
import com.itangcent.idea.plugin.settings.Settings
import com.itangcent.idea.utils.Charsets
import com.itangcent.idea.utils.ConfigurableLogger
import com.itangcent.idea.utils.SwingUtils
import com.itangcent.intellij.extend.rx.AutoComputer
Expand All @@ -29,7 +30,6 @@ import com.itangcent.intellij.logger.Logger
import com.itangcent.suv.http.ConfigurableHttpClientProvider
import org.apache.commons.io.FileUtils
import java.io.File
import java.nio.charset.Charset
import java.util.*
import javax.swing.*

Expand Down Expand Up @@ -73,6 +73,8 @@ class EasyApiSettingGUI {

private var outputDemoCheckBox: JCheckBox? = null

private var outputCharsetComboBox: JComboBox<Charsets>? = null

private var inferEnableCheckBox: JCheckBox? = null

private var maxDeepTextField: JTextField? = null
Expand Down Expand Up @@ -186,6 +188,13 @@ class EasyApiSettingGUI {
.filter { throttleHelper.acquire("settings.logLevel", 300) }
.eval { (it ?: ConfigurableLogger.CoarseLogLevel.LOW).getLevel() }

outputCharsetComboBox!!.model = DefaultComboBoxModel(Charsets.SUPPORTED_CHARSETS)

autoComputer.bind<String?>(this, "settings.outputCharset")
.with(this.outputCharsetComboBox!!)
.filter { throttleHelper.acquire("settings.outputCharset", 300) }
.eval { (it ?: Charsets.UTF_8).displayName() }

autoComputer.bind(this.previewTextArea!!)
.with<String>(this, "settings.recommendConfigs")
.eval { configs -> RecommendConfigReader.buildRecommendConfig(configs.split(",")) }
Expand Down Expand Up @@ -226,6 +235,7 @@ class EasyApiSettingGUI {
autoComputer.value(this::settings, settings.copy())

this.logLevelComboBox!!.selectedItem = ConfigurableLogger.CoarseLogLevel.toLevel(settings.logLevel)
this.outputCharsetComboBox!!.selectedItem = Charsets.forName(settings.outputCharset)

val configs = settings.recommendConfigs.split(",")
RecommendConfigReader.RECOMMEND_CONFIG_CODES.forEach {
Expand Down Expand Up @@ -378,7 +388,7 @@ class EasyApiSettingGUI {
}
val fileWrapper = chooser.save(toSelect, "setting.json")
if (fileWrapper != null) {
com.itangcent.intellij.util.FileUtils.forceSave(fileWrapper.file.path, GsonUtils.toJson(settings).toByteArray(Charset.defaultCharset()))
com.itangcent.intellij.util.FileUtils.forceSave(fileWrapper.file.path, GsonUtils.toJson(settings).toByteArray(kotlin.text.Charsets.UTF_8))
}
}

Expand All @@ -397,8 +407,10 @@ class EasyApiSettingGUI {
val files = chooser.choose(null, toSelect)
if (!files.isNullOrEmpty()) {
val virtualFile = files[0]
val read = com.itangcent.common.utils.FileUtils.read(File(virtualFile.path))
setSettings(GsonUtils.fromJson(read, Settings::class))
val read = com.itangcent.common.utils.FileUtils.read(File(virtualFile.path), kotlin.text.Charsets.UTF_8)
if (!read.isNullOrEmpty()) {
setSettings(GsonUtils.fromJson(read, Settings::class))
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.itangcent.idea.plugin.settings

import com.itangcent.idea.plugin.config.RecommendConfigReader
import com.itangcent.idea.utils.Charsets
import com.itangcent.idea.utils.ConfigurableLogger

class Settings {
Expand Down Expand Up @@ -29,6 +30,8 @@ class Settings {

var outputDemo: Boolean = true

var outputCharset: String = Charsets.UTF_8.displayName()

fun copy(): Settings {
val newSetting = Settings()
newSetting.postmanToken = this.postmanToken
Expand All @@ -42,6 +45,7 @@ class Settings {
newSetting.recommendConfigs = this.recommendConfigs
newSetting.logLevel = this.logLevel
newSetting.outputDemo = this.outputDemo
newSetting.outputCharset = this.outputCharset
return newSetting
}

Expand All @@ -62,6 +66,7 @@ class Settings {
if (recommendConfigs != other.recommendConfigs) return false
if (logLevel != other.logLevel) return false
if (outputDemo != other.outputDemo) return false
if (outputCharset != other.outputCharset) return false

return true
}
Expand All @@ -78,10 +83,10 @@ class Settings {
result = 31 * result + recommendConfigs.hashCode()
result = 31 * result + logLevel
result = 31 * result + outputDemo.hashCode()
result = 31 * result + outputCharset.hashCode()
return result
}


companion object {
const val DEFAULT_INFER_MAX_DEEP = 4
}
Expand Down
87 changes: 87 additions & 0 deletions idea-plugin/src/main/kotlin/com/itangcent/idea/utils/Charsets.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.itangcent.idea.utils

import java.nio.charset.Charset

abstract class Charsets {

companion object {

val UTF_8 = ImmutableCharset(kotlin.text.Charsets.UTF_8)
val US_ASCII = ImmutableCharset(kotlin.text.Charsets.US_ASCII)
val UTF_16 = ImmutableCharset(kotlin.text.Charsets.UTF_16)
val ISO_8859_1 = ImmutableCharset(kotlin.text.Charsets.ISO_8859_1)
val UTF_32 = ImmutableCharset(kotlin.text.Charsets.UTF_32)
val GBK = tryLoadCharset("GBK")
val FOLLOW_SYSTEM = object : MutableCharset("follow system") {
override fun charset(): Charset {
return Charset.defaultCharset()
}
}

fun forName(charset: String): Charsets? {
return SUPPORTED_CHARSETS.firstOrNull { it.displayName() == charset }
?: tryLoadCharset(charset)
}

private fun tryLoadCharset(charset: String): Charsets? {
return try {
ImmutableCharset(Charset.forName(charset))
} catch (e: Exception) {
null
}
}

val SUPPORTED_CHARSETS: Array<Charsets>

init {
val charsets = ArrayList<Charsets>()
charsets.add(FOLLOW_SYSTEM)
charsets.add(UTF_8)
GBK?.let { charsets.add(it) }
charsets.add(US_ASCII)
charsets.add(UTF_16)
charsets.add(ISO_8859_1)
charsets.add(UTF_32)
SUPPORTED_CHARSETS = charsets.toTypedArray()
}
}

abstract fun charset(): Charset

abstract fun displayName(): String

override fun toString(): String {
return displayName()
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

return charset() == (other as Charsets).charset()
}

override fun hashCode(): Int {
return charset().hashCode()
}


class ImmutableCharset(private val charset: Charset) : Charsets() {

override fun charset(): Charset {
return this.charset
}

override fun displayName(): String {
return charset().displayName()
}

}

abstract class MutableCharset(private val displayName: String?) : Charsets() {

override fun displayName(): String {
return displayName ?: charset().displayName()
}
}
}
Loading