Skip to content

Commit ebc575f

Browse files
phodal[Author Name]
and
[Author Name]
committed
feat(provider): add logging to HarmonyOsLivingDocumentation
Add logging to the `HarmonyOsLivingDocumentation` class in order to log warnings and debug information. This is useful for troubleshooting and understanding the state of the code during execution. Co-authored-by: [Author Name] <[email protected]>
1 parent d4612d6 commit ebc575f

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed
Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package cc.unitmesh.devti.provider
22

33
import cc.unitmesh.devti.custom.document.LivingDocumentationType
4+
import com.intellij.codeInsight.daemon.impl.CollectHighlightsUtil
45
import com.intellij.openapi.command.WriteCommandAction
6+
import com.intellij.openapi.diagnostic.logger
57
import com.intellij.openapi.editor.Editor
68
import com.intellij.openapi.editor.SelectionModel
79
import com.intellij.psi.NavigatablePsiElement
810
import com.intellij.psi.PsiElement
911
import com.intellij.psi.PsiNameIdentifierOwner
12+
import com.intellij.psi.PsiNamedElement
1013
import com.intellij.psi.codeStyle.CodeStyleManager
1114
import com.intellij.psi.util.parentOfTypes
1215

1316
class HarmonyOsLivingDocumentation : LivingDocumentation {
1417
override val forbiddenRules: List<String> = listOf(
15-
"ArkTS is an extension of TypeScript, you can use TypeScript's rules",
1618
"do not return example code",
1719
"do not use @author and @version tags"
1820
)
@@ -29,37 +31,50 @@ class HarmonyOsLivingDocumentation : LivingDocumentation {
2931
val project = target.project
3032
val codeStyleManager = CodeStyleManager.getInstance(project)
3133
WriteCommandAction.runWriteCommandAction(project, "Living Document", "cc.unitmesh.livingDoc", {
34+
val text = newDoc + "\n"
3235
val startOffset = target.textRange.startOffset
33-
val newEndOffset = startOffset + newDoc.length
36+
val newEndOffset = startOffset + text.length
3437

35-
editor.document.insertString(startOffset, newDoc)
38+
editor.document.insertString(startOffset, text)
3639
codeStyleManager.reformatText(target.containingFile, startOffset, newEndOffset)
3740
});
3841
}
3942

43+
val logger = logger<HarmonyOsLivingDocumentation>()
44+
4045
override fun findNearestDocumentationTarget(psiElement: PsiElement): PsiNameIdentifierOwner? {
4146
if (psiElement is PsiNameIdentifierOwner) {
47+
logger.warn("psiElement is PsiNameIdentifierOwner, text: ${psiElement.text}")
4248
return psiElement
4349
}
4450

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)
5253

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
5457
}
5558

5659
return null
5760
}
5861

5962
override fun findDocTargetsInSelection(
60-
psiElement: PsiElement,
63+
root: PsiElement,
6164
selectionModel: SelectionModel
6265
): 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+
6378
return listOf()
6479
}
6580
}

0 commit comments

Comments
 (0)