From 9b47be425ba2e4db819bacf2ba6dd35858cc65ed Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Wed, 27 Sep 2023 15:04:24 +0200 Subject: [PATCH] [suggested-refactoring] Fix component size computation for suggested refactoring popup Also: remove SignatureChangePresentationTestApp.kt (was not supported anyway) GitOrigin-RevId: 348e76e8125f900af3ee72f13956945c0c70b44d --- .../SignatureChangePresentationTestApp.kt | 143 ------------------ .../suggested/ChangeSignaturePopup.kt | 9 +- .../suggested/SignatureChangePresentation.kt | 18 +-- 3 files changed, 11 insertions(+), 159 deletions(-) delete mode 100644 java/testFramework/src/com/intellij/refactoring/suggested/SignatureChangePresentationTestApp.kt diff --git a/java/testFramework/src/com/intellij/refactoring/suggested/SignatureChangePresentationTestApp.kt b/java/testFramework/src/com/intellij/refactoring/suggested/SignatureChangePresentationTestApp.kt deleted file mode 100644 index ebfd06883574..000000000000 --- a/java/testFramework/src/com/intellij/refactoring/suggested/SignatureChangePresentationTestApp.kt +++ /dev/null @@ -1,143 +0,0 @@ -// Copyright 2000-2020 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.refactoring.suggested - -import com.intellij.java.refactoring.suggested.JavaSuggestedRefactoringSupport -import com.intellij.openapi.diff.DiffColors -import com.intellij.openapi.editor.colors.impl.EditorColorsSchemeImpl -import com.intellij.openapi.editor.markup.TextAttributes -import com.intellij.refactoring.suggested.SuggestedRefactoringSupport.Parameter -import com.intellij.refactoring.suggested.SuggestedRefactoringSupport.Signature -import java.awt.* -import javax.swing.JComponent -import javax.swing.JFrame -import javax.swing.JLabel -import javax.swing.JPanel -import javax.swing.border.EmptyBorder - -private val refactoringSupport = JavaSuggestedRefactoringSupport() - -private val model1 = run { - val oldSignature = Signature.create("foo", "void", listOf( - Parameter(0, "p1", "String"), - Parameter(1, "p2", "String"), - Parameter(2, "p3", "String"), - Parameter(3, "p4", "String") - ), null)!! - val newSignature = Signature.create("foo", "void", listOf( - Parameter(3, "p4", "String"), - Parameter(1, "p2", "String"), - Parameter(0, "p1", "String"), - Parameter(2, "p3", "String") - ), null)!! - refactoringSupport.ui.buildSignatureChangePresentation(oldSignature, newSignature) -} - -private val model2 = run { - val oldSignature = Signature.create("foo", "void", listOf( - Parameter(0, "p1", "String"), - Parameter(1, "p2", "String"), - Parameter(2, "p3", "String"), - Parameter(3, "p4", "String"), - Parameter(4, "p5", "String"), - Parameter(5, "p6", "String"), - Parameter(6, "p7", "int"), - Parameter(7, "p8", "int") - ), null)!! - - val newSignature = Signature.create("foo", "void", listOf( - Parameter(3, "p4", "String"), - Parameter(0, "p1", "String"), - Parameter(2, "p3", "String"), - Parameter(5, "p6", "String"), - Parameter(4, "p5", "String"), - Parameter(1, "p2", "String"), - Parameter(7, "p8", "int"), - Parameter(6, "p7", "int") - ), null)!! - refactoringSupport.ui.buildSignatureChangePresentation(oldSignature, newSignature) -} - -private val model3 = run { - val oldSignature = Signature.create("foo", "void", listOf( - Parameter(0, "p1", "String"), - Parameter(1, "p2", "String"), - Parameter(2, "p3", "String"), - Parameter(3, "p4", "String") - ), null)!! - - val newSignature = Signature.create("foo", "void", listOf( - Parameter(1, "p2", "String"), - Parameter(0, "p1", "String"), - Parameter(3, "p4", "String"), - Parameter(2, "p3", "String") - ), null)!! - refactoringSupport.ui.buildSignatureChangePresentation(oldSignature, newSignature) -} - -private val model4 = run { - val oldSignature = Signature.create("foo", "void", listOf( - Parameter(0, "p1", "int"), - Parameter(1, "p2", "long"), - Parameter(2, "p3", "int"), - Parameter(3, "p4", "String"), - Parameter(4, "p5", "int") - ), null)!! - - val newSignature = Signature.create("foo", "void", listOf( - Parameter(3, "p4", "String"), - Parameter(0, "p1", "int"), - Parameter(1, "p2", "long"), - Parameter(2, "p3", "int") - ), null)!! - refactoringSupport.ui.buildSignatureChangePresentation(oldSignature, newSignature) -} - -private val model5 = run { - val oldSignature = Signature.create("foo", "void", listOf( - Parameter(0, "p1", "int"), - Parameter(1, "p2", "long"), - Parameter(2, "p3", "int"), - Parameter(3, "p4", "String"), - Parameter(4, "p5", "String") - ), null)!! - - val newSignature = Signature.create("foo", "void", listOf( - Parameter(0, "p1", "double"), - Parameter(2, "p3", "int"), - Parameter(1, "p2", "long"), - Parameter(4, "p5New", "String"), - Parameter(3, "p4", "String") - ), null)!! - refactoringSupport.ui.buildSignatureChangePresentation(oldSignature, newSignature) -} - -fun main() { - val font = Font("Courier New", Font.PLAIN, 12) - val colorsScheme = EditorColorsSchemeImpl(null).apply { - setAttributes(DiffColors.DIFF_MODIFIED, TextAttributes(null, Color.decode("0xCAD9FA"), null, null, Font.PLAIN)) - setAttributes(DiffColors.DIFF_INSERTED, TextAttributes(null, Color.decode("0xBEE6BE"), null, null, Font.PLAIN)) - } - val presentation = SignatureChangePresentation(model2, font, colorsScheme, verticalMode = true) - val frame = JFrame("Signature Change").apply { - contentPane.add(JPanel(BorderLayout()).apply { - border = EmptyBorder(10, 10, 10, 10) - add(JLabel("Update usages to reflect signature changes:"), BorderLayout.NORTH) - add( - object : JComponent() { - init { - preferredSize = presentation.requiredSize - } - - override fun paint(g: Graphics) { - presentation.paint(g as Graphics2D, Rectangle(0, 0, width, height)) - } - }, - BorderLayout.CENTER - ) - }) - } - frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE - frame.pack() - frame.setLocation(400, 300) - frame.isVisible = true -} \ No newline at end of file diff --git a/platform/lang-impl/src/com/intellij/refactoring/suggested/ChangeSignaturePopup.kt b/platform/lang-impl/src/com/intellij/refactoring/suggested/ChangeSignaturePopup.kt index 2bc5709607c9..afeadd373cb6 100644 --- a/platform/lang-impl/src/com/intellij/refactoring/suggested/ChangeSignaturePopup.kt +++ b/platform/lang-impl/src/com/intellij/refactoring/suggested/ChangeSignaturePopup.kt @@ -154,7 +154,7 @@ private class SignatureChangesPage( add( object : JComponent() { init { - preferredSize = presentation.requiredSize + preferredSize = presentation.requiredSize(getFontMetrics(editorFont).fontRenderContext) } override fun paint(g: Graphics) { @@ -182,10 +182,13 @@ private class SignatureChangesPage( val minFontSize = 8 while (true) { var presentation = SignatureChangePresentation(model, font, themeColorsScheme, verticalMode = false) - if (presentation.requiredSize.width > maxWidthHorizontalMode) { + val frc = getFontMetrics(font).fontRenderContext + var requiredSize = presentation.requiredSize(frc) + if (requiredSize.width > maxWidthHorizontalMode) { presentation = SignatureChangePresentation(model, font, themeColorsScheme, verticalMode = true) + requiredSize = presentation.requiredSize(frc) } - if (presentation.requiredSize.width <= maxWidth && presentation.requiredSize.height <= maxHeight || font.size <= minFontSize) { + if (requiredSize.width <= maxWidth && requiredSize.height <= maxHeight || font.size <= minFontSize) { return presentation } font = Font(font.name, font.style, font.size - 1) diff --git a/platform/lang-impl/src/com/intellij/refactoring/suggested/SignatureChangePresentation.kt b/platform/lang-impl/src/com/intellij/refactoring/suggested/SignatureChangePresentation.kt index 2eb7ee617612..d6ebba4a2f74 100644 --- a/platform/lang-impl/src/com/intellij/refactoring/suggested/SignatureChangePresentation.kt +++ b/platform/lang-impl/src/com/intellij/refactoring/suggested/SignatureChangePresentation.kt @@ -1,7 +1,6 @@ // Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.refactoring.suggested -import com.intellij.ide.ui.AntialiasingType import com.intellij.ide.ui.UISettings import com.intellij.openapi.diff.DiffColors import com.intellij.openapi.editor.colors.EditorColorsScheme @@ -10,7 +9,6 @@ import com.intellij.refactoring.suggested.SignatureChangePresentationModel.TextF import com.intellij.ui.JBColor import java.awt.* import java.awt.font.FontRenderContext -import java.awt.geom.AffineTransform import java.awt.geom.Arc2D import java.awt.geom.GeneralPath import java.awt.geom.Point2D @@ -30,16 +28,10 @@ class SignatureChangePresentation( private val connectionStroke = BasicStroke(connectionLineThickness.toFloat()) private val connectionColor = modifiedAttributes.backgroundColor ?: defaultForegroundColor - private val dummyFontRenderContext = FontRenderContext( - AffineTransform(), - AntialiasingType.getKeyForCurrentScope(false), - UISettings.getPreferredFractionalMetricsValue() - ) - - val requiredSize: Dimension by lazy { - val oldSignatureSize = signatureDimensions(model.oldSignature, dummyFontRenderContext) - val newSignatureSize = signatureDimensions(model.newSignature, dummyFontRenderContext) - if (verticalMode) { + fun requiredSize(frc: FontRenderContext): Dimension { + val oldSignatureSize = signatureDimensions(model.oldSignature, frc) + val newSignatureSize = signatureDimensions(model.newSignature, frc) + return if (verticalMode) { Dimension( oldSignatureSize.width + newSignatureSize.width + betweenSignaturesHSpace + leftSpace + rightSpace, max(oldSignatureSize.height, newSignatureSize.height) + topSpace + bottomSpace @@ -51,7 +43,7 @@ class SignatureChangePresentation( oldSignatureSize.height + newSignatureSize.height + betweenSignaturesVSpace + topSpace + bottomSpace ) if (model.oldSignature.any { it.connectionId != null }) { - val router = renderAll(null, dummyFontRenderContext, Rectangle(Point(), size)) as HorizontalModeConnectionRouter + val router = renderAll(null, frc, Rectangle(Point(), size)) as HorizontalModeConnectionRouter if (router.hSegmentLevelsRequired > 0) { size.height += betweenSignaturesVSpaceWithOneHSegment - betweenSignaturesVSpace + betweenHSegmentsVSpace * (router.hSegmentLevelsRequired - 1)