The editor now has context aware completions
This commit is contained in:
parent
1a1bbd0838
commit
960967c8a6
18 changed files with 1984 additions and 0 deletions
|
|
@ -16,6 +16,7 @@
|
|||
#include "json/json.h"
|
||||
#include "OpenDatabase.h"
|
||||
#include "catalog/PgDatabaseCatalog.h"
|
||||
#include "SqlCompletionProvider.h"
|
||||
#include "Pgsql_PgException.h"
|
||||
#include "QueryParamListController.h"
|
||||
#include "util.h"
|
||||
|
|
@ -54,6 +55,7 @@ QueryTool::QueryTool(IDatabaseWindow *context, std::shared_ptr<SqlDocument> docu
|
|||
connect(m_document.get(), &SqlDocument::modifiedChanged, this, &QueryTool::updateTitle);
|
||||
connect(m_document.get(), &SqlDocument::fileNameChanged, this, &QueryTool::updateTitle);
|
||||
ui->queryEdit->setAcceptDrops(false);
|
||||
setupCompletion();
|
||||
|
||||
m_queryParamListController = new QueryParamListController(ui->paramTableView, m_context->openDatabase(), this);
|
||||
connect(ui->addButton, &QPushButton::clicked, m_queryParamListController,
|
||||
|
|
@ -64,6 +66,35 @@ QueryTool::QueryTool(IDatabaseWindow *context, std::shared_ptr<SqlDocument> docu
|
|||
startConnect();
|
||||
}
|
||||
|
||||
void QueryTool::setupCompletion()
|
||||
{
|
||||
connect(m_catalog.get(), &PgDatabaseCatalog::refreshed, this, [this]() {
|
||||
m_completionProvider.reset();
|
||||
});
|
||||
|
||||
ui->queryEdit->setCompletionCallback([this](const QString &text, int pos) {
|
||||
EditorCompletionResult result;
|
||||
if (!m_catalog)
|
||||
return result;
|
||||
if (!m_completionProvider)
|
||||
m_completionProvider = std::make_shared<SqlCompletionProvider>(buildCompletionCatalog(*m_catalog));
|
||||
|
||||
const SqlContext ctx = analyzeSqlContext(text, pos);
|
||||
result.replaceStart = ctx.prefixStart;
|
||||
result.replaceLen = ctx.prefixLen;
|
||||
result.hasQualifier = !ctx.qualifier.isEmpty();
|
||||
for (const auto &item : m_completionProvider->complete(ctx)) {
|
||||
EditorCompletionItem i;
|
||||
i.display = item.display;
|
||||
i.detail = item.detail;
|
||||
i.insertText = item.insertText;
|
||||
i.ambiguous = item.ambiguous;
|
||||
result.items.push_back(std::move(i));
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
QueryTool::~QueryTool()
|
||||
{
|
||||
// Detach the editor from the shared document, it may be destroyed together with this tool
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ class QTableView;
|
|||
|
||||
class QTabWidget;
|
||||
class SqlSyntaxHighlighter;
|
||||
class SqlCompletionProvider;
|
||||
class ExplainRoot;
|
||||
class QueryExplainModel;
|
||||
class PgTypeContainer;
|
||||
|
|
@ -80,6 +81,9 @@ private:
|
|||
|
||||
|
||||
std::shared_ptr<PgDatabaseCatalog> m_catalog;
|
||||
std::shared_ptr<SqlCompletionProvider> m_completionProvider; ///< built on first use, reset when the catalog is refreshed
|
||||
|
||||
void setupCompletion();
|
||||
ASyncDBConnection m_dbConnection;
|
||||
|
||||
std::unique_ptr<QueryExplainModel> explainModel;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue