INISyntaxHighlighter.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/INILexer.h>
  7. #include <LibGUI/INISyntaxHighlighter.h>
  8. #include <LibGfx/Palette.h>
  9. namespace GUI {
  10. static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, IniToken::Type type)
  11. {
  12. switch (type) {
  13. case IniToken::Type::LeftBracket:
  14. case IniToken::Type::RightBracket:
  15. case IniToken::Type::Section:
  16. return { palette.syntax_keyword(), true };
  17. case IniToken::Type::Name:
  18. return { palette.syntax_identifier() };
  19. case IniToken::Type::Value:
  20. return { palette.syntax_string() };
  21. case IniToken::Type::Comment:
  22. return { palette.syntax_comment() };
  23. case IniToken::Type::Equal:
  24. return { palette.syntax_operator(), true };
  25. default:
  26. return { palette.base_text() };
  27. }
  28. }
  29. bool IniSyntaxHighlighter::is_identifier(u64 token) const
  30. {
  31. auto ini_token = static_cast<GUI::IniToken::Type>(token);
  32. return ini_token == GUI::IniToken::Type::Name;
  33. }
  34. void IniSyntaxHighlighter::rehighlight(Palette const& palette)
  35. {
  36. auto text = m_client->get_text();
  37. IniLexer lexer(text);
  38. auto tokens = lexer.lex();
  39. Vector<GUI::TextDocumentSpan> spans;
  40. for (auto& token : tokens) {
  41. GUI::TextDocumentSpan span;
  42. span.range.set_start({ token.m_start.line, token.m_start.column });
  43. span.range.set_end({ token.m_end.line, token.m_end.column });
  44. auto style = style_for_token_type(palette, token.m_type);
  45. span.attributes.color = style.color;
  46. span.attributes.bold = style.bold;
  47. span.is_skippable = token.m_type == IniToken::Type::Whitespace;
  48. span.data = static_cast<u64>(token.m_type);
  49. spans.append(span);
  50. }
  51. m_client->do_set_spans(move(spans));
  52. m_has_brace_buddies = false;
  53. highlight_matching_token_pair();
  54. m_client->do_update();
  55. }
  56. Vector<IniSyntaxHighlighter::MatchingTokenPair> IniSyntaxHighlighter::matching_token_pairs_impl() const
  57. {
  58. static Vector<MatchingTokenPair> pairs;
  59. if (pairs.is_empty()) {
  60. pairs.append({ static_cast<u64>(IniToken::Type::LeftBracket), static_cast<u64>(IniToken::Type::RightBracket) });
  61. }
  62. return pairs;
  63. }
  64. bool IniSyntaxHighlighter::token_types_equal(u64 token1, u64 token2) const
  65. {
  66. return static_cast<GUI::IniToken::Type>(token1) == static_cast<GUI::IniToken::Type>(token2);
  67. }
  68. IniSyntaxHighlighter::~IniSyntaxHighlighter()
  69. {
  70. }
  71. }