Skip to content

Commit 2798643

Browse files
phodal[Author Name]
and
[Author Name]
committed
feat(devins-language): add LineInfo data class and fromString method #101
This commit adds the LineInfo data class and the accompanying `fromString` method. The LineInfo class represents the start and end line numbers of a code segment. The `fromString` method is used to parse a string of format "filepath#L1-L12" into a LineInfo object, where "L1" represents the start line and "L12" represents the end line. This addition enables more precise handling of code segments in the DevTi language compiler. Co-authored-by: [Author Name] <[[email protected]]>
1 parent 8c25c78 commit 2798643

File tree

3 files changed

+94
-13
lines changed

3 files changed

+94
-13
lines changed

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
package cc.unitmesh.devti.language.compiler
22

3+
import cc.unitmesh.devti.language.compiler.data.LineInfo
34
import com.intellij.openapi.diagnostic.logger
45
import com.intellij.openapi.project.Project
5-
import com.intellij.openapi.project.guessProjectDir
6-
import com.intellij.openapi.util.TextRange
7-
import com.intellij.openapi.vfs.VirtualFile
8-
import com.intellij.openapi.vfs.VirtualFileManager
96
import com.intellij.psi.PsiManager
107

118
class FileAutoCommand(private val myProject: Project, private val prop: String) : AutoCommand {
129
private val logger = logger<FileAutoCommand>()
1310
private val output = StringBuilder()
1411

1512
override fun execute(): String? {
16-
val range: TextRange? = if (prop.contains("#")) {
17-
val rangeStr = prop.substringAfter("#")
18-
val start = rangeStr.substringBefore("-").toInt()
19-
val end = rangeStr.substringAfter("-").toInt()
20-
TextRange(start, end)
21-
} else {
22-
null
23-
}
13+
val range: LineInfo? = LineInfo.fromString(prop)
2414

2515
val virtualFile = myProject.lookupFile(prop.trim())
2616

@@ -38,7 +28,7 @@ class FileAutoCommand(private val myProject: Project, private val prop: String)
3828
val content = it.toString(Charsets.UTF_8)
3929
val fileContent = if (range != null) {
4030
val subContent = try {
41-
content.substring(range.startOffset, range.endOffset)
31+
content.substring(range.startLine, range.endLine)
4232
} catch (e: StringIndexOutOfBoundsException) {
4333
content
4434
}
@@ -56,3 +46,4 @@ class FileAutoCommand(private val myProject: Project, private val prop: String)
5646
return output.toString()
5747
}
5848
}
49+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cc.unitmesh.devti.language.compiler.data
2+
3+
data class LineInfo(val startLine: Int, val endLine: Int) {
4+
companion object {
5+
/**
6+
* Convert a string to a TextRange, if possible.
7+
* format: "filepath#L1-L12" means from line 1 to line 12
8+
*/
9+
fun fromString(string: String): LineInfo? {
10+
val lineRange = string.substringAfter('#').split('-')
11+
if (lineRange.size != 2) {
12+
return null
13+
}
14+
15+
val start = lineRange[0].substringAfter('L')
16+
val end = lineRange[1].substringAfter('L')
17+
18+
// use regex to check if the start is a number
19+
if (!start.matches(Regex("\\d+"))) {
20+
return null
21+
}
22+
23+
if (!end.matches(Regex("\\d+"))) {
24+
return null
25+
}
26+
27+
val startLine = start.toInt()
28+
val endLine = end.toInt()
29+
30+
return LineInfo(startLine, endLine)
31+
}
32+
}
33+
34+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cc.unitmesh.devti.language.compiler.data
2+
3+
import junit.framework.TestCase.assertEquals
4+
import org.junit.Test
5+
6+
class LineInfoTest {
7+
8+
@Test
9+
fun should_createLineInfo_when_validStringGiven() {
10+
// given
11+
val validString = "filepath#L1-L12"
12+
13+
// when
14+
val result = LineInfo.fromString(validString)
15+
16+
// then
17+
val expected = LineInfo(1, 12)
18+
assertEquals(expected, result)
19+
}
20+
21+
@Test
22+
fun should_returnNull_when_invalidStringGiven() {
23+
// given
24+
val invalidString = "wrongStringFormat"
25+
26+
// when
27+
val result = LineInfo.fromString(invalidString)
28+
29+
// then
30+
assertEquals(null, result)
31+
}
32+
33+
@Test
34+
fun should_returnNull_when_invalidStartLineGiven() {
35+
// given
36+
val invalidString = "filepath#Lxyz-L12"
37+
38+
// when
39+
val result = LineInfo.fromString(invalidString)
40+
41+
// then
42+
assertEquals(null, result)
43+
}
44+
45+
@Test
46+
fun should_returnNull_when_invalidEndLineGiven() {
47+
// given
48+
val invalidString = "filepath#L1-Lxyz"
49+
50+
// when
51+
val result = LineInfo.fromString(invalidString)
52+
53+
// then
54+
assertEquals(null, result)
55+
}
56+
}

0 commit comments

Comments
 (0)