File tree Expand file tree Collapse file tree 2 files changed +44
-6
lines changed
main/kotlin/cc/unitmesh/devti/prompting/optimizer
test/kotlin/cc/unitmesh/devti/prompting/optimizer Expand file tree Collapse file tree 2 files changed +44
-6
lines changed Original file line number Diff line number Diff line change 1
1
package cc.unitmesh.devti.prompting.optimizer
2
2
3
+ import cc.unitmesh.devti.util.parser.CodeFence
4
+
3
5
object PromptOptimizer {
6
+ fun trimCodeSpace (prompt : String ): String {
7
+ val fences = CodeFence .parseAll(prompt)
8
+ return fences.joinToString(" \n " ) {
9
+ if (it.originLanguage == " python" ) {
10
+ " ```${it.originLanguage} \n ${it.text} \n ```"
11
+ } else {
12
+ trim(it.text)
13
+ }
14
+ }
15
+ }
16
+
4
17
/* *
18
+ * Related to [#317](https://github.com/unit-mesh/auto-dev/issues/317)
19
+ *
5
20
* Similar to the following shell command:
6
21
* ```bash
7
22
* grep -Ev '^[ \t]*$ input.rs | sed 's/^[ \t]*\/\/' | sed 's/[ \t]$//'
8
23
* ```
9
24
*/
10
- fun trimCodeSpace (prompt : String ): String {
11
- // / check language of CodeFence skip for Python
25
+ private fun trim (prompt : String ): String {
12
26
return prompt.lines()
13
27
.filter { it.isNotBlank() }
14
28
.joinToString(" \n " ) { it.trim() }
Original file line number Diff line number Diff line change @@ -140,8 +140,7 @@ impl Plot {
140
140
val result = PromptOptimizer .trimCodeSpace(code)
141
141
142
142
// then
143
- val expected = """
144
- use crate::{find_target, Plot};
143
+ val expected = """ use crate::{find_target, Plot};
145
144
use anyhow::{Context, Result};
146
145
use std::{env, process};
147
146
impl Plot {
@@ -174,7 +173,32 @@ process::Command::new(cargo)
174
173
.context("⚠️ couldn't wait for the afl plot")?;
175
174
Ok(())
176
175
}
177
- }
178
- """
176
+ }"""
177
+
178
+ assertThat(result).isEqualTo(expected)
179
+ }
180
+
181
+ @Test
182
+ fun should_handle_for_python_code_in_issue () {
183
+ @Language(" Markdown" )
184
+ val code = """
185
+ Here is the code:
186
+ ```python
187
+ def foo():
188
+ print("Hello, World!")
189
+ ```
190
+ """ .trimIndent()
191
+
192
+ // when
193
+ val result = PromptOptimizer .trimCodeSpace(code)
194
+
195
+ // then
196
+ val expected = """ Here is the code:
197
+ ```python
198
+ def foo():
199
+ print("Hello, World!")
200
+ ```"""
201
+
202
+ assertThat(result).isEqualTo(expected)
179
203
}
180
204
}
You can’t perform that action at this time.
0 commit comments