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 226c19f
Showing 1 changed file with 22 additions and 7 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 @@ -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 @@ -85,7 +95,7 @@ class KDocProcessorTagCompletionProvider(private val mode: Mode) : CompletionPro
AT_TAG_NAME, AFTER_ASTERISK -> singleCharPattern('@')
}
// findIdentifierPrefix() requires identifier part characters to be a superset of identifier start characters
val prefix = CompletionUtil.findIdentifierPrefix(
var prefix = CompletionUtil.findIdentifierPrefix(
parameters.position.containingFile,
parameters.offset,
StandardPatterns.character().javaIdentifierPart() or charPattern,
Expand All @@ -95,17 +105,22 @@ class KDocProcessorTagCompletionProvider(private val mode: Mode) : CompletionPro
if (parameters.isAutoPopup && prefix.isEmpty()) return

// check special conditions for adding completions
var mode = mode
when (mode) {
AFTER_ASTERISK -> {
// TODO it should succeed even without @
// if (prefix.isNotEmpty() && !prefix.startsWith('@')) return
val text = parameters.position.text.substringBefore('@')
if (text.isNotBlank() || text.length > 3) return
val text = parameters.position.text.removeDummyIdentifier().substringBefore('@')
if (text.isNotBlank() || text.length > 3) {
// should be handled as IN_TEXT
prefix = ""
mode = IN_TEXT
}
}

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

0 comments on commit 226c19f

Please sign in to comment.