@@ -20,9 +20,12 @@ import com.intellij.psi.PsiManager
20
20
* @param myProject the Project in which the file operations are performed
21
21
* @param prop the property string containing the file name and optional line range
22
22
*
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.
23
25
*/
24
26
class FileInsCommand (private val myProject : Project , private val prop : String ) : InsCommand {
25
27
override val commandName: BuiltinCommand = BuiltinCommand .FILE
28
+ private val MAX_LINES = 300
26
29
27
30
override suspend fun execute (): String? {
28
31
val range: LineInfo ? = LineInfo .fromString(prop)
@@ -50,16 +53,7 @@ class FileInsCommand(private val myProject: Project, private val prop: String) :
50
53
51
54
val lang = PsiManager .getInstance(myProject).findFile(virtualFile)?.language?.displayName ? : " "
52
55
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)
63
57
64
58
val realPath = virtualFile.relativePath(myProject)
65
59
@@ -70,5 +64,32 @@ class FileInsCommand(private val myProject: Project, private val prop: String) :
70
64
output.append(" \n ```\n " )
71
65
return output.toString()
72
66
}
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 \n Use `filename#L300-L600` to get more lines."
90
+ } else {
91
+ content
92
+ }
93
+ }
73
94
}
74
95
0 commit comments