2016-08-04 15:26:53 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2016 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 "refactoringengine.h"
|
2016-11-15 15:38:12 +01:00
|
|
|
#include "projectpartutilities.h"
|
|
|
|
|
|
2017-11-16 17:48:53 +01:00
|
|
|
#include <filepath.h>
|
2016-08-04 15:26:53 +02:00
|
|
|
#include <refactoringserverinterface.h>
|
|
|
|
|
|
2017-09-27 10:32:52 +02:00
|
|
|
#include <cpptools/compileroptionsbuilder.h>
|
2016-11-02 12:21:54 +03:00
|
|
|
#include <cpptools/cpptoolsreuse.h>
|
|
|
|
|
|
2017-09-19 15:38:20 +02:00
|
|
|
#include <clangsupport/filepathcachinginterface.h>
|
|
|
|
|
|
2017-10-05 09:54:21 +02:00
|
|
|
#include <utils/algorithm.h>
|
2017-09-19 15:38:20 +02:00
|
|
|
#include <utils/textutils.h>
|
2017-08-03 14:13:42 +02:00
|
|
|
|
2016-08-04 15:26:53 +02:00
|
|
|
#include <QTextCursor>
|
|
|
|
|
#include <QTextDocument>
|
2017-09-19 15:38:20 +02:00
|
|
|
#include <QTextBlock>
|
|
|
|
|
#include <QDir>
|
2016-08-04 15:26:53 +02:00
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
namespace ClangRefactoring {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RefactoringEngine::RefactoringEngine(ClangBackEnd::RefactoringServerInterface &server,
|
2017-09-21 11:43:59 +02:00
|
|
|
ClangBackEnd::RefactoringClientInterface &client,
|
2017-09-19 15:38:20 +02:00
|
|
|
ClangBackEnd::FilePathCachingInterface &filePathCache,
|
|
|
|
|
SymbolQueryInterface &symbolQuery)
|
2017-09-21 11:43:59 +02:00
|
|
|
: m_server(server),
|
|
|
|
|
m_client(client),
|
2017-09-19 15:38:20 +02:00
|
|
|
m_filePathCache(filePathCache),
|
|
|
|
|
m_symbolQuery(symbolQuery)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-19 15:38:20 +02:00
|
|
|
RefactoringEngine::~RefactoringEngine() = default;
|
|
|
|
|
|
2019-07-03 17:57:43 +02:00
|
|
|
void RefactoringEngine::startLocalRenaming(const CppTools::CursorInEditor &,
|
|
|
|
|
CppTools::ProjectPart *,
|
|
|
|
|
RenameCallback &&)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-19 15:38:20 +02:00
|
|
|
CppTools::Usages RefactoringEngine::locationsAt(const CppTools::CursorInEditor &data) const
|
|
|
|
|
{
|
2017-11-23 14:30:09 +01:00
|
|
|
CppTools::Usages usages;
|
|
|
|
|
|
2017-09-19 15:38:20 +02:00
|
|
|
QTextCursor cursor = Utils::Text::wordStartCursor(data.cursor());
|
2017-11-23 14:30:09 +01:00
|
|
|
Utils::OptionalLineColumn lineColumn = Utils::Text::convertPosition(cursor.document(),
|
|
|
|
|
cursor.position());
|
|
|
|
|
|
|
|
|
|
if (lineColumn) {
|
|
|
|
|
const QByteArray filePath = data.filePath().toString().toUtf8();
|
|
|
|
|
const ClangBackEnd::FilePathId filePathId = m_filePathCache.filePathId(ClangBackEnd::FilePathView(filePath));
|
|
|
|
|
|
2018-10-09 17:50:29 +02:00
|
|
|
usages = m_symbolQuery.sourceUsagesAt(filePathId, lineColumn->line, lineColumn->column);
|
2017-11-23 14:30:09 +01:00
|
|
|
}
|
2017-09-19 15:38:20 +02:00
|
|
|
|
2017-11-23 14:30:09 +01:00
|
|
|
return usages;
|
2017-09-19 15:38:20 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-25 10:51:39 +02:00
|
|
|
void RefactoringEngine::globalRename(const CppTools::CursorInEditor &data,
|
|
|
|
|
CppTools::UsagesCallback &&renameUsagesCallback,
|
|
|
|
|
const QString &)
|
|
|
|
|
{
|
|
|
|
|
renameUsagesCallback(locationsAt(data));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-19 15:38:20 +02:00
|
|
|
void RefactoringEngine::findUsages(const CppTools::CursorInEditor &data,
|
|
|
|
|
CppTools::UsagesCallback &&showUsagesCallback) const
|
|
|
|
|
{
|
|
|
|
|
showUsagesCallback(locationsAt(data));
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-21 11:58:16 +01:00
|
|
|
void RefactoringEngine::globalFollowSymbol(const CppTools::CursorInEditor &data,
|
|
|
|
|
Utils::ProcessLinkCallback &&processLinkCallback,
|
|
|
|
|
const CPlusPlus::Snapshot &,
|
|
|
|
|
const CPlusPlus::Document::Ptr &,
|
|
|
|
|
CppTools::SymbolFinder *,
|
|
|
|
|
bool) const
|
2017-10-05 09:54:21 +02:00
|
|
|
{
|
|
|
|
|
// TODO: replace that with specific followSymbol query
|
|
|
|
|
const CppTools::Usages usages = locationsAt(data);
|
|
|
|
|
CppTools::Usage usage = Utils::findOrDefault(usages, [&data](const CppTools::Usage &usage) {
|
|
|
|
|
// We've already searched in the current file, skip it.
|
|
|
|
|
if (usage.path == data.filePath().toString())
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
2018-02-21 11:58:16 +01:00
|
|
|
processLinkCallback(Link(usage.path, usage.line, usage.column));
|
2017-10-05 09:54:21 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-25 16:41:17 +02:00
|
|
|
bool RefactoringEngine::isRefactoringEngineAvailable() const
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
2017-09-25 16:41:17 +02:00
|
|
|
return m_server.isAvailable();
|
2016-08-04 15:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-25 16:41:17 +02:00
|
|
|
void RefactoringEngine::setRefactoringEngineAvailable(bool isAvailable)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
2017-09-25 16:41:17 +02:00
|
|
|
m_server.setAvailable(isAvailable);
|
2016-08-04 15:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace ClangRefactoring
|