refactor to separate UriUtils and use in Diff.kt, GitService.kt

This commit is contained in:
Nate 2025-05-11 10:45:40 -07:00
parent fbf9b16a2e
commit 8364b2a1a7
6 changed files with 69 additions and 58 deletions

View file

@ -62,7 +62,7 @@ class DiffManager(private val project: Project) : DumbAware {
file.createNewFile()
}
file.writeText(replacement)
openDiffWindow(URI(filepath).toString(), file.toURI().toString(), stepIndex)
openDiffWindow(UriUtils.parseUri(filepath).toString(), file.toURI().toString(), stepIndex)
}
private fun cleanUpFile(file2: String) {
@ -80,7 +80,8 @@ class DiffManager(private val project: Project) : DumbAware {
val diffInfo = diffInfoMap[file] ?: return
// Write contents to original file
val virtualFile = LocalFileSystem.getInstance().findFileByPath(URI(diffInfo.originalFilepath).path) ?: return
val virtualFile =
LocalFileSystem.getInstance().findFileByPath(UriUtils.parseUri(diffInfo.originalFilepath).path) ?: return
val document = FileDocumentManager.getInstance().getDocument(virtualFile) ?: return
WriteCommandAction.runWriteCommandAction(project) {
document.setText(File(file).readText())
@ -119,8 +120,8 @@ class DiffManager(private val project: Project) : DumbAware {
lastFile2 = file2
// Create a DiffContent for each of the texts you want to compare
val content1: DiffContent = DiffContentFactory.getInstance().create(File(URI(file1)).readText())
val content2: DiffContent = DiffContentFactory.getInstance().create(File(URI(file2)).readText())
val content1: DiffContent = DiffContentFactory.getInstance().create(UriUtils.uriToFile(file1).readText())
val content2: DiffContent = DiffContentFactory.getInstance().create(UriUtils.uriToFile(file2).readText())
// Create a SimpleDiffRequest and populate it with the DiffContents and titles
val diffRequest = SimpleDiffRequest("Continue Diff", content1, content2, "Old", "New")

View file

@ -7,9 +7,7 @@ import com.intellij.openapi.project.guessProjectDir
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader
import java.net.URI
class GitService(
private val project: Project,
@ -52,7 +50,7 @@ class GitService(
} else {
ProcessBuilder("git", "diff", "--cached")
}
builder.directory(File(URI(workspaceDir)))
builder.directory(UriUtils.uriToFile(workspaceDir))
val process = withContext(Dispatchers.IO) {
builder.start()
}

View file

@ -109,34 +109,6 @@ class IntelliJIDE(
return true
}
companion object {
// This should probably just be "parseUri". Change that, update all the usages, then distribute and try it on the windows machine
// But honestly just push it to the store to test
fun parseUri(uri: String): URI {
try {
// Remove query parameters if present
val uriStr = uri.substringBefore("?")
// Handle Windows file paths with authority component
if (uriStr.startsWith("file://") && !uriStr.startsWith("file:///")) {
val path = uriStr.substringAfter("file://")
return URI("file:///$path")
}
// Standard URI handling for other cases
val uriWithoutQuery = URI(uriStr)
return uriWithoutQuery
} catch (e: Exception) {
println("Error parsing URI: $uri ${e.message}")
throw Exception("Invalid URI: $uri ${e.message}")
}
}
fun uriToFile(uri: String): File {
return File(parseUri(uri))
}
}
override suspend fun getIdeSettings(): IdeSettings {
val settings = service<ContinueExtensionSettings>()
@ -204,7 +176,7 @@ class IntelliJIDE(
// Find any .continuerc.json files
for (file in contents) {
if (file.endsWith(".continuerc.json")) {
val fileContent = uriToFile(file).readText()
val fileContent = UriUtils.uriToFile(file).readText()
configs.add(fileContent)
}
}
@ -215,12 +187,12 @@ class IntelliJIDE(
}
override suspend fun fileExists(filepath: String): Boolean {
val file = uriToFile(filepath)
val file = UriUtils.uriToFile(filepath)
return file.exists()
}
override suspend fun writeFile(path: String, contents: String) {
val file = uriToFile(path)
val file = UriUtils.uriToFile(path)
file.parentFile?.mkdirs()
file.writeText(contents)
}
@ -238,7 +210,7 @@ class IntelliJIDE(
override suspend fun openFile(path: String) {
// Convert URI path to absolute file path
val filePath = uriToFile(path).absolutePath
val filePath = UriUtils.uriToFile(path).absolutePath
// Find the file using the absolute path
val file = withContext(Dispatchers.IO) {
LocalFileSystem.getInstance().refreshAndFindFileByPath(filePath)
@ -263,7 +235,8 @@ class IntelliJIDE(
override suspend fun saveFile(filepath: String) {
ApplicationManager.getApplication().invokeLater {
val file = LocalFileSystem.getInstance().findFileByPath(parseUri(filepath).path) ?: return@invokeLater
val file =
LocalFileSystem.getInstance().findFileByPath(UriUtils.parseUri(filepath).path) ?: return@invokeLater
val fileDocumentManager = FileDocumentManager.getInstance()
val document = fileDocumentManager.getDocument(file)
@ -276,7 +249,7 @@ class IntelliJIDE(
override suspend fun readFile(filepath: String): String {
return try {
val content = ApplicationManager.getApplication().runReadAction<String?> {
val virtualFile = LocalFileSystem.getInstance().findFileByPath(parseUri(filepath).path)
val virtualFile = LocalFileSystem.getInstance().findFileByPath(UriUtils.parseUri(filepath).path)
if (virtualFile != null && FileDocumentManager.getInstance().isFileModified(virtualFile)) {
return@runReadAction FileDocumentManager.getInstance().getDocument(virtualFile)?.text
}
@ -286,7 +259,7 @@ class IntelliJIDE(
if (content != null) {
content
} else {
val file = uriToFile(filepath)
val file = UriUtils.uriToFile(filepath)
if (!file.exists() || file.isDirectory) return ""
withContext(Dispatchers.IO) {
FileInputStream(file).use { fis ->
@ -539,7 +512,7 @@ class IntelliJIDE(
return withContext(Dispatchers.IO) {
try {
val builder = ProcessBuilder("git", "rev-parse", "--abbrev-ref", "HEAD")
builder.directory(uriToFile(dir))
builder.directory(UriUtils.uriToFile(dir))
val process = builder.start()
val reader = BufferedReader(InputStreamReader(process.inputStream))
val output = reader.readLine()
@ -569,7 +542,7 @@ class IntelliJIDE(
override suspend fun getRepoName(dir: String): String? {
return withContext(Dispatchers.IO) {
val directory = uriToFile(dir)
val directory = UriUtils.uriToFile(dir)
val targetDir = if (directory.isFile) directory.parentFile else directory
val builder = ProcessBuilder("git", "config", "--get", "remote.origin.url")
builder.directory(targetDir)
@ -633,7 +606,7 @@ class IntelliJIDE(
override suspend fun getGitRootPath(dir: String): String? {
return withContext(Dispatchers.IO) {
val builder = ProcessBuilder("git", "rev-parse", "--show-toplevel")
builder.directory(uriToFile(dir))
builder.directory(UriUtils.uriToFile(dir))
val process = builder.start()
val reader = BufferedReader(InputStreamReader(process.inputStream))
@ -644,7 +617,7 @@ class IntelliJIDE(
}
override suspend fun listDir(dir: String): List<List<Any>> {
val files = uriToFile(dir).listFiles()?.map {
val files = UriUtils.uriToFile(dir).listFiles()?.map {
listOf(it.name, if (it.isDirectory) FileType.DIRECTORY.value else FileType.FILE.value)
} ?: emptyList()
@ -653,7 +626,7 @@ class IntelliJIDE(
override suspend fun getFileStats(files: List<String>): Map<String, FileStats> {
return files.associateWith { file ->
FileStats(uriToFile(file).lastModified(), uriToFile(file).length())
FileStats(UriUtils.uriToFile(file).lastModified(), UriUtils.uriToFile(file).length())
}
}
@ -671,7 +644,7 @@ class IntelliJIDE(
}
private fun setFileOpen(filepath: String, open: Boolean = true) {
val file = LocalFileSystem.getInstance().findFileByPath(uriToFile(filepath).path)
val file = LocalFileSystem.getInstance().findFileByPath(UriUtils.uriToFile(filepath).path)
file?.let {
if (open) {

View file

@ -0,0 +1,39 @@
package com.github.continuedev.continueintellijextension.`continue`
import java.io.File
import java.net.URI
/**
* Utility class for URI operations
*/
object UriUtils {
/**
* Parses a URI string into a URI object, handling special cases for Windows file paths
*/
fun parseUri(uri: String): URI {
try {
// Remove query parameters if present
val uriStr = uri.substringBefore("?")
// Handle Windows file paths with authority component
if (uriStr.startsWith("file://") && !uriStr.startsWith("file:///")) {
val path = uriStr.substringAfter("file://")
return URI("file:///$path")
}
// Standard URI handling for other cases
val uriWithoutQuery = URI(uriStr)
return uriWithoutQuery
} catch (e: Exception) {
println("Error parsing URI: $uri ${e.message}")
throw Exception("Invalid URI: $uri ${e.message}")
}
}
/**
* Converts a URI string to a File object
*/
fun uriToFile(uri: String): File {
return File(parseUri(uri))
}
}

View file

@ -1,16 +1,16 @@
package com.github.continuedev.continueintellijextension.unit
import com.github.continuedev.continueintellijextension.`continue`.IntelliJIDE
import com.github.continuedev.continueintellijextension.`continue`.UriUtils
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import java.io.File
import kotlin.test.assertEquals
class IntelliJIDETest {
class UriUtilsTest {
@Test
fun testUriToFile() {
val uri = "file:///path/to/file"
val file = IntelliJIDE.uriToFile(uri)
val file = UriUtils.uriToFile(uri)
assertEquals(File("/path/to/file"), file)
}
@ -18,35 +18,35 @@ class IntelliJIDETest {
@Test
fun testUriToFileWithWindowsPath() {
val uri = "file:///C:/path/to/file"
val file = IntelliJIDE.uriToFile(uri)
val file = UriUtils.uriToFile(uri)
assertEquals(File("C:/path/to/file"), file)
}
@Test
fun shouldHandleAuthorityComponent() {
val uri = "file://C:/path/to/file"
val file = IntelliJIDE.uriToFile(uri)
val file = UriUtils.uriToFile(uri)
assertEquals(File("/C:/path/to/file"), file)
}
@Test
fun testUriToFileWithSpaces() {
val uri = "file:///path/to/file%20with%20spaces"
val file = IntelliJIDE.uriToFile(uri)
val file = UriUtils.uriToFile(uri)
assertEquals(File("/path/to/file with spaces"), file)
}
@Test
fun testUriToFileWithSpecialCharacters() {
val uri = "file:///path/to/file%23with%25special%26chars"
val file = IntelliJIDE.uriToFile(uri)
val file = UriUtils.uriToFile(uri)
assertEquals(File("/path/to/file#with%special&chars"), file)
}
@Test
fun testUriToFileWithQueryParams() {
val uri = "file:///path/to/file?param=value"
val file = IntelliJIDE.uriToFile(uri)
val file = UriUtils.uriToFile(uri)
assertEquals(File("/path/to/file"), file)
}
}

View file

@ -1,12 +1,12 @@
{
"name": "continue",
"version": "1.1.31",
"version": "1.1.33",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "continue",
"version": "1.1.31",
"version": "1.1.33",
"license": "Apache-2.0",
"dependencies": {
"@continuedev/config-types": "^1.0.14",