Skip to content

fix intellij extension create file failed #6000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.continuedev.continueintellijextension.editor

import com.github.continuedev.continueintellijextension.utils.toUriOrNull
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.SelectionModel
Expand All @@ -14,6 +15,8 @@ import com.intellij.openapi.util.TextRange
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.ui.JBColor
import com.intellij.openapi.editor.ScrollType
import com.intellij.openapi.vfs.LocalFileSystem
import java.io.File

/**
* Utility class for working with Editor instances.
Expand Down Expand Up @@ -155,10 +158,23 @@ class EditorUtils(val editor: Editor) {
*/
fun getOrOpenEditor(project: Project, filepath: String?): EditorUtils? {
if (!filepath.isNullOrEmpty()) {
val virtualFile = VirtualFileManager.getInstance().findFileByUrl(filepath)
var virtualFile = VirtualFileManager.getInstance().findFileByUrl(filepath)
if (virtualFile == null) {
// file not exists and create
val ioFile = File(filepath.replace("file:///", ""))
if (!ioFile.exists()) {
ioFile.parentFile?.mkdirs()
ioFile.createNewFile()
}
// IntelliJ refresh VirtualFile
virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(ioFile)
}

if (virtualFile != null) {
ApplicationManager.getApplication().invokeAndWait {
FileEditorManager.getInstance(project).openFile(virtualFile, true).first()
runWriteAction {
FileEditorManager.getInstance(project).openFile(virtualFile, true)
}
}
}
}
Expand Down
Loading