Skip to content

Commit

Permalink
Fixing file path for saving new files in windows
Browse files Browse the repository at this point in the history
  • Loading branch information
arafatkatze committed Jan 2, 2025
1 parent b1dbde1 commit f9d3a2c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,15 @@ class CodyAgentClient(private val project: Project, private val webview: NativeW
var fileName = "Untitled.$ext".removeSuffix(".")
var outputDir: VirtualFile? =
if (params.defaultUri != null) {
val defaultUriPath = Paths.get(params.defaultUri)
fileName = defaultUriPath.fileName.toString()
VfsUtil.findFile(defaultUriPath.parent, true)
try {
val defaultUriPath = Paths.get(params.defaultUri)
fileName = defaultUriPath.fileName.toString()
val parentDir = VfsUtil.findFile(defaultUriPath.parent, true)
parentDir
} catch (e: Exception) {
logger.error("code222: Error processing defaultUri", e)
null
}
} else {
project.guessProjectDir()
}
Expand All @@ -248,9 +254,14 @@ class CodyAgentClient(private val project: Project, private val webview: NativeW

val saveFileFuture = CompletableFuture<String>()
runInEdt {
val dialog = FileChooserFactory.getInstance().createSaveFileDialog(descriptor, project)
val result = dialog.save(outputDir, fileName)
saveFileFuture.complete(result?.file?.path)
try {
val dialog = FileChooserFactory.getInstance().createSaveFileDialog(descriptor, project)
val result = dialog.save(outputDir, fileName)
saveFileFuture.complete(result?.file?.path)
} catch (e: Exception) {
logger.error("code222: Error in save dialog", e)
saveFileFuture.completeExceptionally(e)
}
}

return saveFileFuture
Expand Down
24 changes: 18 additions & 6 deletions jetbrains/src/main/kotlin/com/sourcegraph/utils/CodyEditorUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,26 @@ object CodyEditorUtil {

fun findFileOrScratch(project: Project, uriString: String): VirtualFile? {
try {
val uri = URI.create(uriString)

val normalizedUri = if (uriString.matches(Regex("^[A-Za-z]:.+"))) {
"file:///" + uriString.replace('\\', '/')
} else {
uriString
}
val uri = URI.create(normalizedUri)
if (ConfigUtil.isIntegrationTestModeEnabled()) {
return TempFileSystem.getInstance().refreshAndFindFileByPath(uri.path)
} else {
val fixedUri = if (uriString.startsWith("untitled")) uri.withScheme("file") else uri
return LocalFileSystem.getInstance().refreshAndFindFileByNioFile(fixedUri.toPath())
val fixedUri = if (uriString.startsWith("untitled")) {
uri.withScheme("file")
} else uri
val path = fixedUri.toPath()
return LocalFileSystem.getInstance().refreshAndFindFileByNioFile(path)
}
} catch (e: URISyntaxException) {
// Let's try scratch files
val fileName = uriString.substringAfterLast(':').trimStart('/', '\\')
return ScratchRootType.getInstance()
.findFile(project, fileName, ScratchFileService.Option.existing_only)
.findFile(project, fileName, ScratchFileService.Option.existing_only)
}
}

Expand All @@ -219,7 +226,12 @@ object CodyEditorUtil {
content: String? = null
): VirtualFile? {
try {
val uri = URI.create(uriString)
val normalizedUri = if (uriString.matches(Regex("^[A-Za-z]:.+"))) {
"file:///" + uriString.replace('\\', '/')
} else {
uriString
}
val uri = URI.create(normalizedUri)

val fileUri = uri.withScheme("file")
if (!fileUri.toPath().exists()) {
Expand Down

0 comments on commit f9d3a2c

Please sign in to comment.