Skip to content

Commit deb11c0

Browse files
committed
refactor(devins-lang): improve file content slicing and error handling #344
- Replace try-catch with minOf for safer and more efficient line slicing - Update error message to provide more accurate line range for file preview
1 parent 568a99b commit deb11c0

File tree

1 file changed

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

1 file changed

+6
-8
lines changed

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

+6-8
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,9 @@ class FileInsCommand(private val myProject: Project, private val prop: String) :
7474
return if (range == null) {
7575
limitMaxSize(currentSize, content)
7676
} else {
77-
try {
78-
lines.slice(range.startLine - 1 until range.endLine)
79-
.joinToString("\n")
80-
} catch (e: StringIndexOutOfBoundsException) {
81-
limitMaxSize(currentSize, content)
82-
}
77+
val endLine = minOf(range.endLine, currentSize)
78+
lines.slice(range.startLine - 1 until endLine)
79+
.joinToString("\n")
8380
}
8481
}
8582

@@ -88,8 +85,9 @@ class FileInsCommand(private val myProject: Project, private val prop: String) :
8885
val code = content.split("\n")
8986
.slice(0 until MAX_LINES)
9087
.joinToString("\n")
91-
92-
"File too long, only show first $MAX_LINES lines.\n$code\nUse `filename#L300-L600` to get more lines."
88+
89+
val availableEndLine = minOf(size, MAX_LINES * 2)
90+
"File too long, only show first $MAX_LINES lines.\n$code\nUse `filename#L${MAX_LINES}-L${availableEndLine}` to get more lines."
9391
} else {
9492
content
9593
}

0 commit comments

Comments
 (0)