From d9a8eba35d4552082186c32aa7f0d58ffc9abbe8 Mon Sep 17 00:00:00 2001 From: Cristian Adam Date: Wed, 2 Aug 2023 18:57:36 +0200 Subject: [PATCH] CMakePM: Enable cmake format action only if the tool is present If the tool is not present the menu action is disabled. Also add a link to the Qt Creator cmake-format documentation. Fixes: QTCREATORBUG-29415 Change-Id: I4afb33f0d5ce08975b0964d6bee80dfb41cfcde5 Reviewed-by: Reviewed-by: Eike Ziller --- .../cmakeprojectmanager/cmakeformatter.cpp | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/plugins/cmakeprojectmanager/cmakeformatter.cpp b/src/plugins/cmakeprojectmanager/cmakeformatter.cpp index 9b6f88bfb46..9128adccad1 100644 --- a/src/plugins/cmakeprojectmanager/cmakeformatter.cpp +++ b/src/plugins/cmakeprojectmanager/cmakeformatter.cpp @@ -65,8 +65,15 @@ public: setLayouter([this] { using namespace Layouting; + + auto cmakeFormatter = new QLabel( + Tr::tr("CMakeFormat command:") + .arg("qthelp://org.qt-project.qtcreator/doc/" + "creator-project-cmake.html#formatting-cmake-files")); + cmakeFormatter->setOpenExternalLinks(true); + return Column { - Row { Tr::tr("CMakeFormat command:"), command }, + Row { cmakeFormatter, command }, Space(10), Group { title(Tr::tr("Automatic Formatting on File Save")), @@ -97,7 +104,9 @@ public: auto updateActions = [this] { auto editor = EditorManager::currentEditor(); - formatFile.setEnabled(editor && isApplicable(editor->document())); + + formatFile.setEnabled(haveValidFormatCommand && editor + && isApplicable(editor->document())); }; connect(&autoFormatMime, &Utils::StringAspect::changed, @@ -108,6 +117,15 @@ public: this, &CMakeFormatterSettings::applyIfNecessary); readSettings(); + + const FilePath commandPath = command().searchInPath(); + haveValidFormatCommand = commandPath.exists() && commandPath.isExecutableFile(); + + formatFile.setEnabled(haveValidFormatCommand); + connect(&command, &FilePathAspect::validChanged, this, [this](bool validState) { + haveValidFormatCommand = validState; + formatFile.setEnabled(haveValidFormatCommand); + }); } bool isApplicable(const IDocument *document) const; @@ -125,6 +143,7 @@ public: } FilePathAspect command{this}; + bool haveValidFormatCommand{false}; BoolAspect autoFormatOnSave{this}; BoolAspect autoFormatOnlyCurrentProject{this}; StringAspect autoFormatMime{this};