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>
|
|
|
|
|
#include <requestsourcelocationforrenamingmessage.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>
|
|
|
|
|
|
|
|
|
|
#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 {
|
|
|
|
|
|
|
|
|
|
using ClangBackEnd::RequestSourceLocationsForRenamingMessage;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2017-08-03 14:13:42 +02:00
|
|
|
void RefactoringEngine::startLocalRenaming(const CppTools::CursorInEditor &data,
|
2016-08-04 15:26:53 +02:00
|
|
|
CppTools::ProjectPart *projectPart,
|
|
|
|
|
RenameCallback &&renameSymbolsCallback)
|
|
|
|
|
{
|
2017-09-27 10:32:52 +02:00
|
|
|
using CppTools::CompilerOptionsBuilder;
|
2016-12-14 14:56:17 +01:00
|
|
|
|
2017-09-25 16:41:17 +02:00
|
|
|
setRefactoringEngineAvailable(false);
|
2016-08-04 15:26:53 +02:00
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
m_client.setLocalRenamingCallback(std::move(renameSymbolsCallback));
|
2016-08-04 15:26:53 +02:00
|
|
|
|
2017-08-03 14:13:42 +02:00
|
|
|
QString filePath = data.filePath().toString();
|
|
|
|
|
QTextCursor textCursor = data.cursor();
|
2017-09-27 10:32:52 +02:00
|
|
|
CompilerOptionsBuilder optionsBuilder{*projectPart, CLANG_VERSION, CLANG_RESOURCE_DIR};
|
|
|
|
|
Utils::SmallStringVector commandLine{optionsBuilder.build(
|
2017-08-03 14:13:42 +02:00
|
|
|
fileKindInProjectPart(projectPart, filePath),
|
2017-09-11 14:08:00 +02:00
|
|
|
CppTools::getPchUsage())};
|
2016-08-04 15:26:53 +02:00
|
|
|
|
2017-08-03 14:13:42 +02:00
|
|
|
commandLine.push_back(filePath);
|
2016-08-04 15:26:53 +02:00
|
|
|
|
2017-08-03 14:13:42 +02:00
|
|
|
RequestSourceLocationsForRenamingMessage message(ClangBackEnd::FilePath(filePath),
|
2016-08-04 15:26:53 +02:00
|
|
|
uint(textCursor.blockNumber() + 1),
|
|
|
|
|
uint(textCursor.positionInBlock() + 1),
|
|
|
|
|
textCursor.document()->toPlainText(),
|
|
|
|
|
std::move(commandLine),
|
2017-08-03 14:13:42 +02:00
|
|
|
textCursor.document()->revision());
|
2016-08-04 15:26:53 +02:00
|
|
|
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
m_server.requestSourceLocationsForRenamingMessage(std::move(message));
|
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
|
|
|
|
|
{
|
|
|
|
|
int line = 0, column = 0;
|
|
|
|
|
QTextCursor cursor = Utils::Text::wordStartCursor(data.cursor());
|
|
|
|
|
Utils::Text::convertPosition(cursor.document(), cursor.position(), &line, &column);
|
|
|
|
|
|
2017-11-16 17:48:53 +01:00
|
|
|
const QByteArray filePath = data.filePath().toString().toUtf8();
|
|
|
|
|
const ClangBackEnd::FilePathId filePathId = m_filePathCache.filePathId(ClangBackEnd::FilePathView(filePath));
|
2017-10-24 13:39:36 +02:00
|
|
|
return m_symbolQuery.sourceUsagesAt(filePathId, line, column + 1);
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
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
|