Skip to content

Commit

Permalink
feat: init some basic methods
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jul 19, 2023
1 parent 3ccdbca commit fa9f250
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 11 deletions.
Expand Up @@ -15,8 +15,10 @@ import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.codeStyle.CodeStyleManager
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.runBlocking
import kotlin.math.min

class CodeCompletionIntention : AbstractChatIntention() {
Expand Down Expand Up @@ -62,20 +64,21 @@ class CodeCompletionIntention : AbstractChatIntention() {
suffixEnd--
}

// TODO: use suffix to improve the completion
val suffix = document.getText(TextRange(offset, suffixEnd))

val text = ""
val flow = connectorFactory.connector().prompt(prompt)
val presentation = LLMTextPresentation(editor, flow, false)
val editorCustomElementRenderer: EditorCustomElementRenderer = PresentationRenderer(presentation)
editor.inlayModel.addAfterLineEndElement(
offset,
true,
editorCustomElementRenderer
)
runBlocking {
renderInlay(prompt, editor, offset)
}
}

// for future
/**
* Renders an inlay with the given prompt in the specified editor at the specified offset.
*
* @param prompt The prompt to display in the inlay.
* @param editor The editor in which to render the inlay.
* @param offset The offset at which to render the inlay.
*/
private suspend fun renderInlay(
prompt: @NlsSafe String,
editor: Editor,
Expand Down
Expand Up @@ -3,20 +3,29 @@ package cc.unitmesh.devti.presentation
import com.intellij.codeInsight.codeVision.ui.renderers.painters.CodeVisionThemeInfoProvider
import com.intellij.codeWithMe.ClientId
import com.intellij.ide.ui.AntialiasingType
import com.intellij.openapi.application.ApplicationInfo
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.LineExtensionInfo
import com.intellij.openapi.editor.colors.EditorColorsScheme
import com.intellij.openapi.editor.colors.EditorFontType
import com.intellij.openapi.editor.impl.FontInfo
import com.intellij.openapi.editor.markup.TextAttributes
import com.intellij.openapi.util.Key
import com.intellij.openapi.util.text.StringUtil
import com.intellij.ui.JBColor
import com.intellij.util.ui.UIUtil
import java.awt.Color
import java.awt.Font
import java.awt.FontMetrics
import java.awt.font.FontRenderContext
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import java.util.stream.Collectors

object PresentationUtil {
val KEY_CACHED_FONTMETRICS = Key.create<Map<Font, FontMetrics>>("autodev.editorFontMetrics")
private val KEY_CACHED_FONTMETRICS = Key.create<Map<Font, FontMetrics>>("autodev.editorFontMetrics")

fun getTextAttributes(editor: Editor): TextAttributes {
val scheme = editor.colorsScheme
Expand Down Expand Up @@ -49,6 +58,17 @@ object PresentationUtil {
return fontMetrics
}

fun replaceLeadingTabs(lines: List<String?>, tabWidth: Int): List<String> {
return lines.stream().map { line: String? ->
val tabCount = StringUtil.countChars(line!!, '\t', 0, true)
if (tabCount > 0) {
val tabSpaces = StringUtil.repeatSymbol(' ', tabCount * tabWidth)
return@map tabSpaces + tabSpaces
}
line
}.collect(Collectors.toList()) as List<String>
}

fun getThemeInfoProvider(): CodeVisionThemeInfoProvider {
val serviceClass = CodeVisionThemeInfoProvider::class.java
val service = ApplicationManager.getApplication().getService(serviceClass)
Expand All @@ -59,4 +79,36 @@ object PresentationUtil {

return service
}


private val getEditorFontSize2DMethod: Method?

init {
var method: Method? = null
if (ApplicationInfo.getInstance().build.baselineVersion >= 221) {
try {
method = EditorColorsScheme::class.java.getMethod("getEditorFontSize2D", *arrayOfNulls(0))
} catch (noSuchMethodException: NoSuchMethodException) {
}
}
getEditorFontSize2DMethod = method
}

fun getFont(editor: Editor, text: String): Font {
val font = editor.colorsScheme.getFont(EditorFontType.PLAIN).deriveFont(2)
val fallbackFont = UIUtil.getFontWithFallbackIfNeeded(font, text)
return fallbackFont.deriveFont(fontSize(editor))
}

fun fontSize(editor: Editor): Float {
val scheme = editor.colorsScheme
if (getEditorFontSize2DMethod != null) {
try {
return getEditorFontSize2DMethod.invoke(scheme, *arrayOfNulls(0)) as Float
} catch (_: IllegalAccessException) {
} catch (_: InvocationTargetException) {
}
}
return scheme.editorFontSize.toFloat()
}
}

0 comments on commit fa9f250

Please sign in to comment.