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
// 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())) {
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<Package> CMake packages

View File

@@ -160,7 +160,7 @@ static bool isValidIdentifierChar(const QChar &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;
if (!content.isEmpty()) {
@@ -172,6 +172,11 @@ QHash<QString, Utils::Link> getLocalSymbolsHash(const QByteArray &content, const
QHash<QString, Utils::Link> 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);