Skip to content

Commit 72efaef

Browse files
committed
feat(devins-lang): add support for line info in commands
This commit adds support for line information in commands by introducing a new `LineInfo` class and refactoring the `fromString` method to use a regular expression for parsing input strings. The `LineInfo` class now includes start and end line numbers, as well as start and end column numbers, and the `fromString` method has been updated to handle these additional fields. Additionally, several new tests have been added to ensure that the `fromString` method correctly parses strings in various formats.
1 parent d524095 commit 72efaef

File tree

2 files changed

+63
-24
lines changed
  • exts/devins-lang/src
    • main/kotlin/cc/unitmesh/devti/language/compiler/model
    • test/kotlin/cc/unitmesh/devti/language/compiler/data

2 files changed

+63
-24
lines changed

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

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
11
package cc.unitmesh.devti.language.compiler.model
22

3-
data class LineInfo(val startLine: Int, val endLine: Int) {
3+
data class LineInfo(
4+
val startLine: Int,
5+
val endLine: Int,
6+
val startColumn: Int = 0,
7+
val endColumn: Int = 0
8+
) {
49
companion object {
10+
private val regex = Regex("""L(\d+)(?:C(\d+))?(?:-L(\d+)(?:C(\d+))?)?""")
11+
512
/**
6-
* Convert a string to a TextRange, if possible.
7-
* format: "filepath#L1-L12" means from line 1 to line 12
13+
* Convert a string to a `TextRange`, if possible. The string should be in the format: "filepath#L1-L12",
14+
* where "filepath" is the path to the file, "#" is a hash symbol, "L1-L12" is a range of lines from line 1 to line 12.
15+
*
16+
* @param string The string to convert, in the format "filepath#L1-L12".
17+
* @return A `LineInfo` object representing the range of lines, or `null` if the string is not in the correct format.
818
*/
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-
}
19+
fun fromString(input: String): LineInfo? {
20+
val matchResult = regex.find(input) ?: return null
2621

27-
val startLine = start.toInt()
28-
val endLine = end.toInt()
22+
val startLine = matchResult.groupValues[1].toIntOrNull() ?: return null
23+
val startColumn = matchResult.groupValues[2].toIntOrNull() ?: 0
24+
val endLine = matchResult.groupValues[3].toIntOrNull() ?: return null
25+
val endColumn = matchResult.groupValues[4].toIntOrNull() ?: 0
2926

30-
return LineInfo(startLine, endLine)
27+
return LineInfo(startLine, endLine, startColumn, endColumn)
3128
}
3229
}
3330

exts/devins-lang/src/test/kotlin/cc/unitmesh/devti/language/compiler/data/LineInfoTest.kt

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,46 @@ class LineInfoTest {
5454
// then
5555
assertEquals(null, result)
5656
}
57-
}
57+
58+
// L1C0-L2C0
59+
@Test
60+
fun should_createLineInfo_when_validStringWithColumnGiven() {
61+
// given
62+
val validString = "filepath#L1C0-L2C0"
63+
64+
// when
65+
val result = LineInfo.fromString(validString)
66+
67+
// then
68+
val expected = LineInfo(1, 2, 0, 0)
69+
assertEquals(expected, result)
70+
}
71+
72+
// L1-L2C0
73+
@Test
74+
fun should_createLineInfo_when_validStringWithEndColumnGiven() {
75+
// given
76+
val validString = "filepath#L1-L2C0"
77+
78+
// when
79+
val result = LineInfo.fromString(validString)
80+
81+
// then
82+
val expected = LineInfo(1, 2, 0, 0)
83+
assertEquals(expected, result)
84+
}
85+
86+
// L2C0-L3
87+
@Test
88+
fun should_createLineInfo_when_validStringWithStartColumnGiven() {
89+
// given
90+
val validString = "filepath#L2C0-L3"
91+
92+
// when
93+
val result = LineInfo.fromString(validString)
94+
95+
// then
96+
val expected = LineInfo(2, 3, 0, 0)
97+
assertEquals(expected, result)
98+
}
99+
}

0 commit comments

Comments
 (0)