how to completion with title(not contents)? (LookupElementBuilder)

Answered

If I write AddNewLookupElement code like this. 

Autocomplete options are completed by content, not title

title = "name"
contents="Hello world"

LookupElement element = LookupElementBuilder
.create(contents)
.withPresentableText(title)

resultSet.addElement(element);

How do autocomplete options work with title rather than contents?

 

I don't know how to do. pls help me

0
7 comments

What do you want to achieve? Passing in "contents" as String to completion variants - what should that be? Maybe you want com.intellij.codeInsight.lookup.LookupElementBuilder#create(java.lang.String) passing in "title" only?

0
Avatar
Permanently deleted user

Yann Cebron

I want to pass "title" to create, 

But in code, complete with "contents"

0

I think you should look at it as “I want to show and complete one text string but insert another text string” then it makes sense in the IDE API way.

You will need to provide your own InsertHandler. I use one with a few options to manipulate the document text after insert, including replacing the inserted text with completely different text.

import com.intellij.codeInsight.completion.InsertHandler
import com.intellij.codeInsight.completion.InsertionContext
import com.intellij.codeInsight.lookup.LookupElement
import com.vladsch.plugin.util.ifElse
import com.vladsch.plugin.util.minLimit

class MdAlternateTextInsertHandler(val elementText: CharSequence?, val alwaysAddSpace: Boolean = false, val deletePreStartChars: Int = 0) : InsertHandler<LookupElement> {
    override fun handleInsert(context: InsertionContext, item: LookupElement) {
        handleAlternateInsert(context, item.lookupString, elementText, item.isCaseSensitive, alwaysAddSpace, deletePreStartChars)
    }

    companion object {
        fun handleAlternateInsert(context: InsertionContext, lookupString: CharSequence, alternateText: CharSequence?, isCaseSensitive: Boolean, alwaysAddSpace: Boolean, deletePreStartChars: Int) {
            val editor = context.editor
            val document = editor.document
            val startOffset = context.startOffset
            var tailOffset = context.tailOffset
            val elementText = alternateText ?: lookupString
            val needReplacement = alternateText != null && (!isCaseSensitive || alternateText != lookupString)

            if (needReplacement) {
                document.replaceString(startOffset, tailOffset, elementText)
            }

            if (alwaysAddSpace) {
                val isCompletionSpaceOnly = !(context.completionChar != ' ' && context.shouldAddCompletionChar())
                context.setAddCompletionChar(false)

                val tailString = isCompletionSpaceOnly.ifElse(" ") { "${context.completionChar} " }
                document.insertString(startOffset + elementText.length, tailString)
                tailOffset = startOffset + elementText.length

                editor.caretModel.moveToOffset(startOffset + elementText.length + tailString.length)
            }

            if (deletePreStartChars > 0) {
                document.deleteString((startOffset - deletePreStartChars).minLimit(0), startOffset)
                tailOffset += (startOffset - deletePreStartChars).minLimit(0) - startOffset
            }

            context.tailOffset = tailOffset
        }
    }
}

Then use LookupElementDecorator static method withInsertHandler() to wrap your lookup element and use your insert handler.

0
Avatar
Permanently deleted user

Vladimir Schneider

wow.. Thanks you for your kindenss i'll try it soon!

0
Avatar
Permanently deleted user

And where can i find

import com.vladsch.plugin.util.ifElse
import com.vladsch.plugin.util.minLimit

these library?
0

These are libraries that I use in my plugins but have not made them available simply because they are currently in high state of flux and I do not want to support them in this state. So I will not release them until they settle to a more stable condition.

The two functions ifElse and minLimit are simple. ifElse is used to return 1 of two values depending on the boolean true/false state. Here it is used select between space only string if isSpaceOnlyCompletion is true and completion char + space otherwise.

minLimit is an alias for Math.max(). Here it is used to ensure that startOffset is >= 0. You can use Math.max(0, value) as a substitute. I tend to always mess up when needing a min limit and wind up using Math.min() instead of max so created an alias that eliminates the frequent error.

0
Avatar
Permanently deleted user

Vladimir Schneider

Finally I succeed! Thanks for your help!

Thank you very much!

0

Please sign in to leave a comment.