HackStudioWidget.cpp 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  4. * Copyright (c) 2020-2022, 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/JsonParser.h>
  26. #include <AK/LexicalPath.h>
  27. #include <AK/StringBuilder.h>
  28. #include <Kernel/API/InodeWatcherEvent.h>
  29. #include <LibConfig/Client.h>
  30. #include <LibCore/Event.h>
  31. #include <LibCore/EventLoop.h>
  32. #include <LibCore/File.h>
  33. #include <LibCore/FileWatcher.h>
  34. #include <LibDebug/DebugSession.h>
  35. #include <LibDesktop/Launcher.h>
  36. #include <LibGUI/Action.h>
  37. #include <LibGUI/ActionGroup.h>
  38. #include <LibGUI/Application.h>
  39. #include <LibGUI/BoxLayout.h>
  40. #include <LibGUI/Button.h>
  41. #include <LibGUI/Dialog.h>
  42. #include <LibGUI/EditingEngine.h>
  43. #include <LibGUI/FilePicker.h>
  44. #include <LibGUI/FontPicker.h>
  45. #include <LibGUI/InputBox.h>
  46. #include <LibGUI/ItemListModel.h>
  47. #include <LibGUI/Label.h>
  48. #include <LibGUI/Menu.h>
  49. #include <LibGUI/Menubar.h>
  50. #include <LibGUI/MessageBox.h>
  51. #include <LibGUI/ModelEditingDelegate.h>
  52. #include <LibGUI/RegularEditingEngine.h>
  53. #include <LibGUI/Splitter.h>
  54. #include <LibGUI/StackWidget.h>
  55. #include <LibGUI/Statusbar.h>
  56. #include <LibGUI/TabWidget.h>
  57. #include <LibGUI/TableView.h>
  58. #include <LibGUI/TextBox.h>
  59. #include <LibGUI/TextEditor.h>
  60. #include <LibGUI/Toolbar.h>
  61. #include <LibGUI/ToolbarContainer.h>
  62. #include <LibGUI/TreeView.h>
  63. #include <LibGUI/VimEditingEngine.h>
  64. #include <LibGUI/Widget.h>
  65. #include <LibGUI/Window.h>
  66. #include <LibGfx/FontDatabase.h>
  67. #include <LibGfx/Palette.h>
  68. #include <LibThreading/Mutex.h>
  69. #include <LibThreading/Thread.h>
  70. #include <LibVT/TerminalWidget.h>
  71. #include <fcntl.h>
  72. #include <spawn.h>
  73. #include <stdio.h>
  74. #include <sys/stat.h>
  75. #include <sys/types.h>
  76. #include <sys/wait.h>
  77. #include <unistd.h>
  78. namespace HackStudio {
  79. HackStudioWidget::HackStudioWidget(String path_to_project)
  80. : m_editor_font(read_editor_font_from_config())
  81. {
  82. set_fill_with_background_color(true);
  83. set_layout<GUI::VerticalBoxLayout>();
  84. layout()->set_spacing(2);
  85. open_project(path_to_project);
  86. auto& toolbar_container = add<GUI::ToolbarContainer>();
  87. auto& outer_splitter = add<GUI::HorizontalSplitter>();
  88. outer_splitter.layout()->set_spacing(4);
  89. auto& left_hand_splitter = outer_splitter.add<GUI::VerticalSplitter>();
  90. left_hand_splitter.layout()->set_spacing(6);
  91. left_hand_splitter.set_fixed_width(150);
  92. create_project_tab(left_hand_splitter);
  93. m_project_tree_view_context_menu = create_project_tree_view_context_menu();
  94. create_open_files_view(left_hand_splitter);
  95. m_right_hand_splitter = outer_splitter.add<GUI::VerticalSplitter>();
  96. m_right_hand_stack = m_right_hand_splitter->add<GUI::StackWidget>();
  97. // Put a placeholder widget front & center since we don't have a file open yet.
  98. m_right_hand_stack->add<GUI::Widget>();
  99. m_diff_viewer = m_right_hand_stack->add<DiffViewer>();
  100. m_editors_splitter = m_right_hand_stack->add<GUI::VerticalSplitter>();
  101. m_editors_splitter->layout()->set_margins({ 3, 0, 0 });
  102. add_new_editor(*m_editors_splitter);
  103. m_switch_to_next_editor = create_switch_to_next_editor_action();
  104. m_switch_to_previous_editor = create_switch_to_previous_editor_action();
  105. m_remove_current_editor_action = create_remove_current_editor_action();
  106. m_open_action = create_open_action();
  107. m_save_action = create_save_action();
  108. m_save_as_action = create_save_as_action();
  109. m_new_project_action = create_new_project_action();
  110. create_action_tab(*m_right_hand_splitter);
  111. m_add_editor_action = create_add_editor_action();
  112. m_add_terminal_action = create_add_terminal_action();
  113. m_remove_current_terminal_action = create_remove_current_terminal_action();
  114. m_locator = add<Locator>();
  115. m_terminal_wrapper->on_command_exit = [this] {
  116. m_stop_action->set_enabled(false);
  117. };
  118. m_build_action = create_build_action();
  119. m_run_action = create_run_action();
  120. m_stop_action = create_stop_action();
  121. m_debug_action = create_debug_action();
  122. initialize_debugger();
  123. create_toolbar(toolbar_container);
  124. m_statusbar = add<GUI::Statusbar>(3);
  125. m_statusbar->segment(1).set_mode(GUI::Statusbar::Segment::Mode::Auto);
  126. m_statusbar->segment(2).set_mode(GUI::Statusbar::Segment::Mode::Fixed);
  127. auto width = font().width("Ln 0000, Col 000") + font().max_glyph_width();
  128. m_statusbar->segment(2).set_fixed_width(width);
  129. update_statusbar();
  130. GUI::Application::the()->on_action_enter = [this](GUI::Action& action) {
  131. auto text = action.status_tip();
  132. if (text.is_empty())
  133. text = Gfx::parse_ampersand_string(action.text());
  134. m_statusbar->set_override_text(move(text));
  135. };
  136. GUI::Application::the()->on_action_leave = [this](GUI::Action&) {
  137. m_statusbar->set_override_text({});
  138. };
  139. auto maybe_watcher = Core::FileWatcher::create();
  140. if (maybe_watcher.is_error()) {
  141. warnln("Couldn't create a file watcher, deleted files won't be noticed! Error: {}", maybe_watcher.error());
  142. } else {
  143. m_file_watcher = maybe_watcher.release_value();
  144. m_file_watcher->on_change = [this](Core::FileWatcherEvent const& event) {
  145. if (event.type != Core::FileWatcherEvent::Type::Deleted)
  146. return;
  147. if (event.event_path.starts_with(project().root_path())) {
  148. String relative_path = LexicalPath::relative_path(event.event_path, project().root_path());
  149. handle_external_file_deletion(relative_path);
  150. } else {
  151. handle_external_file_deletion(event.event_path);
  152. }
  153. };
  154. }
  155. m_project_builder = make<ProjectBuilder>(*m_terminal_wrapper, *m_project);
  156. }
  157. void HackStudioWidget::update_actions()
  158. {
  159. auto is_remove_terminal_enabled = [this]() {
  160. auto widget = m_action_tab_widget->active_widget();
  161. if (!widget)
  162. return false;
  163. if ("TerminalWrapper"sv != widget->class_name())
  164. return false;
  165. if (!reinterpret_cast<TerminalWrapper*>(widget)->user_spawned())
  166. return false;
  167. return true;
  168. };
  169. m_remove_current_editor_action->set_enabled(m_all_editor_wrappers.size() > 1);
  170. m_remove_current_terminal_action->set_enabled(is_remove_terminal_enabled());
  171. }
  172. void HackStudioWidget::on_action_tab_change()
  173. {
  174. update_actions();
  175. if (auto* active_widget = m_action_tab_widget->active_widget()) {
  176. if (is<GitWidget>(*active_widget))
  177. static_cast<GitWidget&>(*active_widget).refresh();
  178. }
  179. }
  180. Vector<String> HackStudioWidget::read_recent_projects()
  181. {
  182. auto json = Config::read_string("HackStudio", "Global", "RecentProjects");
  183. AK::JsonParser parser(json);
  184. auto value_or_error = parser.parse();
  185. if (value_or_error.is_error())
  186. return {};
  187. auto value = value_or_error.release_value();
  188. if (!value.is_array())
  189. return {};
  190. Vector<String> paths;
  191. for (auto& json_value : value.as_array().values()) {
  192. if (!json_value.is_string())
  193. return {};
  194. paths.append(json_value.as_string());
  195. }
  196. return paths;
  197. }
  198. void HackStudioWidget::open_project(const String& root_path)
  199. {
  200. if (warn_unsaved_changes("There are unsaved changes, do you want to save before closing current project?") == ContinueDecision::No)
  201. return;
  202. if (chdir(root_path.characters()) < 0) {
  203. perror("chdir");
  204. exit(1);
  205. }
  206. if (m_project) {
  207. close_current_project();
  208. }
  209. m_project = Project::open_with_root_path(root_path);
  210. VERIFY(m_project);
  211. if (m_project_tree_view) {
  212. m_project_tree_view->set_model(m_project->model());
  213. m_project_tree_view->update();
  214. }
  215. if (m_git_widget && m_git_widget->initialized()) {
  216. m_git_widget->change_repo(root_path);
  217. m_git_widget->refresh();
  218. }
  219. if (Debugger::is_initialized()) {
  220. auto& debugger = Debugger::the();
  221. debugger.reset_breakpoints();
  222. debugger.set_source_root(m_project->root_path());
  223. }
  224. for (auto& editor_wrapper : m_all_editor_wrappers)
  225. editor_wrapper.set_project_root(m_project->root_path());
  226. m_locations_history.clear();
  227. m_locations_history_end_index = 0;
  228. m_project->model().on_rename_successful = [this](auto& absolute_old_path, auto& absolute_new_path) {
  229. file_renamed(
  230. LexicalPath::relative_path(absolute_old_path, m_project->root_path()),
  231. LexicalPath::relative_path(absolute_new_path, m_project->root_path()));
  232. };
  233. auto recent_projects = read_recent_projects();
  234. recent_projects.remove_all_matching([&](auto& p) { return p == root_path; });
  235. recent_projects.insert(0, root_path);
  236. if (recent_projects.size() > recent_projects_history_size)
  237. recent_projects.shrink(recent_projects_history_size);
  238. Config::write_string("HackStudio", "Global", "RecentProjects", JsonArray(recent_projects).to_string());
  239. update_recent_projects_submenu();
  240. }
  241. Vector<String> HackStudioWidget::selected_file_paths() const
  242. {
  243. Vector<String> files;
  244. m_project_tree_view->selection().for_each_index([&](const GUI::ModelIndex& index) {
  245. String sub_path = index.data().as_string();
  246. GUI::ModelIndex parent_or_invalid = index.parent();
  247. while (parent_or_invalid.is_valid()) {
  248. sub_path = String::formatted("{}/{}", parent_or_invalid.data().as_string(), sub_path);
  249. parent_or_invalid = parent_or_invalid.parent();
  250. }
  251. files.append(sub_path);
  252. });
  253. return files;
  254. }
  255. bool HackStudioWidget::open_file(const String& full_filename, size_t line, size_t column)
  256. {
  257. String filename = full_filename;
  258. if (full_filename.starts_with(project().root_path())) {
  259. filename = LexicalPath::relative_path(full_filename, project().root_path());
  260. }
  261. if (Core::File::is_directory(filename) || !Core::File::exists(filename))
  262. return false;
  263. if (!active_file().is_empty()) {
  264. // Since the file is previously open, it should always be in m_open_files.
  265. VERIFY(m_open_files.find(active_file()) != m_open_files.end());
  266. auto previous_open_project_file = m_open_files.get(active_file()).value();
  267. // Update the scrollbar values of the previous_open_project_file and save them to m_open_files.
  268. previous_open_project_file->vertical_scroll_value(current_editor().vertical_scrollbar().value());
  269. previous_open_project_file->horizontal_scroll_value(current_editor().horizontal_scrollbar().value());
  270. }
  271. RefPtr<ProjectFile> new_project_file = nullptr;
  272. if (auto it = m_open_files.find(filename); it != m_open_files.end()) {
  273. new_project_file = it->value;
  274. } else {
  275. new_project_file = m_project->create_file(filename);
  276. m_open_files.set(filename, *new_project_file);
  277. m_open_files_vector.append(filename);
  278. if (!m_file_watcher.is_null()) {
  279. auto watch_result = m_file_watcher->add_watch(filename, Core::FileWatcherEvent::Type::Deleted);
  280. if (watch_result.is_error()) {
  281. warnln("Couldn't watch '{}'", filename);
  282. }
  283. }
  284. m_open_files_view->model()->invalidate();
  285. }
  286. current_editor().on_cursor_change = nullptr; // Disable callback while we're swapping the document.
  287. current_editor().set_document(const_cast<GUI::TextDocument&>(new_project_file->document()));
  288. if (new_project_file->could_render_text()) {
  289. current_editor_wrapper().set_mode_displayable();
  290. } else {
  291. current_editor_wrapper().set_mode_non_displayable();
  292. }
  293. current_editor().horizontal_scrollbar().set_value(new_project_file->horizontal_scroll_value());
  294. current_editor().vertical_scrollbar().set_value(new_project_file->vertical_scroll_value());
  295. if (current_editor().editing_engine()->is_regular())
  296. current_editor().set_editing_engine(make<GUI::RegularEditingEngine>());
  297. else if (current_editor().editing_engine()->is_vim())
  298. current_editor().set_editing_engine(make<GUI::VimEditingEngine>());
  299. else
  300. VERIFY_NOT_REACHED();
  301. set_edit_mode(EditMode::Text);
  302. String relative_file_path = filename;
  303. if (filename.starts_with(m_project->root_path()))
  304. relative_file_path = filename.substring(m_project->root_path().length() + 1);
  305. m_project_tree_view->update();
  306. current_editor_wrapper().set_filename(filename);
  307. current_editor().set_focus(true);
  308. current_editor().on_cursor_change = [this] { on_cursor_change(); };
  309. current_editor_wrapper().on_change = [this] { update_gml_preview(); };
  310. current_editor().set_cursor(line, column);
  311. update_gml_preview();
  312. return true;
  313. }
  314. void HackStudioWidget::close_file_in_all_editors(String const& filename)
  315. {
  316. m_open_files.remove(filename);
  317. m_open_files_vector.remove_all_matching(
  318. [&filename](String const& element) { return element == filename; });
  319. for (auto& editor_wrapper : m_all_editor_wrappers) {
  320. Editor& editor = editor_wrapper.editor();
  321. String editor_file_path = editor.code_document().file_path();
  322. String relative_editor_file_path = LexicalPath::relative_path(editor_file_path, project().root_path());
  323. if (relative_editor_file_path == filename) {
  324. if (m_open_files_vector.is_empty()) {
  325. editor.set_document(CodeDocument::create());
  326. editor_wrapper.set_filename("");
  327. } else {
  328. auto& first_path = m_open_files_vector[0];
  329. auto& document = m_open_files.get(first_path).value()->code_document();
  330. editor.set_document(document);
  331. editor_wrapper.set_filename(first_path);
  332. }
  333. }
  334. }
  335. m_open_files_view->model()->invalidate();
  336. }
  337. EditorWrapper& HackStudioWidget::current_editor_wrapper()
  338. {
  339. VERIFY(m_current_editor_wrapper);
  340. return *m_current_editor_wrapper;
  341. }
  342. EditorWrapper const& HackStudioWidget::current_editor_wrapper() const
  343. {
  344. VERIFY(m_current_editor_wrapper);
  345. return *m_current_editor_wrapper;
  346. }
  347. GUI::TextEditor& HackStudioWidget::current_editor()
  348. {
  349. return current_editor_wrapper().editor();
  350. }
  351. GUI::TextEditor const& HackStudioWidget::current_editor() const
  352. {
  353. return current_editor_wrapper().editor();
  354. }
  355. void HackStudioWidget::set_edit_mode(EditMode mode)
  356. {
  357. if (mode == EditMode::Text) {
  358. m_right_hand_stack->set_active_widget(m_editors_splitter);
  359. } else if (mode == EditMode::Diff) {
  360. m_right_hand_stack->set_active_widget(m_diff_viewer);
  361. } else {
  362. VERIFY_NOT_REACHED();
  363. }
  364. m_right_hand_stack->active_widget()->update();
  365. }
  366. NonnullRefPtr<GUI::Menu> HackStudioWidget::create_project_tree_view_context_menu()
  367. {
  368. m_new_file_actions.append(create_new_file_action("&C++ Source File", "/res/icons/16x16/filetype-cplusplus.png", "cpp"));
  369. m_new_file_actions.append(create_new_file_action("C++ &Header File", "/res/icons/16x16/filetype-header.png", "h"));
  370. m_new_file_actions.append(create_new_file_action("&GML File", "/res/icons/16x16/filetype-gml.png", "gml"));
  371. m_new_file_actions.append(create_new_file_action("P&ython Source File", "/res/icons/16x16/filetype-python.png", "py"));
  372. m_new_file_actions.append(create_new_file_action("Ja&va Source File", "/res/icons/16x16/filetype-java.png", "java"));
  373. m_new_file_actions.append(create_new_file_action("C Source File", "/res/icons/16x16/filetype-c.png", "c"));
  374. m_new_file_actions.append(create_new_file_action("&JavaScript Source File", "/res/icons/16x16/filetype-javascript.png", "js"));
  375. m_new_file_actions.append(create_new_file_action("HT&ML File", "/res/icons/16x16/filetype-html.png", "html"));
  376. m_new_file_actions.append(create_new_file_action("C&SS File", "/res/icons/16x16/filetype-css.png", "css"));
  377. m_new_file_actions.append(create_new_file_action("&PHP File", "/res/icons/16x16/filetype-php.png", "php"));
  378. m_new_file_actions.append(create_new_file_action("&Wasm File", "/res/icons/16x16/filetype-wasm.png", "wasm"));
  379. m_new_file_actions.append(create_new_file_action("&INI File", "/res/icons/16x16/filetype-ini.png", "ini"));
  380. m_new_file_actions.append(create_new_file_action("JS&ON File", "/res/icons/16x16/filetype-json.png", "json"));
  381. m_new_file_actions.append(create_new_file_action("Mark&down File", "/res/icons/16x16/filetype-markdown.png", "md"));
  382. m_new_plain_file_action = create_new_file_action("Plain &File", "/res/icons/16x16/new.png", "");
  383. m_open_selected_action = create_open_selected_action();
  384. m_show_in_file_manager_action = create_show_in_file_manager_action();
  385. m_new_directory_action = create_new_directory_action();
  386. m_delete_action = create_delete_action();
  387. m_tree_view_rename_action = GUI::CommonActions::make_rename_action([this](GUI::Action const&) {
  388. m_project_tree_view->begin_editing(m_project_tree_view->cursor_index());
  389. });
  390. auto project_tree_view_context_menu = GUI::Menu::construct("Project Files");
  391. auto& new_file_submenu = project_tree_view_context_menu->add_submenu("N&ew...");
  392. for (auto& new_file_action : m_new_file_actions) {
  393. new_file_submenu.add_action(new_file_action);
  394. }
  395. new_file_submenu.set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/new.png").release_value_but_fixme_should_propagate_errors());
  396. new_file_submenu.add_action(*m_new_plain_file_action);
  397. new_file_submenu.add_separator();
  398. new_file_submenu.add_action(*m_new_directory_action);
  399. project_tree_view_context_menu->add_action(*m_open_selected_action);
  400. project_tree_view_context_menu->add_action(*m_show_in_file_manager_action);
  401. // TODO: Cut, copy, duplicate with new name...
  402. project_tree_view_context_menu->add_separator();
  403. project_tree_view_context_menu->add_action(*m_tree_view_rename_action);
  404. project_tree_view_context_menu->add_action(*m_delete_action);
  405. return project_tree_view_context_menu;
  406. }
  407. NonnullRefPtr<GUI::Action> HackStudioWidget::create_new_file_action(String const& label, String const& icon, String const& extension)
  408. {
  409. return GUI::Action::create(label, Gfx::Bitmap::try_load_from_file(icon).release_value_but_fixme_should_propagate_errors(), [this, extension](const GUI::Action&) {
  410. String filename;
  411. if (GUI::InputBox::show(window(), filename, "Enter name of new file:", "Add new file to project") != GUI::InputBox::ExecOK)
  412. return;
  413. if (!extension.is_empty() && !filename.ends_with(String::formatted(".{}", extension))) {
  414. filename = String::formatted("{}.{}", filename, extension);
  415. }
  416. auto path_to_selected = selected_file_paths();
  417. String filepath;
  418. if (!path_to_selected.is_empty()) {
  419. VERIFY(Core::File::exists(path_to_selected.first()));
  420. LexicalPath selected(path_to_selected.first());
  421. String dir_path;
  422. if (Core::File::is_directory(selected.string()))
  423. dir_path = selected.string();
  424. else
  425. dir_path = selected.dirname();
  426. filepath = String::formatted("{}/", dir_path);
  427. }
  428. filepath = String::formatted("{}{}", filepath, filename);
  429. auto file = Core::File::construct(filepath);
  430. if (!file->open((Core::OpenMode)(Core::OpenMode::WriteOnly | Core::OpenMode::MustBeNew))) {
  431. GUI::MessageBox::show(window(), String::formatted("Failed to create '{}'", filepath), "Error", GUI::MessageBox::Type::Error);
  432. return;
  433. }
  434. open_file(filepath);
  435. });
  436. }
  437. NonnullRefPtr<GUI::Action> HackStudioWidget::create_new_directory_action()
  438. {
  439. return GUI::Action::create("&Directory...", { Mod_Ctrl | Mod_Shift, Key_N }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/mkdir.png").release_value_but_fixme_should_propagate_errors(), [this](const GUI::Action&) {
  440. String directory_name;
  441. if (GUI::InputBox::show(window(), directory_name, "Enter name of new directory:", "Add new folder to project") != GUI::InputBox::ExecOK)
  442. return;
  443. auto path_to_selected = selected_file_paths();
  444. if (!path_to_selected.is_empty()) {
  445. LexicalPath selected(path_to_selected.first());
  446. String dir_path;
  447. if (Core::File::is_directory(selected.string()))
  448. dir_path = selected.string();
  449. else
  450. dir_path = selected.dirname();
  451. directory_name = String::formatted("{}/{}", dir_path, directory_name);
  452. }
  453. auto formatted_dir_name = LexicalPath::canonicalized_path(String::formatted("{}/{}", m_project->model().root_path(), directory_name));
  454. int rc = mkdir(formatted_dir_name.characters(), 0755);
  455. if (rc < 0) {
  456. GUI::MessageBox::show(window(), "Failed to create new directory", "Error", GUI::MessageBox::Type::Error);
  457. return;
  458. }
  459. });
  460. }
  461. NonnullRefPtr<GUI::Action> HackStudioWidget::create_open_selected_action()
  462. {
  463. auto open_selected_action = GUI::Action::create("&Open", [this](const GUI::Action&) {
  464. auto files = selected_file_paths();
  465. for (auto& file : files)
  466. open_file(file);
  467. });
  468. open_selected_action->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/open.png").release_value_but_fixme_should_propagate_errors());
  469. open_selected_action->set_enabled(true);
  470. return open_selected_action;
  471. }
  472. NonnullRefPtr<GUI::Action> HackStudioWidget::create_show_in_file_manager_action()
  473. {
  474. auto show_in_file_manager_action = GUI::Action::create("Show in File &Manager", [this](const GUI::Action&) {
  475. auto files = selected_file_paths();
  476. for (auto& file : files)
  477. Desktop::Launcher::open(URL::create_with_file_protocol(m_project->root_path(), file));
  478. });
  479. show_in_file_manager_action->set_enabled(true);
  480. show_in_file_manager_action->set_icon(GUI::Icon::default_icon("app-file-manager").bitmap_for_size(16));
  481. return show_in_file_manager_action;
  482. }
  483. NonnullRefPtr<GUI::Action> HackStudioWidget::create_delete_action()
  484. {
  485. auto delete_action = GUI::CommonActions::make_delete_action([this](const GUI::Action&) {
  486. auto files = selected_file_paths();
  487. if (files.is_empty())
  488. return;
  489. String message;
  490. if (files.size() == 1) {
  491. LexicalPath file(files[0]);
  492. message = String::formatted("Really remove {} from disk?", file.basename());
  493. } else {
  494. message = String::formatted("Really remove {} files from disk?", files.size());
  495. }
  496. auto result = GUI::MessageBox::show(window(),
  497. message,
  498. "Confirm deletion",
  499. GUI::MessageBox::Type::Warning,
  500. GUI::MessageBox::InputType::OKCancel);
  501. if (result == GUI::MessageBox::ExecCancel)
  502. return;
  503. for (auto& file : files) {
  504. struct stat st;
  505. if (lstat(file.characters(), &st) < 0) {
  506. GUI::MessageBox::show(window(),
  507. String::formatted("lstat ({}) failed: {}", file, strerror(errno)),
  508. "Removal failed",
  509. GUI::MessageBox::Type::Error);
  510. break;
  511. }
  512. bool is_directory = S_ISDIR(st.st_mode);
  513. if (auto result = Core::File::remove(file, Core::File::RecursionMode::Allowed, false); result.is_error()) {
  514. auto& error = result.error();
  515. if (is_directory) {
  516. GUI::MessageBox::show(window(),
  517. String::formatted("Removing directory {} from the project failed: {}", error.file, static_cast<Error const&>(error)),
  518. "Removal failed",
  519. GUI::MessageBox::Type::Error);
  520. } else {
  521. GUI::MessageBox::show(window(),
  522. String::formatted("Removing file {} from the project failed: {}", error.file, static_cast<Error const&>(error)),
  523. "Removal failed",
  524. GUI::MessageBox::Type::Error);
  525. }
  526. }
  527. }
  528. },
  529. m_project_tree_view);
  530. delete_action->set_enabled(false);
  531. return delete_action;
  532. }
  533. NonnullRefPtr<GUI::Action> HackStudioWidget::create_new_project_action()
  534. {
  535. return GUI::Action::create("&Project...", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/hackstudio-project.png").release_value_but_fixme_should_propagate_errors(), [this](const GUI::Action&) {
  536. auto dialog = NewProjectDialog::construct(window());
  537. dialog->set_icon(window()->icon());
  538. auto result = dialog->exec();
  539. if (result == GUI::Dialog::ExecResult::ExecOK && dialog->created_project_path().has_value())
  540. open_project(dialog->created_project_path().value());
  541. });
  542. }
  543. void HackStudioWidget::add_new_editor(GUI::Widget& parent)
  544. {
  545. auto wrapper = EditorWrapper::construct();
  546. if (m_action_tab_widget) {
  547. parent.insert_child_before(wrapper, *m_action_tab_widget);
  548. } else {
  549. parent.add_child(wrapper);
  550. }
  551. auto previous_editor_wrapper = m_current_editor_wrapper;
  552. m_current_editor_wrapper = wrapper;
  553. m_all_editor_wrappers.append(wrapper);
  554. wrapper->editor().set_focus(true);
  555. wrapper->editor().set_font(*m_editor_font);
  556. wrapper->set_project_root(m_project->root_path());
  557. wrapper->editor().on_cursor_change = [this] { on_cursor_change(); };
  558. wrapper->on_change = [this] { update_gml_preview(); };
  559. set_edit_mode(EditMode::Text);
  560. if (previous_editor_wrapper && previous_editor_wrapper->editor().editing_engine()->is_regular())
  561. wrapper->editor().set_editing_engine(make<GUI::RegularEditingEngine>());
  562. else if (previous_editor_wrapper && previous_editor_wrapper->editor().editing_engine()->is_vim())
  563. wrapper->editor().set_editing_engine(make<GUI::VimEditingEngine>());
  564. else
  565. wrapper->editor().set_editing_engine(make<GUI::RegularEditingEngine>());
  566. }
  567. NonnullRefPtr<GUI::Action> HackStudioWidget::create_switch_to_next_editor_action()
  568. {
  569. return GUI::Action::create("Switch to &Next Editor", { Mod_Ctrl, Key_E }, [this](auto&) {
  570. if (m_all_editor_wrappers.size() <= 1)
  571. return;
  572. Vector<EditorWrapper&> wrappers;
  573. m_editors_splitter->for_each_child_of_type<EditorWrapper>([&wrappers](auto& child) {
  574. wrappers.append(child);
  575. return IterationDecision::Continue;
  576. });
  577. for (size_t i = 0; i < wrappers.size(); ++i) {
  578. if (m_current_editor_wrapper.ptr() == &wrappers[i]) {
  579. if (i == wrappers.size() - 1)
  580. wrappers[0].editor().set_focus(true);
  581. else
  582. wrappers[i + 1].editor().set_focus(true);
  583. }
  584. }
  585. });
  586. }
  587. NonnullRefPtr<GUI::Action> HackStudioWidget::create_switch_to_previous_editor_action()
  588. {
  589. return GUI::Action::create("Switch to &Previous Editor", { Mod_Ctrl | Mod_Shift, Key_E }, [this](auto&) {
  590. if (m_all_editor_wrappers.size() <= 1)
  591. return;
  592. Vector<EditorWrapper&> wrappers;
  593. m_editors_splitter->for_each_child_of_type<EditorWrapper>([&wrappers](auto& child) {
  594. wrappers.append(child);
  595. return IterationDecision::Continue;
  596. });
  597. for (int i = wrappers.size() - 1; i >= 0; --i) {
  598. if (m_current_editor_wrapper.ptr() == &wrappers[i]) {
  599. if (i == 0)
  600. wrappers.last().editor().set_focus(true);
  601. else
  602. wrappers[i - 1].editor().set_focus(true);
  603. }
  604. }
  605. });
  606. }
  607. NonnullRefPtr<GUI::Action> HackStudioWidget::create_remove_current_editor_action()
  608. {
  609. return GUI::Action::create("&Remove Current Editor", { Mod_Alt | Mod_Shift, Key_E }, Gfx::Bitmap::try_load_from_file("/res/icons/hackstudio/remove-editor.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  610. if (m_all_editor_wrappers.size() <= 1)
  611. return;
  612. auto wrapper = m_current_editor_wrapper;
  613. m_switch_to_next_editor->activate();
  614. m_editors_splitter->remove_child(*wrapper);
  615. m_all_editor_wrappers.remove_first_matching([&wrapper](auto& entry) { return entry == wrapper.ptr(); });
  616. update_actions();
  617. });
  618. }
  619. NonnullRefPtr<GUI::Action> HackStudioWidget::create_open_action()
  620. {
  621. return GUI::Action::create("&Open Project...", { Mod_Ctrl | Mod_Shift, Key_O }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/open.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  622. auto open_path = GUI::FilePicker::get_open_filepath(window(), "Open project", m_project->root_path(), true);
  623. if (!open_path.has_value())
  624. return;
  625. open_project(open_path.value());
  626. update_actions();
  627. });
  628. }
  629. NonnullRefPtr<GUI::Action> HackStudioWidget::create_save_action()
  630. {
  631. return GUI::CommonActions::make_save_action([&](auto&) {
  632. if (active_file().is_empty())
  633. m_save_as_action->activate();
  634. // NOTE active_file() could still be empty after a cancelled save_as_action
  635. if (!active_file().is_empty())
  636. current_editor_wrapper().save();
  637. if (m_git_widget->initialized())
  638. m_git_widget->refresh();
  639. });
  640. }
  641. NonnullRefPtr<GUI::Action> HackStudioWidget::create_save_as_action()
  642. {
  643. return GUI::CommonActions::make_save_as_action([&](auto&) {
  644. auto const old_filename = current_editor_wrapper().filename();
  645. LexicalPath const old_path(old_filename);
  646. Optional<String> save_path = GUI::FilePicker::get_save_filepath(window(),
  647. old_filename.is_null() ? "Untitled" : old_path.title(),
  648. old_filename.is_null() ? "txt" : old_path.extension(),
  649. Core::File::absolute_path(old_path.dirname()));
  650. if (!save_path.has_value()) {
  651. return;
  652. }
  653. String const relative_file_path = LexicalPath::relative_path(save_path.value(), m_project->root_path());
  654. if (current_editor_wrapper().filename().is_null()) {
  655. current_editor_wrapper().set_filename(relative_file_path);
  656. } else {
  657. for (auto& editor_wrapper : m_all_editor_wrappers) {
  658. if (editor_wrapper.filename() == old_filename)
  659. editor_wrapper.set_filename(relative_file_path);
  660. }
  661. }
  662. current_editor_wrapper().save();
  663. auto new_project_file = m_project->create_file(relative_file_path);
  664. m_open_files.set(relative_file_path, *new_project_file);
  665. m_open_files.remove(old_filename);
  666. m_open_files_vector.append(relative_file_path);
  667. m_open_files_vector.remove_all_matching([&old_filename](auto const& element) { return element == old_filename; });
  668. update_window_title();
  669. m_project->model().invalidate();
  670. update_tree_view();
  671. });
  672. }
  673. NonnullRefPtr<GUI::Action> HackStudioWidget::create_remove_current_terminal_action()
  674. {
  675. return GUI::Action::create("Remove &Current Terminal", { Mod_Alt | Mod_Shift, Key_T }, Gfx::Bitmap::try_load_from_file("/res/icons/hackstudio/remove-terminal.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  676. auto widget = m_action_tab_widget->active_widget();
  677. if (!widget)
  678. return;
  679. if (!is<TerminalWrapper>(widget))
  680. return;
  681. auto& terminal = *static_cast<TerminalWrapper*>(widget);
  682. if (!terminal.user_spawned())
  683. return;
  684. m_action_tab_widget->remove_tab(terminal);
  685. update_actions();
  686. });
  687. }
  688. NonnullRefPtr<GUI::Action> HackStudioWidget::create_add_editor_action()
  689. {
  690. return GUI::Action::create("Add New &Editor", { Mod_Ctrl | Mod_Alt, Key_E },
  691. Gfx::Bitmap::try_load_from_file("/res/icons/hackstudio/add-editor.png").release_value_but_fixme_should_propagate_errors(),
  692. [this](auto&) {
  693. add_new_editor(*m_editors_splitter);
  694. update_actions();
  695. });
  696. }
  697. NonnullRefPtr<GUI::Action> HackStudioWidget::create_add_terminal_action()
  698. {
  699. return GUI::Action::create("Add New &Terminal", { Mod_Ctrl | Mod_Alt, Key_T },
  700. Gfx::Bitmap::try_load_from_file("/res/icons/hackstudio/add-terminal.png").release_value_but_fixme_should_propagate_errors(),
  701. [this](auto&) {
  702. auto& terminal_wrapper = m_action_tab_widget->add_tab<TerminalWrapper>("Terminal");
  703. terminal_wrapper.on_command_exit = [&]() {
  704. deferred_invoke([this]() {
  705. m_action_tab_widget->remove_tab(*m_action_tab_widget->active_widget());
  706. });
  707. };
  708. reveal_action_tab(terminal_wrapper);
  709. update_actions();
  710. terminal_wrapper.terminal().set_focus(true);
  711. });
  712. }
  713. void HackStudioWidget::reveal_action_tab(GUI::Widget& widget)
  714. {
  715. if (m_action_tab_widget->min_height() < 200)
  716. m_action_tab_widget->set_fixed_height(200);
  717. m_action_tab_widget->set_active_widget(&widget);
  718. }
  719. NonnullRefPtr<GUI::Action> HackStudioWidget::create_debug_action()
  720. {
  721. return GUI::Action::create("&Debug", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/debug-run.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  722. if (!Core::File::exists(get_project_executable_path())) {
  723. GUI::MessageBox::show(window(), String::formatted("Could not find file: {}. (did you build the project?)", get_project_executable_path()), "Error", GUI::MessageBox::Type::Error);
  724. return;
  725. }
  726. if (Debugger::the().session()) {
  727. GUI::MessageBox::show(window(), "Debugger is already running", "Error", GUI::MessageBox::Type::Error);
  728. return;
  729. }
  730. Debugger::the().set_executable_path(get_project_executable_path());
  731. m_terminal_wrapper->clear_including_history();
  732. // The debugger calls wait() on the debugee, so the TerminalWrapper can't do that.
  733. auto ptm_res = m_terminal_wrapper->setup_master_pseudoterminal(TerminalWrapper::WaitForChildOnExit::No);
  734. if (ptm_res.is_error()) {
  735. perror("setup_master_pseudoterminal");
  736. return;
  737. }
  738. Debugger::the().set_child_setup_callback([this, ptm_res]() {
  739. return m_terminal_wrapper->setup_slave_pseudoterminal(ptm_res.value());
  740. });
  741. m_debugger_thread = Threading::Thread::construct(Debugger::start_static);
  742. m_debugger_thread->start();
  743. m_stop_action->set_enabled(true);
  744. m_run_action->set_enabled(false);
  745. for (auto& editor_wrapper : m_all_editor_wrappers) {
  746. editor_wrapper.set_debug_mode(true);
  747. }
  748. });
  749. }
  750. void HackStudioWidget::initialize_debugger()
  751. {
  752. Debugger::initialize(
  753. m_project->root_path(),
  754. [this](const PtraceRegisters& regs) {
  755. VERIFY(Debugger::the().session());
  756. const auto& debug_session = *Debugger::the().session();
  757. auto source_position = debug_session.get_source_position(regs.ip());
  758. if (!source_position.has_value()) {
  759. dbgln("Could not find source position for address: {:p}", regs.ip());
  760. return Debugger::HasControlPassedToUser::No;
  761. }
  762. dbgln("Debugger stopped at source position: {}:{}", source_position.value().file_path, source_position.value().line_number);
  763. deferred_invoke([this, source_position, &regs] {
  764. m_current_editor_in_execution = get_editor_of_file(source_position.value().file_path);
  765. if (m_current_editor_in_execution)
  766. m_current_editor_in_execution->editor().set_execution_position(source_position.value().line_number - 1);
  767. m_debug_info_widget->update_state(*Debugger::the().session(), regs);
  768. m_debug_info_widget->set_debug_actions_enabled(true);
  769. m_disassembly_widget->update_state(*Debugger::the().session(), regs);
  770. HackStudioWidget::reveal_action_tab(*m_debug_info_widget);
  771. });
  772. Core::EventLoop::wake_current();
  773. return Debugger::HasControlPassedToUser::Yes;
  774. },
  775. [this]() {
  776. deferred_invoke([this] {
  777. m_debug_info_widget->set_debug_actions_enabled(false);
  778. if (m_current_editor_in_execution)
  779. m_current_editor_in_execution->editor().clear_execution_position();
  780. });
  781. Core::EventLoop::wake_current();
  782. },
  783. [this]() {
  784. deferred_invoke([this] {
  785. m_debug_info_widget->set_debug_actions_enabled(false);
  786. if (m_current_editor_in_execution)
  787. m_current_editor_in_execution->editor().clear_execution_position();
  788. m_debug_info_widget->program_stopped();
  789. m_disassembly_widget->program_stopped();
  790. m_stop_action->set_enabled(false);
  791. m_run_action->set_enabled(true);
  792. m_debugger_thread.clear();
  793. for (auto& editor_wrapper : m_all_editor_wrappers) {
  794. editor_wrapper.set_debug_mode(false);
  795. }
  796. HackStudioWidget::hide_action_tabs();
  797. GUI::MessageBox::show(window(), "Program Exited", "Debugger", GUI::MessageBox::Type::Information);
  798. });
  799. Core::EventLoop::wake_current();
  800. });
  801. }
  802. String HackStudioWidget::get_full_path_of_serenity_source(const String& file)
  803. {
  804. auto path_parts = LexicalPath(file).parts();
  805. while (!path_parts.is_empty() && path_parts[0] == "..") {
  806. path_parts.remove(0);
  807. }
  808. StringBuilder relative_path_builder;
  809. relative_path_builder.join("/", path_parts);
  810. constexpr char SERENITY_LIBS_PREFIX[] = "/usr/src/serenity";
  811. LexicalPath serenity_sources_base(SERENITY_LIBS_PREFIX);
  812. return String::formatted("{}/{}", serenity_sources_base, relative_path_builder.to_string());
  813. }
  814. String HackStudioWidget::get_absolute_path(const String& path) const
  815. {
  816. // TODO: We can probably do a more specific condition here, something like
  817. // "if (file.starts_with("../Libraries/") || file.starts_with("../AK/"))"
  818. if (path.starts_with("..")) {
  819. return get_full_path_of_serenity_source(path);
  820. }
  821. return m_project->to_absolute_path(path);
  822. }
  823. RefPtr<EditorWrapper> HackStudioWidget::get_editor_of_file(const String& filename)
  824. {
  825. String file_path = filename;
  826. if (filename.starts_with("../")) {
  827. file_path = get_full_path_of_serenity_source(filename);
  828. }
  829. if (!open_file(file_path))
  830. return nullptr;
  831. return current_editor_wrapper();
  832. }
  833. String HackStudioWidget::get_project_executable_path() const
  834. {
  835. // FIXME: Dumb heuristic ahead!
  836. // e.g /my/project => /my/project/project
  837. // TODO: Perhaps a Makefile rule for getting the value of $(PROGRAM) would be better?
  838. return String::formatted("{}/{}", m_project->root_path(), LexicalPath::basename(m_project->root_path()));
  839. }
  840. void HackStudioWidget::build()
  841. {
  842. auto result = m_project_builder->build(active_file());
  843. if (result.is_error()) {
  844. GUI::MessageBox::show(window(), String::formatted("{}", result.error()), "Build failed", GUI::MessageBox::Type::Error);
  845. }
  846. }
  847. void HackStudioWidget::run()
  848. {
  849. auto result = m_project_builder->run(active_file());
  850. if (result.is_error()) {
  851. GUI::MessageBox::show(window(), String::formatted("{}", result.error()), "Run failed", GUI::MessageBox::Type::Error);
  852. }
  853. }
  854. void HackStudioWidget::hide_action_tabs()
  855. {
  856. m_action_tab_widget->set_fixed_height(24);
  857. };
  858. Project& HackStudioWidget::project()
  859. {
  860. return *m_project;
  861. }
  862. void HackStudioWidget::set_current_editor_wrapper(RefPtr<EditorWrapper> editor_wrapper)
  863. {
  864. m_current_editor_wrapper = editor_wrapper;
  865. update_window_title();
  866. update_tree_view();
  867. }
  868. void HackStudioWidget::file_renamed(String const& old_name, String const& new_name)
  869. {
  870. auto editor_or_none = m_all_editor_wrappers.first_matching([&old_name](auto const& editor) {
  871. return editor->filename() == old_name;
  872. });
  873. if (editor_or_none.has_value()) {
  874. (*editor_or_none)->set_filename(new_name);
  875. (*editor_or_none)->set_name(new_name);
  876. }
  877. if (m_open_files.contains(old_name)) {
  878. VERIFY(m_open_files_vector.remove_first_matching([&old_name](auto const& file) { return file == old_name; }));
  879. m_open_files_vector.append(new_name);
  880. ProjectFile* f = m_open_files.get(old_name).release_value();
  881. m_open_files.set(new_name, *f);
  882. m_open_files.remove(old_name);
  883. m_open_files_view->model()->invalidate();
  884. }
  885. if (m_file_watcher->is_watching(old_name)) {
  886. VERIFY(!m_file_watcher->remove_watch(old_name).is_error());
  887. VERIFY(!m_file_watcher->add_watch(new_name, Core::FileWatcherEvent::Type::Deleted).is_error());
  888. }
  889. }
  890. void HackStudioWidget::configure_project_tree_view()
  891. {
  892. m_project_tree_view->set_model(m_project->model());
  893. m_project_tree_view->set_selection_mode(GUI::AbstractView::SelectionMode::MultiSelection);
  894. m_project_tree_view->set_editable(true);
  895. m_project_tree_view->aid_create_editing_delegate = [](auto&) {
  896. return make<GUI::StringModelEditingDelegate>();
  897. };
  898. for (int column_index = 0; column_index < m_project->model().column_count(); ++column_index)
  899. m_project_tree_view->set_column_visible(column_index, false);
  900. m_project_tree_view->set_column_visible(GUI::FileSystemModel::Column::Name, true);
  901. m_project_tree_view->on_context_menu_request = [this](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  902. if (index.is_valid()) {
  903. m_project_tree_view_context_menu->popup(event.screen_position(), m_open_selected_action);
  904. }
  905. };
  906. m_project_tree_view->on_selection_change = [this] {
  907. m_open_selected_action->set_enabled(!m_project_tree_view->selection().is_empty());
  908. auto selections = m_project_tree_view->selection().indices();
  909. auto it = selections.find_if([&](auto selected_file) {
  910. return access(m_project->model().full_path(selected_file.parent()).characters(), W_OK) == 0;
  911. });
  912. bool has_permissions = it != selections.end();
  913. m_tree_view_rename_action->set_enabled(has_permissions);
  914. m_delete_action->set_enabled(has_permissions);
  915. };
  916. m_project_tree_view->on_activation = [this](auto& index) {
  917. auto full_path_to_file = m_project->model().full_path(index);
  918. open_file(full_path_to_file);
  919. };
  920. }
  921. void HackStudioWidget::create_open_files_view(GUI::Widget& parent)
  922. {
  923. m_open_files_view = parent.add<GUI::ListView>();
  924. auto open_files_model = GUI::ItemListModel<String>::create(m_open_files_vector);
  925. m_open_files_view->set_model(open_files_model);
  926. m_open_files_view->on_activation = [this](auto& index) {
  927. open_file(index.data().to_string());
  928. };
  929. }
  930. void HackStudioWidget::create_toolbar(GUI::Widget& parent)
  931. {
  932. auto& toolbar = parent.add<GUI::Toolbar>();
  933. toolbar.add_action(*m_new_plain_file_action);
  934. toolbar.add_action(*m_new_directory_action);
  935. toolbar.add_action(*m_save_action);
  936. toolbar.add_action(*m_delete_action);
  937. toolbar.add_separator();
  938. toolbar.add_action(GUI::CommonActions::make_cut_action([this](auto&) { current_editor().cut_action().activate(); }));
  939. toolbar.add_action(GUI::CommonActions::make_copy_action([this](auto&) { current_editor().copy_action().activate(); }));
  940. toolbar.add_action(GUI::CommonActions::make_paste_action([this](auto&) { current_editor().paste_action().activate(); }));
  941. toolbar.add_separator();
  942. toolbar.add_action(GUI::CommonActions::make_undo_action([this](auto&) { current_editor().undo_action().activate(); }));
  943. toolbar.add_action(GUI::CommonActions::make_redo_action([this](auto&) { current_editor().redo_action().activate(); }));
  944. toolbar.add_separator();
  945. toolbar.add_action(*m_build_action);
  946. toolbar.add_separator();
  947. toolbar.add_action(*m_run_action);
  948. toolbar.add_action(*m_stop_action);
  949. toolbar.add_separator();
  950. toolbar.add_action(*m_debug_action);
  951. }
  952. NonnullRefPtr<GUI::Action> HackStudioWidget::create_build_action()
  953. {
  954. return GUI::Action::create("&Build", { Mod_Ctrl, Key_B }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/build.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  955. if (warn_unsaved_changes("There are unsaved changes, do you want to save before building?") == ContinueDecision::No)
  956. return;
  957. reveal_action_tab(*m_terminal_wrapper);
  958. build();
  959. m_stop_action->set_enabled(true);
  960. });
  961. }
  962. NonnullRefPtr<GUI::Action> HackStudioWidget::create_run_action()
  963. {
  964. return GUI::Action::create("&Run", { Mod_Ctrl, Key_R }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/program-run.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  965. reveal_action_tab(*m_terminal_wrapper);
  966. run();
  967. m_stop_action->set_enabled(true);
  968. });
  969. }
  970. void HackStudioWidget::create_action_tab(GUI::Widget& parent)
  971. {
  972. m_action_tab_widget = parent.add<GUI::TabWidget>();
  973. m_action_tab_widget->set_fixed_height(24);
  974. m_action_tab_widget->on_change = [this](auto&) {
  975. on_action_tab_change();
  976. static bool first_time = true;
  977. if (!first_time)
  978. m_action_tab_widget->set_fixed_height(200);
  979. first_time = false;
  980. };
  981. m_find_in_files_widget = m_action_tab_widget->add_tab<FindInFilesWidget>("Find in files");
  982. m_todo_entries_widget = m_action_tab_widget->add_tab<ToDoEntriesWidget>("TODO");
  983. m_terminal_wrapper = m_action_tab_widget->add_tab<TerminalWrapper>("Console", false);
  984. m_debug_info_widget = m_action_tab_widget->add_tab<DebugInfoWidget>("Debug");
  985. m_debug_info_widget->on_backtrace_frame_selection = [this](Debug::DebugInfo::SourcePosition const& source_position) {
  986. open_file(get_absolute_path(source_position.file_path), source_position.line_number - 1);
  987. };
  988. m_disassembly_widget = m_action_tab_widget->add_tab<DisassemblyWidget>("Disassembly");
  989. m_git_widget = m_action_tab_widget->add_tab<GitWidget>("Git", m_project->root_path());
  990. m_git_widget->set_view_diff_callback([this](const auto& original_content, const auto& diff) {
  991. m_diff_viewer->set_content(original_content, diff);
  992. set_edit_mode(EditMode::Diff);
  993. });
  994. m_gml_preview_widget = m_action_tab_widget->add_tab<GMLPreviewWidget>("GML Preview", "");
  995. ToDoEntries::the().on_update = [this]() {
  996. m_todo_entries_widget->refresh();
  997. };
  998. }
  999. void HackStudioWidget::create_project_tab(GUI::Widget& parent)
  1000. {
  1001. m_project_tab = parent.add<GUI::TabWidget>();
  1002. m_project_tab->set_tab_position(GUI::TabWidget::TabPosition::Bottom);
  1003. auto& tree_view_container = m_project_tab->add_tab<GUI::Widget>("Files");
  1004. tree_view_container.set_layout<GUI::VerticalBoxLayout>();
  1005. tree_view_container.layout()->set_margins(2);
  1006. m_project_tree_view = tree_view_container.add<GUI::TreeView>();
  1007. configure_project_tree_view();
  1008. auto& class_view_container = m_project_tab->add_tab<GUI::Widget>("Classes");
  1009. class_view_container.set_layout<GUI::VerticalBoxLayout>();
  1010. class_view_container.layout()->set_margins(2);
  1011. m_class_view = class_view_container.add<ClassViewWidget>();
  1012. ProjectDeclarations::the().on_update = [this]() {
  1013. m_class_view->refresh();
  1014. };
  1015. }
  1016. void HackStudioWidget::update_recent_projects_submenu()
  1017. {
  1018. if (!m_recent_projects_submenu)
  1019. return;
  1020. m_recent_projects_submenu->remove_all_actions();
  1021. auto recent_projects = read_recent_projects();
  1022. if (recent_projects.size() <= 1) {
  1023. auto empty_action = GUI::Action::create("Empty...", [](auto&) {});
  1024. empty_action->set_enabled(false);
  1025. m_recent_projects_submenu->add_action(empty_action);
  1026. return;
  1027. }
  1028. for (size_t i = 1; i < recent_projects.size(); i++) {
  1029. auto project_path = recent_projects[i];
  1030. m_recent_projects_submenu->add_action(GUI::Action::create(recent_projects[i], [this, project_path](auto&) {
  1031. open_project(project_path);
  1032. }));
  1033. }
  1034. }
  1035. void HackStudioWidget::create_file_menu(GUI::Window& window)
  1036. {
  1037. auto& file_menu = window.add_menu("&File");
  1038. auto& new_submenu = file_menu.add_submenu("&New...");
  1039. new_submenu.add_action(*m_new_project_action);
  1040. new_submenu.add_separator();
  1041. for (auto& new_file_action : m_new_file_actions) {
  1042. new_submenu.add_action(new_file_action);
  1043. }
  1044. new_submenu.set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/new.png").release_value_but_fixme_should_propagate_errors());
  1045. new_submenu.add_action(*m_new_plain_file_action);
  1046. new_submenu.add_separator();
  1047. new_submenu.add_action(*m_new_directory_action);
  1048. file_menu.add_action(*m_open_action);
  1049. m_recent_projects_submenu = &file_menu.add_submenu("Open &Recent");
  1050. m_recent_projects_submenu->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/open-recent.png").release_value_but_fixme_should_propagate_errors());
  1051. update_recent_projects_submenu();
  1052. file_menu.add_action(*m_save_action);
  1053. file_menu.add_action(*m_save_as_action);
  1054. file_menu.add_separator();
  1055. file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  1056. GUI::Application::the()->quit();
  1057. }));
  1058. }
  1059. void HackStudioWidget::create_edit_menu(GUI::Window& window)
  1060. {
  1061. auto& edit_menu = window.add_menu("&Edit");
  1062. 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").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  1063. reveal_action_tab(*m_find_in_files_widget);
  1064. m_find_in_files_widget->focus_textbox_and_select_all();
  1065. }));
  1066. edit_menu.add_separator();
  1067. auto vim_emulation_setting_action = GUI::Action::create_checkable("&Vim Emulation", { Mod_Ctrl | Mod_Shift | Mod_Alt, Key_V }, [this](auto& action) {
  1068. if (action.is_checked()) {
  1069. for (auto& editor_wrapper : m_all_editor_wrappers)
  1070. editor_wrapper.editor().set_editing_engine(make<GUI::VimEditingEngine>());
  1071. } else {
  1072. for (auto& editor_wrapper : m_all_editor_wrappers)
  1073. editor_wrapper.editor().set_editing_engine(make<GUI::RegularEditingEngine>());
  1074. }
  1075. });
  1076. vim_emulation_setting_action->set_checked(false);
  1077. edit_menu.add_action(vim_emulation_setting_action);
  1078. }
  1079. void HackStudioWidget::create_build_menu(GUI::Window& window)
  1080. {
  1081. auto& build_menu = window.add_menu("&Build");
  1082. build_menu.add_action(*m_build_action);
  1083. build_menu.add_separator();
  1084. build_menu.add_action(*m_run_action);
  1085. build_menu.add_action(*m_stop_action);
  1086. build_menu.add_separator();
  1087. build_menu.add_action(*m_debug_action);
  1088. }
  1089. void HackStudioWidget::create_view_menu(GUI::Window& window)
  1090. {
  1091. auto hide_action_tabs_action = GUI::Action::create("&Hide Action Tabs", { Mod_Ctrl | Mod_Shift, Key_X }, [this](auto&) {
  1092. hide_action_tabs();
  1093. });
  1094. auto open_locator_action = GUI::Action::create("Open &Locator", { Mod_Ctrl, Key_K }, [this](auto&) {
  1095. m_locator->open();
  1096. });
  1097. auto show_dotfiles_action = GUI::Action::create_checkable("S&how Dotfiles", { Mod_Ctrl, Key_H }, [&](auto& checked) {
  1098. project().model().set_should_show_dotfiles(checked.is_checked());
  1099. });
  1100. auto& view_menu = window.add_menu("&View");
  1101. view_menu.add_action(hide_action_tabs_action);
  1102. view_menu.add_action(open_locator_action);
  1103. view_menu.add_action(show_dotfiles_action);
  1104. m_toggle_semantic_highlighting_action = create_toggle_syntax_highlighting_mode_action();
  1105. view_menu.add_action(*m_toggle_semantic_highlighting_action);
  1106. view_menu.add_separator();
  1107. m_wrapping_mode_actions.set_exclusive(true);
  1108. auto& wrapping_mode_menu = view_menu.add_submenu("&Wrapping Mode");
  1109. m_no_wrapping_action = GUI::Action::create_checkable("&No Wrapping", [&](auto&) {
  1110. for (auto& wrapper : m_all_editor_wrappers)
  1111. wrapper.editor().set_wrapping_mode(GUI::TextEditor::WrappingMode::NoWrap);
  1112. });
  1113. m_wrap_anywhere_action = GUI::Action::create_checkable("Wrap &Anywhere", [&](auto&) {
  1114. for (auto& wrapper : m_all_editor_wrappers)
  1115. wrapper.editor().set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAnywhere);
  1116. });
  1117. m_wrap_at_words_action = GUI::Action::create_checkable("Wrap at &Words", [&](auto&) {
  1118. for (auto& wrapper : m_all_editor_wrappers)
  1119. wrapper.editor().set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAtWords);
  1120. });
  1121. m_wrapping_mode_actions.add_action(*m_no_wrapping_action);
  1122. m_wrapping_mode_actions.add_action(*m_wrap_anywhere_action);
  1123. m_wrapping_mode_actions.add_action(*m_wrap_at_words_action);
  1124. wrapping_mode_menu.add_action(*m_no_wrapping_action);
  1125. wrapping_mode_menu.add_action(*m_wrap_anywhere_action);
  1126. wrapping_mode_menu.add_action(*m_wrap_at_words_action);
  1127. m_no_wrapping_action->set_checked(true);
  1128. m_editor_font_action = GUI::Action::create("Editor &Font...", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-font-editor.png").release_value_but_fixme_should_propagate_errors(),
  1129. [&](auto&) {
  1130. auto picker = GUI::FontPicker::construct(&window, m_editor_font, false);
  1131. if (picker->exec() == GUI::Dialog::ExecOK) {
  1132. change_editor_font(picker->font());
  1133. }
  1134. });
  1135. view_menu.add_action(*m_editor_font_action);
  1136. view_menu.add_separator();
  1137. view_menu.add_action(*m_add_editor_action);
  1138. view_menu.add_action(*m_remove_current_editor_action);
  1139. view_menu.add_action(*m_add_terminal_action);
  1140. view_menu.add_action(*m_remove_current_terminal_action);
  1141. view_menu.add_separator();
  1142. create_location_history_actions();
  1143. view_menu.add_action(*m_locations_history_back_action);
  1144. view_menu.add_action(*m_locations_history_forward_action);
  1145. }
  1146. void HackStudioWidget::create_help_menu(GUI::Window& window)
  1147. {
  1148. auto& help_menu = window.add_menu("&Help");
  1149. help_menu.add_action(GUI::CommonActions::make_about_action("Hack Studio", GUI::Icon::default_icon("app-hack-studio"), &window));
  1150. }
  1151. NonnullRefPtr<GUI::Action> HackStudioWidget::create_stop_action()
  1152. {
  1153. auto action = GUI::Action::create("&Stop", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/program-stop.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  1154. if (!Debugger::the().session()) {
  1155. if (auto result = m_terminal_wrapper->kill_running_command(); result.is_error())
  1156. warnln("{}", result.error());
  1157. return;
  1158. }
  1159. Debugger::the().stop();
  1160. });
  1161. action->set_enabled(false);
  1162. return action;
  1163. }
  1164. void HackStudioWidget::initialize_menubar(GUI::Window& window)
  1165. {
  1166. create_file_menu(window);
  1167. create_edit_menu(window);
  1168. create_build_menu(window);
  1169. create_view_menu(window);
  1170. create_help_menu(window);
  1171. }
  1172. void HackStudioWidget::update_statusbar()
  1173. {
  1174. StringBuilder builder;
  1175. if (current_editor().has_selection()) {
  1176. String selected_text = current_editor().selected_text();
  1177. auto word_count = current_editor().number_of_selected_words();
  1178. builder.appendff("Selected: {} {} ({} {})", selected_text.length(), selected_text.length() == 1 ? "character" : "characters", word_count, word_count != 1 ? "words" : "word");
  1179. }
  1180. m_statusbar->set_text(0, builder.to_string());
  1181. m_statusbar->set_text(1, current_editor_wrapper().editor().code_document().language_name());
  1182. m_statusbar->set_text(2, String::formatted("Ln {}, Col {}", current_editor().cursor().line() + 1, current_editor().cursor().column()));
  1183. }
  1184. void HackStudioWidget::handle_external_file_deletion(const String& filepath)
  1185. {
  1186. close_file_in_all_editors(filepath);
  1187. }
  1188. void HackStudioWidget::stop_debugger_if_running()
  1189. {
  1190. if (!m_debugger_thread.is_null()) {
  1191. Debugger::the().stop();
  1192. dbgln("Waiting for debugger thread to terminate");
  1193. auto rc = m_debugger_thread->join();
  1194. if (rc.is_error()) {
  1195. warnln("pthread_join: {}", strerror(rc.error().value()));
  1196. dbgln("error joining debugger thread");
  1197. }
  1198. }
  1199. }
  1200. void HackStudioWidget::close_current_project()
  1201. {
  1202. m_editors_splitter->remove_all_children();
  1203. m_all_editor_wrappers.clear();
  1204. m_open_files.clear();
  1205. m_open_files_vector.clear();
  1206. add_new_editor(*m_editors_splitter);
  1207. m_find_in_files_widget->reset();
  1208. m_todo_entries_widget->clear();
  1209. m_terminal_wrapper->clear_including_history();
  1210. stop_debugger_if_running();
  1211. update_gml_preview();
  1212. }
  1213. HackStudioWidget::~HackStudioWidget()
  1214. {
  1215. stop_debugger_if_running();
  1216. }
  1217. HackStudioWidget::ContinueDecision HackStudioWidget::warn_unsaved_changes(const String& prompt)
  1218. {
  1219. if (!any_document_is_dirty())
  1220. return ContinueDecision::Yes;
  1221. auto result = GUI::MessageBox::show(window(), prompt, "Unsaved changes", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel);
  1222. if (result == GUI::MessageBox::ExecCancel)
  1223. return ContinueDecision::No;
  1224. if (result == GUI::MessageBox::ExecYes) {
  1225. for (auto& editor_wrapper : m_all_editor_wrappers) {
  1226. if (editor_wrapper.editor().document().is_modified()) {
  1227. editor_wrapper.save();
  1228. }
  1229. }
  1230. }
  1231. return ContinueDecision::Yes;
  1232. }
  1233. bool HackStudioWidget::any_document_is_dirty() const
  1234. {
  1235. return any_of(m_all_editor_wrappers, [](auto& editor_wrapper) {
  1236. return editor_wrapper.editor().document().is_modified();
  1237. });
  1238. }
  1239. void HackStudioWidget::update_gml_preview()
  1240. {
  1241. auto gml_content = current_editor_wrapper().filename().ends_with(".gml") ? current_editor_wrapper().editor().text() : "";
  1242. m_gml_preview_widget->load_gml(gml_content);
  1243. }
  1244. void HackStudioWidget::update_tree_view()
  1245. {
  1246. auto index = m_project->model().index(m_current_editor_wrapper->filename(), GUI::FileSystemModel::Column::Name);
  1247. if (index.is_valid()) {
  1248. m_project_tree_view->expand_all_parents_of(index);
  1249. m_project_tree_view->set_cursor(index, GUI::AbstractView::SelectionUpdate::Set);
  1250. }
  1251. }
  1252. void HackStudioWidget::update_window_title()
  1253. {
  1254. window()->set_title(String::formatted("{} - {} - Hack Studio", m_current_editor_wrapper->filename_label().text(), m_project->name()));
  1255. }
  1256. void HackStudioWidget::on_cursor_change()
  1257. {
  1258. update_statusbar();
  1259. if (current_editor_wrapper().filename().is_null())
  1260. return;
  1261. auto current_location = current_project_location();
  1262. if (m_locations_history_end_index != 0) {
  1263. auto last = m_locations_history[m_locations_history_end_index - 1];
  1264. if (current_location.filename == last.filename && current_location.line == last.line)
  1265. return;
  1266. }
  1267. // Clear "Go Forward" locations
  1268. VERIFY(m_locations_history_end_index <= m_locations_history.size());
  1269. m_locations_history.remove(m_locations_history_end_index, m_locations_history.size() - m_locations_history_end_index);
  1270. m_locations_history.append(current_location);
  1271. constexpr size_t max_locations = 30;
  1272. if (m_locations_history.size() > max_locations)
  1273. m_locations_history.take_first();
  1274. m_locations_history_end_index = m_locations_history.size();
  1275. update_history_actions();
  1276. }
  1277. void HackStudioWidget::create_location_history_actions()
  1278. {
  1279. m_locations_history_back_action = GUI::Action::create("Go Back", { Mod_Alt | Mod_Shift, Key_Left }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-back.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  1280. if (m_locations_history_end_index <= 1)
  1281. return;
  1282. auto location = m_locations_history[m_locations_history_end_index - 2];
  1283. --m_locations_history_end_index;
  1284. m_locations_history_disabled = true;
  1285. open_file(location.filename, location.line, location.column);
  1286. m_locations_history_disabled = false;
  1287. update_history_actions();
  1288. });
  1289. m_locations_history_forward_action = GUI::Action::create("Go Forward", { Mod_Alt | Mod_Shift, Key_Right }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-forward.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  1290. if (m_locations_history_end_index == m_locations_history.size())
  1291. return;
  1292. auto location = m_locations_history[m_locations_history_end_index];
  1293. ++m_locations_history_end_index;
  1294. m_locations_history_disabled = true;
  1295. open_file(location.filename, location.line, location.column);
  1296. m_locations_history_disabled = false;
  1297. update_history_actions();
  1298. });
  1299. m_locations_history_forward_action->set_enabled(false);
  1300. }
  1301. HackStudioWidget::ProjectLocation HackStudioWidget::current_project_location() const
  1302. {
  1303. return ProjectLocation { current_editor_wrapper().filename(), current_editor().cursor().line(), current_editor().cursor().column() };
  1304. }
  1305. void HackStudioWidget::update_history_actions()
  1306. {
  1307. if (m_locations_history_end_index <= 1)
  1308. m_locations_history_back_action->set_enabled(false);
  1309. else
  1310. m_locations_history_back_action->set_enabled(true);
  1311. if (m_locations_history_end_index == m_locations_history.size())
  1312. m_locations_history_forward_action->set_enabled(false);
  1313. else
  1314. m_locations_history_forward_action->set_enabled(true);
  1315. }
  1316. RefPtr<Gfx::Font> HackStudioWidget::read_editor_font_from_config()
  1317. {
  1318. auto font_family = Config::read_string("HackStudio", "EditorFont", "Family", "Csilla");
  1319. auto font_variant = Config::read_string("HackStudio", "EditorFont", "Variant", "Regular");
  1320. auto font_size = Config::read_i32("HackStudio", "EditorFont", "Size", 10);
  1321. auto font = Gfx::FontDatabase::the().get(font_family, font_variant, font_size);
  1322. if (font.is_null())
  1323. return Gfx::FontDatabase::the().default_fixed_width_font();
  1324. return font;
  1325. }
  1326. void HackStudioWidget::change_editor_font(RefPtr<Gfx::Font> font)
  1327. {
  1328. m_editor_font = move(font);
  1329. for (auto& editor_wrapper : m_all_editor_wrappers) {
  1330. editor_wrapper.editor().set_font(*m_editor_font);
  1331. }
  1332. Config::write_string("HackStudio", "EditorFont", "Family", m_editor_font->family());
  1333. Config::write_string("HackStudio", "EditorFont", "Variant", m_editor_font->variant());
  1334. Config::write_i32("HackStudio", "EditorFont", "Size", m_editor_font->presentation_size());
  1335. }
  1336. void HackStudioWidget::open_coredump(String const& coredump_path)
  1337. {
  1338. open_project("/usr/src/serenity");
  1339. m_mode = Mode::Coredump;
  1340. m_coredump_inspector = Coredump::Inspector::create(coredump_path, [this](float progress) {
  1341. window()->set_progress(progress * 100);
  1342. });
  1343. window()->set_progress(0);
  1344. if (m_coredump_inspector) {
  1345. m_debug_info_widget->update_state(*m_coredump_inspector, m_coredump_inspector->get_registers());
  1346. reveal_action_tab(*m_debug_info_widget);
  1347. }
  1348. }
  1349. void HackStudioWidget::for_each_open_file(Function<void(ProjectFile const&)> func)
  1350. {
  1351. for (auto& open_file : m_open_files) {
  1352. func(*open_file.value);
  1353. }
  1354. }
  1355. NonnullRefPtr<GUI::Action> HackStudioWidget::create_toggle_syntax_highlighting_mode_action()
  1356. {
  1357. auto action = GUI::Action::create_checkable("&Semantic Highlighting", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-cplusplus.png").release_value_but_fixme_should_propagate_errors(), [this](auto& action) {
  1358. for (auto& editor_wrapper : m_all_editor_wrappers)
  1359. editor_wrapper.editor().set_semantic_syntax_highlighting(action.is_checked());
  1360. });
  1361. return action;
  1362. }
  1363. bool HackStudioWidget::semantic_syntax_highlighting_is_enabled() const
  1364. {
  1365. return m_toggle_semantic_highlighting_action->is_checked();
  1366. }
  1367. }