HackStudioWidget.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  4. * Copyright (c) 2020-2021, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "HackStudioWidget.h"
  9. #include "Debugger/DebugInfoWidget.h"
  10. #include "Debugger/Debugger.h"
  11. #include "Debugger/DisassemblyWidget.h"
  12. #include "Dialogs/NewProjectDialog.h"
  13. #include "Editor.h"
  14. #include "EditorWrapper.h"
  15. #include "FindInFilesWidget.h"
  16. #include "Git/DiffViewer.h"
  17. #include "Git/GitWidget.h"
  18. #include "HackStudio.h"
  19. #include "HackStudioWidget.h"
  20. #include "Locator.h"
  21. #include "Project.h"
  22. #include "ProjectDeclarations.h"
  23. #include "TerminalWrapper.h"
  24. #include "ToDoEntries.h"
  25. #include <AK/LexicalPath.h>
  26. #include <AK/StringBuilder.h>
  27. #include <Kernel/API/InodeWatcherEvent.h>
  28. #include <LibCore/ArgsParser.h>
  29. #include <LibCore/Event.h>
  30. #include <LibCore/EventLoop.h>
  31. #include <LibCore/File.h>
  32. #include <LibCore/FileWatcher.h>
  33. #include <LibDebug/DebugSession.h>
  34. #include <LibDesktop/Launcher.h>
  35. #include <LibGUI/Action.h>
  36. #include <LibGUI/ActionGroup.h>
  37. #include <LibGUI/Application.h>
  38. #include <LibGUI/BoxLayout.h>
  39. #include <LibGUI/Button.h>
  40. #include <LibGUI/Dialog.h>
  41. #include <LibGUI/EditingEngine.h>
  42. #include <LibGUI/FilePicker.h>
  43. #include <LibGUI/InputBox.h>
  44. #include <LibGUI/ItemListModel.h>
  45. #include <LibGUI/Label.h>
  46. #include <LibGUI/Menu.h>
  47. #include <LibGUI/Menubar.h>
  48. #include <LibGUI/MessageBox.h>
  49. #include <LibGUI/RegularEditingEngine.h>
  50. #include <LibGUI/Splitter.h>
  51. #include <LibGUI/StackWidget.h>
  52. #include <LibGUI/TabWidget.h>
  53. #include <LibGUI/TableView.h>
  54. #include <LibGUI/TextBox.h>
  55. #include <LibGUI/TextEditor.h>
  56. #include <LibGUI/Toolbar.h>
  57. #include <LibGUI/ToolbarContainer.h>
  58. #include <LibGUI/TreeView.h>
  59. #include <LibGUI/VimEditingEngine.h>
  60. #include <LibGUI/Widget.h>
  61. #include <LibGUI/Window.h>
  62. #include <LibGfx/FontDatabase.h>
  63. #include <LibGfx/Palette.h>
  64. #include <LibThreading/Lock.h>
  65. #include <LibThreading/Thread.h>
  66. #include <LibVT/TerminalWidget.h>
  67. #include <fcntl.h>
  68. #include <spawn.h>
  69. #include <stdio.h>
  70. #include <sys/stat.h>
  71. #include <sys/types.h>
  72. #include <sys/wait.h>
  73. #include <unistd.h>
  74. namespace HackStudio {
  75. HackStudioWidget::HackStudioWidget(const String& path_to_project)
  76. {
  77. set_fill_with_background_color(true);
  78. set_layout<GUI::VerticalBoxLayout>();
  79. layout()->set_spacing(2);
  80. open_project(path_to_project);
  81. auto& toolbar_container = add<GUI::ToolbarContainer>();
  82. auto& outer_splitter = add<GUI::HorizontalSplitter>();
  83. auto& left_hand_splitter = outer_splitter.add<GUI::VerticalSplitter>();
  84. left_hand_splitter.set_fixed_width(150);
  85. create_project_tab(left_hand_splitter);
  86. m_project_tree_view_context_menu = create_project_tree_view_context_menu();
  87. create_open_files_view(left_hand_splitter);
  88. m_right_hand_splitter = outer_splitter.add<GUI::VerticalSplitter>();
  89. m_right_hand_stack = m_right_hand_splitter->add<GUI::StackWidget>();
  90. // Put a placeholder widget front & center since we don't have a file open yet.
  91. m_right_hand_stack->add<GUI::Widget>();
  92. m_diff_viewer = m_right_hand_stack->add<DiffViewer>();
  93. m_editors_splitter = m_right_hand_stack->add<GUI::VerticalSplitter>();
  94. m_editors_splitter->layout()->set_margins({ 0, 3, 0, 0 });
  95. add_new_editor(*m_editors_splitter);
  96. m_switch_to_next_editor = create_switch_to_next_editor_action();
  97. m_switch_to_previous_editor = create_switch_to_previous_editor_action();
  98. m_remove_current_editor_action = create_remove_current_editor_action();
  99. m_open_action = create_open_action();
  100. m_save_action = create_save_action();
  101. m_new_project_action = create_new_project_action();
  102. create_action_tab(*m_right_hand_splitter);
  103. m_add_editor_action = create_add_editor_action();
  104. m_add_terminal_action = create_add_terminal_action();
  105. m_remove_current_terminal_action = create_remove_current_terminal_action();
  106. m_locator = add<Locator>();
  107. m_terminal_wrapper->on_command_exit = [this] {
  108. m_stop_action->set_enabled(false);
  109. };
  110. m_build_action = create_build_action();
  111. m_run_action = create_run_action();
  112. m_stop_action = create_stop_action();
  113. m_debug_action = create_debug_action();
  114. initialize_debugger();
  115. create_toolbar(toolbar_container);
  116. auto maybe_watcher = Core::FileWatcher::create();
  117. if (maybe_watcher.is_error()) {
  118. warnln("Couldn't create a file watcher, deleted files won't be noticed! Error: {}", maybe_watcher.error());
  119. } else {
  120. m_file_watcher = maybe_watcher.release_value();
  121. m_file_watcher->on_change = [this](Core::FileWatcherEvent const& event) {
  122. if (event.type != Core::FileWatcherEvent::Type::Deleted)
  123. return;
  124. if (event.event_path.starts_with(project().root_path())) {
  125. String relative_path = LexicalPath::relative_path(event.event_path, project().root_path());
  126. handle_external_file_deletion(relative_path);
  127. } else {
  128. handle_external_file_deletion(event.event_path);
  129. }
  130. };
  131. }
  132. }
  133. void HackStudioWidget::update_actions()
  134. {
  135. auto is_remove_terminal_enabled = [this]() {
  136. auto widget = m_action_tab_widget->active_widget();
  137. if (!widget)
  138. return false;
  139. if (StringView { "TerminalWrapper" } != widget->class_name())
  140. return false;
  141. if (!reinterpret_cast<TerminalWrapper*>(widget)->user_spawned())
  142. return false;
  143. return true;
  144. };
  145. m_remove_current_editor_action->set_enabled(m_all_editor_wrappers.size() > 1);
  146. m_remove_current_terminal_action->set_enabled(is_remove_terminal_enabled());
  147. }
  148. void HackStudioWidget::on_action_tab_change()
  149. {
  150. update_actions();
  151. if (auto* active_widget = m_action_tab_widget->active_widget()) {
  152. if (is<GitWidget>(*active_widget))
  153. static_cast<GitWidget&>(*active_widget).refresh();
  154. }
  155. }
  156. void HackStudioWidget::open_project(const String& root_path)
  157. {
  158. if (chdir(root_path.characters()) < 0) {
  159. perror("chdir");
  160. exit(1);
  161. }
  162. m_project = Project::open_with_root_path(root_path);
  163. VERIFY(m_project);
  164. if (m_project_tree_view) {
  165. m_project_tree_view->set_model(m_project->model());
  166. m_project_tree_view->update();
  167. }
  168. if (Debugger::is_initialized()) {
  169. auto& debugger = Debugger::the();
  170. debugger.reset_breakpoints();
  171. debugger.set_source_root(m_project->root_path());
  172. }
  173. for (auto& editor_wrapper : m_all_editor_wrappers)
  174. editor_wrapper.set_project_root(LexicalPath(m_project->root_path()));
  175. }
  176. Vector<String> HackStudioWidget::selected_file_paths() const
  177. {
  178. Vector<String> files;
  179. m_project_tree_view->selection().for_each_index([&](const GUI::ModelIndex& index) {
  180. String sub_path = index.data().as_string();
  181. GUI::ModelIndex parent_or_invalid = index.parent();
  182. while (parent_or_invalid.is_valid()) {
  183. sub_path = String::formatted("{}/{}", parent_or_invalid.data().as_string(), sub_path);
  184. parent_or_invalid = parent_or_invalid.parent();
  185. }
  186. files.append(sub_path);
  187. });
  188. return files;
  189. }
  190. bool HackStudioWidget::open_file(const String& full_filename)
  191. {
  192. String filename = full_filename;
  193. if (full_filename.starts_with(project().root_path())) {
  194. filename = LexicalPath::relative_path(full_filename, project().root_path());
  195. }
  196. dbgln("HackStudio is opening {}", filename);
  197. if (Core::File::is_directory(filename) || !Core::File::exists(filename))
  198. return false;
  199. if (!active_file().is_empty()) {
  200. // Since the file is previously open, it should always be in m_open_files.
  201. VERIFY(m_open_files.find(active_file()) != m_open_files.end());
  202. auto previous_open_project_file = m_open_files.get(active_file()).value();
  203. // Update the scrollbar values of the previous_open_project_file and save them to m_open_files.
  204. previous_open_project_file->vertical_scroll_value(current_editor().vertical_scrollbar().value());
  205. previous_open_project_file->horizontal_scroll_value(current_editor().horizontal_scrollbar().value());
  206. }
  207. RefPtr<ProjectFile> new_project_file = nullptr;
  208. if (auto it = m_open_files.find(filename); it != m_open_files.end()) {
  209. new_project_file = it->value;
  210. } else {
  211. new_project_file = m_project->get_file(filename);
  212. m_open_files.set(filename, *new_project_file);
  213. m_open_files_vector.append(filename);
  214. if (!m_file_watcher.is_null()) {
  215. auto watch_result = m_file_watcher->add_watch(filename, Core::FileWatcherEvent::Type::Deleted);
  216. if (watch_result.is_error()) {
  217. warnln("Couldn't watch '{}'", filename);
  218. }
  219. }
  220. m_open_files_view->model()->update();
  221. }
  222. current_editor().set_document(const_cast<GUI::TextDocument&>(new_project_file->document()));
  223. if (new_project_file->could_render_text()) {
  224. current_editor_wrapper().set_mode_displayable();
  225. } else {
  226. current_editor_wrapper().set_mode_non_displayable();
  227. }
  228. current_editor().horizontal_scrollbar().set_value(new_project_file->horizontal_scroll_value());
  229. current_editor().vertical_scrollbar().set_value(new_project_file->vertical_scroll_value());
  230. current_editor().set_editing_engine(make<GUI::RegularEditingEngine>());
  231. set_edit_mode(EditMode::Text);
  232. String relative_file_path = filename;
  233. if (filename.starts_with(m_project->root_path()))
  234. relative_file_path = filename.substring(m_project->root_path().length() + 1);
  235. window()->set_title(String::formatted("{} - {} - Hack Studio", relative_file_path, m_project->name()));
  236. m_project_tree_view->update();
  237. current_editor_wrapper().set_filename(filename);
  238. current_editor().set_focus(true);
  239. return true;
  240. }
  241. EditorWrapper& HackStudioWidget::current_editor_wrapper()
  242. {
  243. VERIFY(m_current_editor_wrapper);
  244. return *m_current_editor_wrapper;
  245. }
  246. GUI::TextEditor& HackStudioWidget::current_editor()
  247. {
  248. return current_editor_wrapper().editor();
  249. }
  250. void HackStudioWidget::set_edit_mode(EditMode mode)
  251. {
  252. if (mode == EditMode::Text) {
  253. m_right_hand_stack->set_active_widget(m_editors_splitter);
  254. } else if (mode == EditMode::Diff) {
  255. m_right_hand_stack->set_active_widget(m_diff_viewer);
  256. } else {
  257. VERIFY_NOT_REACHED();
  258. }
  259. m_right_hand_stack->active_widget()->update();
  260. }
  261. NonnullRefPtr<GUI::Menu> HackStudioWidget::create_project_tree_view_context_menu()
  262. {
  263. m_open_selected_action = create_open_selected_action();
  264. m_show_in_file_manager_action = create_show_in_file_manager_action();
  265. m_new_file_action = create_new_file_action();
  266. m_new_directory_action = create_new_directory_action();
  267. m_delete_action = create_delete_action();
  268. auto project_tree_view_context_menu = GUI::Menu::construct("Project Files");
  269. project_tree_view_context_menu->add_action(*m_open_selected_action);
  270. project_tree_view_context_menu->add_action(*m_show_in_file_manager_action);
  271. // TODO: Rename, cut, copy, duplicate with new name...
  272. project_tree_view_context_menu->add_separator();
  273. project_tree_view_context_menu->add_action(*m_new_file_action);
  274. project_tree_view_context_menu->add_action(*m_new_directory_action);
  275. project_tree_view_context_menu->add_action(*m_delete_action);
  276. return project_tree_view_context_menu;
  277. }
  278. NonnullRefPtr<GUI::Action> HackStudioWidget::create_new_file_action()
  279. {
  280. return GUI::Action::create("New &File...", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [this](const GUI::Action&) {
  281. String filename;
  282. if (GUI::InputBox::show(window(), filename, "Enter name of new file:", "Add new file to project") != GUI::InputBox::ExecOK)
  283. return;
  284. auto path_to_selected = selected_file_paths();
  285. String filepath;
  286. if (!path_to_selected.is_empty()) {
  287. VERIFY(Core::File::exists(path_to_selected.first()));
  288. LexicalPath selected(path_to_selected.first());
  289. String dir_path;
  290. if (Core::File::is_directory(selected.string()))
  291. dir_path = selected.string();
  292. else
  293. dir_path = selected.dirname();
  294. filepath = String::formatted("{}/", dir_path);
  295. }
  296. filepath = String::formatted("{}{}", filepath, filename);
  297. auto file = Core::File::construct(filepath);
  298. if (!file->open((Core::OpenMode)(Core::OpenMode::WriteOnly | Core::OpenMode::MustBeNew))) {
  299. GUI::MessageBox::show(window(), String::formatted("Failed to create '{}'", filepath), "Error", GUI::MessageBox::Type::Error);
  300. return;
  301. }
  302. open_file(filepath);
  303. });
  304. }
  305. NonnullRefPtr<GUI::Action> HackStudioWidget::create_new_directory_action()
  306. {
  307. return GUI::Action::create("&New Directory...", { Mod_Ctrl | Mod_Shift, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/mkdir.png"), [this](const GUI::Action&) {
  308. String directory_name;
  309. if (GUI::InputBox::show(window(), directory_name, "Enter name of new directory:", "Add new folder to project") != GUI::InputBox::ExecOK)
  310. return;
  311. auto path_to_selected = selected_file_paths();
  312. if (!path_to_selected.is_empty()) {
  313. LexicalPath selected(path_to_selected.first());
  314. String dir_path;
  315. if (Core::File::is_directory(selected.string()))
  316. dir_path = selected.string();
  317. else
  318. dir_path = selected.dirname();
  319. directory_name = String::formatted("{}/{}", dir_path, directory_name);
  320. }
  321. auto formatted_dir_name = LexicalPath::canonicalized_path(String::formatted("{}/{}", m_project->model().root_path(), directory_name));
  322. int rc = mkdir(formatted_dir_name.characters(), 0755);
  323. if (rc < 0) {
  324. GUI::MessageBox::show(window(), "Failed to create new directory", "Error", GUI::MessageBox::Type::Error);
  325. return;
  326. }
  327. });
  328. }
  329. NonnullRefPtr<GUI::Action> HackStudioWidget::create_open_selected_action()
  330. {
  331. auto open_selected_action = GUI::Action::create("Open", [this](const GUI::Action&) {
  332. auto files = selected_file_paths();
  333. for (auto& file : files)
  334. open_file(file);
  335. });
  336. open_selected_action->set_enabled(true);
  337. return open_selected_action;
  338. }
  339. NonnullRefPtr<GUI::Action> HackStudioWidget::create_show_in_file_manager_action()
  340. {
  341. auto show_in_file_manager_action = GUI::Action::create("Show in File Manager", [this](const GUI::Action&) {
  342. auto files = selected_file_paths();
  343. for (auto& file : files)
  344. Desktop::Launcher::open(URL::create_with_file_protocol(m_project->root_path(), file));
  345. });
  346. show_in_file_manager_action->set_enabled(true);
  347. show_in_file_manager_action->set_icon(GUI::Icon::default_icon("app-file-manager").bitmap_for_size(16));
  348. return show_in_file_manager_action;
  349. }
  350. NonnullRefPtr<GUI::Action> HackStudioWidget::create_delete_action()
  351. {
  352. auto delete_action = GUI::CommonActions::make_delete_action([this](const GUI::Action&) {
  353. auto files = selected_file_paths();
  354. if (files.is_empty())
  355. return;
  356. String message;
  357. if (files.size() == 1) {
  358. LexicalPath file(files[0]);
  359. message = String::formatted("Really remove {} from disk?", file.basename());
  360. } else {
  361. message = String::formatted("Really remove {} files from disk?", files.size());
  362. }
  363. auto result = GUI::MessageBox::show(window(),
  364. message,
  365. "Confirm deletion",
  366. GUI::MessageBox::Type::Warning,
  367. GUI::MessageBox::InputType::OKCancel);
  368. if (result == GUI::MessageBox::ExecCancel)
  369. return;
  370. for (auto& file : files) {
  371. struct stat st;
  372. if (lstat(file.characters(), &st) < 0) {
  373. GUI::MessageBox::show(window(),
  374. String::formatted("lstat ({}) failed: {}", file, strerror(errno)),
  375. "Removal failed",
  376. GUI::MessageBox::Type::Error);
  377. break;
  378. }
  379. bool is_directory = S_ISDIR(st.st_mode);
  380. auto result = Core::File::remove(file, Core::File::RecursionMode::Allowed, false);
  381. if (result.is_error()) {
  382. auto& error = result.error();
  383. if (is_directory) {
  384. GUI::MessageBox::show(window(),
  385. String::formatted("Removing directory {} from the project failed: {}", error.file, error.error_code),
  386. "Removal failed",
  387. GUI::MessageBox::Type::Error);
  388. } else {
  389. GUI::MessageBox::show(window(),
  390. String::formatted("Removing file {} from the project failed: {}", error.file, error.error_code),
  391. "Removal failed",
  392. GUI::MessageBox::Type::Error);
  393. }
  394. break;
  395. }
  396. }
  397. });
  398. delete_action->set_enabled(false);
  399. return delete_action;
  400. }
  401. NonnullRefPtr<GUI::Action> HackStudioWidget::create_new_project_action()
  402. {
  403. return GUI::Action::create("&New Project...", { Mod_Ctrl | Mod_Shift, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/hackstudio-project.png"), [this](const GUI::Action&) {
  404. auto dialog = NewProjectDialog::construct(window());
  405. dialog->set_icon(window()->icon());
  406. auto result = dialog->exec();
  407. if (result == GUI::Dialog::ExecResult::ExecOK && dialog->created_project_path().has_value())
  408. open_project(dialog->created_project_path().value());
  409. });
  410. }
  411. void HackStudioWidget::add_new_editor(GUI::Widget& parent)
  412. {
  413. auto wrapper = EditorWrapper::construct();
  414. if (m_action_tab_widget) {
  415. parent.insert_child_before(wrapper, *m_action_tab_widget);
  416. } else {
  417. parent.add_child(wrapper);
  418. }
  419. m_current_editor_wrapper = wrapper;
  420. m_all_editor_wrappers.append(wrapper);
  421. wrapper->editor().set_focus(true);
  422. wrapper->set_project_root(LexicalPath(m_project->root_path()));
  423. }
  424. NonnullRefPtr<GUI::Action> HackStudioWidget::create_switch_to_next_editor_action()
  425. {
  426. return GUI::Action::create("Switch to &Next Editor", { Mod_Ctrl, Key_E }, [this](auto&) {
  427. if (m_all_editor_wrappers.size() <= 1)
  428. return;
  429. Vector<EditorWrapper&> wrappers;
  430. m_editors_splitter->for_each_child_of_type<EditorWrapper>([this, &wrappers](auto& child) {
  431. wrappers.append(child);
  432. return IterationDecision::Continue;
  433. });
  434. for (size_t i = 0; i < wrappers.size(); ++i) {
  435. if (m_current_editor_wrapper.ptr() == &wrappers[i]) {
  436. if (i == wrappers.size() - 1)
  437. wrappers[0].editor().set_focus(true);
  438. else
  439. wrappers[i + 1].editor().set_focus(true);
  440. }
  441. }
  442. });
  443. }
  444. NonnullRefPtr<GUI::Action> HackStudioWidget::create_switch_to_previous_editor_action()
  445. {
  446. return GUI::Action::create("Switch to &Previous Editor", { Mod_Ctrl | Mod_Shift, Key_E }, [this](auto&) {
  447. if (m_all_editor_wrappers.size() <= 1)
  448. return;
  449. Vector<EditorWrapper&> wrappers;
  450. m_editors_splitter->for_each_child_of_type<EditorWrapper>([this, &wrappers](auto& child) {
  451. wrappers.append(child);
  452. return IterationDecision::Continue;
  453. });
  454. for (int i = wrappers.size() - 1; i >= 0; --i) {
  455. if (m_current_editor_wrapper.ptr() == &wrappers[i]) {
  456. if (i == 0)
  457. wrappers.last().editor().set_focus(true);
  458. else
  459. wrappers[i - 1].editor().set_focus(true);
  460. }
  461. }
  462. });
  463. }
  464. NonnullRefPtr<GUI::Action> HackStudioWidget::create_remove_current_editor_action()
  465. {
  466. return GUI::Action::create("&Remove Current Editor", { Mod_Alt | Mod_Shift, Key_E }, [this](auto&) {
  467. if (m_all_editor_wrappers.size() <= 1)
  468. return;
  469. auto wrapper = m_current_editor_wrapper;
  470. m_switch_to_next_editor->activate();
  471. m_editors_splitter->remove_child(*wrapper);
  472. m_all_editor_wrappers.remove_first_matching([&wrapper](auto& entry) { return entry == wrapper.ptr(); });
  473. update_actions();
  474. });
  475. }
  476. NonnullRefPtr<GUI::Action> HackStudioWidget::create_open_action()
  477. {
  478. return GUI::Action::create("&Open Project...", { Mod_Ctrl | Mod_Shift, Key_O }, Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), [this](auto&) {
  479. auto open_path = GUI::FilePicker::get_open_filepath(window(), "Open project", Core::StandardPaths::home_directory(), true);
  480. if (!open_path.has_value())
  481. return;
  482. open_project(open_path.value());
  483. update_actions();
  484. });
  485. }
  486. NonnullRefPtr<GUI::Action> HackStudioWidget::create_save_action()
  487. {
  488. return GUI::CommonActions::make_save_action([&](auto&) {
  489. if (active_file().is_empty())
  490. return;
  491. current_editor_wrapper().save();
  492. if (m_git_widget->initialized())
  493. m_git_widget->refresh();
  494. });
  495. }
  496. NonnullRefPtr<GUI::Action> HackStudioWidget::create_remove_current_terminal_action()
  497. {
  498. return GUI::Action::create("Remove &Current Terminal", { Mod_Alt | Mod_Shift, Key_T }, [this](auto&) {
  499. auto widget = m_action_tab_widget->active_widget();
  500. if (!widget)
  501. return;
  502. if (!is<TerminalWrapper>(widget))
  503. return;
  504. auto& terminal = *static_cast<TerminalWrapper*>(widget);
  505. if (!terminal.user_spawned())
  506. return;
  507. m_action_tab_widget->remove_tab(terminal);
  508. update_actions();
  509. });
  510. }
  511. NonnullRefPtr<GUI::Action> HackStudioWidget::create_add_editor_action()
  512. {
  513. return GUI::Action::create("Add New &Editor", { Mod_Ctrl | Mod_Alt, Key_E },
  514. Gfx::Bitmap::load_from_file("/res/icons/16x16/app-text-editor.png"),
  515. [this](auto&) {
  516. add_new_editor(*m_editors_splitter);
  517. update_actions();
  518. });
  519. }
  520. NonnullRefPtr<GUI::Action> HackStudioWidget::create_add_terminal_action()
  521. {
  522. return GUI::Action::create("Add New &Terminal", { Mod_Ctrl | Mod_Alt, Key_T },
  523. Gfx::Bitmap::load_from_file("/res/icons/16x16/app-terminal.png"),
  524. [this](auto&) {
  525. auto& terminal_wrapper = m_action_tab_widget->add_tab<TerminalWrapper>("Terminal");
  526. reveal_action_tab(terminal_wrapper);
  527. update_actions();
  528. terminal_wrapper.terminal().set_focus(true);
  529. });
  530. }
  531. void HackStudioWidget::reveal_action_tab(GUI::Widget& widget)
  532. {
  533. if (m_action_tab_widget->min_height() < 200)
  534. m_action_tab_widget->set_fixed_height(200);
  535. m_action_tab_widget->set_active_widget(&widget);
  536. }
  537. NonnullRefPtr<GUI::Action> HackStudioWidget::create_debug_action()
  538. {
  539. return GUI::Action::create("&Debug", Gfx::Bitmap::load_from_file("/res/icons/16x16/debug-run.png"), [this](auto&) {
  540. if (!Core::File::exists(get_project_executable_path())) {
  541. GUI::MessageBox::show(window(), String::formatted("Could not find file: {}. (did you build the project?)", get_project_executable_path()), "Error", GUI::MessageBox::Type::Error);
  542. return;
  543. }
  544. if (Debugger::the().session()) {
  545. GUI::MessageBox::show(window(), "Debugger is already running", "Error", GUI::MessageBox::Type::Error);
  546. return;
  547. }
  548. Debugger::the().set_executable_path(get_project_executable_path());
  549. m_debugger_thread = Threading::Thread::construct(Debugger::start_static);
  550. m_debugger_thread->start();
  551. m_stop_action->set_enabled(true);
  552. });
  553. }
  554. void HackStudioWidget::initialize_debugger()
  555. {
  556. Debugger::initialize(
  557. m_project->root_path(),
  558. [this](const PtraceRegisters& regs) {
  559. VERIFY(Debugger::the().session());
  560. const auto& debug_session = *Debugger::the().session();
  561. auto source_position = debug_session.get_source_position(regs.eip);
  562. if (!source_position.has_value()) {
  563. dbgln("Could not find source position for address: {:p}", regs.eip);
  564. return Debugger::HasControlPassedToUser::No;
  565. }
  566. dbgln("Debugger stopped at source position: {}:{}", source_position.value().file_path, source_position.value().line_number);
  567. Core::EventLoop::main().post_event(
  568. *window(),
  569. make<Core::DeferredInvocationEvent>(
  570. [this, source_position, &regs](auto&) {
  571. m_current_editor_in_execution = get_editor_of_file(source_position.value().file_path);
  572. if (m_current_editor_in_execution)
  573. m_current_editor_in_execution->editor().set_execution_position(source_position.value().line_number - 1);
  574. m_debug_info_widget->update_state(*Debugger::the().session(), regs);
  575. m_debug_info_widget->set_debug_actions_enabled(true);
  576. m_disassembly_widget->update_state(*Debugger::the().session(), regs);
  577. HackStudioWidget::reveal_action_tab(*m_debug_info_widget);
  578. }));
  579. Core::EventLoop::wake();
  580. return Debugger::HasControlPassedToUser::Yes;
  581. },
  582. [this]() {
  583. Core::EventLoop::main().post_event(*window(), make<Core::DeferredInvocationEvent>([this](auto&) {
  584. m_debug_info_widget->set_debug_actions_enabled(false);
  585. if (m_current_editor_in_execution)
  586. m_current_editor_in_execution->editor().clear_execution_position();
  587. }));
  588. Core::EventLoop::wake();
  589. },
  590. [this]() {
  591. Core::EventLoop::main().post_event(*window(), make<Core::DeferredInvocationEvent>([this](auto&) {
  592. m_debug_info_widget->set_debug_actions_enabled(false);
  593. if (m_current_editor_in_execution)
  594. m_current_editor_in_execution->editor().clear_execution_position();
  595. m_debug_info_widget->program_stopped();
  596. m_disassembly_widget->program_stopped();
  597. m_stop_action->set_enabled(false);
  598. m_debugger_thread.clear();
  599. HackStudioWidget::hide_action_tabs();
  600. GUI::MessageBox::show(window(), "Program Exited", "Debugger", GUI::MessageBox::Type::Information);
  601. }));
  602. Core::EventLoop::wake();
  603. });
  604. }
  605. String HackStudioWidget::get_full_path_of_serenity_source(const String& file)
  606. {
  607. auto path_parts = LexicalPath(file).parts();
  608. VERIFY(path_parts[0] == "..");
  609. path_parts.remove(0);
  610. StringBuilder relative_path_builder;
  611. relative_path_builder.join("/", path_parts);
  612. constexpr char SERENITY_LIBS_PREFIX[] = "/usr/src/serenity";
  613. LexicalPath serenity_sources_base(SERENITY_LIBS_PREFIX);
  614. return String::formatted("{}/{}", serenity_sources_base, relative_path_builder.to_string());
  615. }
  616. RefPtr<EditorWrapper> HackStudioWidget::get_editor_of_file(const String& filename)
  617. {
  618. String file_path = filename;
  619. // TODO: We can probably do a more specific condition here, something like
  620. // "if (file.starts_with("../Libraries/") || file.starts_with("../AK/"))"
  621. if (filename.starts_with("../")) {
  622. file_path = get_full_path_of_serenity_source(filename);
  623. }
  624. if (!open_file(file_path))
  625. return nullptr;
  626. return current_editor_wrapper();
  627. }
  628. String HackStudioWidget::get_project_executable_path() const
  629. {
  630. // FIXME: Dumb heuristic ahead!
  631. // e.g /my/project => /my/project/project
  632. // TODO: Perhaps a Makefile rule for getting the value of $(PROGRAM) would be better?
  633. return String::formatted("{}/{}", m_project->root_path(), LexicalPath::basename(m_project->root_path()));
  634. }
  635. void HackStudioWidget::build(TerminalWrapper& wrapper)
  636. {
  637. if (active_file().ends_with(".js"))
  638. wrapper.run_command(String::formatted("js -A {}", active_file()));
  639. else
  640. wrapper.run_command("make");
  641. }
  642. void HackStudioWidget::run(TerminalWrapper& wrapper)
  643. {
  644. if (active_file().ends_with(".js"))
  645. wrapper.run_command(String::formatted("js {}", active_file()));
  646. else
  647. wrapper.run_command("make run");
  648. }
  649. void HackStudioWidget::hide_action_tabs()
  650. {
  651. m_action_tab_widget->set_fixed_height(24);
  652. };
  653. Project& HackStudioWidget::project()
  654. {
  655. return *m_project;
  656. }
  657. void HackStudioWidget::set_current_editor_wrapper(RefPtr<EditorWrapper> editor_wrapper)
  658. {
  659. m_current_editor_wrapper = editor_wrapper;
  660. }
  661. void HackStudioWidget::configure_project_tree_view()
  662. {
  663. m_project_tree_view->set_model(m_project->model());
  664. m_project_tree_view->set_selection_mode(GUI::AbstractView::SelectionMode::MultiSelection);
  665. for (int column_index = 0; column_index < m_project->model().column_count(); ++column_index)
  666. m_project_tree_view->set_column_visible(column_index, false);
  667. m_project_tree_view->set_column_visible(GUI::FileSystemModel::Column::Name, true);
  668. m_project_tree_view->on_context_menu_request = [this](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  669. if (index.is_valid()) {
  670. m_project_tree_view_context_menu->popup(event.screen_position(), m_open_selected_action);
  671. }
  672. };
  673. m_project_tree_view->on_selection_change = [this] {
  674. m_open_selected_action->set_enabled(!m_project_tree_view->selection().is_empty());
  675. m_delete_action->set_enabled(!m_project_tree_view->selection().is_empty());
  676. };
  677. m_project_tree_view->on_activation = [this](auto& index) {
  678. auto full_path_to_file = m_project->model().full_path(index);
  679. open_file(full_path_to_file);
  680. };
  681. }
  682. void HackStudioWidget::create_open_files_view(GUI::Widget& parent)
  683. {
  684. m_open_files_view = parent.add<GUI::ListView>();
  685. auto open_files_model = GUI::ItemListModel<String>::create(m_open_files_vector);
  686. m_open_files_view->set_model(open_files_model);
  687. m_open_files_view->on_activation = [this](auto& index) {
  688. open_file(index.data().to_string());
  689. };
  690. }
  691. void HackStudioWidget::create_toolbar(GUI::Widget& parent)
  692. {
  693. auto& toolbar = parent.add<GUI::Toolbar>();
  694. toolbar.add_action(*m_new_file_action);
  695. toolbar.add_action(*m_new_directory_action);
  696. toolbar.add_action(*m_save_action);
  697. toolbar.add_action(*m_delete_action);
  698. toolbar.add_separator();
  699. toolbar.add_action(GUI::CommonActions::make_cut_action([this](auto&) { current_editor().cut_action().activate(); }));
  700. toolbar.add_action(GUI::CommonActions::make_copy_action([this](auto&) { current_editor().copy_action().activate(); }));
  701. toolbar.add_action(GUI::CommonActions::make_paste_action([this](auto&) { current_editor().paste_action().activate(); }));
  702. toolbar.add_separator();
  703. toolbar.add_action(GUI::CommonActions::make_undo_action([this](auto&) { current_editor().undo_action().activate(); }));
  704. toolbar.add_action(GUI::CommonActions::make_redo_action([this](auto&) { current_editor().redo_action().activate(); }));
  705. toolbar.add_separator();
  706. toolbar.add_action(*m_build_action);
  707. toolbar.add_separator();
  708. toolbar.add_action(*m_run_action);
  709. toolbar.add_action(*m_stop_action);
  710. toolbar.add_separator();
  711. toolbar.add_action(*m_debug_action);
  712. }
  713. NonnullRefPtr<GUI::Action> HackStudioWidget::create_build_action()
  714. {
  715. return GUI::Action::create("&Build", { Mod_Ctrl, Key_B }, Gfx::Bitmap::load_from_file("/res/icons/16x16/build.png"), [this](auto&) {
  716. if (warn_unsaved_changes("There are unsaved changes, do you want to save before building?") == ContinueDecision::No)
  717. return;
  718. reveal_action_tab(*m_terminal_wrapper);
  719. build(*m_terminal_wrapper);
  720. m_stop_action->set_enabled(true);
  721. });
  722. }
  723. NonnullRefPtr<GUI::Action> HackStudioWidget::create_run_action()
  724. {
  725. return GUI::Action::create("&Run", { Mod_Ctrl, Key_R }, Gfx::Bitmap::load_from_file("/res/icons/16x16/program-run.png"), [this](auto&) {
  726. reveal_action_tab(*m_terminal_wrapper);
  727. run(*m_terminal_wrapper);
  728. m_stop_action->set_enabled(true);
  729. });
  730. }
  731. void HackStudioWidget::create_action_tab(GUI::Widget& parent)
  732. {
  733. m_action_tab_widget = parent.add<GUI::TabWidget>();
  734. m_action_tab_widget->set_fixed_height(24);
  735. m_action_tab_widget->on_change = [this](auto&) {
  736. on_action_tab_change();
  737. static bool first_time = true;
  738. if (!first_time)
  739. m_action_tab_widget->set_fixed_height(200);
  740. first_time = false;
  741. };
  742. m_find_in_files_widget = m_action_tab_widget->add_tab<FindInFilesWidget>("Find in files");
  743. m_todo_entries_widget = m_action_tab_widget->add_tab<ToDoEntriesWidget>("TODO");
  744. m_terminal_wrapper = m_action_tab_widget->add_tab<TerminalWrapper>("Build", false);
  745. m_debug_info_widget = m_action_tab_widget->add_tab<DebugInfoWidget>("Debug");
  746. m_disassembly_widget = m_action_tab_widget->add_tab<DisassemblyWidget>("Disassembly");
  747. m_git_widget = m_action_tab_widget->add_tab<GitWidget>("Git", LexicalPath(m_project->root_path()));
  748. m_git_widget->set_view_diff_callback([this](const auto& original_content, const auto& diff) {
  749. m_diff_viewer->set_content(original_content, diff);
  750. set_edit_mode(EditMode::Diff);
  751. });
  752. ToDoEntries::the().on_update = [this]() {
  753. m_todo_entries_widget->refresh();
  754. };
  755. }
  756. void HackStudioWidget::create_project_tab(GUI::Widget& parent)
  757. {
  758. m_project_tab = parent.add<GUI::TabWidget>();
  759. m_project_tab->set_tab_position(GUI::TabWidget::TabPosition::Bottom);
  760. auto& tree_view_container = m_project_tab->add_tab<GUI::Widget>("Files");
  761. tree_view_container.set_layout<GUI::VerticalBoxLayout>();
  762. tree_view_container.layout()->set_margins({ 2, 2, 2, 2 });
  763. m_project_tree_view = tree_view_container.add<GUI::TreeView>();
  764. configure_project_tree_view();
  765. auto& class_view_container = m_project_tab->add_tab<GUI::Widget>("Classes");
  766. class_view_container.set_layout<GUI::VerticalBoxLayout>();
  767. class_view_container.layout()->set_margins({ 2, 2, 2, 2 });
  768. m_class_view = class_view_container.add<ClassViewWidget>();
  769. ProjectDeclarations::the().on_update = [this]() {
  770. m_class_view->refresh();
  771. };
  772. }
  773. void HackStudioWidget::create_file_menubar(GUI::Menubar& menubar)
  774. {
  775. auto& file_menu = menubar.add_menu("&File");
  776. file_menu.add_action(*m_new_project_action);
  777. file_menu.add_action(*m_open_action);
  778. file_menu.add_action(*m_save_action);
  779. file_menu.add_separator();
  780. file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  781. GUI::Application::the()->quit();
  782. }));
  783. }
  784. void HackStudioWidget::create_project_menubar(GUI::Menubar& menubar)
  785. {
  786. auto& project_menu = menubar.add_menu("&Project");
  787. project_menu.add_action(*m_new_file_action);
  788. project_menu.add_action(*m_new_directory_action);
  789. }
  790. void HackStudioWidget::create_edit_menubar(GUI::Menubar& menubar)
  791. {
  792. auto& edit_menu = menubar.add_menu("&Edit");
  793. edit_menu.add_action(GUI::Action::create("&Find in Files...", { Mod_Ctrl | Mod_Shift, Key_F }, Gfx::Bitmap::load_from_file("/res/icons/16x16/find.png"), [this](auto&) {
  794. reveal_action_tab(*m_find_in_files_widget);
  795. m_find_in_files_widget->focus_textbox_and_select_all();
  796. }));
  797. edit_menu.add_separator();
  798. auto vim_emulation_setting_action = GUI::Action::create_checkable("&Vim Emulation", { Mod_Ctrl | Mod_Shift | Mod_Alt, Key_V }, [this](auto& action) {
  799. if (action.is_checked())
  800. current_editor().set_editing_engine(make<GUI::VimEditingEngine>());
  801. else
  802. current_editor().set_editing_engine(make<GUI::RegularEditingEngine>());
  803. });
  804. vim_emulation_setting_action->set_checked(false);
  805. edit_menu.add_action(vim_emulation_setting_action);
  806. }
  807. void HackStudioWidget::create_build_menubar(GUI::Menubar& menubar)
  808. {
  809. auto& build_menu = menubar.add_menu("&Build");
  810. build_menu.add_action(*m_build_action);
  811. build_menu.add_separator();
  812. build_menu.add_action(*m_run_action);
  813. build_menu.add_action(*m_stop_action);
  814. build_menu.add_separator();
  815. build_menu.add_action(*m_debug_action);
  816. }
  817. void HackStudioWidget::create_view_menubar(GUI::Menubar& menubar)
  818. {
  819. auto hide_action_tabs_action = GUI::Action::create("&Hide Action Tabs", { Mod_Ctrl | Mod_Shift, Key_X }, [this](auto&) {
  820. hide_action_tabs();
  821. });
  822. auto open_locator_action = GUI::Action::create("Open &Locator", { Mod_Ctrl, Key_K }, [this](auto&) {
  823. m_locator->open();
  824. });
  825. auto& view_menu = menubar.add_menu("&View");
  826. view_menu.add_action(hide_action_tabs_action);
  827. view_menu.add_action(open_locator_action);
  828. view_menu.add_separator();
  829. m_wrapping_mode_actions.set_exclusive(true);
  830. auto& wrapping_mode_menu = view_menu.add_submenu("&Wrapping Mode");
  831. m_no_wrapping_action = GUI::Action::create_checkable("&No Wrapping", [&](auto&) {
  832. for (auto& wrapper : m_all_editor_wrappers)
  833. wrapper.editor().set_wrapping_mode(GUI::TextEditor::WrappingMode::NoWrap);
  834. });
  835. m_wrap_anywhere_action = GUI::Action::create_checkable("Wrap &Anywhere", [&](auto&) {
  836. for (auto& wrapper : m_all_editor_wrappers)
  837. wrapper.editor().set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAnywhere);
  838. });
  839. m_wrap_at_words_action = GUI::Action::create_checkable("Wrap at &Words", [&](auto&) {
  840. for (auto& wrapper : m_all_editor_wrappers)
  841. wrapper.editor().set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAtWords);
  842. });
  843. m_wrapping_mode_actions.add_action(*m_no_wrapping_action);
  844. m_wrapping_mode_actions.add_action(*m_wrap_anywhere_action);
  845. m_wrapping_mode_actions.add_action(*m_wrap_at_words_action);
  846. wrapping_mode_menu.add_action(*m_no_wrapping_action);
  847. wrapping_mode_menu.add_action(*m_wrap_anywhere_action);
  848. wrapping_mode_menu.add_action(*m_wrap_at_words_action);
  849. m_no_wrapping_action->set_checked(true);
  850. view_menu.add_separator();
  851. view_menu.add_action(*m_add_editor_action);
  852. view_menu.add_action(*m_remove_current_editor_action);
  853. view_menu.add_action(*m_add_terminal_action);
  854. view_menu.add_action(*m_remove_current_terminal_action);
  855. }
  856. void HackStudioWidget::create_help_menubar(GUI::Menubar& menubar)
  857. {
  858. auto& help_menu = menubar.add_menu("&Help");
  859. help_menu.add_action(GUI::CommonActions::make_about_action("Hack Studio", GUI::Icon::default_icon("app-hack-studio"), window()));
  860. }
  861. NonnullRefPtr<GUI::Action> HackStudioWidget::create_stop_action()
  862. {
  863. auto action = GUI::Action::create("&Stop", Gfx::Bitmap::load_from_file("/res/icons/16x16/program-stop.png"), [this](auto&) {
  864. if (!Debugger::the().session()) {
  865. m_terminal_wrapper->kill_running_command();
  866. return;
  867. }
  868. Debugger::the().stop();
  869. });
  870. action->set_enabled(false);
  871. return action;
  872. }
  873. void HackStudioWidget::initialize_menubar(GUI::Menubar& menubar)
  874. {
  875. create_file_menubar(menubar);
  876. create_project_menubar(menubar);
  877. create_edit_menubar(menubar);
  878. create_build_menubar(menubar);
  879. create_view_menubar(menubar);
  880. create_help_menubar(menubar);
  881. }
  882. void HackStudioWidget::handle_external_file_deletion(const String& filepath)
  883. {
  884. m_open_files.remove(filepath);
  885. m_open_files_vector.remove_all_matching(
  886. [&filepath](const String& element) { return element == filepath; });
  887. for (auto& editor_wrapper : m_all_editor_wrappers) {
  888. Editor& editor = editor_wrapper.editor();
  889. String editor_file_path = editor.code_document().file_path();
  890. String relative_editor_file_path = LexicalPath::relative_path(editor_file_path, project().root_path());
  891. if (relative_editor_file_path == filepath) {
  892. if (m_open_files_vector.is_empty()) {
  893. editor.set_document(CodeDocument::create());
  894. editor_wrapper.set_filename("");
  895. } else {
  896. auto& first_path = m_open_files_vector[0];
  897. auto& document = m_open_files.get(first_path).value()->code_document();
  898. editor.set_document(document);
  899. editor_wrapper.set_filename(first_path);
  900. }
  901. }
  902. }
  903. m_open_files_view->model()->update();
  904. }
  905. HackStudioWidget::~HackStudioWidget()
  906. {
  907. if (!m_debugger_thread.is_null()) {
  908. Debugger::the().stop();
  909. dbgln("Waiting for debugger thread to terminate");
  910. auto rc = m_debugger_thread->join();
  911. if (rc.is_error()) {
  912. warnln("pthread_join: {}", strerror(rc.error().value()));
  913. dbgln("error joining debugger thread");
  914. }
  915. }
  916. }
  917. HackStudioWidget::ContinueDecision HackStudioWidget::warn_unsaved_changes(const String& prompt)
  918. {
  919. if (!any_document_is_dirty())
  920. return ContinueDecision::Yes;
  921. auto result = GUI::MessageBox::show(window(), prompt, "Unsaved changes", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel);
  922. if (result == GUI::MessageBox::ExecCancel)
  923. return ContinueDecision::No;
  924. if (result == GUI::MessageBox::ExecYes) {
  925. for (auto& editor_wrapper : m_all_editor_wrappers) {
  926. if (editor_wrapper.document_dirty()) {
  927. editor_wrapper.save();
  928. }
  929. }
  930. }
  931. return ContinueDecision::Yes;
  932. }
  933. bool HackStudioWidget::any_document_is_dirty() const
  934. {
  935. for (auto& editor_wrapper : m_all_editor_wrappers) {
  936. if (editor_wrapper.document_dirty()) {
  937. return true;
  938. }
  939. }
  940. return false;
  941. }
  942. }