SyntaxHighlighter.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2021, Dylan Katz <dykatz@uw.edu>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Debug.h>
  8. #include <LibGfx/Palette.h>
  9. #include <LibSQL/AST/Lexer.h>
  10. #include <LibSQL/AST/SyntaxHighlighter.h>
  11. namespace SQL::AST {
  12. static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, TokenType type)
  13. {
  14. switch (Token::category(type)) {
  15. case TokenCategory::Keyword:
  16. return { palette.syntax_keyword(), true };
  17. case TokenCategory::Identifier:
  18. return { palette.syntax_identifier(), false };
  19. case TokenCategory::Number:
  20. return { palette.syntax_number(), false };
  21. case TokenCategory::Blob:
  22. case TokenCategory::String:
  23. return { palette.syntax_string(), false };
  24. case TokenCategory::Operator:
  25. return { palette.syntax_operator(), false };
  26. case TokenCategory::Punctuation:
  27. return { palette.syntax_punctuation(), false };
  28. case TokenCategory::Invalid:
  29. default:
  30. return { palette.base_text(), false };
  31. }
  32. }
  33. bool SyntaxHighlighter::is_identifier(u64 token) const
  34. {
  35. auto sql_token = static_cast<TokenType>(static_cast<size_t>(token));
  36. return sql_token == TokenType::Identifier;
  37. }
  38. void SyntaxHighlighter::rehighlight(Palette const& palette)
  39. {
  40. auto text = m_client->get_text();
  41. Lexer lexer(text);
  42. Vector<GUI::TextDocumentSpan> spans;
  43. auto append_token = [&](Token const& token) {
  44. if (token.value().is_empty())
  45. return;
  46. GUI::TextDocumentSpan span;
  47. span.range.set_start({ token.start_position().line - 1, token.start_position().column - 1 });
  48. span.range.set_end({ token.end_position().line - 1, token.end_position().column - 1 });
  49. auto style = style_for_token_type(palette, token.type());
  50. span.attributes.color = style.color;
  51. span.attributes.bold = style.bold;
  52. span.data = static_cast<u64>(token.type());
  53. spans.append(span);
  54. dbgln_if(SYNTAX_HIGHLIGHTING_DEBUG, "{} @ '{}' {}:{} - {}:{}",
  55. token.name(),
  56. token.value(),
  57. span.range.start().line(), span.range.start().column(),
  58. span.range.end().line(), span.range.end().column());
  59. };
  60. for (;;) {
  61. auto token = lexer.next();
  62. append_token(token);
  63. if (token.type() == TokenType::Eof)
  64. break;
  65. }
  66. m_client->do_set_spans(move(spans));
  67. m_has_brace_buddies = false;
  68. highlight_matching_token_pair();
  69. m_client->do_update();
  70. }
  71. Vector<SyntaxHighlighter::MatchingTokenPair> SyntaxHighlighter::matching_token_pairs_impl() const
  72. {
  73. static Vector<SyntaxHighlighter::MatchingTokenPair> pairs;
  74. if (pairs.is_empty()) {
  75. pairs.append({ static_cast<u64>(TokenType::ParenOpen), static_cast<u64>(TokenType::ParenClose) });
  76. }
  77. return pairs;
  78. }
  79. bool SyntaxHighlighter::token_types_equal(u64 token1, u64 token2) const
  80. {
  81. return static_cast<TokenType>(token1) == static_cast<TokenType>(token2);
  82. }
  83. }