|
1 | 1 | package cc.unitmesh.devti.intentions.action.task
|
2 | 2 |
|
| 3 | +import cc.unitmesh.devti.settings.AutoDevSettingsState |
3 | 4 | import com.intellij.temporary.similar.chunks.SimilarChunksWithPaths
|
4 | 5 | import com.intellij.lang.LanguageCommenters
|
5 | 6 |
|
@@ -28,20 +29,34 @@ class SimilarCodeCompletionTask(private val request: CodeCompletionRequest) : Ba
|
28 | 29 | private val commenter = LanguageCommenters.INSTANCE.forLanguage(request.element!!.language)
|
29 | 30 | private val commentPrefix = commenter?.lineCommentPrefix
|
30 | 31 |
|
| 32 | + val start = "code complete for given code, just return rest part of code. \n" |
| 33 | + val end = "\nreturn rest code:" |
31 | 34 | override fun promptText(): String {
|
32 | 35 | val documentLength = request.editor.document.textLength
|
| 36 | + val prefix = generatePrefix(documentLength) |
| 37 | + |
| 38 | + return if (chunksString == null) { |
| 39 | + "$start$prefix\n$end" |
| 40 | + } else { |
| 41 | + "$start: \n$commentPrefix\n$chunksString\n$prefix\n$end" |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private fun generatePrefix(documentLength: Int): String { |
33 | 46 | val prefix = if (request.offset > documentLength) {
|
34 | 47 | request.prefixText
|
35 | 48 | } else {
|
36 |
| - val text = request.editor.document.text |
37 |
| - text.substring(0, request.offset) |
| 49 | + request.editor.document.text.substring(0, request.offset) |
38 | 50 | }
|
39 | 51 |
|
40 |
| - return if (chunksString == null) { |
41 |
| - "code complete for given code, just return rest part of code. \n$prefix\n\nreturn rest code:" |
42 |
| - } else { |
43 |
| - "complete code for given code, just return rest part of code.: \n$commentPrefix\n$chunksString\n$prefix\n\nreturn rest code:" |
| 52 | + val prefixMaxLength = |
| 53 | + AutoDevSettingsState.maxTokenLength - start.length - (commentPrefix?.length ?: 0) - (chunksString?.length |
| 54 | + ?: 0) |
| 55 | + if (prefix.length >= prefixMaxLength){ |
| 56 | + return prefix.substring(prefix.length - prefixMaxLength) |
44 | 57 | }
|
| 58 | + |
| 59 | + return prefix |
45 | 60 | }
|
46 | 61 | }
|
47 | 62 |
|
0 commit comments