EditorWrapper.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "EditorWrapper.h"
  8. #include "Editor.h"
  9. #include "HackStudio.h"
  10. #include <LibGUI/Application.h>
  11. #include <LibGUI/BoxLayout.h>
  12. #include <LibGUI/Label.h>
  13. #include <LibGfx/Font/Font.h>
  14. #include <LibGfx/Font/FontDatabase.h>
  15. #include <LibGfx/Palette.h>
  16. namespace HackStudio {
  17. EditorWrapper::EditorWrapper()
  18. {
  19. set_layout<GUI::VerticalBoxLayout>();
  20. m_filename_title = untitled_label;
  21. // FIXME: Propagate errors instead of giving up
  22. m_editor = MUST(Editor::try_create());
  23. m_find_widget = add<FindWidget>(*m_editor);
  24. add_child(*m_editor);
  25. m_editor->set_ruler_visible(true);
  26. m_editor->set_automatic_indentation_enabled(true);
  27. m_editor->on_focus = [this] {
  28. set_current_editor_wrapper(this);
  29. };
  30. m_editor->on_open = [](String const& path) {
  31. open_file(path);
  32. };
  33. m_editor->on_modified_change = [this](bool) {
  34. update_title();
  35. };
  36. }
  37. LanguageClient& EditorWrapper::language_client() { return m_editor->language_client(); }
  38. void EditorWrapper::set_mode_displayable()
  39. {
  40. editor().set_mode(GUI::TextEditor::Editable);
  41. editor().set_background_role(Gfx::ColorRole::Base);
  42. editor().set_palette(GUI::Application::the()->palette());
  43. }
  44. void EditorWrapper::set_mode_non_displayable()
  45. {
  46. editor().set_mode(GUI::TextEditor::ReadOnly);
  47. editor().set_background_role(Gfx::ColorRole::InactiveSelection);
  48. auto palette = editor().palette();
  49. palette.set_color(Gfx::ColorRole::BaseText, Color::from_rgb(0xffffff));
  50. editor().set_palette(palette);
  51. editor().document().set_text("The contents of this file could not be displayed. Is it a binary file?");
  52. }
  53. void EditorWrapper::set_filename(String const& filename)
  54. {
  55. m_filename = filename;
  56. update_title();
  57. update_diff();
  58. }
  59. void EditorWrapper::save()
  60. {
  61. editor().write_to_file(filename());
  62. update_diff();
  63. editor().update();
  64. }
  65. void EditorWrapper::update_diff()
  66. {
  67. if (m_git_repo)
  68. m_hunks = Diff::parse_hunks(m_git_repo->unstaged_diff(filename()).value());
  69. }
  70. void EditorWrapper::set_project_root(String const& project_root)
  71. {
  72. m_project_root = project_root;
  73. auto result = GitRepo::try_to_create(*m_project_root);
  74. switch (result.type) {
  75. case GitRepo::CreateResult::Type::Success:
  76. m_git_repo = result.repo;
  77. break;
  78. case GitRepo::CreateResult::Type::GitProgramNotFound:
  79. break;
  80. case GitRepo::CreateResult::Type::NoGitRepo:
  81. break;
  82. default:
  83. VERIFY_NOT_REACHED();
  84. }
  85. }
  86. void EditorWrapper::update_title()
  87. {
  88. StringBuilder title;
  89. if (m_filename.is_null())
  90. title.append(untitled_label);
  91. else
  92. title.append(m_filename);
  93. if (editor().document().is_modified())
  94. title.append(" (*)");
  95. m_filename_title = title.to_string();
  96. }
  97. void EditorWrapper::set_debug_mode(bool enabled)
  98. {
  99. m_editor->set_debug_mode(enabled);
  100. }
  101. void EditorWrapper::search_action()
  102. {
  103. if (m_find_widget->visible())
  104. m_find_widget->hide();
  105. else
  106. m_find_widget->show();
  107. }
  108. }