Skip to content

Commit 216a231

Browse files
author
Jia Liu
committed
fix: pick up presentationUtil #109
1 parent 26f933a commit 216a231

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.intellij.temporary.inlay.presentation
2+
3+
import com.intellij.codeInsight.codeVision.ui.renderers.painters.CodeVisionThemeInfoProvider
4+
import com.intellij.codeWithMe.ClientId
5+
import com.intellij.ide.ui.AntialiasingType
6+
import com.intellij.openapi.application.ApplicationInfo
7+
import com.intellij.openapi.application.ApplicationManager
8+
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors
9+
import com.intellij.openapi.editor.Editor
10+
import com.intellij.openapi.editor.colors.EditorColorsScheme
11+
import com.intellij.openapi.editor.colors.EditorFontType
12+
import com.intellij.openapi.editor.impl.FontInfo
13+
import com.intellij.openapi.editor.markup.TextAttributes
14+
import com.intellij.openapi.util.Key
15+
import com.intellij.openapi.util.text.StringUtil
16+
import com.intellij.ui.JBColor
17+
import com.intellij.util.ui.UIUtil
18+
import java.awt.Color
19+
import java.awt.Font
20+
import java.awt.FontMetrics
21+
import java.awt.font.FontRenderContext
22+
import java.lang.reflect.InvocationTargetException
23+
import java.lang.reflect.Method
24+
import java.util.stream.Collectors
25+
26+
object PresentationUtil {
27+
private val KEY_CACHED_FONTMETRICS = Key.create<Map<Font, FontMetrics>>("autodev.editorFontMetrics")
28+
29+
fun getTextAttributes(editor: Editor): TextAttributes {
30+
val scheme = editor.colorsScheme
31+
val themeAttributes = scheme.getAttributes(DefaultLanguageHighlighterColors.INLAY_TEXT_WITHOUT_BACKGROUND)
32+
if (themeAttributes != null && themeAttributes.foregroundColor != null) {
33+
return themeAttributes
34+
}
35+
val customAttributes = themeAttributes?.clone() ?: TextAttributes()
36+
if (customAttributes.foregroundColor == null) {
37+
customAttributes.foregroundColor = JBColor.GRAY as Color
38+
}
39+
40+
return customAttributes
41+
}
42+
43+
fun fontMetrics(editor: Editor, font: Font): FontMetrics {
44+
val editorContext = FontInfo.getFontRenderContext(editor.contentComponent)
45+
val context = FontRenderContext(
46+
editorContext.transform,
47+
AntialiasingType.getKeyForCurrentScope(false),
48+
editorContext.fractionalMetricsHint,
49+
)
50+
val cachedMap = KEY_CACHED_FONTMETRICS[editor, emptyMap()]
51+
var fontMetrics = cachedMap[font]
52+
if (fontMetrics == null || !context.equals(fontMetrics.fontRenderContext)) {
53+
fontMetrics = FontInfo.getFontMetrics(font, context)
54+
KEY_CACHED_FONTMETRICS.set(editor, cachedMap + mapOf(font to fontMetrics))
55+
}
56+
57+
return fontMetrics
58+
}
59+
60+
fun replaceLeadingTabs(lines: List<String?>, tabWidth: Int): List<String> {
61+
return lines.stream().map { line: String? ->
62+
val tabCount = StringUtil.countChars(line!!, '\t', 0, true)
63+
if (tabCount > 0) {
64+
val tabSpaces = StringUtil.repeatSymbol(' ', tabCount * tabWidth)
65+
return@map tabSpaces + tabSpaces
66+
}
67+
line
68+
}.collect(Collectors.toList()) as List<String>
69+
}
70+
71+
fun getThemeInfoProvider(): CodeVisionThemeInfoProvider {
72+
val serviceClass = CodeVisionThemeInfoProvider::class.java
73+
val service = ApplicationManager.getApplication().getService(serviceClass)
74+
?: throw RuntimeException(
75+
"Cannot find service ${serviceClass.name} (classloader=${serviceClass.classLoader}, " +
76+
"client=${ClientId.currentOrNull})"
77+
)
78+
79+
return service
80+
}
81+
82+
83+
private val getEditorFontSize2DMethod: Method?
84+
85+
init {
86+
var method: Method? = null
87+
if (ApplicationInfo.getInstance().build.baselineVersion >= 221) {
88+
try {
89+
method = EditorColorsScheme::class.java.getMethod("getEditorFontSize2D", *arrayOfNulls(0))
90+
} catch (_: NoSuchMethodException) {
91+
}
92+
}
93+
getEditorFontSize2DMethod = method
94+
}
95+
96+
fun getFont(editor: Editor, text: String): Font {
97+
val font = editor.colorsScheme.getFont(EditorFontType.PLAIN).deriveFont(2)
98+
val fallbackFont = UIUtil.getFontWithFallbackIfNeeded(font, text)
99+
return fallbackFont.deriveFont(fontSize(editor))
100+
}
101+
102+
fun fontSize(editor: Editor): Float {
103+
val scheme = editor.colorsScheme
104+
if (getEditorFontSize2DMethod != null) {
105+
try {
106+
return getEditorFontSize2DMethod.invoke(scheme, *arrayOfNulls(0)) as Float
107+
} catch (_: IllegalAccessException) {
108+
} catch (_: InvocationTargetException) {
109+
}
110+
}
111+
return scheme.editorFontSize.toFloat()
112+
}
113+
}

0 commit comments

Comments
 (0)