mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-18 12:31:26 +07:00
[java, completion] IJPL-207762 replace beforeHandle and afterHandle with insert handlers
GitOrigin-RevId: 35cce5f78c2bd7d9191f820077665bd9a1066c95
This commit is contained in:
committed by
intellij-monorepo-bot
parent
2a566df3f0
commit
1d70a8b9ab
@@ -2,6 +2,7 @@
|
||||
package com.intellij.codeInsight.completion
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightSettings
|
||||
import com.intellij.codeInsight.completion.JavaMethodCallInsertHandler.Companion.findInsertedCall
|
||||
import com.intellij.codeInsight.completion.util.MethodParenthesesHandler
|
||||
import com.intellij.codeInsight.hint.ParameterInfoControllerBase
|
||||
import com.intellij.codeInsight.hint.ShowParameterInfoContext
|
||||
@@ -14,6 +15,7 @@ import com.intellij.injected.editor.EditorWindow
|
||||
import com.intellij.openapi.Disposable
|
||||
import com.intellij.openapi.editor.Document
|
||||
import com.intellij.openapi.util.Disposer
|
||||
import com.intellij.openapi.util.Key
|
||||
import com.intellij.openapi.util.registry.Registry
|
||||
import com.intellij.openapi.util.text.StringUtil
|
||||
import com.intellij.psi.*
|
||||
@@ -22,7 +24,20 @@ import com.intellij.psi.util.PsiTreeUtil
|
||||
import com.intellij.util.ThreeState
|
||||
import kotlin.math.min
|
||||
|
||||
public open class JavaMethodCallInsertHandler<MethodCallElement : JavaMethodCallElement> : InsertHandler<MethodCallElement> {
|
||||
public open class JavaMethodCallInsertHandler<MethodCallElement : JavaMethodCallElement>(
|
||||
/**
|
||||
* Called before insertion methods. Performs any necessary pre-processing or setup.
|
||||
*/
|
||||
private val beforeHandler: InsertHandler<MethodCallElement>? = null,
|
||||
|
||||
/**
|
||||
* Called after insertion methods. Performs any necessary post-processing or cleanup.
|
||||
*
|
||||
* Use [findInsertedCall] to get PsiCallExpression representing the inserted code, or null if no code was inserted
|
||||
*/
|
||||
private val afterHandler: InsertHandler<MethodCallElement>? = null,
|
||||
|
||||
) : InsertHandler<MethodCallElement> {
|
||||
override fun handleInsert(context: InsertionContext, item: MethodCallElement) {
|
||||
val document = context.document
|
||||
val file = context.file
|
||||
@@ -40,7 +55,7 @@ public open class JavaMethodCallInsertHandler<MethodCallElement : JavaMethodCall
|
||||
|
||||
val offset = context.startOffset
|
||||
val refStart = context.trackOffset(offset, true)
|
||||
beforeHandle(context)
|
||||
beforeHandler?.handleInsert(context, item)
|
||||
if (item.isNeedExplicitTypeParameters) {
|
||||
qualifyMethodCall(item, file, context.getOffset(refStart), document)
|
||||
insertExplicitTypeParameters(item, context, refStart)
|
||||
@@ -64,7 +79,8 @@ public open class JavaMethodCallInsertHandler<MethodCallElement : JavaMethodCall
|
||||
handleNegation(context, document, methodCall, item.isNegatable)
|
||||
}
|
||||
|
||||
afterHandle(context, methodCall)
|
||||
item.putUserData(callKey, methodCall)
|
||||
afterHandler?.handleInsert(context, item)
|
||||
|
||||
if (canStartArgumentLiveTemplate()) {
|
||||
JavaMethodCallElement.startArgumentLiveTemplate(context, method)
|
||||
@@ -72,23 +88,6 @@ public open class JavaMethodCallInsertHandler<MethodCallElement : JavaMethodCall
|
||||
showParameterHints(item, context, method, methodCall)
|
||||
}
|
||||
|
||||
/**
|
||||
* Called before insertion methods. Performs any necessary pre-processing or setup.
|
||||
*
|
||||
* @param context the insertion context for the code template, must not be null
|
||||
*/
|
||||
protected open fun beforeHandle(context: InsertionContext) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Called after insertion methods. Performs any necessary post-processing or cleanup.
|
||||
*
|
||||
* @param context the insertion context for the code template
|
||||
* @param call the PsiCallExpression representing the inserted code, or null if no code was inserted
|
||||
*/
|
||||
protected open fun afterHandle(context: InsertionContext, call: PsiCallExpression?) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the argument live template can be started.
|
||||
* see registry key java.completion.argument.live.template.description.
|
||||
@@ -105,7 +104,7 @@ public open class JavaMethodCallInsertHandler<MethodCallElement : JavaMethodCall
|
||||
document: Document,
|
||||
file: PsiFile,
|
||||
method: PsiMethod,
|
||||
startOffset: Int
|
||||
startOffset: Int,
|
||||
) {
|
||||
if (!needImportOrQualify()) {
|
||||
return
|
||||
@@ -165,7 +164,7 @@ public open class JavaMethodCallInsertHandler<MethodCallElement : JavaMethodCall
|
||||
private fun insertExplicitTypeParameters(
|
||||
item: MethodCallElement,
|
||||
context: InsertionContext,
|
||||
refStart: OffsetKey
|
||||
refStart: OffsetKey,
|
||||
) {
|
||||
context.commitDocument()
|
||||
|
||||
@@ -180,7 +179,7 @@ public open class JavaMethodCallInsertHandler<MethodCallElement : JavaMethodCall
|
||||
context: InsertionContext,
|
||||
document: Document,
|
||||
methodCall: PsiCallExpression,
|
||||
negatable: Boolean
|
||||
negatable: Boolean,
|
||||
) {
|
||||
if (context.completionChar == '!' && negatable) {
|
||||
context.setAddCompletionChar(false)
|
||||
@@ -201,7 +200,7 @@ public open class JavaMethodCallInsertHandler<MethodCallElement : JavaMethodCall
|
||||
element: LookupElement,
|
||||
context: InsertionContext,
|
||||
method: PsiMethod,
|
||||
methodCall: PsiCallExpression?
|
||||
methodCall: PsiCallExpression?,
|
||||
) {
|
||||
if (!CodeInsightSettings.getInstance().SHOW_PARAMETER_NAME_HINTS_ON_COMPLETION ||
|
||||
context.completionChar == Lookup.COMPLETE_STATEMENT_SELECT_CHAR ||
|
||||
@@ -268,5 +267,16 @@ public open class JavaMethodCallInsertHandler<MethodCallElement : JavaMethodCall
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Use [findInsertedCall] to get PsiCallExpression representing the inserted code, or null if no code was inserted
|
||||
* Can be called in [afterHandler]
|
||||
*/
|
||||
@JvmStatic
|
||||
public fun findInsertedCall(element: LookupElement, context: InsertionContext): PsiCallExpression? {
|
||||
return element.getUserData(callKey)
|
||||
}
|
||||
|
||||
private val callKey = Key.create<PsiCallExpression>("JavaMethodCallInsertHandler.call")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -329,7 +329,7 @@ public final class JavaQualifierAsArgumentContributor extends CompletionContribu
|
||||
public void handleInsert(@NotNull InsertionContext context) {
|
||||
JavaContributorCollectors.logInsertHandle(context.getProject(), JavaContributorCollectors.STATIC_QUALIFIER_TYPE,
|
||||
myIsSmart ? CompletionType.SMART : CompletionType.BASIC);
|
||||
new InsertHandler().handleInsert(context, this);
|
||||
new MyInsertHandler().handleInsert(context, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -390,26 +390,36 @@ public final class JavaQualifierAsArgumentContributor extends CompletionContribu
|
||||
}
|
||||
}
|
||||
|
||||
private class InsertHandler extends JavaMethodCallInsertHandler<JavaQualifierAsParameterMethodCallElement> {
|
||||
private class MyInsertHandler extends JavaMethodCallInsertHandler<JavaQualifierAsParameterMethodCallElement> {
|
||||
private MyInsertHandler() {
|
||||
super(new BeforeInsertHandler(), new AfterInsertHandler());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean needImportOrQualify() {
|
||||
return myShouldImportOrQualify;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void beforeHandle(@NotNull InsertionContext context) {
|
||||
protected boolean canStartArgumentLiveTemplate() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private class BeforeInsertHandler implements InsertHandler<JavaQualifierAsParameterMethodCallElement> {
|
||||
@Override
|
||||
public void handleInsert(@NotNull InsertionContext context,
|
||||
@NotNull JavaQualifierAsArgumentContributor.JavaQualifierAsParameterMethodCallElement item) {
|
||||
TextRange range = myOldQualifierExpression.getTextRange();
|
||||
context.getDocument().deleteString(range.getStartOffset(), range.getEndOffset() + 1);
|
||||
context.commitDocument();
|
||||
}
|
||||
|
||||
}
|
||||
private class AfterInsertHandler implements InsertHandler<JavaQualifierAsParameterMethodCallElement> {
|
||||
@Override
|
||||
protected boolean canStartArgumentLiveTemplate() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void afterHandle(@NotNull InsertionContext context, @Nullable PsiCallExpression call) {
|
||||
public void handleInsert(@NotNull InsertionContext context,
|
||||
@NotNull JavaQualifierAsArgumentContributor.JavaQualifierAsParameterMethodCallElement item) {
|
||||
PsiCallExpression call = JavaMethodCallInsertHandler.findInsertedCall(item, context);
|
||||
context.commitDocument();
|
||||
PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(context.getDocument());
|
||||
if (call != null) {
|
||||
@@ -417,9 +427,11 @@ public final class JavaQualifierAsArgumentContributor extends CompletionContribu
|
||||
if (list != null) {
|
||||
TextRange argumentList = list.getTextRange();
|
||||
String text = myOldQualifierExpression.getText();
|
||||
boolean hasOneArgument = ContainerUtil.exists(myMethods, method -> method.getParameterList().getParameters().length == 1);
|
||||
boolean hasOneArgument =
|
||||
ContainerUtil.exists(myMethods, method -> method.getParameterList().getParameters().length == 1);
|
||||
if (!hasOneArgument) {
|
||||
CommonCodeStyleSettings codeStyleSettings = CodeStyle.getLanguageSettings(myOldQualifierExpression.getContainingFile());
|
||||
CommonCodeStyleSettings codeStyleSettings =
|
||||
CodeStyle.getLanguageSettings(myOldQualifierExpression.getContainingFile());
|
||||
text += ",";
|
||||
if (codeStyleSettings.SPACE_AFTER_COMMA) {
|
||||
text += " ";
|
||||
|
||||
Reference in New Issue
Block a user