1
+ package org.tabooproject.development.completion
2
+
3
+ import com.intellij.codeInsight.completion.CompletionContributor
4
+ import com.intellij.codeInsight.completion.CompletionParameters
5
+ import com.intellij.codeInsight.completion.CompletionResultSet
6
+ import com.intellij.codeInsight.completion.PrioritizedLookupElement
7
+ import com.intellij.codeInsight.lookup.LookupElementBuilder
8
+ import com.intellij.psi.PsiDocumentManager
9
+ import com.intellij.psi.util.PsiTreeUtil
10
+ import com.intellij.util.PlatformIcons
11
+ import org.jetbrains.kotlin.psi.KtFile
12
+ import org.jetbrains.kotlin.psi.KtImportDirective
13
+ import org.jetbrains.kotlin.psi.KtPsiFactory
14
+ import org.jetbrains.kotlin.resolve.ImportPath
15
+
16
+
17
+ class InfoFuncCompletion : CompletionContributor () {
18
+
19
+ override fun fillCompletionVariants (parameters : CompletionParameters , result : CompletionResultSet ) {
20
+ result.addElement(
21
+ PrioritizedLookupElement .withPriority(
22
+ LookupElementBuilder .create(" info" )
23
+ .withIcon(PlatformIcons .METHOD_ICON )
24
+ .withTailText(" 打印日志 " , true )
25
+ .withInsertHandler handler@{ context, _ ->
26
+ val editor = context.editor
27
+ val startOffset = context.startOffset
28
+ val tailOffset = context.tailOffset
29
+ val element = context.file.findElementAt(startOffset)
30
+ if (element != null ) {
31
+ val parent = element.parent.parent
32
+
33
+ val ktFile = element.containingFile as ? KtFile ? : return @handler
34
+
35
+ if (parent != null ) {
36
+ try {
37
+ val objectText = parent.text.let {
38
+ val index = it.indexOfLast { c -> c == ' .' }
39
+ it.substring(0 , index)
40
+ }
41
+ context.document.replaceString(
42
+ parent.textRange.startOffset,
43
+ tailOffset,
44
+ " info($objectText )"
45
+ )
46
+ editor.caretModel
47
+ .moveToOffset(parent.textRange.startOffset + 5 + objectText.length)
48
+
49
+ PsiDocumentManager .getInstance(element.project).commitDocument(context.document)
50
+ } catch (e: Exception ) {
51
+ e.printStackTrace()
52
+ }
53
+
54
+ // 检查和引入info包
55
+ val import =
56
+ PsiTreeUtil .findChildrenOfType(ktFile, KtImportDirective ::class .java)
57
+ val hasImport =
58
+ import.any { it.importPath?.pathStr == " taboolib.common.platform.function.info" }
59
+ if (! hasImport) {
60
+ val factory = KtPsiFactory (context.project)
61
+ val importDirective =
62
+ factory.createImportDirective(ImportPath .fromString(" taboolib.common.platform.function.info" ))
63
+ ktFile.importList?.add(importDirective)
64
+ }
65
+ }
66
+ }
67
+ },
68
+ - 1000.0 ,
69
+ ),
70
+ )
71
+ }
72
+
73
+ }
0 commit comments