mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Inlay parameter hints: add hints for enum constructors and new expressions: IDEA-195733
This commit is contained in:
@@ -16,7 +16,7 @@ import com.intellij.util.IncorrectOperationException
|
||||
|
||||
object JavaInlayHintsProvider {
|
||||
|
||||
fun hints(callExpression: PsiCallExpression): Set<InlayInfo> {
|
||||
fun hints(callExpression: PsiCall): Set<InlayInfo> {
|
||||
if (JavaMethodCallElement.isCompletionMode(callExpression)) {
|
||||
val argumentList = callExpression.argumentList?:return emptySet()
|
||||
val text = argumentList.text
|
||||
@@ -101,7 +101,7 @@ object JavaInlayHintsProvider {
|
||||
.toSet()
|
||||
}
|
||||
|
||||
private fun methodHints(callExpression: PsiCallExpression, resolveResult: ResolveResult): Set<InlayInfo> {
|
||||
private fun methodHints(callExpression: PsiCall, resolveResult: ResolveResult): Set<InlayInfo> {
|
||||
val element = resolveResult.element
|
||||
val substitutor = (resolveResult as? JavaResolveResult)?.substitutor ?: PsiSubstitutor.EMPTY
|
||||
|
||||
@@ -175,7 +175,7 @@ object JavaInlayHintsProvider {
|
||||
|
||||
private fun isShowForParamsWithSameType() = JavaInlayParameterHintsProvider.getInstance().isShowForParamsWithSameType.get()
|
||||
|
||||
private fun isMethodToShow(method: PsiMethod, callExpression: PsiCallExpression): Boolean {
|
||||
private fun isMethodToShow(method: PsiMethod, callExpression: PsiCall): Boolean {
|
||||
val params = method.parameterList.parameters
|
||||
if (params.isEmpty()) return false
|
||||
if (params.size == 1) {
|
||||
@@ -195,7 +195,7 @@ object JavaInlayHintsProvider {
|
||||
}
|
||||
|
||||
|
||||
private fun isBuilderLike(expression: PsiCallExpression, method: PsiMethod): Boolean {
|
||||
private fun isBuilderLike(expression: PsiCall, method: PsiMethod): Boolean {
|
||||
if (expression is PsiNewExpression) return false
|
||||
|
||||
val returnType = TypeConversionUtil.erasure(method.returnType) ?: return false
|
||||
@@ -212,7 +212,7 @@ object JavaInlayHintsProvider {
|
||||
return false
|
||||
}
|
||||
|
||||
private fun callInfo(callExpression: PsiCallExpression, method: PsiMethod): CallInfo {
|
||||
private fun callInfo(callExpression: PsiCall, method: PsiMethod): CallInfo {
|
||||
val params = method.parameterList.parameters
|
||||
val hasVarArg = params.lastOrNull()?.isVarArgs ?: false
|
||||
val regularParamsCount = if (hasVarArg) params.size - 1 else params.size
|
||||
|
||||
+16
-6
@@ -6,9 +6,7 @@ import com.intellij.codeInsight.completion.JavaMethodCallElement
|
||||
import com.intellij.codeInsight.hints.HintInfo.MethodInfo
|
||||
import com.intellij.lang.java.JavaLanguage
|
||||
import com.intellij.openapi.util.text.StringUtil
|
||||
import com.intellij.psi.PsiCallExpression
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiMethod
|
||||
import com.intellij.psi.*
|
||||
|
||||
class JavaInlayParameterHintsProvider : InlayParameterHintsProvider {
|
||||
|
||||
@@ -17,7 +15,7 @@ class JavaInlayParameterHintsProvider : InlayParameterHintsProvider {
|
||||
}
|
||||
|
||||
override fun getHintInfo(element: PsiElement): MethodInfo? {
|
||||
if (element is PsiCallExpression) {
|
||||
if (element is PsiCallExpression && element !is PsiEnumConstant) {
|
||||
val resolvedElement = (if(JavaMethodCallElement.isCompletionMode(element)) CompletionMemory.getChosenMethod(element) else null)
|
||||
?: element.resolveMethodGenerics().element
|
||||
if (resolvedElement is PsiMethod) {
|
||||
@@ -28,7 +26,9 @@ class JavaInlayParameterHintsProvider : InlayParameterHintsProvider {
|
||||
}
|
||||
|
||||
override fun getParameterHints(element: PsiElement): List<InlayInfo> {
|
||||
if (element is PsiCallExpression) {
|
||||
if (element is PsiCall) {
|
||||
if (element is PsiEnumConstant && !isShowHintsForEnumConstants.get()) return emptyList()
|
||||
if (element is PsiNewExpression && !isShowHintsForNewExpressions.get()) return emptyList()
|
||||
return JavaInlayHintsProvider.hints(element).toList()
|
||||
}
|
||||
return emptyList()
|
||||
@@ -117,13 +117,23 @@ class JavaInlayParameterHintsProvider : InlayParameterHintsProvider {
|
||||
"Show hints even when type of expression is clear",
|
||||
false)
|
||||
|
||||
val isShowHintsForEnumConstants: Option = Option("java.enums",
|
||||
"Show hints for enum constants",
|
||||
true)
|
||||
|
||||
val isShowHintsForNewExpressions: Option = Option("java.new.expr",
|
||||
"Show hints for 'new' expressions",
|
||||
true)
|
||||
|
||||
override fun getSupportedOptions(): List<Option> {
|
||||
return listOf(
|
||||
isDoNotShowIfMethodNameContainsParameterName,
|
||||
isShowForParamsWithSameType,
|
||||
isDoNotShowForBuilderLikeMethods,
|
||||
ignoreOneCharOneDigitHints,
|
||||
isShowHintWhenExpressionTypeIsClear
|
||||
isShowHintWhenExpressionTypeIsClear,
|
||||
isShowHintsForEnumConstants,
|
||||
isShowHintsForNewExpressions
|
||||
)
|
||||
}
|
||||
}
|
||||
+87
-1
@@ -1066,6 +1066,92 @@ class Test {
|
||||
""")
|
||||
}
|
||||
|
||||
|
||||
fun `test enum parameter names`() {
|
||||
check("""
|
||||
public enum Thingy {
|
||||
ONE(<hint text="green:"/>false, <hint text="striped:"/>true),
|
||||
TWO(<hint text="green:"/>false, <hint text="striped:"/>false),
|
||||
THREE(<hint text="...x:"/>12,32,3,2,32,3,2,3,23);
|
||||
private boolean green;
|
||||
private boolean striped;
|
||||
|
||||
Thingy(final boolean green, final boolean striped) {
|
||||
this.green = green;
|
||||
this.striped = striped;
|
||||
}
|
||||
|
||||
Thingy(int... x) {
|
||||
}
|
||||
}""")
|
||||
}
|
||||
|
||||
|
||||
fun `test enum parameter names disabled`() {
|
||||
JavaInlayParameterHintsProvider.getInstance().isShowHintsForEnumConstants.set(false)
|
||||
check("""
|
||||
public enum Thingy {
|
||||
ONE(false, true),
|
||||
TWO(false, false),
|
||||
THREE(12,32,3,2,32,3,2,3,23);
|
||||
private boolean green;
|
||||
private boolean striped;
|
||||
|
||||
Thingy(final boolean green, final boolean striped) {
|
||||
this.green = green;
|
||||
this.striped = striped;
|
||||
}
|
||||
|
||||
Thingy(int... x) {
|
||||
}
|
||||
}""")
|
||||
}
|
||||
|
||||
|
||||
fun `test constructor call`() {
|
||||
JavaInlayParameterHintsProvider.getInstance().isShowHintsForNewExpressions.set(true)
|
||||
check("""
|
||||
public class Test {
|
||||
static class A {
|
||||
A(boolean hardName){}
|
||||
}
|
||||
|
||||
void foo() {
|
||||
new A(<hint text="hardName:"/>true);
|
||||
}
|
||||
}""")
|
||||
}
|
||||
|
||||
|
||||
fun `test constructor call disabled`() {
|
||||
JavaInlayParameterHintsProvider.getInstance().isShowHintsForNewExpressions.set(false)
|
||||
check("""
|
||||
public class Test {
|
||||
static class A {
|
||||
A(boolean hardName){}
|
||||
}
|
||||
|
||||
void foo() {
|
||||
new A(true);
|
||||
}
|
||||
}""")
|
||||
}
|
||||
|
||||
fun `test constructor call with other features`() {
|
||||
JavaInlayParameterHintsProvider.getInstance().isShowHintsForNewExpressions.set(true)
|
||||
JavaInlayParameterHintsProvider.getInstance().ignoreOneCharOneDigitHints.set(true)
|
||||
check("""
|
||||
public class Test {
|
||||
static class A {
|
||||
A(boolean a1, boolean a2){}
|
||||
}
|
||||
|
||||
void foo() {
|
||||
new A(true, false);
|
||||
}
|
||||
}""")
|
||||
}
|
||||
|
||||
fun getHints(): List<String> {
|
||||
val document = myFixture.getDocument(myFixture.file)
|
||||
val manager = ParameterHintsPresentationManager.getInstance()
|
||||
@@ -1074,7 +1160,7 @@ class Test {
|
||||
.getInlineElementsInRange(0, document.textLength)
|
||||
.mapNotNull { manager.getHintText(it) }
|
||||
}
|
||||
|
||||
|
||||
|
||||
fun assertSingleInlayWithText(expectedText: String) {
|
||||
val inlays = myFixture.editor.inlayModel.getInlineElementsInRange(0, editor.document.textLength)
|
||||
|
||||
@@ -45,8 +45,7 @@ class ShowSettingsWithAddedPattern : AnAction() {
|
||||
val editor = CommonDataKeys.EDITOR.getData(e.dataContext) ?: return
|
||||
|
||||
val offset = editor.caretModel.offset
|
||||
val info = getHintInfoFromProvider(offset, file, editor) ?: return
|
||||
|
||||
val info = getHintInfoFromProvider(offset, file, editor)
|
||||
if (info is HintInfo.MethodInfo) {
|
||||
e.presentation.setText(CodeInsightBundle.message("inlay.hints.show.settings", info.getMethodName()), false)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user