Skip to content

Commit a7ce747

Browse files
committed
refactor: simplify file path handling
1 parent cf54098 commit a7ce747

1 file changed

Lines changed: 12 additions & 27 deletions

File tree

src/main/kotlin/com/github/xepozz/mago/qualityTool/MagoAnnotatorProxy.kt

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ import com.intellij.codeInspection.InspectionProfile
55
import com.intellij.execution.configurations.ParametersList
66
import com.intellij.openapi.diagnostic.Logger
77
import com.intellij.openapi.project.Project
8+
import com.intellij.openapi.util.io.FileUtil
89
import com.intellij.psi.PsiFile
910
import com.jetbrains.php.tools.quality.QualityToolAnnotator
1011
import com.jetbrains.php.tools.quality.QualityToolAnnotatorInfo
1112
import com.jetbrains.php.tools.quality.QualityToolConfiguration
1213
import java.io.File
13-
import java.nio.file.Path
14-
import java.nio.file.Paths
1514

1615
open class MagoAnnotatorProxy : QualityToolAnnotator<MagoValidationInspection>() {
1716
companion object {
@@ -43,35 +42,21 @@ open class MagoAnnotatorProxy : QualityToolAnnotator<MagoValidationInspection>()
4342
.plus(ParametersList.parse(settings.analyzeAdditionalParameters))
4443
.apply { println("analyze options: ${this.joinToString(" ")}") }
4544

46-
private fun toWorkspaceRelativePath(project: Project, rawFilePath: String): String {
47-
val base = project.basePath ?: return ensureMagoPath(rawFilePath)
45+
private fun toWorkspaceRelativePath(project: Project, absoluteFilePath: String): String {
46+
val base = project.basePath ?: return ensureMagoPath(absoluteFilePath)
47+
println("project base: $base")
4848

49-
val relative = try {
50-
val basePath: Path = Paths.get(base).normalize()
51-
val filePath: Path = Paths.get(rawFilePath).normalize()
52-
53-
basePath.relativize(filePath).toString()
54-
} catch (_: Throwable) {
55-
rawFilePath
56-
}
57-
return ensureMagoPath(relative)
49+
val relative = FileUtil.getRelativePath(base, absoluteFilePath, File.separatorChar)
50+
return ensureMagoPath(relative ?: absoluteFilePath)
5851
}
5952

6053
private fun ensureMagoPath(path: String): String {
61-
if (path.isEmpty()) {
62-
return path
63-
}
64-
return try {
65-
if (Paths.get(path).isAbsolute) {
66-
path
67-
} else if (path.startsWith("./") || path.startsWith(".\\")) {
68-
path
69-
} else {
70-
// Mago ignores relative paths unless they are explicitly prefixed.
71-
".${File.separator}$path"
72-
}
73-
} catch (_: Throwable) {
74-
path
54+
return when {
55+
path.isEmpty() -> path
56+
FileUtil.isAbsolute(path) -> path
57+
path.startsWith("./") || path.startsWith(".\\") -> path
58+
// Mago ignores relative paths unless they are explicitly prefixed.
59+
else -> ".${File.separator}$path"
7560
}
7661
}
7762

0 commit comments

Comments
 (0)