|
1 | 1 | package cc.unitmesh.devti.language.compiler.model
|
2 | 2 |
|
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 | +) { |
4 | 9 | companion object {
|
| 10 | + private val regex = Regex("""L(\d+)(?:C(\d+))?(?:-L(\d+)(?:C(\d+))?)?""") |
| 11 | + |
5 | 12 | /**
|
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. |
8 | 18 | */
|
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 |
26 | 21 |
|
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 |
29 | 26 |
|
30 |
| - return LineInfo(startLine, endLine) |
| 27 | + return LineInfo(startLine, endLine, startColumn, endColumn) |
31 | 28 | }
|
32 | 29 | }
|
33 | 30 |
|
|
0 commit comments