Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
YiiGuxing committed Dec 10, 2019
2 parents 13b9786 + 90a200e commit 134b041
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [v2.6.1](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.6.1) (2019-12-10)

- 修复了文档注释翻译窗口大小异常的问题

## [v2.6.0](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.6.0) (2019-12-05)

- 新增了文档注释翻译功能
Expand Down
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,9 @@ FAQ

更新日志
--------
## [v2.6.0](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.6.0) (2019-12-05)
## [v2.6.1](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.6.1) (2019-12-10)

- 新增了文档注释翻译功能
- 更新了有道翻译支持语言列表,支持超过100种语言
- 优化了字体预览
- 修复了使用百度翻译引擎翻译带有换行内容时显示翻译内容不全的问题
- 修复了文档注释翻译窗口大小异常的问题

[完整的更新历史记录](./CHANGELOG.md)

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# suppress inspection "UnusedProperty" for whole file
version=2.6.0
version=2.6.1
buildNumber=
ideaVersion=2017.1
javaVersion=1.8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.popup.JBPopupFactory
import com.intellij.openapi.util.Computable
import com.intellij.openapi.util.DimensionService
Expand All @@ -25,9 +26,11 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.presentation.java.SymbolPresentationUtil
import com.intellij.ui.popup.AbstractPopup
import com.intellij.util.ui.JBDimension
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import java.awt.Dimension
import java.io.StringReader
import java.lang.ref.WeakReference
import javax.swing.text.html.HTMLDocument
Expand Down Expand Up @@ -77,7 +80,10 @@ class TranslateDocumentationAction : PsiElementTranslateAction() {
val e = editorRef.get()?.takeUnless { it.isDisposed }
if (element.isValid && e != null) {
documentationComponent.setContent(translatedDocumentation, element)
(documentationComponent.hint as? AbstractPopup)?.showInEditor(e)
(documentationComponent.hint as? AbstractPopup)?.apply {
showInEditor(e)
updateSize(project)
}
} else {
documentationComponent.hint?.cancel()
}
Expand Down Expand Up @@ -176,25 +182,28 @@ class TranslateDocumentationAction : PsiElementTranslateAction() {
.setFocusable(true)
.setCancelOnClickOutside(true)
.setModalContext(false)
.setDimensionServiceKey(project, DOCUMENTATION_POPUP_SIZE, false)
.setKeyEventHandler { e ->
if (AbstractPopup.isCloseRequest(e)) {
component.hint?.cancel()?.let { true } ?: false
} else false
}
.setCancelCallback {
component.hint?.size?.let { size ->
DimensionService.getInstance()
.setSize(DOCUMENTATION_POPUP_SIZE, size.takeIf { it.height > MIN_HEIGHT }, project)
}
true
}
.createPopup() as AbstractPopup

Disposer.register(hint, component)

component.hint = hint
hint.dimensionServiceKey =
if (DimensionService.getInstance().getSize(NEW_JAVADOC_LOCATION_AND_SIZE, project) != null) {
NEW_JAVADOC_LOCATION_AND_SIZE
} else {
DocumentationManager.JAVADOC_LOCATION_AND_SIZE
}

component.setContent(message("documentation.loading"))
hint.showInEditor(editor)
hint.updateSize(project, false)

return component
}
Expand All @@ -203,7 +212,7 @@ class TranslateDocumentationAction : PsiElementTranslateAction() {
companion object {
private const val NOTIFICATION_DISPLAY_ID = "Document Translation"

private const val NEW_JAVADOC_LOCATION_AND_SIZE = "javadoc.popup.new"
private const val DOCUMENTATION_POPUP_SIZE = "documentation.popup.size"

private const val CSS_QUERY_DEFINITION = ".definition"
private const val CSS_QUERY_CONTENT = ".content"
Expand All @@ -217,6 +226,9 @@ class TranslateDocumentationAction : PsiElementTranslateAction() {
private val HTML_HEAD_REGEX = Regex("""<(?<tag>.+?) class="(?<class>.+?)">""")
private const val HTML_HEAD_REPLACEMENT = "<${'$'}{tag} class='${'$'}{class}'>"

private const val MIN_HEIGHT = 50
private val MAX_DEFAULT = JBDimension(500, 350)

private val HTML_KIT = HTMLEditorKit()

private val LOGGER: Logger = Logger.getInstance(TranslateDocumentationAction::class.java)
Expand Down Expand Up @@ -253,5 +265,28 @@ class TranslateDocumentationAction : PsiElementTranslateAction() {
showInBestPositionFor(editor)
}
}

private fun AbstractPopup.updateSize(project: Project?, restore: Boolean = true) {
val restoreSize = if (restore) {
DimensionService.getInstance().getSize(DOCUMENTATION_POPUP_SIZE, project)
} else null

val sizeToSet = if (restoreSize == null) {
val preferredSize = component.preferredSize
val width = minOf(preferredSize.width, MAX_DEFAULT.width)
val height = minOf(preferredSize.height, MAX_DEFAULT.height)
Dimension(width, height)
} else restoreSize

if (restore) {
getUserData(Dimension::class.java)?.let {
sizeToSet.width = maxOf(sizeToSet.width, it.width)
sizeToSet.height = maxOf(sizeToSet.height, it.height)
}
}

size = sizeToSet
setUserData(if (!restore) listOf(sizeToSet.clone()) else null)
}
}
}
5 changes: 1 addition & 4 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@

<change-notes><![CDATA[
<ul>
<li>新增了文档注释翻译功能</li>
<li>更新了有道翻译支持语言列表,支持超过100种语言</li>
<li>优化了字体预览</li>
<li>修复了使用百度翻译引擎翻译带有换行内容时显示翻译内容不全的问题</li>
<li>修复了文档注释翻译窗口大小异常的问题</li>
</ul>
<a href="https://github.com/YiiGuxing/TranslationPlugin/blob/master/CHANGELOG.md"><b>Full Changelog History</b></a>
]]></change-notes>
Expand Down

0 comments on commit 134b041

Please sign in to comment.