diff --git a/src/main/kotlin/com/github/qczone/switch2cursor/actions/OpenFileInCursorAction.kt b/src/main/kotlin/com/github/qczone/switch2cursor/actions/OpenFileInCursorAction.kt index bae398d..76fc56b 100644 --- a/src/main/kotlin/com/github/qczone/switch2cursor/actions/OpenFileInCursorAction.kt +++ b/src/main/kotlin/com/github/qczone/switch2cursor/actions/OpenFileInCursorAction.kt @@ -31,7 +31,8 @@ class OpenFileInCursorAction : AnAction() { val settings = AppSettingsState.getInstance() val cursorPath = settings.cursorPath - val command = when { + // Command to open file and position cursor + val fileCommand = when { System.getProperty("os.name").lowercase().contains("mac") -> { arrayOf("open", "-a", "$cursorPath", "cursor://file$filePath:$line:$column") } @@ -43,27 +44,65 @@ class OpenFileInCursorAction : AnAction() { } } - try { - logger.info("Executing command: ${command.joinToString(" ")}") - ProcessBuilder(*command).start() - } catch (ex: Exception) { - logger.error("Failed to execute cursor command: ${ex.message}", ex) - com.intellij.openapi.ui.Messages.showErrorDialog( - project, - """ - ${ex.message} + if (settings.openProjectWithFile) { + // Command to open project + val projectPath = project.basePath ?: return + val projectCommand = when { + System.getProperty("os.name").lowercase().contains("mac") -> { + arrayOf("open", "-a", "$cursorPath", projectPath) + } + System.getProperty("os.name").lowercase().contains("windows") -> { + arrayOf("cmd", "/c", "$cursorPath", projectPath) + } + else -> { + arrayOf(cursorPath, projectPath) + } + } + + try { + logger.info("Executing project command: ${projectCommand.joinToString(" ")}") + ProcessBuilder(*projectCommand).start() + + // Give some time for the project to open, then open the file and position the cursor + Thread.sleep(1000) - Please check: - 1. Cursor path is correctly configured in Settings > Tools > Switch2Cursor - 2. Cursor is properly installed on your system - 3. The configured path points to a valid Cursor executable - """.trimIndent(), - "Error" - ) + // Then open the file and position the cursor + logger.info("Executing file command: ${fileCommand.joinToString(" ")}") + ProcessBuilder(*fileCommand).start() + } catch (ex: Exception) { + logger.error("Failed to execute cursor command: ${ex.message}", ex) + showErrorDialog(project, ex) + return + } + } else { + // Only open the file and position the cursor + try { + logger.info("Executing file command: ${fileCommand.joinToString(" ")}") + ProcessBuilder(*fileCommand).start() + } catch (ex: Exception) { + logger.error("Failed to execute cursor command: ${ex.message}", ex) + showErrorDialog(project, ex) + return + } } WindowUtils.activeWindow() } + + private fun showErrorDialog(project: Project, ex: Exception) { + com.intellij.openapi.ui.Messages.showErrorDialog( + project, + """ + ${ex.message} + + Please check: + 1. Cursor path is correctly configured in Settings > Tools > Switch2Cursor + 2. Cursor is properly installed on your system + 3. The configured path points to a valid Cursor executable + """.trimIndent(), + "Error" + ) + } override fun update(e: AnActionEvent) { val project = e.project diff --git a/src/main/kotlin/com/github/qczone/switch2cursor/settings/AppSettingsConfigurable.kt b/src/main/kotlin/com/github/qczone/switch2cursor/settings/AppSettingsConfigurable.kt index f9612a7..7bd268b 100644 --- a/src/main/kotlin/com/github/qczone/switch2cursor/settings/AppSettingsConfigurable.kt +++ b/src/main/kotlin/com/github/qczone/switch2cursor/settings/AppSettingsConfigurable.kt @@ -4,6 +4,7 @@ import com.intellij.openapi.options.Configurable import javax.swing.JComponent import javax.swing.JPanel import javax.swing.JTextField +import javax.swing.JCheckBox import com.intellij.ui.components.JBLabel import com.intellij.util.ui.FormBuilder @@ -19,17 +20,20 @@ class AppSettingsConfigurable : Configurable { override fun isModified(): Boolean { val settings = AppSettingsState.getInstance() - return mySettingsComponent!!.cursorPath != settings.cursorPath + return mySettingsComponent!!.cursorPath != settings.cursorPath || + mySettingsComponent!!.openProjectWithFile != settings.openProjectWithFile } override fun apply() { val settings = AppSettingsState.getInstance() settings.cursorPath = mySettingsComponent!!.cursorPath + settings.openProjectWithFile = mySettingsComponent!!.openProjectWithFile } override fun reset() { val settings = AppSettingsState.getInstance() mySettingsComponent!!.cursorPath = settings.cursorPath + mySettingsComponent!!.openProjectWithFile = settings.openProjectWithFile } override fun disposeUIResources() { @@ -40,10 +44,12 @@ class AppSettingsConfigurable : Configurable { class AppSettingsComponent { val panel: JPanel private val cursorPathText = JTextField() + private val openProjectWithFileCheckBox = JCheckBox("Also open project when using Option+Shift+O") init { panel = FormBuilder.createFormBuilder() .addLabeledComponent(JBLabel("Cursor Path: "), cursorPathText, 1, false) + .addComponent(openProjectWithFileCheckBox, 1) .addComponentFillVertically(JPanel(), 0) .panel } @@ -53,4 +59,10 @@ class AppSettingsComponent { set(value) { cursorPathText.text = value } + + var openProjectWithFile: Boolean + get() = openProjectWithFileCheckBox.isSelected + set(value) { + openProjectWithFileCheckBox.isSelected = value + } } \ No newline at end of file diff --git a/src/main/kotlin/com/github/qczone/switch2cursor/settings/AppSettingsState.kt b/src/main/kotlin/com/github/qczone/switch2cursor/settings/AppSettingsState.kt index e1fa4d0..5b0b04b 100644 --- a/src/main/kotlin/com/github/qczone/switch2cursor/settings/AppSettingsState.kt +++ b/src/main/kotlin/com/github/qczone/switch2cursor/settings/AppSettingsState.kt @@ -12,6 +12,7 @@ import com.intellij.util.xmlb.XmlSerializerUtil ) class AppSettingsState : PersistentStateComponent { var cursorPath: String = "cursor" + var openProjectWithFile: Boolean = true override fun getState(): AppSettingsState = this