diff --git a/jetbrains/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt b/jetbrains/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt index b06d7db54842..6775f4dcb974 100644 --- a/jetbrains/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt +++ b/jetbrains/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt @@ -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() } @@ -248,9 +254,14 @@ class CodyAgentClient(private val project: Project, private val webview: NativeW val saveFileFuture = CompletableFuture() 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 diff --git a/jetbrains/src/main/kotlin/com/sourcegraph/utils/CodyEditorUtil.kt b/jetbrains/src/main/kotlin/com/sourcegraph/utils/CodyEditorUtil.kt index 0ae1f2fd1b73..e2cedaced342 100644 --- a/jetbrains/src/main/kotlin/com/sourcegraph/utils/CodyEditorUtil.kt +++ b/jetbrains/src/main/kotlin/com/sourcegraph/utils/CodyEditorUtil.kt @@ -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) } } @@ -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()) {