CppSyntaxHighlighter.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 <LibGUI/CppLexer.h>
  27. #include <LibGUI/CppSyntaxHighlighter.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, CppToken::Type type)
  33. {
  34. switch (type) {
  35. case CppToken::Type::Keyword:
  36. return { palette.syntax_keyword(), &Gfx::Font::default_bold_fixed_width_font() };
  37. case CppToken::Type::KnownType:
  38. return { palette.syntax_type(), &Gfx::Font::default_bold_fixed_width_font() };
  39. case CppToken::Type::Identifier:
  40. return { palette.syntax_identifier() };
  41. case CppToken::Type::DoubleQuotedString:
  42. case CppToken::Type::SingleQuotedString:
  43. return { palette.syntax_string() };
  44. case CppToken::Type::Integer:
  45. case CppToken::Type::Float:
  46. return { palette.syntax_number() };
  47. case CppToken::Type::IncludePath:
  48. return { palette.syntax_preprocessor_value() };
  49. case CppToken::Type::EscapeSequence:
  50. return { palette.syntax_keyword(), &Gfx::Font::default_bold_fixed_width_font() };
  51. case CppToken::Type::PreprocessorStatement:
  52. case CppToken::Type::IncludeStatement:
  53. return { palette.syntax_preprocessor_statement() };
  54. case CppToken::Type::Comment:
  55. return { palette.syntax_comment() };
  56. default:
  57. return { palette.base_text() };
  58. }
  59. }
  60. bool CppSyntaxHighlighter::is_identifier(void* token) const
  61. {
  62. auto cpp_token = static_cast<GUI::CppToken::Type>(reinterpret_cast<size_t>(token));
  63. return cpp_token == GUI::CppToken::Type::Identifier;
  64. }
  65. bool CppSyntaxHighlighter::is_navigatable(void* token) const
  66. {
  67. auto cpp_token = static_cast<GUI::CppToken::Type>(reinterpret_cast<size_t>(token));
  68. return cpp_token == GUI::CppToken::Type::IncludePath;
  69. }
  70. void CppSyntaxHighlighter::rehighlight(Gfx::Palette palette)
  71. {
  72. ASSERT(m_editor);
  73. auto text = m_editor->text();
  74. CppLexer lexer(text);
  75. auto tokens = lexer.lex();
  76. Vector<GUI::TextDocumentSpan> spans;
  77. for (auto& token : tokens) {
  78. #ifdef DEBUG_SYNTAX_HIGHLIGHTING
  79. dbg() << token.to_string() << " @ " << token.m_start.line << ":" << token.m_start.column << " - " << token.m_end.line << ":" << token.m_end.column;
  80. #endif
  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.color = style.color;
  86. span.font = style.font;
  87. span.is_skippable = token.m_type == CppToken::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> CppSyntaxHighlighter::matching_token_pairs() const
  97. {
  98. static Vector<SyntaxHighlighter::MatchingTokenPair> pairs;
  99. if (pairs.is_empty()) {
  100. pairs.append({ reinterpret_cast<void*>(CppToken::Type::LeftCurly), reinterpret_cast<void*>(CppToken::Type::RightCurly) });
  101. pairs.append({ reinterpret_cast<void*>(CppToken::Type::LeftParen), reinterpret_cast<void*>(CppToken::Type::RightParen) });
  102. pairs.append({ reinterpret_cast<void*>(CppToken::Type::LeftBracket), reinterpret_cast<void*>(CppToken::Type::RightBracket) });
  103. }
  104. return pairs;
  105. }
  106. bool CppSyntaxHighlighter::token_types_equal(void* token1, void* token2) const
  107. {
  108. return static_cast<GUI::CppToken::Type>(reinterpret_cast<size_t>(token1)) == static_cast<GUI::CppToken::Type>(reinterpret_cast<size_t>(token2));
  109. }
  110. CppSyntaxHighlighter::~CppSyntaxHighlighter()
  111. {
  112. }
  113. }