Skip to content

Commit 3bfd9c0

Browse files
committed
improve CCDB discovery
1 parent 50472d1 commit 3bfd9c0

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

source/served/extension.d

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import std.format : format;
2121
import std.functional : toDelegate;
2222
import std.meta : AliasSeq;
2323
import std.path : baseName, buildNormalizedPath, buildPath, chainPath, dirName,
24-
globMatch, relativePath;
24+
dirSeparator, globMatch, relativePath;
2525
import std.string : join;
2626

2727
import io = std.stdio;
@@ -696,17 +696,31 @@ void delayedProjectActivation(WorkspaceD.Instance instance, string workspaceRoot
696696

697697
string discoverCcdb(string root)
698698
{
699+
import std.algorithm : count, sort;
700+
699701
trace("discovering CCDB in ", root);
700702

701703
if (fs.exists(chainPath(root, "compile_commands.json")))
702704
return buildNormalizedPath(root, "compile_commands.json");
703705

704-
foreach (entry; tryDirEntries(root, "compile_commands.json", fs.SpanMode.breadth))
705-
{
706-
return buildNormalizedPath(entry.name);
707-
}
706+
string[] dbs = tryDirEntries(root, "compile_commands.json", fs.SpanMode.breadth)
707+
.map!(e => buildNormalizedPath(e.name))
708+
.array;
709+
710+
// using in priority:
711+
// - those which have fewer directory depth
712+
// - lexical order
713+
dbs.sort!((a, b) {
714+
const depthA = count(a, dirSeparator);
715+
const depthB = count(b, dirSeparator);
716+
if (depthA != depthB)
717+
return depthA < depthB;
718+
return a < b;
719+
});
720+
721+
tracef("discovered following CCDB:%-(\n - %s%)", dbs);
708722

709-
return null;
723+
return dbs.length ? dbs[0] : null;
710724
}
711725

712726
void didLoadDubProject()

0 commit comments

Comments
 (0)