forked from qt-creator/qt-creator
Nim: Hide most of NimSuggestCache interface
Change-Id: I8fc114d253e7ca3a0cc3bed4542402acebfd9e86 Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
@@ -130,7 +130,7 @@ private:
|
|||||||
|
|
||||||
static Suggest::NimSuggest *nimSuggestInstance(const AssistInterface *interface)
|
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,
|
static std::shared_ptr<Suggest::NimSuggestClientRequest> sendRequest(const AssistInterface *interface,
|
||||||
@@ -154,7 +154,7 @@ private:
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static AssistProposalItemInterface *createProposal(const Nim::Suggest::Line &line)
|
static AssistProposalItemInterface *createProposal(const Suggest::Line &line)
|
||||||
{
|
{
|
||||||
auto item = new AssistProposalItem();
|
auto item = new AssistProposalItem();
|
||||||
item->setIcon(Utils::CodeModelIcon::iconForType(symbolIcon(line.symbol_kind)));
|
item->setIcon(Utils::CodeModelIcon::iconForType(symbolIcon(line.symbol_kind)));
|
||||||
|
@@ -39,7 +39,7 @@ void NimTextEditorWidget::findLinkAt(const QTextCursor &c, const Utils::LinkHand
|
|||||||
{
|
{
|
||||||
const Utils::FilePath &path = textDocument()->filePath();
|
const Utils::FilePath &path = textDocument()->filePath();
|
||||||
|
|
||||||
NimSuggest *suggest = NimSuggestCache::instance().get(path);
|
NimSuggest *suggest = Nim::Suggest::getFromCache(path);
|
||||||
if (!suggest)
|
if (!suggest)
|
||||||
return processLinkCallback(Utils::Link());
|
return processLinkCallback(Utils::Link());
|
||||||
|
|
||||||
|
@@ -10,72 +10,79 @@
|
|||||||
#include <coreplugin/editormanager/editormanager.h>
|
#include <coreplugin/editormanager/editormanager.h>
|
||||||
#include <coreplugin/editormanager/ieditor.h>
|
#include <coreplugin/editormanager/ieditor.h>
|
||||||
|
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
using namespace Utils;
|
using namespace Utils;
|
||||||
|
|
||||||
namespace Nim::Suggest {
|
namespace Nim::Suggest {
|
||||||
|
|
||||||
NimSuggestCache &NimSuggestCache::instance()
|
class NimSuggestCache final : public QObject
|
||||||
{
|
{
|
||||||
static NimSuggestCache instance;
|
public:
|
||||||
return instance;
|
NimSuggestCache()
|
||||||
}
|
{
|
||||||
|
|
||||||
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()
|
|
||||||
{
|
|
||||||
setExecutablePath(settings().nimSuggestPath());
|
|
||||||
QObject::connect(&settings().nimSuggestPath, &StringAspect::changed, [this] {
|
|
||||||
setExecutablePath(settings().nimSuggestPath());
|
setExecutablePath(settings().nimSuggestPath());
|
||||||
});
|
QObject::connect(&settings().nimSuggestPath, &StringAspect::changed, [this] {
|
||||||
|
setExecutablePath(settings().nimSuggestPath());
|
||||||
|
});
|
||||||
|
|
||||||
Core::EditorManager *editorManager = Core::EditorManager::instance();
|
Core::EditorManager *editorManager = Core::EditorManager::instance();
|
||||||
connect(editorManager, &Core::EditorManager::editorOpened,
|
connect(editorManager, &Core::EditorManager::editorOpened,
|
||||||
this, &NimSuggestCache::onEditorOpened);
|
this, &NimSuggestCache::onEditorOpened);
|
||||||
connect(editorManager, &Core::EditorManager::editorAboutToClose,
|
connect(editorManager, &Core::EditorManager::editorAboutToClose,
|
||||||
this, &NimSuggestCache::onEditorClosed);
|
this, &NimSuggestCache::onEditorClosed);
|
||||||
}
|
|
||||||
|
|
||||||
FilePath NimSuggestCache::executablePath() const
|
|
||||||
{
|
|
||||||
return m_executablePath;
|
|
||||||
}
|
|
||||||
|
|
||||||
void NimSuggestCache::setExecutablePath(const FilePath &path)
|
|
||||||
{
|
|
||||||
if (m_executablePath == path)
|
|
||||||
return;
|
|
||||||
|
|
||||||
m_executablePath = path;
|
|
||||||
|
|
||||||
for (const auto &pair : m_nimSuggestInstances) {
|
|
||||||
pair.second->setExecutablePath(path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setExecutablePath(const FilePath &path)
|
||||||
|
{
|
||||||
|
if (m_executablePath == path)
|
||||||
|
return;
|
||||||
|
|
||||||
|
m_executablePath = path;
|
||||||
|
|
||||||
|
for (const auto &pair : m_nimSuggestInstances)
|
||||||
|
pair.second->setExecutablePath(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
void onEditorOpened(Core::IEditor *editor)
|
||||||
|
{
|
||||||
|
if (editor->document()->mimeType() == Constants::C_NIM_MIMETYPE)
|
||||||
|
getFromCache(editor->document()->filePath());
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NimSuggestCache::onEditorOpened(Core::IEditor *editor)
|
NimSuggest *getFromCache(const FilePath &filePath)
|
||||||
{
|
{
|
||||||
if (editor->document()->mimeType() == Constants::C_NIM_MIMETYPE)
|
return cache().get(filePath);
|
||||||
get(editor->document()->filePath());
|
|
||||||
}
|
|
||||||
|
|
||||||
void NimSuggestCache::onEditorClosed(Core::IEditor *editor)
|
|
||||||
{
|
|
||||||
auto it = m_nimSuggestInstances.find(editor->document()->filePath());
|
|
||||||
if (it != m_nimSuggestInstances.end())
|
|
||||||
m_nimSuggestInstances.erase(it);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // Nim::Suggest
|
} // Nim::Suggest
|
||||||
|
@@ -3,38 +3,12 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <utils/filepath.h>
|
namespace Utils { class FilePath; }
|
||||||
|
|
||||||
#include <QObject>
|
|
||||||
|
|
||||||
#include <unordered_map>
|
|
||||||
|
|
||||||
namespace Core { class IEditor; }
|
|
||||||
|
|
||||||
namespace Nim::Suggest {
|
namespace Nim::Suggest {
|
||||||
|
|
||||||
class NimSuggest;
|
class NimSuggest;
|
||||||
|
|
||||||
class NimSuggestCache final : public QObject
|
NimSuggest *getFromCache(const Utils::FilePath &filePath);
|
||||||
{
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // Nim::Suggest
|
} // Nim::Suggest
|
||||||
|
Reference in New Issue
Block a user