diff --git a/.java-version b/.java-version new file mode 100644 index 0000000..98d9bcb --- /dev/null +++ b/.java-version @@ -0,0 +1 @@ +17 diff --git a/build.gradle.kts b/build.gradle.kts index 12ae8dd..f85d4f9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } group = "me.xfcy.idea" -version = "0.0.6" +version = "0.0.7" repositories { mavenCentral() diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 diff --git a/src/main/kotlin/me/xfcy/idea/copythepath/action/CopyAction.kt b/src/main/kotlin/me/xfcy/idea/copythepath/action/CopyAction.kt index 7d73810..88e30ba 100644 --- a/src/main/kotlin/me/xfcy/idea/copythepath/action/CopyAction.kt +++ b/src/main/kotlin/me/xfcy/idea/copythepath/action/CopyAction.kt @@ -14,6 +14,7 @@ package me.xfcy.idea.copythepath.action import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.CommonDataKeys +import me.xfcy.idea.copythepath.setting.PathType import me.xfcy.idea.copythepath.setting.SettingState import java.awt.Toolkit import java.awt.datatransfer.StringSelection @@ -27,18 +28,42 @@ class CopyAction: AnAction() { val state = project?.getService(SettingState::class.java) val prefix = state?.pathPrefix ?: "" + val pathType = state?.pathType ?: PathType.RELATIVE val file = CommonDataKeys.VIRTUAL_FILE.getData(e.dataContext) var filePath = file?.canonicalPath ?: "" - if (filePath == projectPath) { - filePath = prefix - } else if ( - projectPath.isNotBlank() && - filePath.startsWith(projectPath) - ) { - val subStart = projectPath.length + 1 - filePath = prefix + filePath.substring(subStart) + if (pathType == PathType.RELATIVE) { + if (filePath == projectPath) { + filePath = prefix + } else if ( + projectPath.isNotBlank() && + filePath.startsWith(projectPath) + ) { + val subStart = projectPath.length + 1 + filePath = prefix + filePath.substring(subStart) + } + } else { + // ABSOLUTE: use absolute path with prefix + filePath = prefix + filePath + } + + // Get editor selection and append line number + val editor = CommonDataKeys.EDITOR.getData(e.dataContext) + val selectionModel = editor?.selectionModel + + if (selectionModel?.hasSelection() == true) { + val startOffset = selectionModel.selectionStart + val endOffset = selectionModel.selectionEnd + + val startLine = editor.document.getLineNumber(startOffset) + 1 + val endLine = editor.document.getLineNumber(endOffset) + 1 + + filePath += if (startLine == endLine) { + ":$startLine" + } else { + ":$startLine-$endLine" + } } val stringSelection = StringSelection(filePath) diff --git a/src/main/kotlin/me/xfcy/idea/copythepath/setting/SettingComponent.kt b/src/main/kotlin/me/xfcy/idea/copythepath/setting/SettingComponent.kt index b35ec98..82e2579 100644 --- a/src/main/kotlin/me/xfcy/idea/copythepath/setting/SettingComponent.kt +++ b/src/main/kotlin/me/xfcy/idea/copythepath/setting/SettingComponent.kt @@ -15,6 +15,7 @@ import com.intellij.openapi.project.Project import com.intellij.ui.components.JBLabel import com.intellij.ui.components.JBTextField import com.intellij.util.ui.FormBuilder +import javax.swing.JComboBox import javax.swing.JPanel import javax.swing.event.DocumentEvent import javax.swing.event.DocumentListener @@ -22,31 +23,44 @@ import javax.swing.event.DocumentListener class SettingComponent(project: Project) { private var prefix = "" + private var pathType = PathType.RELATIVE private val examplePath = "path/to/your/file.ext" val mainPanel: JPanel val pathPrefixText: JBTextField + val pathTypeCombo: JComboBox val preview: JBLabel init { val state = SettingState.getInstance(project) prefix = state?.pathPrefix ?: "" + pathType = state?.pathType ?: PathType.RELATIVE pathPrefixText = JBTextField(prefix) + pathTypeCombo = JComboBox(PathType.values()) + pathTypeCombo.selectedItem = pathType preview = JBLabel(prefix + examplePath) mainPanel = FormBuilder.createFormBuilder() + .addLabeledComponent(JBLabel("Path type: "), pathTypeCombo, 1, false) .addLabeledComponent(JBLabel("Path prefix: "), pathPrefixText, 1, false) .addLabeledComponent(JBLabel("Preview: "), preview, 1, false) .addComponentFillVertically(JPanel(), 0) .panel pathPrefixText.document.addDocumentListener(object: DocumentListener { - override fun insertUpdate(p0: DocumentEvent?) { changePreview() } - override fun removeUpdate(p0: DocumentEvent?) { changePreview() } - override fun changedUpdate(p0: DocumentEvent?) { changePreview() } - fun changePreview() { - preview.text = pathPrefixText.text + examplePath - } + override fun insertUpdate(p0: DocumentEvent?) { updatePreview() } + override fun removeUpdate(p0: DocumentEvent?) { updatePreview() } + override fun changedUpdate(p0: DocumentEvent?) { updatePreview() } }) + + pathTypeCombo.addActionListener { updatePreview() } + } + + private fun updatePreview() { + val selectedType = pathTypeCombo.selectedItem as PathType + preview.text = when (selectedType) { + PathType.RELATIVE -> pathPrefixText.text + examplePath + PathType.ABSOLUTE -> "/$examplePath" + } } } \ No newline at end of file diff --git a/src/main/kotlin/me/xfcy/idea/copythepath/setting/SettingConfigurable.kt b/src/main/kotlin/me/xfcy/idea/copythepath/setting/SettingConfigurable.kt index 3251442..bbd1181 100644 --- a/src/main/kotlin/me/xfcy/idea/copythepath/setting/SettingConfigurable.kt +++ b/src/main/kotlin/me/xfcy/idea/copythepath/setting/SettingConfigurable.kt @@ -26,12 +26,15 @@ class SettingConfigurable(private val project: Project): Configurable { override fun isModified(): Boolean { val state = SettingState.getInstance(project) val prefix = state?.pathPrefix ?: "" - return (prefix != component.pathPrefixText.text) + val pathType = state?.pathType ?: PathType.RELATIVE + return (prefix != component.pathPrefixText.text) || + (pathType != component.pathTypeCombo.selectedItem) } override fun apply() { val state = SettingState.getInstance(project) state?.pathPrefix = component.pathPrefixText.text + state?.pathType = component.pathTypeCombo.selectedItem as PathType } override fun getDisplayName(): String { @@ -41,5 +44,6 @@ class SettingConfigurable(private val project: Project): Configurable { override fun reset() { val state = SettingState.getInstance(project) component.pathPrefixText.text = state?.pathPrefix + component.pathTypeCombo.selectedItem = state?.pathType ?: PathType.RELATIVE } } \ No newline at end of file diff --git a/src/main/kotlin/me/xfcy/idea/copythepath/setting/SettingState.kt b/src/main/kotlin/me/xfcy/idea/copythepath/setting/SettingState.kt index f80d96e..245d32a 100644 --- a/src/main/kotlin/me/xfcy/idea/copythepath/setting/SettingState.kt +++ b/src/main/kotlin/me/xfcy/idea/copythepath/setting/SettingState.kt @@ -15,11 +15,16 @@ import com.intellij.openapi.components.* import com.intellij.openapi.project.Project import com.intellij.util.xmlb.XmlSerializerUtil +enum class PathType { + RELATIVE, + ABSOLUTE +} + @State( name = "CopyThePathConfig", storages = [Storage(StoragePathMacros.WORKSPACE_FILE)] ) -class SettingState(var pathPrefix: String? = ""): PersistentStateComponent { +class SettingState(var pathPrefix: String? = "", var pathType: PathType = PathType.RELATIVE): PersistentStateComponent { companion object { fun getInstance(project: Project): SettingState? {