CellSyntaxHighlighter.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CellSyntaxHighlighter.h"
  7. #include <LibGUI/TextEditor.h>
  8. #include <LibGfx/Palette.h>
  9. #include <LibJS/Lexer.h>
  10. namespace Spreadsheet {
  11. void CellSyntaxHighlighter::rehighlight(const Palette& palette)
  12. {
  13. auto text = m_client->get_text();
  14. m_client->spans().clear();
  15. if (!text.starts_with('=')) {
  16. m_client->do_update();
  17. return;
  18. }
  19. JS::SyntaxHighlighter::rehighlight(palette);
  20. // Highlight the '='
  21. m_client->spans().empend(
  22. GUI::TextRange { { 0, 0 }, { 0, 1 } },
  23. Gfx::TextAttributes {
  24. palette.syntax_keyword(),
  25. Optional<Color> {},
  26. false,
  27. false,
  28. },
  29. (u64)-1,
  30. false);
  31. if (m_cell && m_cell->thrown_value().has_value()) {
  32. if (auto value = m_cell->thrown_value().value(); value.is_object() && is<JS::Error>(value.as_object())) {
  33. auto& error = static_cast<JS::Error const&>(value.as_object());
  34. auto& traceback = error.traceback();
  35. auto& range = traceback.first().source_range;
  36. GUI::TextRange text_range { { range.start.line - 1, range.start.column }, { range.end.line - 1, range.end.column } };
  37. m_client->spans().prepend(
  38. GUI::TextDocumentSpan {
  39. text_range,
  40. Gfx::TextAttributes {
  41. Color::Black,
  42. Color::Red,
  43. false,
  44. false,
  45. },
  46. (u64)-1,
  47. false });
  48. }
  49. }
  50. m_client->do_update();
  51. }
  52. CellSyntaxHighlighter::~CellSyntaxHighlighter()
  53. {
  54. }
  55. }