SyntaxHighlighter.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  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 <AK/Debug.h>
  27. #include <LibCpp/Lexer.h>
  28. #include <LibCpp/SyntaxHighlighter.h>
  29. #include <LibGUI/TextEditor.h>
  30. #include <LibGfx/Font.h>
  31. #include <LibGfx/Palette.h>
  32. namespace Cpp {
  33. static Syntax::TextStyle style_for_token_type(const Gfx::Palette& palette, Cpp::Token::Type type)
  34. {
  35. switch (type) {
  36. case Cpp::Token::Type::Keyword:
  37. return { palette.syntax_keyword(), true };
  38. case Cpp::Token::Type::KnownType:
  39. return { palette.syntax_type(), true };
  40. case Cpp::Token::Type::Identifier:
  41. return { palette.syntax_identifier(), false };
  42. case Cpp::Token::Type::DoubleQuotedString:
  43. case Cpp::Token::Type::SingleQuotedString:
  44. case Cpp::Token::Type::RawString:
  45. return { palette.syntax_string(), false };
  46. case Cpp::Token::Type::Integer:
  47. case Cpp::Token::Type::Float:
  48. return { palette.syntax_number(), false };
  49. case Cpp::Token::Type::IncludePath:
  50. return { palette.syntax_preprocessor_value(), false };
  51. case Cpp::Token::Type::EscapeSequence:
  52. return { palette.syntax_keyword(), true };
  53. case Cpp::Token::Type::PreprocessorStatement:
  54. case Cpp::Token::Type::IncludeStatement:
  55. return { palette.syntax_preprocessor_statement(), false };
  56. case Cpp::Token::Type::Comment:
  57. return { palette.syntax_comment(), false };
  58. default:
  59. return { palette.base_text(), false };
  60. }
  61. }
  62. bool SyntaxHighlighter::is_identifier(void* token) const
  63. {
  64. auto cpp_token = static_cast<Cpp::Token::Type>(reinterpret_cast<size_t>(token));
  65. return cpp_token == Cpp::Token::Type::Identifier;
  66. }
  67. bool SyntaxHighlighter::is_navigatable(void* token) const
  68. {
  69. auto cpp_token = static_cast<Cpp::Token::Type>(reinterpret_cast<size_t>(token));
  70. return cpp_token == Cpp::Token::Type::IncludePath;
  71. }
  72. void SyntaxHighlighter::rehighlight(Gfx::Palette palette)
  73. {
  74. ASSERT(m_editor);
  75. auto text = m_editor->text();
  76. Cpp::Lexer lexer(text);
  77. auto tokens = lexer.lex();
  78. Vector<GUI::TextDocumentSpan> spans;
  79. for (auto& token : tokens) {
  80. dbgln<SYNTAX_HIGHLIGHTING_DEBUG>("{} @ {}:{} - {}:{}", token.to_string(), token.m_start.line, token.m_start.column, token.m_end.line, token.m_end.column);
  81. GUI::TextDocumentSpan span;
  82. span.range.set_start({ token.m_start.line, token.m_start.column });
  83. span.range.set_end({ token.m_end.line, token.m_end.column });
  84. auto style = style_for_token_type(palette, token.m_type);
  85. span.attributes.color = style.color;
  86. span.attributes.bold = style.bold;
  87. span.is_skippable = token.m_type == Cpp::Token::Type::Whitespace;
  88. span.data = reinterpret_cast<void*>(token.m_type);
  89. spans.append(span);
  90. }
  91. m_editor->document().set_spans(spans);
  92. m_has_brace_buddies = false;
  93. highlight_matching_token_pair();
  94. m_editor->update();
  95. }
  96. Vector<SyntaxHighlighter::MatchingTokenPair> SyntaxHighlighter::matching_token_pairs() const
  97. {
  98. static Vector<SyntaxHighlighter::MatchingTokenPair> pairs;
  99. if (pairs.is_empty()) {
  100. pairs.append({ reinterpret_cast<void*>(Cpp::Token::Type::LeftCurly), reinterpret_cast<void*>(Cpp::Token::Type::RightCurly) });
  101. pairs.append({ reinterpret_cast<void*>(Cpp::Token::Type::LeftParen), reinterpret_cast<void*>(Cpp::Token::Type::RightParen) });
  102. pairs.append({ reinterpret_cast<void*>(Cpp::Token::Type::LeftBracket), reinterpret_cast<void*>(Cpp::Token::Type::RightBracket) });
  103. }
  104. return pairs;
  105. }
  106. bool SyntaxHighlighter::token_types_equal(void* token1, void* token2) const
  107. {
  108. return static_cast<Cpp::Token::Type>(reinterpret_cast<size_t>(token1)) == static_cast<Cpp::Token::Type>(reinterpret_cast<size_t>(token2));
  109. }
  110. SyntaxHighlighter::~SyntaxHighlighter()
  111. {
  112. }
  113. }