GMLSyntaxHighlighter.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/GMLLexer.h>
  27. #include <LibGUI/GMLSyntaxHighlighter.h>
  28. #include <LibGUI/TextEditor.h>
  29. #include <LibGfx/Font.h>
  30. #include <LibGfx/Palette.h>
  31. namespace GUI {
  32. static TextStyle style_for_token_type(Gfx::Palette palette, GMLToken::Type type)
  33. {
  34. switch (type) {
  35. case GMLToken::Type::LeftCurly:
  36. case GMLToken::Type::RightCurly:
  37. return { palette.syntax_punctuation() };
  38. case GMLToken::Type::ClassMarker:
  39. return { palette.syntax_keyword() };
  40. case GMLToken::Type::ClassName:
  41. return { palette.syntax_identifier(), &Gfx::Font::default_bold_fixed_width_font() };
  42. case GMLToken::Type::Identifier:
  43. return { palette.syntax_identifier() };
  44. case GMLToken::Type::JsonValue:
  45. return { palette.syntax_string() };
  46. case GMLToken::Type::Comment:
  47. return { palette.syntax_comment() };
  48. default:
  49. return { palette.base_text() };
  50. }
  51. }
  52. bool GMLSyntaxHighlighter::is_identifier(void* token) const
  53. {
  54. auto ini_token = static_cast<GUI::GMLToken::Type>(reinterpret_cast<size_t>(token));
  55. return ini_token == GUI::GMLToken::Type::Identifier;
  56. }
  57. void GMLSyntaxHighlighter::rehighlight(Gfx::Palette palette)
  58. {
  59. ASSERT(m_editor);
  60. auto text = m_editor->text();
  61. GMLLexer lexer(text);
  62. auto tokens = lexer.lex();
  63. Vector<GUI::TextDocumentSpan> spans;
  64. for (auto& token : tokens) {
  65. GUI::TextDocumentSpan span;
  66. span.range.set_start({ token.m_start.line, token.m_start.column });
  67. span.range.set_end({ token.m_end.line, token.m_end.column });
  68. auto style = style_for_token_type(palette, token.m_type);
  69. span.color = style.color;
  70. span.font = style.font;
  71. span.is_skippable = false;
  72. span.data = reinterpret_cast<void*>(token.m_type);
  73. spans.append(span);
  74. }
  75. m_editor->document().set_spans(spans);
  76. m_has_brace_buddies = false;
  77. highlight_matching_token_pair();
  78. m_editor->update();
  79. }
  80. Vector<GMLSyntaxHighlighter::MatchingTokenPair> GMLSyntaxHighlighter::matching_token_pairs() const
  81. {
  82. static Vector<SyntaxHighlighter::MatchingTokenPair> pairs;
  83. if (pairs.is_empty()) {
  84. pairs.append({ reinterpret_cast<void*>(GMLToken::Type::LeftCurly), reinterpret_cast<void*>(GMLToken::Type::RightCurly) });
  85. }
  86. return pairs;
  87. }
  88. bool GMLSyntaxHighlighter::token_types_equal(void* token1, void* token2) const
  89. {
  90. return static_cast<GUI::GMLToken::Type>(reinterpret_cast<size_t>(token1)) == static_cast<GUI::GMLToken::Type>(reinterpret_cast<size_t>(token2));
  91. }
  92. GMLSyntaxHighlighter::~GMLSyntaxHighlighter()
  93. {
  94. }
  95. }