HackStudioWidget.cpp 44 KB

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