Skip to content

Commit 856a066

Browse files
authored
[Amazon Q chat]: Adding TextUtils to core folder for AB testing. (#4351)
Existing plugin is using TextUtils from toolkit folder so Standalone extension not able to access this folder.
1 parent e323647 commit 856a066

File tree

7 files changed

+57
-52
lines changed

7 files changed

+57
-52
lines changed

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/controller/chat/messenger/ChatPromptHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import kotlinx.coroutines.flow.onCompletion
1010
import kotlinx.coroutines.flow.onStart
1111
import software.amazon.awssdk.awscore.exception.AwsServiceException
1212
import software.amazon.awssdk.services.codewhispererstreaming.model.CodeWhispererStreamingException
13+
import software.aws.toolkits.core.utils.convertMarkdownToHTML
1314
import software.aws.toolkits.jetbrains.services.cwc.clients.chat.exceptions.ChatApiException
1415
import software.aws.toolkits.jetbrains.services.cwc.clients.chat.model.ChatRequestData
1516
import software.aws.toolkits.jetbrains.services.cwc.clients.chat.model.ChatResponseEvent
@@ -21,7 +22,6 @@ import software.aws.toolkits.jetbrains.services.cwc.messages.FollowUp
2122
import software.aws.toolkits.jetbrains.services.cwc.messages.RecommendationContentSpan
2223
import software.aws.toolkits.jetbrains.services.cwc.messages.Suggestion
2324
import software.aws.toolkits.jetbrains.services.cwc.storage.ChatSessionInfo
24-
import software.aws.toolkits.jetbrains.utils.convertMarkdownToHTML
2525

2626
class ChatPromptHandler(private val telemetryHelper: TelemetryHelper) {
2727

plugins/toolkit/core/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ dependencies {
2323
// api(libs.aws.ssooidc)
2424
api(libs.aws.sts)
2525
api(libs.bundles.jackson)
26-
26+
implementation(libs.commonmark)
2727
testImplementation(libs.junit4)
2828

2929
testRuntimeOnly(libs.junit5.jupiterVintage)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package software.aws.toolkits.core.utils
5+
6+
import org.commonmark.node.FencedCodeBlock
7+
import org.commonmark.node.Node
8+
import org.commonmark.parser.Parser
9+
import org.commonmark.renderer.NodeRenderer
10+
import org.commonmark.renderer.html.HtmlRenderer
11+
import org.commonmark.renderer.html.HtmlWriter
12+
13+
fun convertMarkdownToHTML(markdown: String): String {
14+
val parser: Parser = Parser.builder().build()
15+
val document: Node = parser.parse(markdown)
16+
val htmlRenderer: HtmlRenderer = HtmlRenderer.builder().nodeRendererFactory { CodeBlockRenderer(it.writer) }.build()
17+
return htmlRenderer.render(document)
18+
}
19+
20+
class CodeBlockRenderer(private val html: HtmlWriter) : NodeRenderer {
21+
override fun getNodeTypes(): Set<Class<out Node>> = setOf(FencedCodeBlock::class.java)
22+
override fun render(node: Node?) {
23+
val codeBlock = node as FencedCodeBlock
24+
val language = codeBlock.info
25+
26+
html.line()
27+
html.tag("div", mapOf("class" to "code-block"))
28+
29+
if (language == "diff") {
30+
codeBlock.literal.lines().forEach {
31+
when {
32+
it.startsWith("-") -> html.tag("div", mapOf("class" to "deletion"))
33+
it.startsWith("+") -> html.tag("div", mapOf("class" to "addition"))
34+
it.startsWith("@@") -> html.tag("div", mapOf("class" to "meta"))
35+
else -> html.tag("div")
36+
}
37+
html.tag("pre")
38+
html.text(it)
39+
html.tag("/pre")
40+
html.tag("/div")
41+
}
42+
} else {
43+
html.tag("pre")
44+
html.tag("code", mapOf("class" to "language-$language"))
45+
html.text(codeBlock.literal)
46+
html.tag("/code")
47+
html.tag("/pre")
48+
}
49+
50+
html.tag("/div")
51+
html.line()
52+
}
53+
}

plugins/toolkit/jetbrains-core/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ dependencies {
139139
implementation(project(":plugin-amazonq:mynah-ui"))
140140
implementation(libs.bundles.jackson)
141141
implementation(libs.zjsonpatch)
142-
implementation(libs.commonmark)
143142
// CodeWhispererTelemetryService uses a CircularFifoQueue, transitive from zjsonpatch
144143
implementation(libs.commons.collections)
145144

plugins/toolkit/jetbrains-core/src/software/aws/toolkits/jetbrains/services/codewhisperer/codescan/listeners/CodeWhispererCodeScanEditorMouseMotionListener.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.intellij.ui.awt.RelativePoint
2121
import com.intellij.ui.components.JBScrollPane
2222
import icons.AwsIcons
2323
import software.amazon.awssdk.services.codewhispererruntime.model.CodeWhispererRuntimeException
24+
import software.aws.toolkits.core.utils.convertMarkdownToHTML
2425
import software.aws.toolkits.core.utils.debug
2526
import software.aws.toolkits.core.utils.error
2627
import software.aws.toolkits.core.utils.getLogger
@@ -33,7 +34,6 @@ import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.CodeWhis
3334
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererColorUtil.getHexString
3435
import software.aws.toolkits.jetbrains.services.codewhisperer.util.runIfIdcConnectionOrTelemetryEnabled
3536
import software.aws.toolkits.jetbrains.utils.applyPatch
36-
import software.aws.toolkits.jetbrains.utils.convertMarkdownToHTML
3737
import software.aws.toolkits.jetbrains.utils.notifyError
3838
import software.aws.toolkits.jetbrains.utils.notifyInfo
3939
import software.aws.toolkits.resources.message

plugins/toolkit/jetbrains-core/src/software/aws/toolkits/jetbrains/utils/TextUtils.kt

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ import com.intellij.openapi.project.Project
1212
import com.intellij.openapi.util.text.StringUtil
1313
import com.intellij.psi.PsiFileFactory
1414
import com.intellij.psi.codeStyle.CodeStyleManager
15-
import org.commonmark.node.FencedCodeBlock
16-
import org.commonmark.node.Node
17-
import org.commonmark.parser.Parser
18-
import org.commonmark.renderer.NodeRenderer
19-
import org.commonmark.renderer.html.HtmlRenderer
20-
import org.commonmark.renderer.html.HtmlWriter
2115

2216
fun formatText(project: Project, language: Language, content: String): String {
2317
var result = content
@@ -31,54 +25,12 @@ fun formatText(project: Project, language: Language, content: String): String {
3125
return result
3226
}
3327

34-
fun convertMarkdownToHTML(markdown: String): String {
35-
val parser: Parser = Parser.builder().build()
36-
val document: Node = parser.parse(markdown)
37-
val htmlRenderer: HtmlRenderer = HtmlRenderer.builder().nodeRendererFactory { CodeBlockRenderer(it.writer) }.build()
38-
return htmlRenderer.render(document)
39-
}
40-
4128
/**
4229
* Designed to convert underscore separated words (e.g. UPDATE_COMPLETE) into title cased human readable text
4330
* (e.g. Update Complete)
4431
*/
4532
fun String.toHumanReadable() = StringUtil.toTitleCase(toLowerCase().replace('_', ' '))
4633

47-
class CodeBlockRenderer(private val html: HtmlWriter) : NodeRenderer {
48-
override fun getNodeTypes(): Set<Class<out Node>> = setOf(FencedCodeBlock::class.java)
49-
override fun render(node: Node?) {
50-
val codeBlock = node as FencedCodeBlock
51-
val language = codeBlock.info
52-
53-
html.line()
54-
html.tag("div", mapOf("class" to "code-block"))
55-
56-
if (language == "diff") {
57-
codeBlock.literal.lines().forEach {
58-
when {
59-
it.startsWith("-") -> html.tag("div", mapOf("class" to "deletion"))
60-
it.startsWith("+") -> html.tag("div", mapOf("class" to "addition"))
61-
it.startsWith("@@") -> html.tag("div", mapOf("class" to "meta"))
62-
else -> html.tag("div")
63-
}
64-
html.tag("pre")
65-
html.text(it)
66-
html.tag("/pre")
67-
html.tag("/div")
68-
}
69-
} else {
70-
html.tag("pre")
71-
html.tag("code", mapOf("class" to "language-$language"))
72-
html.text(codeBlock.literal)
73-
html.tag("/code")
74-
html.tag("/pre")
75-
}
76-
77-
html.tag("/div")
78-
html.line()
79-
}
80-
}
81-
8234
fun generateUnifiedPatch(patch: String, filePath: String): TextFilePatch {
8335
val unifiedPatch = "--- $filePath\n+++ $filePath\n$patch"
8436
val patchReader = PatchReader(unifiedPatch)

plugins/toolkit/jetbrains-core/tst/software/aws/toolkits/jetbrains/utils/TextUtilsTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.assertj.core.api.Assertions.assertThat
1010
import org.intellij.lang.annotations.Language
1111
import org.junit.Rule
1212
import org.junit.Test
13+
import software.aws.toolkits.core.utils.convertMarkdownToHTML
1314

1415
class TextUtilsTest {
1516
@Rule

0 commit comments

Comments
 (0)