From 3bfd9c0507e800f3c7dea5ae476b6271de4a70f6 Mon Sep 17 00:00:00 2001 From: Remi Thebault Date: Sat, 19 Nov 2022 15:45:46 +0100 Subject: [PATCH] improve CCDB discovery --- source/served/extension.d | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/source/served/extension.d b/source/served/extension.d index c85fcd42..f627abe3 100644 --- a/source/served/extension.d +++ b/source/served/extension.d @@ -21,7 +21,7 @@ import std.format : format; import std.functional : toDelegate; import std.meta : AliasSeq; import std.path : baseName, buildNormalizedPath, buildPath, chainPath, dirName, - globMatch, relativePath; + dirSeparator, globMatch, relativePath; import std.string : join; import io = std.stdio; @@ -696,17 +696,31 @@ void delayedProjectActivation(WorkspaceD.Instance instance, string workspaceRoot string discoverCcdb(string root) { + import std.algorithm : count, sort; + trace("discovering CCDB in ", root); if (fs.exists(chainPath(root, "compile_commands.json"))) return buildNormalizedPath(root, "compile_commands.json"); - foreach (entry; tryDirEntries(root, "compile_commands.json", fs.SpanMode.breadth)) - { - return buildNormalizedPath(entry.name); - } + string[] dbs = tryDirEntries(root, "compile_commands.json", fs.SpanMode.breadth) + .map!(e => buildNormalizedPath(e.name)) + .array; + + // using in priority: + // - those which have fewer directory depth + // - lexical order + dbs.sort!((a, b) { + const depthA = count(a, dirSeparator); + const depthB = count(b, dirSeparator); + if (depthA != depthB) + return depthA < depthB; + return a < b; + }); + + tracef("discovered following CCDB:%-(\n - %s%)", dbs); - return null; + return dbs.length ? dbs[0] : null; } void didLoadDubProject()