CMakePM: Add F2 jump for CMAKE_SOURCE_DIR and more

This will allow opening CMake files via F2 when using include statements
like: include(${CMAKE_SOURCE_DIR}/cmake/api.cmake)

CMAKE_BINARY_DIR and CMAKE_CURRENT_BINARY_DIR are also supported.

Fixes: QTCREATORBUG-29467
Change-Id: I9adb417d98e8a8fccf6cd3e5038562db02cb28c2
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Cristian Adam
2023-08-22 18:44:59 +02:00
parent 79d5493d36
commit bd36af75eb

View File

@@ -11,6 +11,10 @@
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/coreplugintr.h>
#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/buildsystem.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projecttree.h>
#include <texteditor/textdocument.h>
#include <texteditor/texteditoractionhandler.h>
#include <utils/textutils.h>
@@ -21,6 +25,7 @@
#include <functional>
using namespace Core;
using namespace ProjectExplorer;
using namespace TextEditor;
namespace CMakeProjectManager::Internal {
@@ -184,6 +189,26 @@ void CMakeEditorWidget::findLinkAt(const QTextCursor &cursor,
QDir dir(textDocument()->filePath().toFileInfo().absolutePath());
buffer.replace("${CMAKE_CURRENT_SOURCE_DIR}", dir.path());
buffer.replace("${CMAKE_CURRENT_LIST_DIR}", dir.path());
if (auto project = ProjectTree::currentProject()) {
buffer.replace("${CMAKE_SOURCE_DIR}", project->projectDirectory().path());
if (auto bs = ProjectTree::currentBuildSystem()) {
buffer.replace("${CMAKE_BINARY_DIR}", bs->buildConfiguration()->buildDirectory().path());
// Get the path suffix from current source dir to project source dir and apply it
// for the binary dir
const QString relativePathSuffix = textDocument()
->filePath()
.parentDir()
.relativePathFrom(project->projectDirectory())
.path();
buffer.replace("${CMAKE_CURRENT_BINARY_DIR}",
bs->buildConfiguration()
->buildDirectory()
.pathAppended(relativePathSuffix)
.path());
}
}
// TODO: Resolve more variables
QString fileName = dir.filePath(unescape(buffer));