@@ -21,7 +21,7 @@ import std.format : format;
2121import std.functional : toDelegate;
2222import std.meta : AliasSeq;
2323import std.path : baseName, buildNormalizedPath, buildPath, chainPath, dirName,
24- globMatch, relativePath;
24+ dirSeparator, globMatch, relativePath;
2525import std.string : join;
2626
2727import io = std.stdio ;
@@ -696,17 +696,31 @@ void delayedProjectActivation(WorkspaceD.Instance instance, string workspaceRoot
696696
697697string 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
712726void didLoadDubProject ()
0 commit comments