Nim: Hide most of NimSuggestCache interface

Change-Id: I8fc114d253e7ca3a0cc3bed4542402acebfd9e86
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
hjk
2023-07-21 10:36:06 +02:00
parent 5f38ac700c
commit 032ba45fae
4 changed files with 67 additions and 86 deletions

View File

@@ -130,7 +130,7 @@ private:
static Suggest::NimSuggest *nimSuggestInstance(const AssistInterface *interface)
{
return Nim::Suggest::NimSuggestCache::instance().get(interface->filePath());
return Suggest::getFromCache(interface->filePath());
}
static std::shared_ptr<Suggest::NimSuggestClientRequest> sendRequest(const AssistInterface *interface,
@@ -154,7 +154,7 @@ private:
return result;
}
static AssistProposalItemInterface *createProposal(const Nim::Suggest::Line &line)
static AssistProposalItemInterface *createProposal(const Suggest::Line &line)
{
auto item = new AssistProposalItem();
item->setIcon(Utils::CodeModelIcon::iconForType(symbolIcon(line.symbol_kind)));

View File

@@ -39,7 +39,7 @@ void NimTextEditorWidget::findLinkAt(const QTextCursor &c, const Utils::LinkHand
{
const Utils::FilePath &path = textDocument()->filePath();
NimSuggest *suggest = NimSuggestCache::instance().get(path);
NimSuggest *suggest = Nim::Suggest::getFromCache(path);
if (!suggest)
return processLinkCallback(Utils::Link());

View File

@@ -10,32 +10,17 @@
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <unordered_map>
using namespace Utils;
namespace Nim::Suggest {
NimSuggestCache &NimSuggestCache::instance()
{
static NimSuggestCache instance;
return instance;
}
NimSuggestCache::~NimSuggestCache() = default;
NimSuggest *NimSuggestCache::get(const FilePath &filename)
{
auto it = m_nimSuggestInstances.find(filename);
if (it == m_nimSuggestInstances.end()) {
auto instance = std::make_unique<Suggest::NimSuggest>(this);
instance->setProjectFile(filename);
instance->setExecutablePath(m_executablePath);
it = m_nimSuggestInstances.emplace(filename, std::move(instance)).first;
}
return it->second.get();
}
NimSuggestCache::NimSuggestCache()
class NimSuggestCache final : public QObject
{
public:
NimSuggestCache()
{
setExecutablePath(settings().nimSuggestPath());
QObject::connect(&settings().nimSuggestPath, &StringAspect::changed, [this] {
setExecutablePath(settings().nimSuggestPath());
@@ -46,36 +31,58 @@ NimSuggestCache::NimSuggestCache()
this, &NimSuggestCache::onEditorOpened);
connect(editorManager, &Core::EditorManager::editorAboutToClose,
this, &NimSuggestCache::onEditorClosed);
}
}
FilePath NimSuggestCache::executablePath() const
{
return m_executablePath;
}
void NimSuggestCache::setExecutablePath(const FilePath &path)
{
void setExecutablePath(const FilePath &path)
{
if (m_executablePath == path)
return;
m_executablePath = path;
for (const auto &pair : m_nimSuggestInstances) {
for (const auto &pair : m_nimSuggestInstances)
pair.second->setExecutablePath(path);
}
}
void NimSuggestCache::onEditorOpened(Core::IEditor *editor)
{
void onEditorOpened(Core::IEditor *editor)
{
if (editor->document()->mimeType() == Constants::C_NIM_MIMETYPE)
get(editor->document()->filePath());
}
getFromCache(editor->document()->filePath());
}
void NimSuggestCache::onEditorClosed(Core::IEditor *editor)
{
void onEditorClosed(Core::IEditor *editor)
{
auto it = m_nimSuggestInstances.find(editor->document()->filePath());
if (it != m_nimSuggestInstances.end())
m_nimSuggestInstances.erase(it);
}
NimSuggest *get(const FilePath &filePath)
{
auto it = m_nimSuggestInstances.find(filePath);
if (it == m_nimSuggestInstances.end()) {
auto instance = std::make_unique<NimSuggest>(this);
instance->setProjectFile(filePath);
instance->setExecutablePath(m_executablePath);
it = m_nimSuggestInstances.emplace(filePath, std::move(instance)).first;
}
return it->second.get();
}
std::unordered_map<FilePath, std::unique_ptr<Suggest::NimSuggest>> m_nimSuggestInstances;
FilePath m_executablePath;
};
static NimSuggestCache &cache()
{
static NimSuggestCache theCache;
return theCache;
}
NimSuggest *getFromCache(const FilePath &filePath)
{
return cache().get(filePath);
}
} // Nim::Suggest

View File

@@ -3,38 +3,12 @@
#pragma once
#include <utils/filepath.h>
#include <QObject>
#include <unordered_map>
namespace Core { class IEditor; }
namespace Utils { class FilePath; }
namespace Nim::Suggest {
class NimSuggest;
class NimSuggestCache final : public QObject
{
public:
static NimSuggestCache &instance();
NimSuggest *get(const Utils::FilePath &filename);
Utils::FilePath executablePath() const;
void setExecutablePath(const Utils::FilePath &path);
private:
NimSuggestCache();
~NimSuggestCache();
void onEditorOpened(Core::IEditor *editor);
void onEditorClosed(Core::IEditor *editor);
std::unordered_map<Utils::FilePath, std::unique_ptr<Suggest::NimSuggest>> m_nimSuggestInstances;
Utils::FilePath m_executablePath;
};
NimSuggest *getFromCache(const Utils::FilePath &filePath);
} // Nim::Suggest