CMakePM: Allow navigation to ${PROJECT_NAME}

Task-number: QTCREATORBUG-27211
Change-Id: I23e2c6f39ff3d4c89ef78a66c10e619e3df3245f
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
Cristian Adam
2023-10-30 16:01:52 +01:00
parent 973e9dcf90
commit bcc45d8bb9
3 changed files with 22 additions and 12 deletions

View File

@@ -1310,7 +1310,7 @@ void CMakeBuildSystem::setupCMakeSymbolsHash()
// Handle project targets, unfortunately the CMake file-api doesn't deliver the // 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 // column of the target, just the line. Make sure to find it out
QHash<FilePath, int> projectTargetsSourceAndLine; QHash<FilePath, QPair<int, QString>> projectTargetsSourceAndLine;
for (const auto &target : std::as_const(buildTargets())) { for (const auto &target : std::as_const(buildTargets())) {
if (target.targetType == TargetType::UtilityType) if (target.targetType == TargetType::UtilityType)
continue; continue;
@@ -1318,11 +1318,11 @@ void CMakeBuildSystem::setupCMakeSymbolsHash()
continue; continue;
projectTargetsSourceAndLine.insert(target.backtrace.last().path, projectTargetsSourceAndLine.insert(target.backtrace.last().path,
target.backtrace.last().line); {target.backtrace.last().line, target.title});
} }
auto handleProjectTargets = [&](const CMakeFileInfo &cmakeFile, const cmListFileFunction &func) { auto handleProjectTargets = [&](const CMakeFileInfo &cmakeFile, const cmListFileFunction &func) {
if (!projectTargetsSourceAndLine.contains(cmakeFile.path) const auto it = projectTargetsSourceAndLine.find(cmakeFile.path);
|| projectTargetsSourceAndLine.value(cmakeFile.path) != func.Line()) if (it == projectTargetsSourceAndLine.end() || it->first != func.Line())
return; return;
if (func.Arguments().size() == 0) if (func.Arguments().size() == 0)
@@ -1333,7 +1333,7 @@ void CMakeBuildSystem::setupCMakeSymbolsHash()
link.targetFilePath = cmakeFile.path; link.targetFilePath = cmakeFile.path;
link.targetLine = arg.Line; link.targetLine = arg.Line;
link.targetColumn = arg.Column - 1; 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<Package> CMake packages // Gather the exported variables for the Find<Package> CMake packages

View File

@@ -160,7 +160,7 @@ static bool isValidIdentifierChar(const QChar &chr)
return chr.isLetterOrNumber() || chr == '_' || chr == '-'; return chr.isLetterOrNumber() || chr == '_' || chr == '-';
} }
QHash<QString, Utils::Link> getLocalSymbolsHash(const QByteArray &content, const Utils::FilePath &filePath) QHash<QString, Utils::Link> getLocalSymbolsHash(const QByteArray &content, const Utils::FilePath &filePath, QString &projectName)
{ {
cmListFile cmakeListFile; cmListFile cmakeListFile;
if (!content.isEmpty()) { if (!content.isEmpty()) {
@@ -172,6 +172,11 @@ QHash<QString, Utils::Link> getLocalSymbolsHash(const QByteArray &content, const
QHash<QString, Utils::Link> hash; QHash<QString, Utils::Link> hash;
for (const auto &func : cmakeListFile.Functions) { 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" if (func.LowerCaseName() != "function" && func.LowerCaseName() != "macro"
&& func.LowerCaseName() != "set" && func.LowerCaseName() != "option") && func.LowerCaseName() != "set" && func.LowerCaseName() != "option")
continue; continue;
@@ -310,6 +315,14 @@ void CMakeEditorWidget::findLinkAt(const QTextCursor &cursor,
const int funcStart = findFunctionStart(); const int funcStart = findFunctionStart();
const int funcEnd = findFunctionEnd(); 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()) { if (auto project = ProjectTree::currentProject()) {
buffer.replace("${CMAKE_SOURCE_DIR}", project->projectDirectory().path()); buffer.replace("${CMAKE_SOURCE_DIR}", project->projectDirectory().path());
if (auto bs = ProjectTree::currentBuildSystem(); bs->buildConfiguration()) { if (auto bs = ProjectTree::currentBuildSystem(); bs->buildConfiguration()) {
@@ -366,10 +379,6 @@ void CMakeEditorWidget::findLinkAt(const QTextCursor &cursor,
} }
// TODO: Resolve more variables // TODO: Resolve more variables
// Resolve local variables and functions
auto hash = getLocalSymbolsHash(textDocument()->textAt(0, funcEnd + 1).toUtf8(),
textDocument()->filePath());
// Strip variable coating // Strip variable coating
if (buffer.startsWith("${") && buffer.endsWith("}")) if (buffer.startsWith("${") && buffer.endsWith("}"))
buffer = buffer.mid(2, buffer.size() - 3); buffer = buffer.mid(2, buffer.size() - 3);

View File

@@ -61,7 +61,7 @@ endif()
# and F1 should open the help page # and F1 should open the help page
message(STATUS $ENV{CXX}) 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" # here code completino on "target_link_libraries(comp|" should poupup "completion"
# with a hammer icon, which suggests a project target # 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 # F2 on "completion" would jump above to qt_add_executable
target_link_libraries(completion PRIVATE Qt6::Widgets) 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 # F1 on WIN32_EXECUTABLE should open the help
WIN32_EXECUTABLE ON WIN32_EXECUTABLE ON
MACOSX_BUNDLE ON MACOSX_BUNDLE ON