From a3d5a9b855fe74bb2e89c23202605e92d54c4950 Mon Sep 17 00:00:00 2001 From: "Roman.Ivanov" Date: Fri, 14 Jun 2019 17:32:15 +0700 Subject: [PATCH] Inlay hints: join multiple presentations in annotation hints GitOrigin-RevId: 7c43f03d96127b78e559ee549b831ecc92feadb8 --- .../hints/AnnotationInlayProvider.kt | 17 +++++++++----- .../daemon/inlays/AnnotationHintsTest.kt | 22 +++++++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/inlays/AnnotationHintsTest.kt diff --git a/java/java-impl/src/com/intellij/codeInsight/hints/AnnotationInlayProvider.kt b/java/java-impl/src/com/intellij/codeInsight/hints/AnnotationInlayProvider.kt index d463786af199..3d7391562998 100644 --- a/java/java-impl/src/com/intellij/codeInsight/hints/AnnotationInlayProvider.kt +++ b/java/java-impl/src/com/intellij/codeInsight/hints/AnnotationInlayProvider.kt @@ -4,8 +4,10 @@ package com.intellij.codeInsight.hints import com.intellij.codeInsight.ExternalAnnotationsManager import com.intellij.codeInsight.InferredAnnotationsManager import com.intellij.codeInsight.MakeInferredAnnotationExplicit +import com.intellij.codeInsight.hints.presentation.InlayPresentation import com.intellij.codeInsight.hints.presentation.InsetPresentation import com.intellij.codeInsight.hints.presentation.MenuOnClickPresentation +import com.intellij.codeInsight.hints.presentation.SequencePresentation import com.intellij.codeInsight.javadoc.JavaDocInfoGenerator import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnActionEvent @@ -14,6 +16,7 @@ import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project import com.intellij.psi.* import com.intellij.ui.layout.* +import com.intellij.util.SmartList import javax.swing.JComponent import kotlin.reflect.KMutableProperty0 @@ -24,6 +27,8 @@ class AnnotationInlayProvider : InlayHintsProvider() + override fun collect(element: PsiElement, editor: Editor, sink: InlayHintsSink): Boolean { if (element is PsiModifierListOwner) { var annotations = emptySequence() @@ -39,14 +44,14 @@ class AnnotationInlayProvider : InlayHintsProvider sink.addBlockElement(offset, false, true, 0, presentation) - else -> sink.addInlineElement(offset, true, presentation) - } + presentations.add(createPresentation(it, element)) } } + val offset = element.modifierList!!.textRange.startOffset + if (presentations.isNotEmpty()) { + sink.addInlineElement(offset, false, SequencePresentation(SmartList(presentations))) + } + presentations.clear() } return true } diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/inlays/AnnotationHintsTest.kt b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/inlays/AnnotationHintsTest.kt new file mode 100644 index 000000000000..a9aab426624e --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/inlays/AnnotationHintsTest.kt @@ -0,0 +1,22 @@ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.java.codeInsight.daemon.inlays + +import com.intellij.codeInsight.hints.presentation.PresentationRenderer +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase +import org.intellij.lang.annotations.Language + +class AnnotationHintsTest: LightCodeInsightFixtureTestCase() { + fun check(@Language("Java") text: String) { + myFixture.configureByText("A.java", text) + myFixture.testInlays({ (it.renderer as PresentationRenderer).presentation.toString() }, { it.renderer is PresentationRenderer }) + } + + fun `test contract inferred annotation`() { + check(""" +class Demo { + private static int pure(int x, int y) { + return x * y + 10; + } +}""") + } +} \ No newline at end of file