-
-
Notifications
You must be signed in to change notification settings - Fork 782
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
545 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/main/kotlin/cn/yiiguxing/plugin/translate/provider/CSharpDocumentationElementProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package cn.yiiguxing.plugin.translate.provider | ||
|
||
import cn.yiiguxing.plugin.translate.util.IdeVersion | ||
import cn.yiiguxing.plugin.translate.util.SKIP_WHITE_SPACE | ||
import cn.yiiguxing.plugin.translate.util.elementType | ||
import cn.yiiguxing.plugin.translate.util.getNextSiblingSkippingCondition | ||
import com.intellij.lang.documentation.DocumentationProvider | ||
import com.intellij.psi.PsiComment | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiFile | ||
import com.intellij.psi.PsiWhiteSpace | ||
import com.jetbrains.rider.ideaInterop.fileTypes.csharp.lexer.CSharpTokenType | ||
import com.jetbrains.rider.ideaInterop.fileTypes.csharp.psi.CSharpDummyNode | ||
import com.jetbrains.rider.ideaInterop.fileTypes.csharp.psi.impl.CSharpDummyDeclaration | ||
|
||
/** | ||
* 由于`C#`的[DocumentationProvider]的机制与常规的不同,并隐藏了文档生成的细节,通过 | ||
* `C#`的[DocumentationProvider]并不能正确地获取到生成的文档,因此该类并不能实现最终 | ||
* 的目的。 | ||
* 另见:`com.jetbrains.rdclient.quickDoc.FrontendDocumentationProvider` | ||
*/ | ||
class CSharpDocumentationElementProvider : DocumentationElementProvider { | ||
|
||
override fun findDocumentationElementAt(psiFile: PsiFile, offset: Int): PsiElement? { | ||
// 2019版本之前没有C#的PSI | ||
if (!IdeVersion.isIde2019OrNewer) { | ||
return null | ||
} | ||
|
||
val element = psiFile.findElementAt(offset) | ||
return (element as? PsiComment)?.takeIf { it.owner != null } | ||
} | ||
|
||
override fun getDocumentationOwner(documentationElement: PsiElement): PsiElement? { | ||
return (documentationElement as? PsiComment)?.owner | ||
} | ||
|
||
private companion object { | ||
val SKIP_WHITE_SPACE_AND_COMMENT: (PsiElement) -> Boolean = { it is PsiWhiteSpace || it is PsiComment } | ||
|
||
val PsiComment.isDocComment: Boolean | ||
get() = when (elementType) { | ||
CSharpTokenType.END_OF_LINE_COMMENT -> text.startsWith("///") | ||
CSharpTokenType.C_STYLE_COMMENT -> text.startsWith("/**") | ||
else -> false | ||
} | ||
|
||
val PsiComment.owner: PsiElement? | ||
get() { | ||
if (!isDocComment) { | ||
return null | ||
} | ||
|
||
val element = getNextSiblingSkippingCondition(SKIP_WHITE_SPACE_AND_COMMENT) as? CSharpDummyNode | ||
?: return null | ||
val nextNode = element.getNextSiblingSkippingCondition(SKIP_WHITE_SPACE) | ||
if (nextNode is CSharpDummyDeclaration) { | ||
return nextNode.declaredElement | ||
} | ||
|
||
return element.identifier | ||
} | ||
|
||
val CSharpDummyNode.identifier: PsiElement? | ||
get() { | ||
var brackets = 0 | ||
var element = firstChild | ||
while (element != null) { | ||
when (element.elementType) { | ||
CSharpTokenType.LBRACKET, CSharpTokenType.LPARENTH -> brackets++ | ||
CSharpTokenType.RBRACKET, CSharpTokenType.RPARENTH -> brackets-- | ||
CSharpTokenType.IDENTIFIER -> { | ||
if (brackets == 0) { | ||
return element | ||
} | ||
} | ||
} | ||
|
||
element = element.nextSibling | ||
} | ||
|
||
return null | ||
} | ||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
src/main/kotlin/cn/yiiguxing/plugin/translate/provider/DartDocumentationElementProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package cn.yiiguxing.plugin.translate.provider | ||
|
||
import cn.yiiguxing.plugin.translate.util.elementType | ||
import cn.yiiguxing.plugin.translate.util.findChildOfType | ||
import cn.yiiguxing.plugin.translate.util.getNextSiblingSkippingCondition | ||
import cn.yiiguxing.plugin.translate.util.getPrevSiblingSkippingCondition | ||
import com.intellij.psi.PsiComment | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiFile | ||
import com.intellij.psi.PsiWhiteSpace | ||
import com.jetbrains.lang.dart.DartTokenTypesSets | ||
import com.jetbrains.lang.dart.psi.DartComponent | ||
import com.jetbrains.lang.dart.psi.DartComponentName | ||
import com.jetbrains.lang.dart.psi.DartDocComment | ||
import com.jetbrains.lang.dart.psi.DartVarDeclarationList | ||
|
||
class DartDocumentationElementProvider : DocumentationElementProvider { | ||
|
||
override fun findDocumentationElementAt(psiFile: PsiFile, offset: Int): PsiElement? { | ||
val element = psiFile.findElementAt(offset) ?: return null | ||
val type = element.elementType | ||
val docElement = when { | ||
element is PsiComment && type == DartTokenTypesSets.SINGLE_LINE_DOC_COMMENT -> element | ||
element.parent is DartDocComment -> element.parent | ||
else -> return null | ||
} as PsiComment | ||
|
||
return docElement.takeIf { it.owner != null } | ||
} | ||
|
||
override fun getDocumentationOwner(documentationElement: PsiElement): PsiElement? { | ||
return (documentationElement as? PsiComment)?.owner | ||
} | ||
|
||
companion object { | ||
private val SKIPPING_CONDITION: (PsiElement) -> Boolean = { | ||
it is PsiWhiteSpace || (it is PsiComment && it !is DartDocComment) | ||
} | ||
|
||
private val DART_COMPONENT_NAME_CONDITION: (PsiElement) -> Boolean = { it is DartComponentName } | ||
|
||
/** | ||
* 向上检查是否存在多行文档注释 | ||
*/ | ||
private fun PsiComment.checkPreviousComments(): Boolean { | ||
return getPrevSiblingSkippingCondition(SKIPPING_CONDITION) !is DartDocComment | ||
} | ||
|
||
/** | ||
* 找到注释目标 | ||
*/ | ||
private val PsiComment.owner: PsiElement? | ||
get() { | ||
// 文档注释类型中,多行注释有最高的优先级。 | ||
// 如果当前注释不是多行注释(DartDocComment),则向上寻找,如果上方有多行注释,则说明当前的注释是无效的。 | ||
// 且,最下方的多行文档注释有最高的优先级,向下寻找时如遇文档注释,则当前的注释也是无效的。 | ||
|
||
if (this !is DartDocComment && !checkPreviousComments()) { | ||
return null | ||
} | ||
|
||
return when (val sibling = getNextSiblingSkippingCondition(SKIPPING_CONDITION)) { | ||
is DartComponent -> sibling.componentName | ||
is DartVarDeclarationList -> sibling.findChildOfType(DartComponent::class.java)?.componentName | ||
else -> null | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/kotlin/cn/yiiguxing/plugin/translate/provider/GoDocumentationElementProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cn.yiiguxing.plugin.translate.provider | ||
|
||
import cn.yiiguxing.plugin.translate.util.findChildOfType | ||
import cn.yiiguxing.plugin.translate.util.getNextSiblingSkippingCondition | ||
import com.goide.psi.* | ||
import com.intellij.psi.PsiComment | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiFile | ||
import com.intellij.psi.PsiWhiteSpace | ||
|
||
class GoDocumentationElementProvider : DocumentationElementProvider { | ||
|
||
override fun findDocumentationElementAt(psiFile: PsiFile, offset: Int): PsiElement? { | ||
val element = psiFile.findElementAt(offset) | ||
return (element as? PsiComment)?.takeIf { it.owner != null } | ||
} | ||
|
||
override fun getDocumentationOwner(documentationElement: PsiElement): PsiElement? { | ||
return (documentationElement as? PsiComment)?.owner | ||
} | ||
|
||
private companion object { | ||
val SKIP_WHITE_SPACE_AND_COMMENT: (PsiElement) -> Boolean = { | ||
(it is PsiWhiteSpace && it.text.count { char -> char == '\n' } <= 1) || it is PsiComment | ||
} | ||
|
||
val GoTypeDeclaration.innerOwner: PsiElement? | ||
get() = findChildOfType(GoTypeSpec::class.java) | ||
|
||
val GoVarDeclaration.innerOwner: PsiElement? | ||
get() = findChildOfType(GoVarDefinition::class.java, true) | ||
|
||
val PsiComment.owner: PsiElement? | ||
get() { | ||
val element = getNextSiblingSkippingCondition(SKIP_WHITE_SPACE_AND_COMMENT) | ||
|
||
println(element) | ||
println(element is GoMethodSpec) | ||
println(element?.javaClass?.name) | ||
|
||
return when (element) { | ||
is GoPackageClause -> element.takeIf { parent is GoFile } | ||
is GoTypeDeclaration -> element.innerOwner | ||
is GoMethodDeclaration -> element | ||
is GoMethodSpec -> element | ||
is GoVarDeclaration -> element.innerOwner | ||
else -> null | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.