generated from JetBrains/intellij-platform-plugin-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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@domain.com]>
- Loading branch information
Showing
3 changed files
with
94 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/compiler/data/LineInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cc.unitmesh.devti.language.compiler.data | ||
|
||
data class LineInfo(val startLine: Int, val endLine: Int) { | ||
companion object { | ||
/** | ||
* Convert a string to a TextRange, if possible. | ||
* format: "filepath#L1-L12" means from line 1 to line 12 | ||
*/ | ||
fun fromString(string: String): LineInfo? { | ||
val lineRange = string.substringAfter('#').split('-') | ||
if (lineRange.size != 2) { | ||
return null | ||
} | ||
|
||
val start = lineRange[0].substringAfter('L') | ||
val end = lineRange[1].substringAfter('L') | ||
|
||
// use regex to check if the start is a number | ||
if (!start.matches(Regex("\\d+"))) { | ||
return null | ||
} | ||
|
||
if (!end.matches(Regex("\\d+"))) { | ||
return null | ||
} | ||
|
||
val startLine = start.toInt() | ||
val endLine = end.toInt() | ||
|
||
return LineInfo(startLine, endLine) | ||
} | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
exts/devins-lang/src/test/kotlin/cc/unitmesh/devti/language/compiler/data/LineInfoTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package cc.unitmesh.devti.language.compiler.data | ||
|
||
import junit.framework.TestCase.assertEquals | ||
import org.junit.Test | ||
|
||
class LineInfoTest { | ||
|
||
@Test | ||
fun should_createLineInfo_when_validStringGiven() { | ||
// given | ||
val validString = "filepath#L1-L12" | ||
|
||
// when | ||
val result = LineInfo.fromString(validString) | ||
|
||
// then | ||
val expected = LineInfo(1, 12) | ||
assertEquals(expected, result) | ||
} | ||
|
||
@Test | ||
fun should_returnNull_when_invalidStringGiven() { | ||
// given | ||
val invalidString = "wrongStringFormat" | ||
|
||
// when | ||
val result = LineInfo.fromString(invalidString) | ||
|
||
// then | ||
assertEquals(null, result) | ||
} | ||
|
||
@Test | ||
fun should_returnNull_when_invalidStartLineGiven() { | ||
// given | ||
val invalidString = "filepath#Lxyz-L12" | ||
|
||
// when | ||
val result = LineInfo.fromString(invalidString) | ||
|
||
// then | ||
assertEquals(null, result) | ||
} | ||
|
||
@Test | ||
fun should_returnNull_when_invalidEndLineGiven() { | ||
// given | ||
val invalidString = "filepath#L1-Lxyz" | ||
|
||
// when | ||
val result = LineInfo.fromString(invalidString) | ||
|
||
// then | ||
assertEquals(null, result) | ||
} | ||
} |