SyntaxHighlighter.cpp 3.3 KB

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