kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

face_registry.hh (1688B)


      1 #ifndef face_registry_hh_INCLUDED
      2 #define face_registry_hh_INCLUDED
      3 
      4 #include "face.hh"
      5 #include "hash_map.hh"
      6 #include "ranges.hh"
      7 #include "string.hh"
      8 #include "safe_ptr.hh"
      9 
     10 namespace Kakoune
     11 {
     12 
     13 struct FaceSpec
     14 {
     15     Face face = {};
     16     String base = {};
     17 
     18     friend bool operator==(const FaceSpec&, const FaceSpec&) = default;
     19 };
     20 
     21 class FaceRegistry : public SafeCountable
     22 {
     23 public:
     24     FaceRegistry(FaceRegistry& parent) : SafeCountable{}, m_parent(&parent) {}
     25 
     26     void reparent(FaceRegistry& parent) { m_parent = &parent; }
     27 
     28     Face operator[](StringView facedesc) const;
     29     Face operator[](const FaceSpec& facespec) const;
     30     void add_face(StringView name, StringView facedesc, bool override = false);
     31     void remove_face(StringView name);
     32 
     33     using FaceMap = HashMap<String, FaceSpec, MemoryDomain::Faces>;
     34 
     35     auto flatten_faces() const
     36     {
     37         auto merge = [](auto&& first, const FaceMap& second) {
     38             return concatenated(std::forward<decltype(first)>(first)
     39                                 | filter([&second](auto& i) { return not second.contains(i.key); }),
     40                                 second);
     41         };
     42         static const FaceMap empty;
     43         auto& parent = m_parent ? m_parent->m_faces : empty;
     44         auto& grand_parent = (m_parent and m_parent->m_parent) ? m_parent->m_parent->m_faces : empty;
     45         return merge(merge(grand_parent, parent), m_faces);
     46     }
     47 
     48 private:
     49     Face resolve_spec(const FaceSpec& spec) const;
     50 
     51     friend class Scope;
     52     FaceRegistry();
     53 
     54     SafePtr<FaceRegistry> m_parent;
     55     FaceMap m_faces;
     56 };
     57 
     58 FaceSpec parse_face(StringView facedesc);
     59 String to_string(Face face);
     60 
     61 }
     62 
     63 #endif // face_registry_hh_INCLUDED