diff --git a/src/plugins/cmakeprojectmanager/cmakebuildsystem.cpp b/src/plugins/cmakeprojectmanager/cmakebuildsystem.cpp index b5630faf28c..b05fbbe9174 100644 --- a/src/plugins/cmakeprojectmanager/cmakebuildsystem.cpp +++ b/src/plugins/cmakeprojectmanager/cmakebuildsystem.cpp @@ -1310,7 +1310,7 @@ void CMakeBuildSystem::setupCMakeSymbolsHash() // Handle project targets, unfortunately the CMake file-api doesn't deliver the // column of the target, just the line. Make sure to find it out - QHash projectTargetsSourceAndLine; + QHash> projectTargetsSourceAndLine; for (const auto &target : std::as_const(buildTargets())) { if (target.targetType == TargetType::UtilityType) continue; @@ -1318,11 +1318,11 @@ void CMakeBuildSystem::setupCMakeSymbolsHash() continue; projectTargetsSourceAndLine.insert(target.backtrace.last().path, - target.backtrace.last().line); + {target.backtrace.last().line, target.title}); } auto handleProjectTargets = [&](const CMakeFileInfo &cmakeFile, const cmListFileFunction &func) { - if (!projectTargetsSourceAndLine.contains(cmakeFile.path) - || projectTargetsSourceAndLine.value(cmakeFile.path) != func.Line()) + const auto it = projectTargetsSourceAndLine.find(cmakeFile.path); + if (it == projectTargetsSourceAndLine.end() || it->first != func.Line()) return; if (func.Arguments().size() == 0) @@ -1333,7 +1333,7 @@ void CMakeBuildSystem::setupCMakeSymbolsHash() link.targetFilePath = cmakeFile.path; link.targetLine = arg.Line; link.targetColumn = arg.Column - 1; - m_cmakeSymbolsHash.insert(QString::fromUtf8(arg.Value), link); + m_cmakeSymbolsHash.insert(it->second, link); }; // Gather the exported variables for the Find CMake packages diff --git a/src/plugins/cmakeprojectmanager/cmakeeditor.cpp b/src/plugins/cmakeprojectmanager/cmakeeditor.cpp index a821648f24b..85822697d2e 100644 --- a/src/plugins/cmakeprojectmanager/cmakeeditor.cpp +++ b/src/plugins/cmakeprojectmanager/cmakeeditor.cpp @@ -160,7 +160,7 @@ static bool isValidIdentifierChar(const QChar &chr) return chr.isLetterOrNumber() || chr == '_' || chr == '-'; } -QHash getLocalSymbolsHash(const QByteArray &content, const Utils::FilePath &filePath) +QHash getLocalSymbolsHash(const QByteArray &content, const Utils::FilePath &filePath, QString &projectName) { cmListFile cmakeListFile; if (!content.isEmpty()) { @@ -172,6 +172,11 @@ QHash getLocalSymbolsHash(const QByteArray &content, const QHash hash; for (const auto &func : cmakeListFile.Functions) { + if (func.LowerCaseName() == "project" && func.Arguments().size() > 0) { + projectName = QString::fromUtf8(func.Arguments()[0].Value); + continue; + } + if (func.LowerCaseName() != "function" && func.LowerCaseName() != "macro" && func.LowerCaseName() != "set" && func.LowerCaseName() != "option") continue; @@ -310,6 +315,14 @@ void CMakeEditorWidget::findLinkAt(const QTextCursor &cursor, const int funcStart = findFunctionStart(); const int funcEnd = findFunctionEnd(); + // Resolve local variables and functions + QString projectName; + auto hash = getLocalSymbolsHash(textDocument()->textAt(0, funcEnd + 1).toUtf8(), + textDocument()->filePath(), + projectName); + if (!projectName.isEmpty()) + buffer.replace("${PROJECT_NAME}", projectName); + if (auto project = ProjectTree::currentProject()) { buffer.replace("${CMAKE_SOURCE_DIR}", project->projectDirectory().path()); if (auto bs = ProjectTree::currentBuildSystem(); bs->buildConfiguration()) { @@ -366,10 +379,6 @@ void CMakeEditorWidget::findLinkAt(const QTextCursor &cursor, } // TODO: Resolve more variables - // Resolve local variables and functions - auto hash = getLocalSymbolsHash(textDocument()->textAt(0, funcEnd + 1).toUtf8(), - textDocument()->filePath()); - // Strip variable coating if (buffer.startsWith("${") && buffer.endsWith("}")) buffer = buffer.mid(2, buffer.size() - 3); diff --git a/tests/manual/cmakeprojectmanager/completion/CMakeLists.txt b/tests/manual/cmakeprojectmanager/completion/CMakeLists.txt index a3b5ddf4efb..ae03231e90d 100644 --- a/tests/manual/cmakeprojectmanager/completion/CMakeLists.txt +++ b/tests/manual/cmakeprojectmanager/completion/CMakeLists.txt @@ -61,7 +61,7 @@ endif() # and F1 should open the help page message(STATUS $ENV{CXX}) -qt_add_executable(completion ${SOURCE_FILES}) +qt_add_executable(${PROJECT_NAME} ${SOURCE_FILES}) # here code completino on "target_link_libraries(comp|" should poupup "completion" # with a hammer icon, which suggests a project target @@ -76,7 +76,8 @@ qt_add_executable(completion ${SOURCE_FILES}) # F2 on "completion" would jump above to qt_add_executable target_link_libraries(completion PRIVATE Qt6::Widgets) -set_target_properties(completion PROPERTIES +# F2 on ${PROJECT_NAME} would jump above to qt_add_executable +set_target_properties(${PROJECT_NAME} PROPERTIES # F1 on WIN32_EXECUTABLE should open the help WIN32_EXECUTABLE ON MACOSX_BUNDLE ON