Skip to content

Commit 6143d26

Browse files
authored
Merge pull request #2177 from Kila2/feature/sort_prefixmapper
2 parents 69150c2 + c375dc5 commit 6143d26

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Sources/SourceKitLSP/Workspace.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,16 @@ package final class Workspace: Sendable, BuildSystemManagerDelegate {
307307
let prefixMappings =
308308
(indexOptions.indexPrefixMap ?? [:])
309309
.map { PathMapping(original: $0.key, replacement: $0.value) }
310+
.sorted {
311+
// Fixes an issue where remapPath might match the shortest path first when multiple common prefixes exist
312+
// Sort by path length descending to prioritize more specific paths;
313+
// when lengths are equal, sort lexicographically in ascending order
314+
if $0.original.count != $1.original.count {
315+
return $0.original.count > $1.original.count // Prefer longer paths (more specific)
316+
} else {
317+
return $0.original < $1.original // Alphabetical sort when lengths are equal, ensures stable ordering
318+
}
319+
}
310320
if let indexInjector = hooks.indexHooks.indexInjector {
311321
let indexStoreDB = try await indexInjector.createIndex(
312322
storePath: indexStorePath,

0 commit comments

Comments
 (0)