Skip to content

Commit

Permalink
fixed behavior kdocs autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
Jolanrensen committed Dec 10, 2024
1 parent 81630ab commit 1ca8350
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.intellij.codeInsight.completion.CompletionProvider
import com.intellij.codeInsight.completion.CompletionResultSet
import com.intellij.codeInsight.completion.CompletionType
import com.intellij.codeInsight.completion.CompletionUtil
import com.intellij.codeInsight.completion.CompletionUtilCore
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.icons.AllIcons
import com.intellij.openapi.editor.EditorModificationUtil
Expand Down Expand Up @@ -50,7 +51,7 @@ class KDocCompletionContributor : CompletionContributor() {
extend(
CompletionType.BASIC,
// for inline tags: activates anywhere in the kdoc text
psiElement(KDocTokens.TEXT),
psiElement(KDocTokens.TEXT) or psiElement(KDocTokens.CODE_BLOCK_TEXT),
KDocProcessorTagCompletionProvider(IN_TEXT),
)
}
Expand All @@ -62,6 +63,15 @@ enum class Mode {
IN_TEXT, // anywhere in the kdoc text
}

// keeps leading blanks in place, removes the rest
internal fun String.removeDummyIdentifier(): String =
if (trimStart().startsWith(CompletionUtilCore.DUMMY_IDENTIFIER_TRIMMED)) {
val numberOfBlanks = length - trimStart().length
substring(0, numberOfBlanks)
} else {
this
}

class KDocProcessorTagCompletionProvider(private val mode: Mode) : CompletionProvider<CompletionParameters>() {
private val activeTagDocProcessors = getLoadedTagProcessors()

Expand All @@ -81,8 +91,8 @@ class KDocProcessorTagCompletionProvider(private val mode: Mode) : CompletionPro
result: CompletionResultSet,
) {
val charPattern = when (mode) {
IN_TEXT -> singleCharPattern('@') or singleCharPattern('{') or singleCharPattern('$')
AT_TAG_NAME, AFTER_ASTERISK -> singleCharPattern('@')
IN_TEXT, AFTER_ASTERISK -> singleCharPattern('@') or singleCharPattern('{') or singleCharPattern('$')
AT_TAG_NAME -> singleCharPattern('@')
}
// findIdentifierPrefix() requires identifier part characters to be a superset of identifier start characters
val prefix = CompletionUtil.findIdentifierPrefix(
Expand All @@ -97,15 +107,15 @@ class KDocProcessorTagCompletionProvider(private val mode: Mode) : CompletionPro
// check special conditions for adding completions
when (mode) {
AFTER_ASTERISK -> {
// TODO it should succeed even without @
// if (prefix.isNotEmpty() && !prefix.startsWith('@')) return
val text = parameters.position.text.substringBefore('@')
val text = parameters.position.text.removeDummyIdentifier().substringBefore('@')
if (text.isNotBlank() || text.length > 3) return
}

AT_TAG_NAME -> if (prefix.isNotEmpty() && !prefix.startsWith('@')) return
AT_TAG_NAME ->
if (prefix.isNotEmpty() && !prefix.startsWith('@')) return

IN_TEXT -> if (prefix.isNotEmpty() && prefix.first() !in setOf('@', '{', '$')) return
IN_TEXT ->
if (prefix.isNotEmpty() && prefix.first() !in setOf('@', '{', '$')) return
}

val resultWithPrefix = result.withPrefixMatcher(prefix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ class KDocCompletionInvoker : TypedHandlerDelegate() {
private fun autoPopupIfInKdoc(project: Project, editor: Editor) {
AutoPopupController.getInstance(project).autoPopupMemberLookup(editor) { file ->
val offset = editor.caretModel.offset
// val chars = editor.document.charsSequence
val elementAtCaret = file.findElementAt(offset - 1)
val lastNodeType = elementAtCaret?.node?.elementType ?: return@autoPopupMemberLookup false

lastNodeType === KDocTokens.TEXT
lastNodeType === KDocTokens.TEXT ||
lastNodeType === KDocTokens.TAG_NAME ||
lastNodeType === KDocTokens.LEADING_ASTERISK ||
lastNodeType === KDocTokens.CODE_BLOCK_TEXT ||
lastNodeType === KDocTokens.START
}
}

Expand All @@ -35,9 +38,7 @@ class KDocCompletionInvoker : TypedHandlerDelegate() {
): Result {
if (file.fileType != KotlinFileType.INSTANCE) return Result.CONTINUE
when (c) {
// @ is already handled by KotlinTypedHandler

'{', '$' -> autoPopupIfInKdoc(project, editor)
'{', '$', '@' -> autoPopupIfInKdoc(project, editor)
}
return Result.CONTINUE
}
Expand Down

0 comments on commit 1ca8350

Please sign in to comment.