Skip to content

Commit

Permalink
feat(devins-language): add LineInfo data class and fromString method #…
Browse files Browse the repository at this point in the history
…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
phodal and [Author Name] committed Mar 16, 2024
1 parent 8c25c78 commit 2798643
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 13 deletions.
@@ -1,26 +1,16 @@
package cc.unitmesh.devti.language.compiler

import cc.unitmesh.devti.language.compiler.data.LineInfo
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.util.TextRange
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.psi.PsiManager

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

override fun execute(): String? {
val range: TextRange? = if (prop.contains("#")) {
val rangeStr = prop.substringAfter("#")
val start = rangeStr.substringBefore("-").toInt()
val end = rangeStr.substringAfter("-").toInt()
TextRange(start, end)
} else {
null
}
val range: LineInfo? = LineInfo.fromString(prop)

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

Expand All @@ -38,7 +28,7 @@ class FileAutoCommand(private val myProject: Project, private val prop: String)
val content = it.toString(Charsets.UTF_8)
val fileContent = if (range != null) {
val subContent = try {
content.substring(range.startOffset, range.endOffset)
content.substring(range.startLine, range.endLine)
} catch (e: StringIndexOutOfBoundsException) {
content
}
Expand All @@ -56,3 +46,4 @@ class FileAutoCommand(private val myProject: Project, private val prop: String)
return output.toString()
}
}

@@ -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)
}
}

}
@@ -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)
}
}

0 comments on commit 2798643

Please sign in to comment.