1
1
package cc.unitmesh.devti.provider
2
2
3
3
import cc.unitmesh.devti.custom.document.LivingDocumentationType
4
+ import com.intellij.codeInsight.daemon.impl.CollectHighlightsUtil
4
5
import com.intellij.openapi.command.WriteCommandAction
6
+ import com.intellij.openapi.diagnostic.logger
5
7
import com.intellij.openapi.editor.Editor
6
8
import com.intellij.openapi.editor.SelectionModel
7
9
import com.intellij.psi.NavigatablePsiElement
8
10
import com.intellij.psi.PsiElement
9
11
import com.intellij.psi.PsiNameIdentifierOwner
12
+ import com.intellij.psi.PsiNamedElement
10
13
import com.intellij.psi.codeStyle.CodeStyleManager
11
14
import com.intellij.psi.util.parentOfTypes
12
15
13
16
class HarmonyOsLivingDocumentation : LivingDocumentation {
14
17
override val forbiddenRules: List <String > = listOf (
15
- " ArkTS is an extension of TypeScript, you can use TypeScript's rules" ,
16
18
" do not return example code" ,
17
19
" do not use @author and @version tags"
18
20
)
@@ -29,37 +31,50 @@ class HarmonyOsLivingDocumentation : LivingDocumentation {
29
31
val project = target.project
30
32
val codeStyleManager = CodeStyleManager .getInstance(project)
31
33
WriteCommandAction .runWriteCommandAction(project, " Living Document" , " cc.unitmesh.livingDoc" , {
34
+ val text = newDoc + " \n "
32
35
val startOffset = target.textRange.startOffset
33
- val newEndOffset = startOffset + newDoc .length
36
+ val newEndOffset = startOffset + text .length
34
37
35
- editor.document.insertString(startOffset, newDoc )
38
+ editor.document.insertString(startOffset, text )
36
39
codeStyleManager.reformatText(target.containingFile, startOffset, newEndOffset)
37
40
});
38
41
}
39
42
43
+ val logger = logger<HarmonyOsLivingDocumentation >()
44
+
40
45
override fun findNearestDocumentationTarget (psiElement : PsiElement ): PsiNameIdentifierOwner ? {
41
46
if (psiElement is PsiNameIdentifierOwner ) {
47
+ logger.warn(" psiElement is PsiNameIdentifierOwner, text: ${psiElement.text} " )
42
48
return psiElement
43
49
}
44
50
45
- var candidate: PsiElement ? =
46
- psiElement.parentOfTypes(PsiNameIdentifierOwner ::class , NavigatablePsiElement ::class )
47
-
48
- while (candidate != null ) {
49
- if (candidate is PsiNameIdentifierOwner ) {
50
- return candidate
51
- }
51
+ val candidate: PsiElement ? =
52
+ psiElement.parentOfTypes(PsiNamedElement ::class , NavigatablePsiElement ::class )
52
53
53
- candidate = candidate.parentOfTypes(PsiNameIdentifierOwner ::class , NavigatablePsiElement ::class )
54
+ if (candidate != null ) {
55
+ logger.warn(" candidate is PsiNameIdentifierOwner: text: ${candidate.text} " )
56
+ return candidate as ? PsiNameIdentifierOwner
54
57
}
55
58
56
59
return null
57
60
}
58
61
59
62
override fun findDocTargetsInSelection (
60
- psiElement : PsiElement ,
63
+ root : PsiElement ,
61
64
selectionModel : SelectionModel
62
65
): List <PsiNameIdentifierOwner > {
66
+ val findCommonParent = CollectHighlightsUtil .findCommonParent(
67
+ root,
68
+ selectionModel.selectionStart,
69
+ selectionModel.selectionEnd
70
+ ) ? : return emptyList()
71
+
72
+ val target = findNearestDocumentationTarget(findCommonParent) ? : return emptyList()
73
+
74
+ if (containsElement(selectionModel, target)) {
75
+ return listOf (target)
76
+ }
77
+
63
78
return listOf ()
64
79
}
65
80
}
0 commit comments