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
69
pglablib/SqlCompletionProvider.h
Normal file
69
pglablib/SqlCompletionProvider.h
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
#ifndef SQLCOMPLETIONPROVIDER_H
|
||||
#define SQLCOMPLETIONPROVIDER_H
|
||||
|
||||
#include "SqlContext.h"
|
||||
#include <QString>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
class PgDatabaseCatalog;
|
||||
|
||||
struct CompletionItem {
|
||||
QString display; // text shown in the list
|
||||
QString detail; // dimmed extra information, for example the relation a column belongs to
|
||||
QString insertText; // replaces the typed prefix when accepted
|
||||
CompletionKind kind = CompletionKind::None;
|
||||
bool ambiguous = false; // name occurs in more than one relation in scope
|
||||
};
|
||||
|
||||
/// Plain snapshot of the parts of the catalog needed for completion, so the provider does not need a database.
|
||||
struct CompletionRelation {
|
||||
QString schema;
|
||||
QString name;
|
||||
char kind = 'r'; // pg_class.relkind
|
||||
std::vector<QString> columns; // in column order, without dropped and system columns
|
||||
};
|
||||
|
||||
struct CompletionFunction {
|
||||
QString schema;
|
||||
QString name;
|
||||
char kind = 'f'; // pg_proc.prokind: f function, a aggregate, w window
|
||||
};
|
||||
|
||||
struct CompletionForeignKey {
|
||||
QString childSchema, childTable; // table that has the constraint
|
||||
QString parentSchema, parentTable; // referenced table
|
||||
std::vector<QString> childColumns;
|
||||
std::vector<QString> parentColumns; // same length as childColumns
|
||||
};
|
||||
|
||||
struct CompletionCatalog {
|
||||
std::vector<QString> schemas;
|
||||
std::vector<CompletionRelation> relations;
|
||||
std::vector<CompletionFunction> functions;
|
||||
std::vector<CompletionForeignKey> foreignKeys;
|
||||
/// Schemas that are searched when a relation is not schema qualified, in order
|
||||
std::vector<QString> searchPath = { QStringLiteral("public"), QStringLiteral("pg_catalog") };
|
||||
};
|
||||
|
||||
CompletionCatalog buildCompletionCatalog(const PgDatabaseCatalog &catalog);
|
||||
|
||||
class SqlCompletionProvider {
|
||||
public:
|
||||
SqlCompletionProvider() = default;
|
||||
explicit SqlCompletionProvider(CompletionCatalog catalog);
|
||||
|
||||
std::vector<CompletionItem> complete(const SqlContext &ctx) const;
|
||||
|
||||
private:
|
||||
CompletionCatalog m_catalog;
|
||||
|
||||
const CompletionRelation* resolve(const RelationRef &ref) const;
|
||||
std::vector<CompletionItem> relationItems(const SqlContext &ctx, const QString &schema) const;
|
||||
std::vector<CompletionItem> columnItems(const SqlContext &ctx) const;
|
||||
std::vector<CompletionItem> qualifiedColumnItems(const SqlContext &ctx) const;
|
||||
std::vector<CompletionItem> joinItems(const SqlContext &ctx) const;
|
||||
void addFunctionItems(const SqlContext &ctx, std::vector<CompletionItem> &items) const;
|
||||
};
|
||||
|
||||
#endif // SQLCOMPLETIONPROVIDER_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue