Skip to content
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
1 change: 1 addition & 0 deletions .java-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
17
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "me.xfcy.idea"
version = "0.0.6"
version = "0.0.7"

repositories {
mavenCentral()
Expand Down
Empty file modified gradlew
100644 → 100755
Empty file.
41 changes: 33 additions & 8 deletions src/main/kotlin/me/xfcy/idea/copythepath/action/CopyAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,52 @@ 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

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<PathType>
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"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<SettingState> {
class SettingState(var pathPrefix: String? = "", var pathType: PathType = PathType.RELATIVE): PersistentStateComponent<SettingState> {

companion object {
fun getInstance(project: Project): SettingState? {
Expand Down