Clang: implement findUsages with existing index

Functionality is limited to the abilities of
current index which is not updated and is
generated only at project open.
Search box temporarily doesn't allow to "Search again".

Change-Id: Id1047f27ad0aafc901f06aa51ad38ceab95eaebb
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
Ivan Donchevskii
2017-09-19 15:38:20 +02:00
parent 263cdc0397
commit 050b4dd2f5
19 changed files with 289 additions and 44 deletions

View File

@@ -12,7 +12,8 @@ HEADERS += \
$$PWD/clangqueryhighlightmarker.h \
$$PWD/clangqueryexamplehighlighter.h \
$$PWD/clangqueryhighlighter.h \
$$PWD/refactoringprojectupdater.h
$$PWD/refactoringprojectupdater.h \
$$PWD/symbolqueryinterface.h
SOURCES += \
$$PWD/refactoringengine.cpp \

View File

@@ -24,8 +24,13 @@
****************************************************************************/
#include "clangrefactoringplugin.h"
#include "symbolquery.h"
#include "sqlitereadstatement.h"
#include "sqlitedatabase.h"
#include "querysqlitestatementfactory.h"
#include <clangpchmanager/qtcreatorprojectupdater.h>
#include <clangsupport/refactoringdatabaseinitializer.h>
#include <cpptools/cppmodelmanager.h>
@@ -41,6 +46,7 @@
#include <utils/hostosinfo.h>
#include <QDir>
#include <QApplication>
namespace ClangRefactoring {
@@ -59,21 +65,23 @@ std::unique_ptr<ClangRefactoringPluginData> ClangRefactoringPlugin::d;
class ClangRefactoringPluginData
{
using ProjectUpdater = ClangPchManager::QtCreatorProjectUpdater<ClangPchManager::ProjectUpdater>;
public:
using QuerySqliteStatementFactory = QuerySqliteStatementFactory<Sqlite::Database,
Sqlite::ReadStatement>;
Sqlite::Database database{Utils::PathString{QDir::tempPath() + "/symbol.db"}};
ClangBackEnd::RefactoringDatabaseInitializer<Sqlite::Database> databaseInitializer{database};
ClangBackEnd::FilePathCaching filePathCache{database};
RefactoringClient refactoringClient;
ClangBackEnd::RefactoringConnectionClient connectionClient{&refactoringClient};
RefactoringEngine engine{connectionClient.serverProxy(), refactoringClient, filePathCache};
QuerySqliteStatementFactory statementFactory{database};
SymbolQuery<QuerySqliteStatementFactory> symbolQuery{statementFactory};
RefactoringEngine engine{connectionClient.serverProxy(), refactoringClient, filePathCache, symbolQuery};
QtCreatorSearch qtCreatorSearch{*Core::SearchResultWindow::instance()};
QtCreatorClangQueryFindFilter qtCreatorfindFilter{connectionClient.serverProxy(),
qtCreatorSearch,
refactoringClient};
ProjectUpdater projectUpdate{connectionClient.serverProxy()};
};
ClangRefactoringPlugin::ClangRefactoringPlugin()

View File

@@ -24,20 +24,22 @@
****************************************************************************/
#include "refactoringengine.h"
#include "projectpartutilities.h"
#include <refactoringserverinterface.h>
#include <requestsourcelocationforrenamingmessage.h>
#include <cpptools/compileroptionsbuilder.h>
#include <cpptools/cpptoolsreuse.h>
#include <texteditor/textdocument.h>
#include <clangsupport/filepathcachinginterface.h>
#include <utils/textutils.h>
#include <QTextCursor>
#include <QTextDocument>
#include <QTextBlock>
#include <QDir>
#include <algorithm>
@@ -47,13 +49,17 @@ using ClangBackEnd::RequestSourceLocationsForRenamingMessage;
RefactoringEngine::RefactoringEngine(ClangBackEnd::RefactoringServerInterface &server,
ClangBackEnd::RefactoringClientInterface &client,
ClangBackEnd::FilePathCachingInterface &filePathCache)
ClangBackEnd::FilePathCachingInterface &filePathCache,
SymbolQueryInterface &symbolQuery)
: m_server(server),
m_client(client),
m_filePathCache(filePathCache)
m_filePathCache(filePathCache),
m_symbolQuery(symbolQuery)
{
}
RefactoringEngine::~RefactoringEngine() = default;
void RefactoringEngine::startLocalRenaming(const CppTools::CursorInEditor &data,
CppTools::ProjectPart *projectPart,
RenameCallback &&renameSymbolsCallback)
@@ -89,6 +95,35 @@ void RefactoringEngine::startGlobalRenaming(const CppTools::CursorInEditor &)
// TODO: implement
}
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);
const QByteArray filePath = data.filePath().toString().toLatin1();
const ClangBackEnd::FilePathId filePathId = m_filePathCache.filePathId(filePath.constData());
ClangRefactoring::SourceLocations usages = m_symbolQuery.locationsAt(filePathId, line,
column + 1);
CppTools::Usages result;
result.reserve(usages.size());
for (const auto &location : usages) {
const Utils::SmallStringView path = m_filePathCache.filePath(location.filePathId).path();
result.push_back({path, location.line, location.column});
}
return result;
}
void RefactoringEngine::findUsages(const CppTools::CursorInEditor &data,
CppTools::UsagesCallback &&showUsagesCallback) const
{
int line = 0, column = 0;
QTextCursor cursor = Utils::Text::wordStartCursor(data.cursor());
Utils::Text::convertPosition(cursor.document(), cursor.position(), &line, &column);
showUsagesCallback(locationsAt(data));
}
bool RefactoringEngine::isRefactoringEngineAvailable() const
{
return m_server.isAvailable();

View File

@@ -25,7 +25,9 @@
#pragma once
#include <filepathcachingfwd.h>
#include "symbolqueryinterface.h"
#include <clangsupport/filepathcachingfwd.h>
#include <cpptools/refactoringengineinterface.h>
@@ -41,25 +43,33 @@ class RefactoringEngine : public CppTools::RefactoringEngineInterface
public:
RefactoringEngine(ClangBackEnd::RefactoringServerInterface &m_server,
ClangBackEnd::RefactoringClientInterface &m_client,
ClangBackEnd::FilePathCachingInterface &filePathCache);
ClangBackEnd::FilePathCachingInterface &filePathCache,
SymbolQueryInterface &symbolQuery);
~RefactoringEngine() override;
void startLocalRenaming(const CppTools::CursorInEditor &data,
CppTools::ProjectPart *projectPart,
RenameCallback &&renameSymbolsCallback) override;
void startGlobalRenaming(const CppTools::CursorInEditor &data) override;
void findUsages(const CppTools::CursorInEditor &data,
CppTools::UsagesCallback &&showUsagesCallback) const override;
bool isRefactoringEngineAvailable() const override;
void setRefactoringEngineAvailable(bool isAvailable);
ClangBackEnd::FilePathCachingInterface &filePathCache()
const ClangBackEnd::FilePathCachingInterface &filePathCache() const
{
return m_filePathCache;
}
private:
CppTools::Usages locationsAt(const CppTools::CursorInEditor &data) const;
ClangBackEnd::RefactoringServerInterface &m_server;
ClangBackEnd::RefactoringClientInterface &m_client;
ClangBackEnd::FilePathCachingInterface &m_filePathCache;
SymbolQueryInterface &m_symbolQuery;
};
} // namespace ClangRefactoring

View File

@@ -25,7 +25,7 @@
#pragma once
#include <utils/smallstring.h>
#include "symbolqueryinterface.h"
#include <filepathid.h>
#include <sourcelocations.h>
@@ -37,7 +37,7 @@
namespace ClangRefactoring {
template <typename StatementFactory>
class SymbolQuery
class SymbolQuery final : public SymbolQueryInterface
{
using ReadStatement = typename StatementFactory::ReadStatementType;
@@ -46,7 +46,7 @@ public:
: m_statementFactory(statementFactory)
{}
SourceLocations locationsAt(ClangBackEnd::FilePathId filePathId, int line, int utf8Column)
SourceLocations locationsAt(ClangBackEnd::FilePathId filePathId, int line, int utf8Column) override
{
ReadStatement &locationsStatement = m_statementFactory.selectLocationsForSymbolLocation;

View File

@@ -0,0 +1,38 @@
/****************************************************************************
**
** 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.
**
****************************************************************************/
#pragma once
#include "sourcelocations.h"
namespace ClangRefactoring {
class SymbolQueryInterface
{
public:
virtual SourceLocations locationsAt(ClangBackEnd::FilePathId filePathId, int line, int utf8Column) = 0;
};
} // namespace ClangRefactoring