2017-08-03 16:43:38 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2017 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "clangeditordocumentprocessor.h"
|
2017-09-08 09:33:35 +02:00
|
|
|
#include "clangfollowsymbol.h"
|
|
|
|
|
|
2019-01-07 08:49:01 +01:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2017-10-05 09:54:21 +02:00
|
|
|
#include <cpptools/cppmodelmanager.h>
|
2017-09-08 09:33:35 +02:00
|
|
|
#include <texteditor/texteditor.h>
|
|
|
|
|
|
2017-12-05 09:42:35 +01:00
|
|
|
#include <clangsupport/tokeninfocontainer.h>
|
2017-09-08 09:33:35 +02:00
|
|
|
|
2017-09-21 12:35:24 +02:00
|
|
|
#include <utils/textutils.h>
|
2017-09-08 09:33:35 +02:00
|
|
|
#include <utils/algorithm.h>
|
2017-08-03 16:43:38 +02:00
|
|
|
|
2019-02-07 12:44:25 +01:00
|
|
|
#include <memory>
|
|
|
|
|
|
2017-08-03 16:43:38 +02:00
|
|
|
namespace ClangCodeModel {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2017-09-08 09:33:35 +02:00
|
|
|
// Returns invalid Mark if it is not found at (line, column)
|
2017-12-05 09:42:35 +01:00
|
|
|
static bool findMark(const QVector<ClangBackEnd::TokenInfoContainer> &marks,
|
2017-09-08 09:33:35 +02:00
|
|
|
uint line,
|
|
|
|
|
uint column,
|
2017-12-05 09:42:35 +01:00
|
|
|
ClangBackEnd::TokenInfoContainer &mark)
|
2017-09-08 09:33:35 +02:00
|
|
|
{
|
|
|
|
|
mark = Utils::findOrDefault(marks,
|
2017-12-05 09:42:35 +01:00
|
|
|
[line, column](const ClangBackEnd::TokenInfoContainer &curMark) {
|
2018-04-04 18:25:23 +02:00
|
|
|
if (curMark.line != line)
|
2017-09-08 09:33:35 +02:00
|
|
|
return false;
|
2018-04-04 18:25:23 +02:00
|
|
|
if (curMark.column == column)
|
2017-09-08 09:33:35 +02:00
|
|
|
return true;
|
2018-04-04 18:25:23 +02:00
|
|
|
if (curMark.column < column && curMark.column + curMark.length > column)
|
2017-09-08 09:33:35 +02:00
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
if (mark.isInvalid())
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-05 09:42:35 +01:00
|
|
|
static int getMarkPos(QTextCursor cursor, const ClangBackEnd::TokenInfoContainer &mark)
|
2017-09-08 09:33:35 +02:00
|
|
|
{
|
|
|
|
|
cursor.setPosition(0);
|
2018-04-04 18:25:23 +02:00
|
|
|
cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, mark.line - 1);
|
|
|
|
|
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, mark.column - 1);
|
2017-09-08 09:33:35 +02:00
|
|
|
return cursor.position();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-05 10:50:37 +01:00
|
|
|
static bool isValidIncludePathToken(const ClangBackEnd::TokenInfoContainer &token)
|
|
|
|
|
{
|
|
|
|
|
if (!token.extraInfo.includeDirectivePath)
|
|
|
|
|
return false;
|
|
|
|
|
const Utf8String &tokenName = token.extraInfo.token;
|
|
|
|
|
return !tokenName.startsWith("include") && tokenName != "<" && tokenName != ">"
|
|
|
|
|
&& tokenName != "#";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int includePathStartIndex(const QVector<ClangBackEnd::TokenInfoContainer> &marks,
|
|
|
|
|
int currentIndex)
|
|
|
|
|
{
|
|
|
|
|
int startIndex = currentIndex - 1;
|
|
|
|
|
while (startIndex >= 0 && isValidIncludePathToken(marks[startIndex]))
|
|
|
|
|
--startIndex;
|
|
|
|
|
return startIndex + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int includePathEndIndex(const QVector<ClangBackEnd::TokenInfoContainer> &marks,
|
2018-09-18 14:42:20 +02:00
|
|
|
int currentIndex)
|
2018-01-05 10:50:37 +01:00
|
|
|
{
|
|
|
|
|
int endIndex = currentIndex + 1;
|
2018-09-18 14:42:20 +02:00
|
|
|
while (endIndex < marks.size() && isValidIncludePathToken(marks[endIndex]))
|
2018-01-05 10:50:37 +01:00
|
|
|
++endIndex;
|
|
|
|
|
return endIndex - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Utils::Link linkAtCursor(const QTextCursor &cursor,
|
|
|
|
|
const QString &filePath,
|
|
|
|
|
uint line,
|
|
|
|
|
uint column,
|
2017-10-05 09:45:33 +02:00
|
|
|
ClangEditorDocumentProcessor *processor)
|
2017-09-08 09:33:35 +02:00
|
|
|
{
|
2017-10-05 09:45:33 +02:00
|
|
|
using Link = Utils::Link;
|
2017-09-08 09:33:35 +02:00
|
|
|
|
2017-12-05 09:42:35 +01:00
|
|
|
const QVector<ClangBackEnd::TokenInfoContainer> &marks
|
|
|
|
|
= processor->tokenInfos();
|
|
|
|
|
ClangBackEnd::TokenInfoContainer mark;
|
2017-09-08 09:33:35 +02:00
|
|
|
if (!findMark(marks, line, column, mark))
|
|
|
|
|
return Link();
|
|
|
|
|
|
2018-01-05 10:50:37 +01:00
|
|
|
if (mark.extraInfo.includeDirectivePath && !isValidIncludePathToken(mark))
|
|
|
|
|
return Link();
|
2017-09-08 09:33:35 +02:00
|
|
|
|
2018-04-04 18:25:23 +02:00
|
|
|
Link token(filePath, mark.line, mark.column);
|
2017-09-08 09:33:35 +02:00
|
|
|
token.linkTextStart = getMarkPos(cursor, mark);
|
2018-04-04 18:25:23 +02:00
|
|
|
token.linkTextEnd = token.linkTextStart + mark.length;
|
2018-01-05 10:50:37 +01:00
|
|
|
|
2018-04-04 18:25:23 +02:00
|
|
|
if (mark.extraInfo.includeDirectivePath) {
|
2018-01-05 10:50:37 +01:00
|
|
|
// Tweak include paths to cover everything between "" or <>.
|
|
|
|
|
if (mark.extraInfo.token.startsWith("\"")) {
|
|
|
|
|
token.linkTextStart++;
|
|
|
|
|
token.linkTextEnd--;
|
|
|
|
|
} else {
|
|
|
|
|
// '#include <path/file.h>' case. Clang gives us a separate token for each part of
|
|
|
|
|
// the path. We want to have the full range instead therefore we search for < and >
|
|
|
|
|
// tokens around the current token.
|
|
|
|
|
const int index = marks.indexOf(mark);
|
|
|
|
|
const int startIndex = includePathStartIndex(marks, index);
|
|
|
|
|
const int endIndex = includePathEndIndex(marks, index);
|
|
|
|
|
|
|
|
|
|
if (startIndex != index)
|
|
|
|
|
token.linkTextStart = getMarkPos(cursor, marks[startIndex]);
|
|
|
|
|
if (endIndex != index)
|
|
|
|
|
token.linkTextEnd = getMarkPos(cursor, marks[endIndex]) + marks[endIndex].length;
|
|
|
|
|
}
|
|
|
|
|
return token;
|
2017-09-08 09:33:35 +02:00
|
|
|
}
|
2018-01-05 10:50:37 +01:00
|
|
|
|
2018-08-23 09:13:31 +02:00
|
|
|
if (mark.extraInfo.identifier || mark.extraInfo.token == "operator"
|
|
|
|
|
|| mark.extraInfo.token == "auto") {
|
2017-09-08 09:33:35 +02:00
|
|
|
return token;
|
2018-08-23 09:13:31 +02:00
|
|
|
}
|
2017-09-08 09:33:35 +02:00
|
|
|
return Link();
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-01 10:46:20 +02:00
|
|
|
static ::Utils::ProcessLinkCallback extendedCallback(::Utils::ProcessLinkCallback &&callback,
|
|
|
|
|
const CppTools::SymbolInfo &result)
|
|
|
|
|
{
|
|
|
|
|
// If globalFollowSymbol finds nothing follow to the declaration.
|
|
|
|
|
return [original_callback = std::move(callback), result](const ::Utils::Link &link) {
|
2018-06-07 14:23:07 +02:00
|
|
|
if (link.linkTextStart < 0 && result.isResultOnlyForFallBack) {
|
2018-06-01 10:46:20 +02:00
|
|
|
return original_callback(::Utils::Link(result.fileName, result.startLine,
|
|
|
|
|
result.startColumn - 1));
|
|
|
|
|
}
|
|
|
|
|
return original_callback(link);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-07 08:49:01 +01:00
|
|
|
static bool isSameInvocationContext(const Utils::FileName &filePath)
|
|
|
|
|
{
|
|
|
|
|
return TextEditor::BaseTextEditor::currentTextEditor()->editorWidget()->isVisible()
|
|
|
|
|
&& Core::EditorManager::currentDocument()->filePath() == filePath;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-21 11:58:16 +01:00
|
|
|
void ClangFollowSymbol::findLink(const CppTools::CursorInEditor &data,
|
|
|
|
|
::Utils::ProcessLinkCallback &&processLinkCallback,
|
|
|
|
|
bool resolveTarget,
|
|
|
|
|
const CPlusPlus::Snapshot &snapshot,
|
|
|
|
|
const CPlusPlus::Document::Ptr &documentFromSemanticInfo,
|
|
|
|
|
CppTools::SymbolFinder *symbolFinder,
|
|
|
|
|
bool inNextSplit)
|
2017-08-03 16:43:38 +02:00
|
|
|
{
|
2018-11-02 14:17:12 +01:00
|
|
|
int line = 0;
|
|
|
|
|
int column = 0;
|
2017-09-21 12:35:24 +02:00
|
|
|
QTextCursor cursor = Utils::Text::wordStartCursor(data.cursor());
|
2018-11-02 14:17:12 +01:00
|
|
|
Utils::Text::convertPosition(cursor.document(), cursor.position(), &line, &column);
|
2017-09-08 09:33:35 +02:00
|
|
|
|
2017-08-03 16:43:38 +02:00
|
|
|
ClangEditorDocumentProcessor *processor = ClangEditorDocumentProcessor::get(
|
|
|
|
|
data.filePath().toString());
|
|
|
|
|
if (!processor)
|
2018-02-21 11:58:16 +01:00
|
|
|
return processLinkCallback(Utils::Link());
|
2017-09-08 09:33:35 +02:00
|
|
|
|
2018-02-21 11:58:16 +01:00
|
|
|
if (!resolveTarget) {
|
2018-11-02 14:17:12 +01:00
|
|
|
processLinkCallback(linkAtCursor(cursor,
|
|
|
|
|
data.filePath().toString(),
|
|
|
|
|
static_cast<uint>(line),
|
|
|
|
|
static_cast<uint>(column),
|
2018-02-21 11:58:16 +01:00
|
|
|
processor));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-08-03 16:43:38 +02:00
|
|
|
|
2018-02-21 11:58:16 +01:00
|
|
|
QFuture<CppTools::SymbolInfo> infoFuture
|
2017-08-03 16:43:38 +02:00
|
|
|
= processor->requestFollowSymbol(static_cast<int>(line),
|
2017-09-08 09:33:35 +02:00
|
|
|
static_cast<int>(column));
|
|
|
|
|
|
2018-02-21 11:58:16 +01:00
|
|
|
if (infoFuture.isCanceled())
|
|
|
|
|
return processLinkCallback(Utils::Link());
|
|
|
|
|
|
2018-01-05 10:50:37 +01:00
|
|
|
if (m_watcher)
|
|
|
|
|
m_watcher->cancel();
|
|
|
|
|
|
2019-02-07 12:44:25 +01:00
|
|
|
m_watcher = std::make_unique<FutureSymbolWatcher>();
|
2018-01-05 10:50:37 +01:00
|
|
|
|
2019-01-07 08:49:01 +01:00
|
|
|
QObject::connect(m_watcher.get(), &FutureSymbolWatcher::finished, [=, filePath=data.filePath(),
|
|
|
|
|
callback=std::move(processLinkCallback)]() mutable {
|
|
|
|
|
if (m_watcher->isCanceled() || !isSameInvocationContext(filePath))
|
2018-03-20 10:29:55 +01:00
|
|
|
return callback(Utils::Link());
|
2018-01-05 10:50:37 +01:00
|
|
|
CppTools::SymbolInfo result = m_watcher->result();
|
2018-02-21 11:58:16 +01:00
|
|
|
// We did not fail but the result is empty
|
2018-06-07 14:23:07 +02:00
|
|
|
if (result.fileName.isEmpty() || result.isResultOnlyForFallBack) {
|
2018-02-21 11:58:16 +01:00
|
|
|
const CppTools::RefactoringEngineInterface &refactoringEngine
|
|
|
|
|
= *CppTools::CppModelManager::instance();
|
|
|
|
|
refactoringEngine.globalFollowSymbol(data,
|
2018-06-01 10:46:20 +02:00
|
|
|
extendedCallback(std::move(callback), result),
|
2018-02-21 11:58:16 +01:00
|
|
|
snapshot,
|
|
|
|
|
documentFromSemanticInfo,
|
|
|
|
|
symbolFinder,
|
|
|
|
|
inNextSplit);
|
|
|
|
|
} else {
|
|
|
|
|
callback(Link(result.fileName, result.startLine, result.startColumn - 1));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-01-05 10:50:37 +01:00
|
|
|
m_watcher->setFuture(infoFuture);
|
2017-08-03 16:43:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace ClangCodeModel
|