Skip to content
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

🔥 Add support for multiline selection #19

Merged
merged 2 commits into from
May 11, 2024
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

## [Unreleased]

### Added

- Add support for generating multiline selection links for GitHub and Gitlab

## [1.0.7]

### Changed

- Merge changes from the 1.9.0 version of the IntelliJ plugin template
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pluginGroup = com.github.lunakoly.quicklink
pluginName = Quick Link
pluginRepositoryUrl = https://github.com/lunakoly/QuickLink
# SemVer format -> https://semver.org
pluginVersion = 1.0.7
pluginVersion = 1.0.8

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 211
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import com.intellij.openapi.vfs.VfsUtilCore
import org.lunakoly.quicklink.repository.getRepositoryInfo
import org.lunakoly.quicklink.ui.showClickableListIfNeeded
import org.lunakoly.quicklink.ui.toast
import org.lunakoly.quicklink.urlbuilder.LineOffset
import org.lunakoly.quicklink.urlbuilder.Selection
import org.lunakoly.quicklink.urlbuilder.UrlBuilders
import org.lunakoly.quicklink.utils.PopupException
import org.lunakoly.quicklink.utils.catchingPopupExceptions
Expand Down Expand Up @@ -50,7 +52,19 @@ class CopyLineLinkAction : DumbAwareAction() {
val currentFile = event.dataContext.getData(CommonDataKeys.VIRTUAL_FILE)?.canonicalFile
?: throw NoActiveFileException()

val lineNumber = 1 + editor.document.getLineNumber(editor.caretModel.offset)
val selection = if (editor.selectionModel.hasSelection()) {
val startLine = 1 + editor.selectionModel.selectionStartPosition!!.getLine()
val endLine = 1 + editor.selectionModel.selectionEndPosition!!.getLine()
val startColumn = 1 + editor.selectionModel.selectionStartPosition!!.getColumn()
val endColumn = 1 + editor.selectionModel.selectionEndPosition!!.getColumn()
val startOffset = LineOffset(startLine, startColumn)
val endOffset = LineOffset(endLine, endColumn)
Selection.MultilineSelection(startOffset, endOffset)
} else {
val lineOffset = LineOffset(1 + editor.document.getLineNumber(editor.caretModel.offset), 0)
Selection.SingleLinkSelection(lineOffset)
}
lunakoly marked this conversation as resolved.
Show resolved Hide resolved

val repositoryInfo = getRepositoryInfo(project, currentFile)

val filePath = VfsUtilCore
Expand All @@ -73,7 +87,7 @@ class CopyLineLinkAction : DumbAwareAction() {
?: return@showClickableListIfNeeded

val urlBuilder = UrlBuilders.fromDomain(remoteLink.toDomain())
val url = urlBuilder.buildUrl(remoteLink, repositoryInfo, filePath, lineNumber)
val url = urlBuilder.buildUrl(remoteLink, repositoryInfo, filePath, selection)

CopyPasteManager.getInstance().setContents(StringSelection(url))
project.toast("Line link copied: $url")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ interface UrlBuilder {
remoteLink: String,
repositoryInfo: RepositoryInfo,
filePath: String,
lineNumber: Int,
selection: Selection,
): String
}

data class LineOffset(val lineNumber: Int, val columnNumber: Int)

sealed class Selection {
data class MultilineSelection(val start: LineOffset, val end: LineOffset) : Selection()
data class SingleLinkSelection(val lineOffset: LineOffset) : Selection()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.lunakoly.quicklink.urlbuilder.implementations

import org.lunakoly.quicklink.repository.RepositoryInfo
import org.lunakoly.quicklink.urlbuilder.Selection
import org.lunakoly.quicklink.urlbuilder.UrlBuilder
import org.lunakoly.quicklink.utils.cleanSSHUrl
import org.lunakoly.quicklink.utils.removeProtocol
Expand All @@ -13,7 +14,7 @@ class GithubUrlBuilder : UrlBuilder {
remoteLink: String,
repositoryInfo: RepositoryInfo,
filePath: String,
lineNumber: Int,
selection: Selection,
): String {
val importantPart = remoteLink
.removeProtocol()
Expand All @@ -34,7 +35,20 @@ class GithubUrlBuilder : UrlBuilder {
append('/')
append(filePath)
append("#L")
append(lineNumber)
when (selection) {
is Selection.MultilineSelection -> {
append(selection.start.lineNumber)
append("C")
append(selection.start.columnNumber)
append("-L")
append(selection.end.lineNumber)
append("C")
append(selection.end.columnNumber)
}
is Selection.SingleLinkSelection -> {
append(selection.lineOffset.lineNumber)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.lunakoly.quicklink.urlbuilder.implementations

import org.lunakoly.quicklink.repository.RepositoryInfo
import org.lunakoly.quicklink.urlbuilder.Selection
import org.lunakoly.quicklink.urlbuilder.UrlBuilder
import org.lunakoly.quicklink.utils.cleanSSHUrl
import org.lunakoly.quicklink.utils.removeProtocol
Expand All @@ -13,7 +14,7 @@ class GitlabUrlBuilder : UrlBuilder {
remoteLink: String,
repositoryInfo: RepositoryInfo,
filePath: String,
lineNumber: Int
selection: Selection,
): String {
val importantPart = remoteLink
.removeProtocol()
Expand All @@ -34,7 +35,17 @@ class GitlabUrlBuilder : UrlBuilder {
append('/')
append(filePath)
append("#L")
append(lineNumber)
when (selection) {
is Selection.MultilineSelection -> {
// Gitlab currently doesn't support column selection
append(selection.start.lineNumber)
append("-L")
append(selection.end.lineNumber)
}
is Selection.SingleLinkSelection -> {
append(selection.lineOffset.lineNumber)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.lunakoly.quicklink.urlbuilder.implementations

import org.lunakoly.quicklink.repository.RepositoryInfo
import org.lunakoly.quicklink.urlbuilder.Selection
import org.lunakoly.quicklink.urlbuilder.UrlBuilder
import org.lunakoly.quicklink.utils.cleanSSHUrl
import org.lunakoly.quicklink.utils.removeProtocol
Expand All @@ -13,7 +14,7 @@ class SpaceUrlBuilder : UrlBuilder {
remoteLink: String,
repositoryInfo: RepositoryInfo,
filePath: String,
lineNumber: Int,
selection: Selection,
): String {
val importantPart = remoteLink
.removeProtocol()
Expand All @@ -34,8 +35,18 @@ class SpaceUrlBuilder : UrlBuilder {
append('/')
append(filePath)
append("?tab=source&line=")
append(lineNumber)
append("&lines-count=1")
when (selection) {
is Selection.MultilineSelection -> {
append(selection.start.lineNumber)
append("&lines-count=")
append(selection.end.lineNumber - selection.start.lineNumber)
}
is Selection.SingleLinkSelection -> {
append(selection.lineOffset.lineNumber)
append("&lines-count=")
append("1")
}
}
}
}
}
Loading