Highlighter.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Noncopyable.h>
  8. #include <AK/WeakPtr.h>
  9. #include <LibGUI/TextDocument.h>
  10. #include <LibGfx/Palette.h>
  11. #include <LibSyntax/HighlighterClient.h>
  12. namespace Syntax {
  13. enum class Language {
  14. Cpp,
  15. GML,
  16. HTML,
  17. INI,
  18. JavaScript,
  19. PlainText,
  20. SQL,
  21. Shell,
  22. };
  23. struct TextStyle {
  24. const Gfx::Color color;
  25. const bool bold { false };
  26. };
  27. class Highlighter {
  28. AK_MAKE_NONCOPYABLE(Highlighter);
  29. AK_MAKE_NONMOVABLE(Highlighter);
  30. public:
  31. virtual ~Highlighter();
  32. virtual Language language() const = 0;
  33. virtual void rehighlight(const Palette&) = 0;
  34. virtual void highlight_matching_token_pair();
  35. virtual bool is_identifier(u64) const { return false; };
  36. virtual bool is_navigatable(u64) const { return false; };
  37. void attach(HighlighterClient&);
  38. void detach();
  39. void cursor_did_change();
  40. struct MatchingTokenPair {
  41. u64 open;
  42. u64 close;
  43. };
  44. Vector<MatchingTokenPair> matching_token_pairs() const;
  45. protected:
  46. Highlighter() { }
  47. // FIXME: This should be WeakPtr somehow
  48. HighlighterClient* m_client { nullptr };
  49. virtual Vector<MatchingTokenPair> matching_token_pairs_impl() const = 0;
  50. virtual bool token_types_equal(u64, u64) const = 0;
  51. void register_nested_token_pairs(Vector<MatchingTokenPair>);
  52. void clear_nested_token_pairs() { m_nested_token_pairs.clear(); }
  53. size_t first_free_token_kind_serial_value() const { return m_nested_token_pairs.size(); }
  54. struct BuddySpan {
  55. int index { -1 };
  56. GUI::TextDocumentSpan span_backup;
  57. };
  58. bool m_has_brace_buddies { false };
  59. BuddySpan m_brace_buddies[2];
  60. HashTable<MatchingTokenPair> m_nested_token_pairs;
  61. };
  62. class ProxyHighlighterClient final : public Syntax::HighlighterClient {
  63. public:
  64. ProxyHighlighterClient(Syntax::HighlighterClient& client, GUI::TextPosition start, u64 nested_kind_start_value, StringView source)
  65. : m_document(client.get_document())
  66. , m_text(source)
  67. , m_start(start)
  68. , m_nested_kind_start_value(nested_kind_start_value)
  69. {
  70. }
  71. Vector<GUI::TextDocumentSpan> corrected_spans() const
  72. {
  73. Vector<GUI::TextDocumentSpan> spans { m_spans };
  74. for (auto& entry : spans) {
  75. entry.range.start() = {
  76. entry.range.start().line() + m_start.line(),
  77. entry.range.start().line() == 0 ? entry.range.start().column() + m_start.column() : entry.range.start().column(),
  78. };
  79. entry.range.end() = {
  80. entry.range.end().line() + m_start.line(),
  81. entry.range.end().line() == 0 ? entry.range.end().column() + m_start.column() : entry.range.end().column(),
  82. };
  83. if (entry.data != (u64)-1)
  84. entry.data += m_nested_kind_start_value;
  85. }
  86. return spans;
  87. }
  88. Vector<Syntax::Highlighter::MatchingTokenPair> corrected_token_pairs(Vector<Syntax::Highlighter::MatchingTokenPair> pairs) const
  89. {
  90. for (auto& pair : pairs) {
  91. pair.close += m_nested_kind_start_value;
  92. pair.open += m_nested_kind_start_value;
  93. }
  94. return pairs;
  95. }
  96. private:
  97. virtual Vector<GUI::TextDocumentSpan>& spans() override { return m_spans; }
  98. virtual const Vector<GUI::TextDocumentSpan>& spans() const override { return m_spans; }
  99. virtual void set_span_at_index(size_t index, GUI::TextDocumentSpan span) override { m_spans.at(index) = move(span); }
  100. virtual String highlighter_did_request_text() const override { return m_text; }
  101. virtual void highlighter_did_request_update() override { }
  102. virtual GUI::TextDocument& highlighter_did_request_document() override { return m_document; }
  103. virtual GUI::TextPosition highlighter_did_request_cursor() const override { return {}; }
  104. virtual void highlighter_did_set_spans(Vector<GUI::TextDocumentSpan> spans) override { m_spans = move(spans); }
  105. Vector<GUI::TextDocumentSpan> m_spans;
  106. GUI::TextDocument& m_document;
  107. StringView m_text;
  108. GUI::TextPosition m_start;
  109. u64 m_nested_kind_start_value { 0 };
  110. };
  111. }
  112. template<>
  113. struct AK::Traits<Syntax::Highlighter::MatchingTokenPair> : public AK::GenericTraits<Syntax::Highlighter::MatchingTokenPair> {
  114. static unsigned hash(Syntax::Highlighter::MatchingTokenPair const& pair)
  115. {
  116. return pair_int_hash(u64_hash(pair.open), u64_hash(pair.close));
  117. }
  118. static bool equals(Syntax::Highlighter::MatchingTokenPair const& a, Syntax::Highlighter::MatchingTokenPair const& b)
  119. {
  120. return a.open == b.open && a.close == b.close;
  121. }
  122. };