-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from marcelkliemannel/develop
4.1.0
- Loading branch information
Showing
15 changed files
with
854 additions
and
470 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...dev/turingcomplete/intellijbytecodeplugin/openclassfiles/_internal/ClassFileCandidates.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package dev.turingcomplete.intellijbytecodeplugin.openclassfiles._internal | ||
|
||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.project.guessProjectDir | ||
import dev.turingcomplete.intellijbytecodeplugin.common._internal.joinAsNaturalLanguage | ||
import org.jsoup.internal.StringUtil.StringJoiner | ||
import java.nio.file.Path | ||
import kotlin.io.path.relativeToOrNull | ||
|
||
sealed class ClassFileCandidates private constructor(val primaryPath: Path, private val fallbackPaths: List<Path> = emptyList()) { | ||
// -- Properties -------------------------------------------------------------------------------------------------- // | ||
// -- Initialization ---------------------------------------------------------------------------------------------- // | ||
// -- Exported Methods -------------------------------------------------------------------------------------------- // | ||
|
||
fun allPaths() = listOf(primaryPath) + fallbackPaths | ||
|
||
fun formatNotFoundError(postfix: String = "cannot be found.", project: Project? = null): String = | ||
StringJoiner(" ").apply { | ||
add("Class file") | ||
add("'${formatPath(primaryPath, project)}'") | ||
if (fallbackPaths.isNotEmpty()) { | ||
add("or possible fallback class file${if (fallbackPaths.size >= 2) "s" else ""}") | ||
add(fallbackPaths.joinAsNaturalLanguage { "'${formatPath(it, project)}'" }) | ||
} | ||
add(postfix) | ||
}.complete() | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is ClassFileCandidates) return false | ||
|
||
if (primaryPath != other.primaryPath) return false | ||
if (fallbackPaths != other.fallbackPaths) return false | ||
|
||
return true | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = primaryPath.hashCode() | ||
result = 31 * result + fallbackPaths.hashCode() | ||
return result | ||
} | ||
|
||
override fun toString(): String { | ||
return "ClassFileCandidates(primaryPath=$primaryPath, fallbackPaths=$fallbackPaths)" | ||
} | ||
|
||
// -- Private Methods --------------------------------------------------------------------------------------------- // | ||
|
||
private fun formatPath(path: Path, project: Project?): String { | ||
val projectDir = project?.guessProjectDir()?.toNioPath() | ||
return if (projectDir != null) { | ||
path.relativeToOrNull(projectDir)?.toString() ?: path.toString() | ||
} | ||
else { | ||
path.toString() | ||
} | ||
} | ||
|
||
// -- Inner Type -------------------------------------------------------------------------------------------------- // | ||
|
||
class RelativeClassFileCandidates(primaryPath: Path, fallbackPaths: List<Path>) | ||
: ClassFileCandidates(primaryPath, fallbackPaths) | ||
|
||
// -- Inner Type -------------------------------------------------------------------------------------------------- // | ||
|
||
class AbsoluteClassFileCandidates(primaryPath: Path, fallbackPaths: List<Path>) | ||
: ClassFileCandidates(primaryPath, fallbackPaths) | ||
|
||
// -- Companion Object -------------------------------------------------------------------------------------------- // | ||
|
||
companion object { | ||
|
||
fun fromRelativePaths(vararg paths: Path): RelativeClassFileCandidates { | ||
assert(paths.all { !it.isAbsolute }) | ||
assert(paths.isNotEmpty()) | ||
|
||
val fallbackPaths = if (paths.size > 1) paths.sliceArray(1 until paths.size) else emptyArray() | ||
return RelativeClassFileCandidates(primaryPath = paths.first(), fallbackPaths = fallbackPaths.toList()) | ||
} | ||
|
||
fun fromAbsolutePaths(vararg paths: Path): AbsoluteClassFileCandidates { | ||
assert(paths.all { it.isAbsolute }) | ||
assert(paths.isNotEmpty()) | ||
|
||
val fallbackPaths = if (paths.size > 1) paths.sliceArray(1 until paths.size) else emptyArray() | ||
return AbsoluteClassFileCandidates(primaryPath = paths.first(), fallbackPaths = fallbackPaths.toList()) | ||
} | ||
} | ||
} |
Oops, something went wrong.