Skip to content

feat: Add option to open project with file #22

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
Show file tree
Hide file tree
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 @@ -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")
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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() {
Expand All @@ -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
}
Expand All @@ -53,4 +59,10 @@ class AppSettingsComponent {
set(value) {
cursorPathText.text = value
}

var openProjectWithFile: Boolean
get() = openProjectWithFileCheckBox.isSelected
set(value) {
openProjectWithFileCheckBox.isSelected = value
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.intellij.util.xmlb.XmlSerializerUtil
)
class AppSettingsState : PersistentStateComponent<AppSettingsState> {
var cursorPath: String = "cursor"
var openProjectWithFile: Boolean = true

override fun getState(): AppSettingsState = this

Expand Down