From 16b9e3ebbfd12d71d7adbf42b5e1c8ba47dfc72c Mon Sep 17 00:00:00 2001 From: foolgry <981852406@qq.com> Date: Mon, 16 Mar 2026 16:47:13 +0800 Subject: [PATCH 1/3] feat: add path type selection and line number support - Add PathType enum for choosing between relative and absolute paths - Add line number copying when text is selected in editor - Update settings UI to allow path type selection --- .../idea/copythepath/action/CopyAction.kt | 41 +++++++++++++++---- .../copythepath/setting/SettingComponent.kt | 26 +++++++++--- .../setting/SettingConfigurable.kt | 6 ++- .../idea/copythepath/setting/SettingState.kt | 7 +++- 4 files changed, 64 insertions(+), 16 deletions(-) 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..aafe436 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 + } + + // 获取编辑器中的选中范围并附加行号 + 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? { From ee5e852e7603589f7a2ad34c3f228572166e4888 Mon Sep 17 00:00:00 2001 From: foolgry <981852406@qq.com> Date: Mon, 16 Mar 2026 16:49:19 +0800 Subject: [PATCH 2/3] chore: convert Chinese comment to English --- .java-version | 1 + gradlew | 0 src/main/kotlin/me/xfcy/idea/copythepath/action/CopyAction.kt | 2 +- 3 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 .java-version mode change 100644 => 100755 gradlew 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/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 aafe436..88e30ba 100644 --- a/src/main/kotlin/me/xfcy/idea/copythepath/action/CopyAction.kt +++ b/src/main/kotlin/me/xfcy/idea/copythepath/action/CopyAction.kt @@ -48,7 +48,7 @@ class CopyAction: AnAction() { filePath = prefix + filePath } - // 获取编辑器中的选中范围并附加行号 + // Get editor selection and append line number val editor = CommonDataKeys.EDITOR.getData(e.dataContext) val selectionModel = editor?.selectionModel From 720380a3bf49211d6982edb3096b2e3e3d16602d Mon Sep 17 00:00:00 2001 From: foolgry <981852406@qq.com> Date: Mon, 16 Mar 2026 16:50:28 +0800 Subject: [PATCH 3/3] feat: bump version to 0.0.7 --- build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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()