Git commit 2239a2aa6c20f1268072e5fed76babc911bb03f1 by Christoph Cullmann, on behalf of Eric Armbruster. Committed on 06/11/2022 at 19:52. Pushed by cullmann into branch 'master'.
lspclient: add rootfile pattern to detect rootpath This makes it possible to use file patterns to detect the rootpath. M +33 -17 addons/lspclient/lspclientservermanager.cpp M +2 -1 doc/kate/plugins.docbook https://invent.kde.org/utilities/kate/commit/2239a2aa6c20f1268072e5fed76babc911bb03f1 diff --git a/addons/lspclient/lspclientservermanager.cpp b/addons/lspclient/lspclientservermanager.cpp index 538c40aaa..68d6521e5 100644 --- a/addons/lspclient/lspclientservermanager.cpp +++ b/addons/lspclient/lspclientservermanager.cpp @@ -40,8 +40,8 @@ typedef QMap<QString, QString> QStringMap; Q_DECLARE_METATYPE(QStringMap) -// helper to find a proper root dir for the given document & file name that indicate the root dir -static QString rootForDocumentAndRootIndicationFileName(KTextEditor::Document *document, const QString &rootIndicationFileName) +// helper to find a proper root dir for the given document & file name/pattern that indicates the root dir +static QString findRootForDocument(KTextEditor::Document *document, const QStringList &rootIndicationFileNames, const QStringList &rootIndicationFilePatterns) { // search only feasible if document is local file if (!document->url().isLocalFile()) { @@ -56,7 +56,15 @@ static QString rootForDocumentAndRootIndicationFileName(KTextEditor::Document *d seenDirectories.insert(dir.absolutePath()); // the file that indicates the root dir is there => all fine - if (dir.exists(rootIndicationFileName)) { + for (const auto &fileName : rootIndicationFileNames) { + if (dir.exists(fileName)) { + return dir.absolutePath(); + } + } + + // look for matching file patterns + dir.setNameFilters(rootIndicationFilePatterns); + if (!dir.entryList().isEmpty()) { return dir.absolutePath(); } @@ -70,6 +78,22 @@ static QString rootForDocumentAndRootIndicationFileName(KTextEditor::Document *d return QString(); } +static QStringList indicationDataToStringList(const QJsonValue &indicationData) +{ + if (indicationData.isArray()) { + QStringList indications; + for (auto indication : indicationData.toArray()) { + if (indication.isString()) { + indications << indication.toString(); + } + } + + return indications; + } + + return {}; +}; + #include <memory> // helper guard to handle revision (un)lock @@ -624,20 +648,12 @@ private: * clangd does */ if (!rootpath) { - const auto fileNamesForDetection = serverConfig.value(QStringLiteral("rootIndicationFileNames")); - if (fileNamesForDetection.isArray()) { - // we try each file name alternative in the listed order - // this allows to have preferences - const auto fileNames = fileNamesForDetection.toArray(); - for (auto name : fileNames) { - if (name.isString()) { - auto root = rootForDocumentAndRootIndicationFileName(document, name.toString()); - if (!root.isEmpty()) { - rootpath = root; - break; - } - } - } + const auto fileNamesForDetection = indicationDataToStringList(serverConfig.value(QStringLiteral("rootIndicationFileNames"))); + const auto filePatternsForDetection = indicationDataToStringList(serverConfig.value(QStringLiteral("rootIndicationFilePatterns"))); + + auto root = findRootForDocument(document, fileNamesForDetection, filePatternsForDetection); + if (!root.isEmpty()) { + rootpath = root; } } diff --git a/doc/kate/plugins.docbook b/doc/kate/plugins.docbook index b03998268..ab950b333 100644 --- a/doc/kate/plugins.docbook +++ b/doc/kate/plugins.docbook @@ -2686,7 +2686,8 @@ to the <quote>projectBase</quote> (as determined by the <link linkend="kate-application-plugin-projects">Project plugin</link>) if applicable, or otherwise relative to the document's directory. If not specified and "rootIndicationFileNames" is an array as filenames, then a parent directory of -current document containing such a file is selected. As a last fallback, the +current document containing such a file is selected. Alternatively, if "root" is not specified and +"rootIndicationFilePatterns" is an array of file patterns, then a parent directory of the current document matching the file pattern is selected. As a last fallback, the home directory is selected as "root". For any document, the resulting "root" then determines whether or not a separate instance is needed. If so, the "root" is passed as rootUri/rootPath. </para>
