Skip to content

Commit d4b10b9

Browse files
committed
feat(compiler): limit file content to 300 lines
Add logic to limit file content to 300 lines and provide a message for larger files. Refactor code to extract line splitting and size limiting into separate methods for better readability and maintainability.
1 parent fefc3d9 commit d4b10b9

File tree

1 file changed

+31
-10
lines changed
  • exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/compiler/exec

1 file changed

+31
-10
lines changed

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/compiler/exec/FileInsCommand.kt

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ import com.intellij.psi.PsiManager
2020
* @param myProject the Project in which the file operations are performed
2121
* @param prop the property string containing the file name and optional line range
2222
*
23+
* In JetBrins Junie early version, only return 300 lines of file content with a comments for others.
24+
* In Cursor and Windsurf, will fetch 30 lines and structure the content with a code block.
2325
*/
2426
class FileInsCommand(private val myProject: Project, private val prop: String) : InsCommand {
2527
override val commandName: BuiltinCommand = BuiltinCommand.FILE
28+
private val MAX_LINES = 300
2629

2730
override suspend fun execute(): String? {
2831
val range: LineInfo? = LineInfo.fromString(prop)
@@ -50,16 +53,7 @@ class FileInsCommand(private val myProject: Project, private val prop: String) :
5053

5154
val lang = PsiManager.getInstance(myProject).findFile(virtualFile)?.language?.displayName ?: ""
5255

53-
val fileContent = if (range == null) {
54-
content
55-
} else {
56-
try {
57-
content.split("\n").slice(range.startLine - 1 until range.endLine)
58-
.joinToString("\n")
59-
} catch (e: StringIndexOutOfBoundsException) {
60-
content
61-
}
62-
}
56+
val fileContent = splitLines(range, content)
6357

6458
val realPath = virtualFile.relativePath(myProject)
6559

@@ -70,5 +64,32 @@ class FileInsCommand(private val myProject: Project, private val prop: String) :
7064
output.append("\n```\n")
7165
return output.toString()
7266
}
67+
68+
private fun splitLines(range: LineInfo?, content: String): String {
69+
val lines = content.lines()
70+
val currentSize = lines.size
71+
return if (range == null) {
72+
limitMaxSize(currentSize, content)
73+
} else {
74+
try {
75+
lines.slice(range.startLine - 1 until range.endLine)
76+
.joinToString("\n")
77+
} catch (e: StringIndexOutOfBoundsException) {
78+
limitMaxSize(currentSize, content)
79+
}
80+
}
81+
}
82+
83+
private fun limitMaxSize(size: Int, content: String): String {
84+
return if (size > MAX_LINES) {
85+
val code = content.split("\n")
86+
.slice(0 until MAX_LINES)
87+
.joinToString("\n")
88+
89+
"File too long, only show first $MAX_LINES lines.\n$code\nUse `filename#L300-L600` to get more lines."
90+
} else {
91+
content
92+
}
93+
}
7394
}
7495

0 commit comments

Comments
 (0)