Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
YiiGuxing committed Aug 18, 2018
2 parents 834f5d9 + d5d503c commit a2a2d00
Show file tree
Hide file tree
Showing 29 changed files with 214 additions and 117 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.idea/workspace.xml
.gradle/
build/
sandbox/
out/
.DS_Store
*.iml
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## [v2.1.1](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.1.1) (2018-08-18)

- 语言列表排序
- 保留文本格式选项
- BUG修复

## [v2.1.0](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.1.0) (2018-07-30)

- 实时翻译
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ FAQ

更新日志
--------
## [v2.1.0](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.1.0) (2018-07-30)
## [v2.1.1](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.1.1) (2018-08-18)

- 实时翻译
- 忽略内容配置
- 语言列表排序
- 保留文本格式选项
- BUG修复

[完整的更新历史记录](./CHANGELOG.md)
Expand Down
7 changes: 3 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import org.apache.tools.ant.filters.EscapeUnicode

buildscript {
ext.kotlinVersion = kotlinVersion
repositories {
maven { url = 'http://maven.aliyun.com/nexus/content/groups/public/' }
jcenter()
mavenCentral()
}
Expand All @@ -13,11 +11,11 @@ buildscript {
}

plugins {
id 'org.jetbrains.intellij' version '0.3.4'
id 'org.jetbrains.intellij' version '0.3.5'
}

repositories {
maven { url = 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url = 'https://maven.aliyun.com/repository/public/' }
jcenter()
mavenCentral()
}
Expand All @@ -32,6 +30,7 @@ intellij {
version = ideaVersion
pluginName = 'TranslationPlugin'
downloadSources = Boolean.valueOf(sources)
sandboxDirectory 'sandbox'
}

patchPluginXml {
Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# suppress inspection "UnusedProperty" for whole file
version=2.1.0
buildNumber=
version=2.1.1
buildNumber=SNAPSHOT
ideaVersion=2017.1
kotlinVersion=1.2.51
kotlinVersion=1.2.60
javaVersion=1.8
javaTargetVersion=1.8
kotlinLanguageVersion=1.2
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.7-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-all.zip
16 changes: 12 additions & 4 deletions src/main/java/cn/yiiguxing/plugin/translate/ui/form/Settings.form
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
<properties/>
<border type="none"/>
<children>
<component id="ae64f" class="javax.swing.JCheckBox" binding="mFontCheckBox">
<component id="ae64f" class="com.intellij.ui.components.JBCheckBox" binding="mFontCheckBox">
<constraints>
<grid row="0" column="0" row-span="1" col-span="5" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
Expand Down Expand Up @@ -223,30 +223,38 @@
</component>
</children>
</grid>
<grid id="4114f" binding="mOptionsPanel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="4114f" binding="mOptionsPanel" layout-manager="GridLayoutManager" row-count="3" 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="4" 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/>
<border type="none"/>
<children>
<component id="aa0d7" class="javax.swing.JCheckBox" binding="mShowStatusIconCheckBox">
<component id="aa0d7" class="com.intellij.ui.components.JBCheckBox" binding="mShowStatusIconCheckBox">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/TranslationBundle" key="settings.options.showStatusBarIcon"/>
</properties>
</component>
<component id="7f941" class="javax.swing.JCheckBox" binding="mFoldOriginalCheckBox">
<component id="7f941" class="com.intellij.ui.components.JBCheckBox" binding="mFoldOriginalCheckBox">
<constraints>
<grid row="1" 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 resource-bundle="messages/TranslationBundle" key="settings.options.foldOriginal"/>
</properties>
</component>
<component id="3b376" class="com.intellij.ui.components.JBCheckBox" binding="mKeepFormat">
<constraints>
<grid row="2" 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 resource-bundle="messages/TranslationBundle" key="settings.options.keepFormat"/>
</properties>
</component>
</children>
</grid>
</children>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class SettingsForm {
private JPanel mOptionsPanel;
private JCheckBox mShowStatusIconCheckBox;
private JCheckBox mFoldOriginalCheckBox;
private JCheckBox mKeepFormat;

private final Settings mSettings;
private final AppStorage mAppStorage;
Expand Down Expand Up @@ -180,4 +181,9 @@ public final JCheckBox getShowStatusIconCheckBox() {
public final JCheckBox getFoldOriginalCheckBox() {
return mFoldOriginalCheckBox;
}

@NotNull
public final JCheckBox getKeepFormatCheckBox() {
return mKeepFormat;
}
}
18 changes: 18 additions & 0 deletions src/main/kotlin/cn/yiiguxing/plugin/translate/AppStorage.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.yiiguxing.plugin.translate

import cn.yiiguxing.plugin.translate.trans.Lang
import cn.yiiguxing.plugin.translate.util.trimToSize
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.PersistentStateComponent
Expand All @@ -9,8 +10,10 @@ import com.intellij.openapi.components.Storage
import com.intellij.util.messages.Topic
import com.intellij.util.xmlb.XmlSerializerUtil
import com.intellij.util.xmlb.annotations.CollectionBean
import com.intellij.util.xmlb.annotations.MapAnnotation
import com.intellij.util.xmlb.annotations.Transient
import java.util.*
import kotlin.collections.HashMap
import kotlin.properties.Delegates

/**
Expand All @@ -22,6 +25,9 @@ class AppStorage : PersistentStateComponent<AppStorage> {
@CollectionBean
private val histories: MutableList<String> = ArrayList(DEFAULT_HISTORY_SIZE)

@MapAnnotation
private val languageScores: MutableMap<Lang, Int> = HashMap()

/**
* 最大历史记录长度
*/
Expand All @@ -44,6 +50,18 @@ class AppStorage : PersistentStateComponent<AppStorage> {
XmlSerializerUtil.copyBean(state, this)
}

/**
* @return 语言常用评分
*/
fun getLanguageScore(lang: Lang): Int = languageScores[lang] ?: 0

/**
* 设置语言常用评分
*/
fun setLanguageScore(lang: Lang, score: Int) {
languageScores[lang] = score
}

private fun trimHistoriesSize(maxSize: Int) {
if (histories.trimToSize(maxSize)) {
dataChangePublisher.onHistoriesChanged()
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/cn/yiiguxing/plugin/translate/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ class Settings : PersistentStateComponent<Settings> {
*/
var ignoreRegExp: String? = null

/**
* 翻译时保留文本格式
*/
var keepFormat: Boolean = false

/**
* 状态栏图标
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TranslationPresenter(private val view: View, private val recordHistory: Bo

private val translateService = TranslateService
private val appStorage = AppStorage
private var lastRequest: Presenter.Request? = null
private var currentRequest: Presenter.Request? = null

override val histories: List<String> get() = appStorage.getHistories()

Expand All @@ -29,13 +29,13 @@ class TranslationPresenter(private val view: View, private val recordHistory: Bo

override fun translate(text: String, srcLang: Lang, targetLang: Lang) {
val request = Presenter.Request(text, srcLang, targetLang, translateService.translator.id)
if (text.isBlank() || request == lastRequest) {
if (text.isBlank() || request == currentRequest) {
return
}

TextToSpeech.stop()

lastRequest = request
currentRequest = request
if (recordHistory) {
appStorage.addHistory(text)
}
Expand All @@ -51,8 +51,9 @@ class TranslationPresenter(private val view: View, private val recordHistory: Bo
}

private inline fun onPostResult(request: Presenter.Request, block: View.() -> Unit) {
if (request == lastRequest && !view.disposed) {
if (request == currentRequest && !view.disposed) {
view.block()
currentRequest = null
}
}

Expand All @@ -61,17 +62,11 @@ class TranslationPresenter(private val view: View, private val recordHistory: Bo
private val presenterRef: WeakReference<TranslationPresenter> = WeakReference(presenter)

override fun onSuccess(translation: Translation) {
presenterRef.get()?.apply {
onPostResult(request) { showTranslation(request, translation, false) }
lastRequest = null
}
presenterRef.get()?.onPostResult(request) { showTranslation(request, translation, false) }
}

override fun onError(message: String, throwable: Throwable) {
presenterRef.get()?.apply {
onPostResult(request) { showError(request, message, throwable) }
lastRequest = null
}
presenterRef.get()?.onPostResult(request) { showError(request, message, throwable) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ abstract class AutoSelectAction(
/**
* 更新Action
*
* @param e 事件
* @param active 是否活动的,表示是否可以取到词
* @param e 事件
*/
protected open fun onUpdate(e: AnActionEvent, active: Boolean) {}
protected open fun onUpdate(e: AnActionEvent): Boolean = true

/**
* 执行操作
Expand All @@ -42,11 +41,8 @@ abstract class AutoSelectAction(
protected open val AnActionEvent.editor: Editor? get() = CommonDataKeys.EDITOR.getData(dataContext)

override fun update(e: AnActionEvent) {
val active = e.editor?.let { editor ->
checkSelection && editor.selectionModel.hasSelection() || editor.canSelect()
} ?: false

onUpdate(e, active)
val active = e.editor?.run { hasValidSelection() || canSelect() } ?: false
e.presentation.isEnabledAndVisible = active && onUpdate(e)
}

override fun actionPerformed(e: AnActionEvent) {
Expand All @@ -58,6 +54,12 @@ abstract class AutoSelectAction(
e.getSelectionRange()?.takeUnless { it.isEmpty }?.let { onActionPerformed(e, editor, it) }
}

private fun Editor.hasValidSelection(): Boolean {
return checkSelection && selectionModel.run {
hasSelection() && (selectedText?.filterIgnore()?.any(wordPartCondition) ?: false)
}
}

private fun Editor.canSelect(): Boolean {
val offset = caretModel.offset
val textLength = document.textLength
Expand All @@ -67,6 +69,7 @@ abstract class AutoSelectAction(

return TextRange(maxOf(0, offset - 1), minOf(textLength, offset + 1))
.let { document.getText(it) }
.filterIgnore()
.any(wordPartCondition)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cn.yiiguxing.plugin.translate.action
import cn.yiiguxing.plugin.translate.ui.BalloonPositionTracker
import cn.yiiguxing.plugin.translate.util.SelectionMode
import cn.yiiguxing.plugin.translate.util.TranslationUIManager
import cn.yiiguxing.plugin.translate.util.splitWords
import cn.yiiguxing.plugin.translate.util.processBeforeTranslate
import com.intellij.codeInsight.highlighting.HighlightManager
import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.AnActionEvent
Expand Down Expand Up @@ -31,13 +31,9 @@ open class TranslateAction(checkSelection: Boolean = false, icon: Icon? = null)
override val selectionMode
get() = SelectionMode.INCLUSIVE

override fun onUpdate(e: AnActionEvent, active: Boolean) {
e.presentation.isEnabledAndVisible = active
}

override fun onActionPerformed(e: AnActionEvent, editor: Editor, selectionRange: TextRange) {
val project = editor.project ?: return
editor.document.getText(selectionRange).splitWords()?.let { text ->
editor.document.getText(selectionRange).processBeforeTranslate()?.let { text ->
val highlightManager = HighlightManager.getInstance(project)
val highlighters = ArrayList<RangeHighlighter>()
HighlightManager.getInstance(project).addRangeHighlight(editor, selectionRange.startOffset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ class TranslateAndReplaceAction : AutoSelectAction(true, NON_LATIN_CONDITION) {
override val selectionMode: SelectionMode
get() = Settings.autoSelectionMode

override fun onUpdate(e: AnActionEvent, active: Boolean) {
e.presentation.isEnabledAndVisible = active
&& e.editor
?.selectionModel
?.takeIf { it.hasSelection() }
?.selectedText
?.any(NON_LATIN_CONDITION)
?: active
}

override fun onActionPerformed(e: AnActionEvent, editor: Editor, selectionRange: TextRange) {
val project = e.project ?: return
e.getData(PlatformDataKeys.VIRTUAL_FILE)?.let {
Expand All @@ -64,7 +54,8 @@ class TranslateAndReplaceAction : AutoSelectAction(true, NON_LATIN_CONDITION) {

override fun onError(message: String, throwable: Throwable) {
editorRef.get()?.let {
Notifications.showErrorNotification(it.project, NOTIFICATION_DISPLAY_ID, message, throwable)
Notifications.showErrorNotification(it.project, NOTIFICATION_DISPLAY_ID,
"Translate and Replace", message, throwable)
}
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cn.yiiguxing.plugin.translate.action

import cn.yiiguxing.plugin.translate.message
import cn.yiiguxing.plugin.translate.util.TranslationUIManager
import cn.yiiguxing.plugin.translate.util.splitWords
import cn.yiiguxing.plugin.translate.util.processBeforeTranslate
import com.intellij.codeInsight.documentation.DocumentationManager
import com.intellij.codeInsight.hint.HintManagerImpl
import com.intellij.openapi.actionSystem.AnAction
Expand Down Expand Up @@ -30,7 +30,7 @@ class TranslateQuickDocAction : AnAction(), DumbAware, HintManagerImpl.ActionToI

override fun actionPerformed(e: AnActionEvent) {
e.getData(DocumentationManager.SELECTED_QUICK_DOC_TEXT)
?.splitWords()
?.processBeforeTranslate()
?.let {
e.project.let { project ->
project?.hideDocInfoHint()
Expand Down
Loading

0 comments on commit a2a2d00

Please sign in to comment.