#include "name_mapping.h" #include namespace NSQLComplete { namespace { TString ToIdentifier(TString content, const TLocalSyntaxContext& local) { if (IsPlain(content) && !local.IsQuoted.AtLhs && !local.IsQuoted.AtRhs) { return content; } SubstGlobal(content, "`", "``"); if (!local.IsQuoted.AtLhs) { content.prepend('`'); } if (!local.IsQuoted.AtRhs) { content.append('`'); } return content; } } // namespace TCandidate ToCandidate(TKeyword name, TLocalSyntaxContext& local) { TVector& seq = local.Keywords[name.Content]; seq.insert(std::begin(seq), name.Content); TCandidate candidate = { .Kind = ECandidateKind::Keyword, .Content = FormatKeywords(seq), }; if (candidate.Content.EndsWith('(')) { candidate.Content += ')'; candidate.CursorShift = 1; } return candidate; } TCandidate ToCandidate(TPragmaName name) { return {.Kind = ECandidateKind::PragmaName, .Content = std::move(name.Identifier)}; } TCandidate ToCandidate(TTypeName name) { TCandidate candidate = { .Kind = ECandidateKind::TypeName, .Content = std::move(name.Identifier), }; switch (name.Kind) { case TTypeName::EKind::Simple: { } break; case TTypeName::EKind::Container: { candidate.Content += "<>"; candidate.CursorShift = 1; } break; case TTypeName::EKind::Parameterized: { candidate.Content += "()"; candidate.CursorShift = 1; } break; } return candidate; } TCandidate ToCandidate(TFunctionName name) { TCandidate candidate = { .Kind = ECandidateKind::FunctionName, .Content = std::move(name.Identifier), }; candidate.Content += "()"; candidate.CursorShift = 1; return candidate; } TCandidate ToCandidate(THintName name) { return {.Kind = ECandidateKind::HintName, .Content = std::move(name.Identifier)}; } TCandidate ToCandidate(TFolderName name, TLocalSyntaxContext& local) { TCandidate candidate = { .Kind = ECandidateKind::FolderName, .Content = std::move(name.Identifier), }; if (!local.IsQuoted.AtLhs) { candidate.Content.prepend('`'); } candidate.Content.append('/'); if (!local.IsQuoted.AtRhs) { candidate.Content.append('`'); candidate.CursorShift = 1; } return candidate; } TCandidate ToCandidate(TTableName name, TLocalSyntaxContext& local) { if (!local.IsQuoted.AtLhs) { name.Identifier.prepend('`'); } if (!local.IsQuoted.AtRhs) { name.Identifier.append('`'); } return {.Kind = ECandidateKind::TableName, .Content = std::move(name.Identifier)}; } TCandidate ToCandidate(TClusterName name) { return {.Kind = ECandidateKind::ClusterName, .Content = std::move(name.Identifier)}; } TCandidate ToCandidate(TColumnName name, TLocalSyntaxContext& local) { name.Identifier = ToIdentifier(std::move(name.Identifier), local); if (local.Column->Table.empty() && !name.TableAlias.empty()) { name.Identifier.prepend('.'); name.Identifier.prepend(ToIdentifier(std::move(name.TableAlias), local)); } return {.Kind = ECandidateKind::ColumnName, .Content = std::move(name.Identifier)}; } TCandidate ToCandidate(TBindingName name) { name.Identifier.prepend('$'); return {.Kind = ECandidateKind::BindingName, .Content = std::move(name.Identifier)}; } TCandidate ToCandidate(TUnknownName name, TLocalSyntaxContext& local) { name.Content = ToIdentifier(std::move(name.Content), local); return {.Kind = ECandidateKind::UnknownName, .Content = std::move(name.Content)}; } TCandidate ToCandidate(TGenericName generic, TLocalSyntaxContext& local) { return std::visit([&](auto&& name) -> TCandidate { using T = std::decay_t; constexpr bool IsContextSensitive = std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v; constexpr bool IsDocumented = std::is_base_of_v; TMaybe documentation; if constexpr (IsDocumented) { documentation = std::move(name.Description); } TCandidate candidate; if constexpr (IsContextSensitive) { candidate = ToCandidate(std::forward(name), local); } else { candidate = ToCandidate(std::forward(name)); } candidate.Documentation = std::move(documentation); return candidate; }, std::move(generic)); } TVector ToCandidate(TVector names, TLocalSyntaxContext local) { TVector candidates; candidates.reserve(names.size()); for (auto& name : names) { candidates.emplace_back(ToCandidate(std::move(name), local)); } return candidates; } } // namespace NSQLComplete