Skip to content

Commit 14cdf4e

Browse files
committed
fix: convert file paths to workspace-relative paths
1 parent 387a3f0 commit 14cdf4e

1 file changed

Lines changed: 38 additions & 2 deletions

File tree

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import com.intellij.psi.PsiFile
99
import com.jetbrains.php.tools.quality.QualityToolAnnotator
1010
import com.jetbrains.php.tools.quality.QualityToolAnnotatorInfo
1111
import com.jetbrains.php.tools.quality.QualityToolConfiguration
12+
import java.io.File
13+
import java.nio.file.Path
14+
import java.nio.file.Paths
1215

1316
open class MagoAnnotatorProxy : QualityToolAnnotator<MagoValidationInspection>() {
1417
companion object {
@@ -22,7 +25,8 @@ open class MagoAnnotatorProxy : QualityToolAnnotator<MagoValidationInspection>()
2225
addConfig(project, settings)
2326

2427
add("fmt")
25-
addAll(files)
28+
files.map { toWorkspaceRelativePath(project, it) }
29+
.forEach { add(it) }
2630
}
2731
.plus(ParametersList.parse(settings.formatAdditionalParameters))
2832
.apply { println("format options: ${this.joinToString(" ")}") }
@@ -32,13 +36,45 @@ open class MagoAnnotatorProxy : QualityToolAnnotator<MagoValidationInspection>()
3236
addConfig(project, settings)
3337

3438
add("analyze")
35-
add(filePath)
39+
add(toWorkspaceRelativePath(project, filePath))
3640
add("--reporting-format=json")
3741
// filePath?.let { add(it) }
3842
}
3943
.plus(ParametersList.parse(settings.analyzeAdditionalParameters))
4044
.apply { println("analyze options: ${this.joinToString(" ")}") }
4145

46+
private fun toWorkspaceRelativePath(project: Project, rawFilePath: String): String {
47+
val base = project.basePath ?: return ensureMagoPath(rawFilePath)
48+
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)
58+
}
59+
60+
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
75+
}
76+
}
77+
4278
private fun MutableList<String>.addWorkspace(project: Project) {
4379
val projectPath = updateIfRemoteMappingExists(
4480
project.basePath ?: return,

0 commit comments

Comments
 (0)