CellSyntaxHighlighter.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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->exception()) {
  32. auto& traceback = m_cell->exception()->traceback();
  33. auto& range = traceback.first().source_range;
  34. GUI::TextRange text_range { { range.start.line - 1, range.start.column }, { range.end.line - 1, range.end.column - 1 } };
  35. m_client->spans().prepend(
  36. GUI::TextDocumentSpan {
  37. text_range,
  38. Gfx::TextAttributes {
  39. Color::Black,
  40. Color::Red,
  41. false,
  42. false,
  43. },
  44. (u64)-1,
  45. false });
  46. }
  47. m_client->do_update();
  48. }
  49. CellSyntaxHighlighter::~CellSyntaxHighlighter()
  50. {
  51. }
  52. }