HackStudioWidget.cpp 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  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("JavaScript Source File", "/res/icons/16x16/filetype-javascript.png", "js"));
  372. m_new_file_actions.append(create_new_file_action("HTML File", "/res/icons/16x16/filetype-html.png", "html"));
  373. m_new_file_actions.append(create_new_file_action("CSS File", "/res/icons/16x16/filetype-css.png", "css"));
  374. m_new_plain_file_action = create_new_file_action("Plain File", "/res/icons/16x16/new.png", "");
  375. m_open_selected_action = create_open_selected_action();
  376. m_show_in_file_manager_action = create_show_in_file_manager_action();
  377. m_new_directory_action = create_new_directory_action();
  378. m_delete_action = create_delete_action();
  379. m_tree_view_rename_action = GUI::CommonActions::make_rename_action([this](GUI::Action const&) {
  380. m_project_tree_view->begin_editing(m_project_tree_view->cursor_index());
  381. });
  382. auto project_tree_view_context_menu = GUI::Menu::construct("Project Files");
  383. auto& new_file_submenu = project_tree_view_context_menu->add_submenu("New");
  384. for (auto& new_file_action : m_new_file_actions) {
  385. new_file_submenu.add_action(new_file_action);
  386. }
  387. new_file_submenu.set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/new.png").release_value_but_fixme_should_propagate_errors());
  388. new_file_submenu.add_action(*m_new_plain_file_action);
  389. new_file_submenu.add_separator();
  390. new_file_submenu.add_action(*m_new_directory_action);
  391. project_tree_view_context_menu->add_action(*m_open_selected_action);
  392. project_tree_view_context_menu->add_action(*m_show_in_file_manager_action);
  393. // TODO: Cut, copy, duplicate with new name...
  394. project_tree_view_context_menu->add_separator();
  395. project_tree_view_context_menu->add_action(*m_tree_view_rename_action);
  396. project_tree_view_context_menu->add_action(*m_delete_action);
  397. return project_tree_view_context_menu;
  398. }
  399. NonnullRefPtr<GUI::Action> HackStudioWidget::create_new_file_action(String const& label, String const& icon, String const& extension)
  400. {
  401. return GUI::Action::create(label, Gfx::Bitmap::try_load_from_file(icon).release_value_but_fixme_should_propagate_errors(), [this, extension](const GUI::Action&) {
  402. String filename;
  403. if (GUI::InputBox::show(window(), filename, "Enter name of new file:", "Add new file to project") != GUI::InputBox::ExecOK)
  404. return;
  405. if (!extension.is_empty() && !filename.ends_with(String::formatted(".{}", extension))) {
  406. filename = String::formatted("{}.{}", filename, extension);
  407. }
  408. auto path_to_selected = selected_file_paths();
  409. String filepath;
  410. if (!path_to_selected.is_empty()) {
  411. VERIFY(Core::File::exists(path_to_selected.first()));
  412. LexicalPath selected(path_to_selected.first());
  413. String dir_path;
  414. if (Core::File::is_directory(selected.string()))
  415. dir_path = selected.string();
  416. else
  417. dir_path = selected.dirname();
  418. filepath = String::formatted("{}/", dir_path);
  419. }
  420. filepath = String::formatted("{}{}", filepath, filename);
  421. auto file = Core::File::construct(filepath);
  422. if (!file->open((Core::OpenMode)(Core::OpenMode::WriteOnly | Core::OpenMode::MustBeNew))) {
  423. GUI::MessageBox::show(window(), String::formatted("Failed to create '{}'", filepath), "Error", GUI::MessageBox::Type::Error);
  424. return;
  425. }
  426. open_file(filepath);
  427. });
  428. }
  429. NonnullRefPtr<GUI::Action> HackStudioWidget::create_new_directory_action()
  430. {
  431. 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&) {
  432. String directory_name;
  433. if (GUI::InputBox::show(window(), directory_name, "Enter name of new directory:", "Add new folder to project") != GUI::InputBox::ExecOK)
  434. return;
  435. auto path_to_selected = selected_file_paths();
  436. if (!path_to_selected.is_empty()) {
  437. LexicalPath selected(path_to_selected.first());
  438. String dir_path;
  439. if (Core::File::is_directory(selected.string()))
  440. dir_path = selected.string();
  441. else
  442. dir_path = selected.dirname();
  443. directory_name = String::formatted("{}/{}", dir_path, directory_name);
  444. }
  445. auto formatted_dir_name = LexicalPath::canonicalized_path(String::formatted("{}/{}", m_project->model().root_path(), directory_name));
  446. int rc = mkdir(formatted_dir_name.characters(), 0755);
  447. if (rc < 0) {
  448. GUI::MessageBox::show(window(), "Failed to create new directory", "Error", GUI::MessageBox::Type::Error);
  449. return;
  450. }
  451. });
  452. }
  453. NonnullRefPtr<GUI::Action> HackStudioWidget::create_open_selected_action()
  454. {
  455. auto open_selected_action = GUI::Action::create("Open", [this](const GUI::Action&) {
  456. auto files = selected_file_paths();
  457. for (auto& file : files)
  458. open_file(file);
  459. });
  460. open_selected_action->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/open.png").release_value_but_fixme_should_propagate_errors());
  461. open_selected_action->set_enabled(true);
  462. return open_selected_action;
  463. }
  464. NonnullRefPtr<GUI::Action> HackStudioWidget::create_show_in_file_manager_action()
  465. {
  466. auto show_in_file_manager_action = GUI::Action::create("Show in File Manager", [this](const GUI::Action&) {
  467. auto files = selected_file_paths();
  468. for (auto& file : files)
  469. Desktop::Launcher::open(URL::create_with_file_protocol(m_project->root_path(), file));
  470. });
  471. show_in_file_manager_action->set_enabled(true);
  472. show_in_file_manager_action->set_icon(GUI::Icon::default_icon("app-file-manager").bitmap_for_size(16));
  473. return show_in_file_manager_action;
  474. }
  475. NonnullRefPtr<GUI::Action> HackStudioWidget::create_delete_action()
  476. {
  477. auto delete_action = GUI::CommonActions::make_delete_action([this](const GUI::Action&) {
  478. auto files = selected_file_paths();
  479. if (files.is_empty())
  480. return;
  481. String message;
  482. if (files.size() == 1) {
  483. LexicalPath file(files[0]);
  484. message = String::formatted("Really remove {} from disk?", file.basename());
  485. } else {
  486. message = String::formatted("Really remove {} files from disk?", files.size());
  487. }
  488. auto result = GUI::MessageBox::show(window(),
  489. message,
  490. "Confirm deletion",
  491. GUI::MessageBox::Type::Warning,
  492. GUI::MessageBox::InputType::OKCancel);
  493. if (result == GUI::MessageBox::ExecCancel)
  494. return;
  495. for (auto& file : files) {
  496. struct stat st;
  497. if (lstat(file.characters(), &st) < 0) {
  498. GUI::MessageBox::show(window(),
  499. String::formatted("lstat ({}) failed: {}", file, strerror(errno)),
  500. "Removal failed",
  501. GUI::MessageBox::Type::Error);
  502. break;
  503. }
  504. bool is_directory = S_ISDIR(st.st_mode);
  505. if (auto result = Core::File::remove(file, Core::File::RecursionMode::Allowed, false); result.is_error()) {
  506. auto& error = result.error();
  507. if (is_directory) {
  508. GUI::MessageBox::show(window(),
  509. String::formatted("Removing directory {} from the project failed: {}", error.file, static_cast<Error const&>(error)),
  510. "Removal failed",
  511. GUI::MessageBox::Type::Error);
  512. } else {
  513. GUI::MessageBox::show(window(),
  514. String::formatted("Removing file {} from the project failed: {}", error.file, static_cast<Error const&>(error)),
  515. "Removal failed",
  516. GUI::MessageBox::Type::Error);
  517. }
  518. }
  519. }
  520. },
  521. m_project_tree_view);
  522. delete_action->set_enabled(false);
  523. return delete_action;
  524. }
  525. NonnullRefPtr<GUI::Action> HackStudioWidget::create_new_project_action()
  526. {
  527. 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&) {
  528. auto dialog = NewProjectDialog::construct(window());
  529. dialog->set_icon(window()->icon());
  530. auto result = dialog->exec();
  531. if (result == GUI::Dialog::ExecResult::ExecOK && dialog->created_project_path().has_value())
  532. open_project(dialog->created_project_path().value());
  533. });
  534. }
  535. void HackStudioWidget::add_new_editor(GUI::Widget& parent)
  536. {
  537. auto wrapper = EditorWrapper::construct();
  538. if (m_action_tab_widget) {
  539. parent.insert_child_before(wrapper, *m_action_tab_widget);
  540. } else {
  541. parent.add_child(wrapper);
  542. }
  543. auto previous_editor_wrapper = m_current_editor_wrapper;
  544. m_current_editor_wrapper = wrapper;
  545. m_all_editor_wrappers.append(wrapper);
  546. wrapper->editor().set_focus(true);
  547. wrapper->editor().set_font(*m_editor_font);
  548. wrapper->set_project_root(m_project->root_path());
  549. wrapper->editor().on_cursor_change = [this] { on_cursor_change(); };
  550. wrapper->on_change = [this] { update_gml_preview(); };
  551. set_edit_mode(EditMode::Text);
  552. if (previous_editor_wrapper && previous_editor_wrapper->editor().editing_engine()->is_regular())
  553. wrapper->editor().set_editing_engine(make<GUI::RegularEditingEngine>());
  554. else if (previous_editor_wrapper && previous_editor_wrapper->editor().editing_engine()->is_vim())
  555. wrapper->editor().set_editing_engine(make<GUI::VimEditingEngine>());
  556. else
  557. wrapper->editor().set_editing_engine(make<GUI::RegularEditingEngine>());
  558. }
  559. NonnullRefPtr<GUI::Action> HackStudioWidget::create_switch_to_next_editor_action()
  560. {
  561. return GUI::Action::create("Switch to &Next Editor", { Mod_Ctrl, Key_E }, [this](auto&) {
  562. if (m_all_editor_wrappers.size() <= 1)
  563. return;
  564. Vector<EditorWrapper&> wrappers;
  565. m_editors_splitter->for_each_child_of_type<EditorWrapper>([&wrappers](auto& child) {
  566. wrappers.append(child);
  567. return IterationDecision::Continue;
  568. });
  569. for (size_t i = 0; i < wrappers.size(); ++i) {
  570. if (m_current_editor_wrapper.ptr() == &wrappers[i]) {
  571. if (i == wrappers.size() - 1)
  572. wrappers[0].editor().set_focus(true);
  573. else
  574. wrappers[i + 1].editor().set_focus(true);
  575. }
  576. }
  577. });
  578. }
  579. NonnullRefPtr<GUI::Action> HackStudioWidget::create_switch_to_previous_editor_action()
  580. {
  581. return GUI::Action::create("Switch to &Previous Editor", { Mod_Ctrl | Mod_Shift, Key_E }, [this](auto&) {
  582. if (m_all_editor_wrappers.size() <= 1)
  583. return;
  584. Vector<EditorWrapper&> wrappers;
  585. m_editors_splitter->for_each_child_of_type<EditorWrapper>([&wrappers](auto& child) {
  586. wrappers.append(child);
  587. return IterationDecision::Continue;
  588. });
  589. for (int i = wrappers.size() - 1; i >= 0; --i) {
  590. if (m_current_editor_wrapper.ptr() == &wrappers[i]) {
  591. if (i == 0)
  592. wrappers.last().editor().set_focus(true);
  593. else
  594. wrappers[i - 1].editor().set_focus(true);
  595. }
  596. }
  597. });
  598. }
  599. NonnullRefPtr<GUI::Action> HackStudioWidget::create_remove_current_editor_action()
  600. {
  601. 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&) {
  602. if (m_all_editor_wrappers.size() <= 1)
  603. return;
  604. auto wrapper = m_current_editor_wrapper;
  605. m_switch_to_next_editor->activate();
  606. m_editors_splitter->remove_child(*wrapper);
  607. m_all_editor_wrappers.remove_first_matching([&wrapper](auto& entry) { return entry == wrapper.ptr(); });
  608. update_actions();
  609. });
  610. }
  611. NonnullRefPtr<GUI::Action> HackStudioWidget::create_open_action()
  612. {
  613. 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&) {
  614. auto open_path = GUI::FilePicker::get_open_filepath(window(), "Open project", m_project->root_path(), true);
  615. if (!open_path.has_value())
  616. return;
  617. open_project(open_path.value());
  618. update_actions();
  619. });
  620. }
  621. NonnullRefPtr<GUI::Action> HackStudioWidget::create_save_action()
  622. {
  623. return GUI::CommonActions::make_save_action([&](auto&) {
  624. if (active_file().is_empty())
  625. m_save_as_action->activate();
  626. // NOTE active_file() could still be empty after a cancelled save_as_action
  627. if (!active_file().is_empty())
  628. current_editor_wrapper().save();
  629. if (m_git_widget->initialized())
  630. m_git_widget->refresh();
  631. });
  632. }
  633. NonnullRefPtr<GUI::Action> HackStudioWidget::create_save_as_action()
  634. {
  635. return GUI::CommonActions::make_save_as_action([&](auto&) {
  636. auto const old_filename = current_editor_wrapper().filename();
  637. LexicalPath const old_path(old_filename);
  638. Optional<String> save_path = GUI::FilePicker::get_save_filepath(window(),
  639. old_filename.is_null() ? "Untitled" : old_path.title(),
  640. old_filename.is_null() ? "txt" : old_path.extension(),
  641. Core::File::absolute_path(old_path.dirname()));
  642. if (!save_path.has_value()) {
  643. return;
  644. }
  645. String const relative_file_path = LexicalPath::relative_path(save_path.value(), m_project->root_path());
  646. if (current_editor_wrapper().filename().is_null()) {
  647. current_editor_wrapper().set_filename(relative_file_path);
  648. } else {
  649. for (auto& editor_wrapper : m_all_editor_wrappers) {
  650. if (editor_wrapper.filename() == old_filename)
  651. editor_wrapper.set_filename(relative_file_path);
  652. }
  653. }
  654. current_editor_wrapper().save();
  655. auto new_project_file = m_project->create_file(relative_file_path);
  656. m_open_files.set(relative_file_path, *new_project_file);
  657. m_open_files.remove(old_filename);
  658. m_open_files_vector.append(relative_file_path);
  659. m_open_files_vector.remove_all_matching([&old_filename](auto const& element) { return element == old_filename; });
  660. update_window_title();
  661. m_project->model().invalidate();
  662. update_tree_view();
  663. });
  664. }
  665. NonnullRefPtr<GUI::Action> HackStudioWidget::create_remove_current_terminal_action()
  666. {
  667. 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&) {
  668. auto widget = m_action_tab_widget->active_widget();
  669. if (!widget)
  670. return;
  671. if (!is<TerminalWrapper>(widget))
  672. return;
  673. auto& terminal = *static_cast<TerminalWrapper*>(widget);
  674. if (!terminal.user_spawned())
  675. return;
  676. m_action_tab_widget->remove_tab(terminal);
  677. update_actions();
  678. });
  679. }
  680. NonnullRefPtr<GUI::Action> HackStudioWidget::create_add_editor_action()
  681. {
  682. return GUI::Action::create("Add New &Editor", { Mod_Ctrl | Mod_Alt, Key_E },
  683. Gfx::Bitmap::try_load_from_file("/res/icons/hackstudio/add-editor.png").release_value_but_fixme_should_propagate_errors(),
  684. [this](auto&) {
  685. add_new_editor(*m_editors_splitter);
  686. update_actions();
  687. });
  688. }
  689. NonnullRefPtr<GUI::Action> HackStudioWidget::create_add_terminal_action()
  690. {
  691. return GUI::Action::create("Add New &Terminal", { Mod_Ctrl | Mod_Alt, Key_T },
  692. Gfx::Bitmap::try_load_from_file("/res/icons/hackstudio/add-terminal.png").release_value_but_fixme_should_propagate_errors(),
  693. [this](auto&) {
  694. auto& terminal_wrapper = m_action_tab_widget->add_tab<TerminalWrapper>("Terminal");
  695. reveal_action_tab(terminal_wrapper);
  696. update_actions();
  697. terminal_wrapper.terminal().set_focus(true);
  698. });
  699. }
  700. void HackStudioWidget::reveal_action_tab(GUI::Widget& widget)
  701. {
  702. if (m_action_tab_widget->min_height() < 200)
  703. m_action_tab_widget->set_fixed_height(200);
  704. m_action_tab_widget->set_active_widget(&widget);
  705. }
  706. NonnullRefPtr<GUI::Action> HackStudioWidget::create_debug_action()
  707. {
  708. 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&) {
  709. if (!Core::File::exists(get_project_executable_path())) {
  710. GUI::MessageBox::show(window(), String::formatted("Could not find file: {}. (did you build the project?)", get_project_executable_path()), "Error", GUI::MessageBox::Type::Error);
  711. return;
  712. }
  713. if (Debugger::the().session()) {
  714. GUI::MessageBox::show(window(), "Debugger is already running", "Error", GUI::MessageBox::Type::Error);
  715. return;
  716. }
  717. Debugger::the().set_executable_path(get_project_executable_path());
  718. m_terminal_wrapper->clear_including_history();
  719. // The debugger calls wait() on the debugee, so the TerminalWrapper can't do that.
  720. auto ptm_res = m_terminal_wrapper->setup_master_pseudoterminal(TerminalWrapper::WaitForChildOnExit::No);
  721. if (ptm_res.is_error()) {
  722. perror("setup_master_pseudoterminal");
  723. return;
  724. }
  725. Debugger::the().set_child_setup_callback([this, ptm_res]() {
  726. return m_terminal_wrapper->setup_slave_pseudoterminal(ptm_res.value());
  727. });
  728. m_debugger_thread = Threading::Thread::construct(Debugger::start_static);
  729. m_debugger_thread->start();
  730. m_stop_action->set_enabled(true);
  731. m_run_action->set_enabled(false);
  732. for (auto& editor_wrapper : m_all_editor_wrappers) {
  733. editor_wrapper.set_debug_mode(true);
  734. }
  735. });
  736. }
  737. void HackStudioWidget::initialize_debugger()
  738. {
  739. Debugger::initialize(
  740. m_project->root_path(),
  741. [this](const PtraceRegisters& regs) {
  742. VERIFY(Debugger::the().session());
  743. const auto& debug_session = *Debugger::the().session();
  744. auto source_position = debug_session.get_source_position(regs.ip());
  745. if (!source_position.has_value()) {
  746. dbgln("Could not find source position for address: {:p}", regs.ip());
  747. return Debugger::HasControlPassedToUser::No;
  748. }
  749. dbgln("Debugger stopped at source position: {}:{}", source_position.value().file_path, source_position.value().line_number);
  750. deferred_invoke([this, source_position, &regs] {
  751. m_current_editor_in_execution = get_editor_of_file(source_position.value().file_path);
  752. if (m_current_editor_in_execution)
  753. m_current_editor_in_execution->editor().set_execution_position(source_position.value().line_number - 1);
  754. m_debug_info_widget->update_state(*Debugger::the().session(), regs);
  755. m_debug_info_widget->set_debug_actions_enabled(true);
  756. m_disassembly_widget->update_state(*Debugger::the().session(), regs);
  757. HackStudioWidget::reveal_action_tab(*m_debug_info_widget);
  758. });
  759. Core::EventLoop::wake_current();
  760. return Debugger::HasControlPassedToUser::Yes;
  761. },
  762. [this]() {
  763. deferred_invoke([this] {
  764. m_debug_info_widget->set_debug_actions_enabled(false);
  765. if (m_current_editor_in_execution)
  766. m_current_editor_in_execution->editor().clear_execution_position();
  767. });
  768. Core::EventLoop::wake_current();
  769. },
  770. [this]() {
  771. deferred_invoke([this] {
  772. m_debug_info_widget->set_debug_actions_enabled(false);
  773. if (m_current_editor_in_execution)
  774. m_current_editor_in_execution->editor().clear_execution_position();
  775. m_debug_info_widget->program_stopped();
  776. m_disassembly_widget->program_stopped();
  777. m_stop_action->set_enabled(false);
  778. m_run_action->set_enabled(true);
  779. m_debugger_thread.clear();
  780. for (auto& editor_wrapper : m_all_editor_wrappers) {
  781. editor_wrapper.set_debug_mode(false);
  782. }
  783. HackStudioWidget::hide_action_tabs();
  784. GUI::MessageBox::show(window(), "Program Exited", "Debugger", GUI::MessageBox::Type::Information);
  785. });
  786. Core::EventLoop::wake_current();
  787. });
  788. }
  789. String HackStudioWidget::get_full_path_of_serenity_source(const String& file)
  790. {
  791. auto path_parts = LexicalPath(file).parts();
  792. while (!path_parts.is_empty() && path_parts[0] == "..") {
  793. path_parts.remove(0);
  794. }
  795. StringBuilder relative_path_builder;
  796. relative_path_builder.join("/", path_parts);
  797. constexpr char SERENITY_LIBS_PREFIX[] = "/usr/src/serenity";
  798. LexicalPath serenity_sources_base(SERENITY_LIBS_PREFIX);
  799. return String::formatted("{}/{}", serenity_sources_base, relative_path_builder.to_string());
  800. }
  801. String HackStudioWidget::get_absolute_path(const String& path) const
  802. {
  803. // TODO: We can probably do a more specific condition here, something like
  804. // "if (file.starts_with("../Libraries/") || file.starts_with("../AK/"))"
  805. if (path.starts_with("..")) {
  806. return get_full_path_of_serenity_source(path);
  807. }
  808. return m_project->to_absolute_path(path);
  809. }
  810. RefPtr<EditorWrapper> HackStudioWidget::get_editor_of_file(const String& filename)
  811. {
  812. String file_path = filename;
  813. if (filename.starts_with("../")) {
  814. file_path = get_full_path_of_serenity_source(filename);
  815. }
  816. if (!open_file(file_path))
  817. return nullptr;
  818. return current_editor_wrapper();
  819. }
  820. String HackStudioWidget::get_project_executable_path() const
  821. {
  822. // FIXME: Dumb heuristic ahead!
  823. // e.g /my/project => /my/project/project
  824. // TODO: Perhaps a Makefile rule for getting the value of $(PROGRAM) would be better?
  825. return String::formatted("{}/{}", m_project->root_path(), LexicalPath::basename(m_project->root_path()));
  826. }
  827. void HackStudioWidget::build()
  828. {
  829. auto result = m_project_builder->build(active_file());
  830. if (result.is_error()) {
  831. GUI::MessageBox::show(window(), String::formatted("{}", result.error()), "Build failed", GUI::MessageBox::Type::Error);
  832. }
  833. }
  834. void HackStudioWidget::run()
  835. {
  836. auto result = m_project_builder->run(active_file());
  837. if (result.is_error()) {
  838. GUI::MessageBox::show(window(), String::formatted("{}", result.error()), "Run failed", GUI::MessageBox::Type::Error);
  839. }
  840. }
  841. void HackStudioWidget::hide_action_tabs()
  842. {
  843. m_action_tab_widget->set_fixed_height(24);
  844. };
  845. Project& HackStudioWidget::project()
  846. {
  847. return *m_project;
  848. }
  849. void HackStudioWidget::set_current_editor_wrapper(RefPtr<EditorWrapper> editor_wrapper)
  850. {
  851. m_current_editor_wrapper = editor_wrapper;
  852. update_window_title();
  853. update_tree_view();
  854. }
  855. void HackStudioWidget::file_renamed(String const& old_name, String const& new_name)
  856. {
  857. auto editor_or_none = m_all_editor_wrappers.first_matching([&old_name](auto const& editor) {
  858. return editor->filename() == old_name;
  859. });
  860. if (editor_or_none.has_value()) {
  861. (*editor_or_none)->set_filename(new_name);
  862. (*editor_or_none)->set_name(new_name);
  863. }
  864. if (m_open_files.contains(old_name)) {
  865. VERIFY(m_open_files_vector.remove_first_matching([&old_name](auto const& file) { return file == old_name; }));
  866. m_open_files_vector.append(new_name);
  867. ProjectFile* f = m_open_files.get(old_name).release_value();
  868. m_open_files.set(new_name, *f);
  869. m_open_files.remove(old_name);
  870. m_open_files_view->model()->invalidate();
  871. }
  872. if (m_file_watcher->is_watching(old_name)) {
  873. VERIFY(!m_file_watcher->remove_watch(old_name).is_error());
  874. VERIFY(!m_file_watcher->add_watch(new_name, Core::FileWatcherEvent::Type::Deleted).is_error());
  875. }
  876. }
  877. void HackStudioWidget::configure_project_tree_view()
  878. {
  879. m_project_tree_view->set_model(m_project->model());
  880. m_project_tree_view->set_selection_mode(GUI::AbstractView::SelectionMode::MultiSelection);
  881. m_project_tree_view->set_editable(true);
  882. m_project_tree_view->aid_create_editing_delegate = [](auto&) {
  883. return make<GUI::StringModelEditingDelegate>();
  884. };
  885. for (int column_index = 0; column_index < m_project->model().column_count(); ++column_index)
  886. m_project_tree_view->set_column_visible(column_index, false);
  887. m_project_tree_view->set_column_visible(GUI::FileSystemModel::Column::Name, true);
  888. m_project_tree_view->on_context_menu_request = [this](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  889. if (index.is_valid()) {
  890. m_project_tree_view_context_menu->popup(event.screen_position(), m_open_selected_action);
  891. }
  892. };
  893. m_project_tree_view->on_selection_change = [this] {
  894. m_open_selected_action->set_enabled(!m_project_tree_view->selection().is_empty());
  895. auto selections = m_project_tree_view->selection().indices();
  896. auto it = selections.find_if([&](auto selected_file) {
  897. return access(m_project->model().full_path(selected_file.parent()).characters(), W_OK) == 0;
  898. });
  899. bool has_permissions = it != selections.end();
  900. m_tree_view_rename_action->set_enabled(has_permissions);
  901. m_delete_action->set_enabled(has_permissions);
  902. };
  903. m_project_tree_view->on_activation = [this](auto& index) {
  904. auto full_path_to_file = m_project->model().full_path(index);
  905. open_file(full_path_to_file);
  906. };
  907. }
  908. void HackStudioWidget::create_open_files_view(GUI::Widget& parent)
  909. {
  910. m_open_files_view = parent.add<GUI::ListView>();
  911. auto open_files_model = GUI::ItemListModel<String>::create(m_open_files_vector);
  912. m_open_files_view->set_model(open_files_model);
  913. m_open_files_view->on_activation = [this](auto& index) {
  914. open_file(index.data().to_string());
  915. };
  916. }
  917. void HackStudioWidget::create_toolbar(GUI::Widget& parent)
  918. {
  919. auto& toolbar = parent.add<GUI::Toolbar>();
  920. toolbar.add_action(*m_new_plain_file_action);
  921. toolbar.add_action(*m_new_directory_action);
  922. toolbar.add_action(*m_save_action);
  923. toolbar.add_action(*m_delete_action);
  924. toolbar.add_separator();
  925. toolbar.add_action(GUI::CommonActions::make_cut_action([this](auto&) { current_editor().cut_action().activate(); }));
  926. toolbar.add_action(GUI::CommonActions::make_copy_action([this](auto&) { current_editor().copy_action().activate(); }));
  927. toolbar.add_action(GUI::CommonActions::make_paste_action([this](auto&) { current_editor().paste_action().activate(); }));
  928. toolbar.add_separator();
  929. toolbar.add_action(GUI::CommonActions::make_undo_action([this](auto&) { current_editor().undo_action().activate(); }));
  930. toolbar.add_action(GUI::CommonActions::make_redo_action([this](auto&) { current_editor().redo_action().activate(); }));
  931. toolbar.add_separator();
  932. toolbar.add_action(*m_build_action);
  933. toolbar.add_separator();
  934. toolbar.add_action(*m_run_action);
  935. toolbar.add_action(*m_stop_action);
  936. toolbar.add_separator();
  937. toolbar.add_action(*m_debug_action);
  938. }
  939. NonnullRefPtr<GUI::Action> HackStudioWidget::create_build_action()
  940. {
  941. 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&) {
  942. if (warn_unsaved_changes("There are unsaved changes, do you want to save before building?") == ContinueDecision::No)
  943. return;
  944. reveal_action_tab(*m_terminal_wrapper);
  945. build();
  946. m_stop_action->set_enabled(true);
  947. });
  948. }
  949. NonnullRefPtr<GUI::Action> HackStudioWidget::create_run_action()
  950. {
  951. 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&) {
  952. reveal_action_tab(*m_terminal_wrapper);
  953. run();
  954. m_stop_action->set_enabled(true);
  955. });
  956. }
  957. void HackStudioWidget::create_action_tab(GUI::Widget& parent)
  958. {
  959. m_action_tab_widget = parent.add<GUI::TabWidget>();
  960. m_action_tab_widget->set_fixed_height(24);
  961. m_action_tab_widget->on_change = [this](auto&) {
  962. on_action_tab_change();
  963. static bool first_time = true;
  964. if (!first_time)
  965. m_action_tab_widget->set_fixed_height(200);
  966. first_time = false;
  967. };
  968. m_find_in_files_widget = m_action_tab_widget->add_tab<FindInFilesWidget>("Find in files");
  969. m_todo_entries_widget = m_action_tab_widget->add_tab<ToDoEntriesWidget>("TODO");
  970. m_terminal_wrapper = m_action_tab_widget->add_tab<TerminalWrapper>("Console", false);
  971. m_debug_info_widget = m_action_tab_widget->add_tab<DebugInfoWidget>("Debug");
  972. m_debug_info_widget->on_backtrace_frame_selection = [this](Debug::DebugInfo::SourcePosition const& source_position) {
  973. open_file(get_absolute_path(source_position.file_path), source_position.line_number - 1);
  974. };
  975. m_disassembly_widget = m_action_tab_widget->add_tab<DisassemblyWidget>("Disassembly");
  976. m_git_widget = m_action_tab_widget->add_tab<GitWidget>("Git", m_project->root_path());
  977. m_git_widget->set_view_diff_callback([this](const auto& original_content, const auto& diff) {
  978. m_diff_viewer->set_content(original_content, diff);
  979. set_edit_mode(EditMode::Diff);
  980. });
  981. m_gml_preview_widget = m_action_tab_widget->add_tab<GMLPreviewWidget>("GML Preview", "");
  982. ToDoEntries::the().on_update = [this]() {
  983. m_todo_entries_widget->refresh();
  984. };
  985. }
  986. void HackStudioWidget::create_project_tab(GUI::Widget& parent)
  987. {
  988. m_project_tab = parent.add<GUI::TabWidget>();
  989. m_project_tab->set_tab_position(GUI::TabWidget::TabPosition::Bottom);
  990. auto& tree_view_container = m_project_tab->add_tab<GUI::Widget>("Files");
  991. tree_view_container.set_layout<GUI::VerticalBoxLayout>();
  992. tree_view_container.layout()->set_margins(2);
  993. m_project_tree_view = tree_view_container.add<GUI::TreeView>();
  994. configure_project_tree_view();
  995. auto& class_view_container = m_project_tab->add_tab<GUI::Widget>("Classes");
  996. class_view_container.set_layout<GUI::VerticalBoxLayout>();
  997. class_view_container.layout()->set_margins(2);
  998. m_class_view = class_view_container.add<ClassViewWidget>();
  999. ProjectDeclarations::the().on_update = [this]() {
  1000. m_class_view->refresh();
  1001. };
  1002. }
  1003. void HackStudioWidget::update_recent_projects_submenu()
  1004. {
  1005. if (!m_recent_projects_submenu)
  1006. return;
  1007. m_recent_projects_submenu->remove_all_actions();
  1008. auto recent_projects = read_recent_projects();
  1009. if (recent_projects.size() <= 1) {
  1010. auto empty_action = GUI::Action::create("Empty...", [](auto&) {});
  1011. empty_action->set_enabled(false);
  1012. m_recent_projects_submenu->add_action(empty_action);
  1013. return;
  1014. }
  1015. for (size_t i = 1; i < recent_projects.size(); i++) {
  1016. auto project_path = recent_projects[i];
  1017. m_recent_projects_submenu->add_action(GUI::Action::create(recent_projects[i], [this, project_path](auto&) {
  1018. open_project(project_path);
  1019. }));
  1020. }
  1021. }
  1022. void HackStudioWidget::create_file_menu(GUI::Window& window)
  1023. {
  1024. auto& file_menu = window.add_menu("&File");
  1025. auto& new_submenu = file_menu.add_submenu("New...");
  1026. new_submenu.add_action(*m_new_project_action);
  1027. new_submenu.add_separator();
  1028. for (auto& new_file_action : m_new_file_actions) {
  1029. new_submenu.add_action(new_file_action);
  1030. }
  1031. new_submenu.set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/new.png").release_value_but_fixme_should_propagate_errors());
  1032. new_submenu.add_action(*m_new_plain_file_action);
  1033. new_submenu.add_separator();
  1034. new_submenu.add_action(*m_new_directory_action);
  1035. file_menu.add_action(*m_open_action);
  1036. m_recent_projects_submenu = &file_menu.add_submenu("Open Recent");
  1037. 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());
  1038. update_recent_projects_submenu();
  1039. file_menu.add_action(*m_save_action);
  1040. file_menu.add_action(*m_save_as_action);
  1041. file_menu.add_separator();
  1042. file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  1043. GUI::Application::the()->quit();
  1044. }));
  1045. }
  1046. void HackStudioWidget::create_edit_menu(GUI::Window& window)
  1047. {
  1048. auto& edit_menu = window.add_menu("&Edit");
  1049. 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&) {
  1050. reveal_action_tab(*m_find_in_files_widget);
  1051. m_find_in_files_widget->focus_textbox_and_select_all();
  1052. }));
  1053. edit_menu.add_separator();
  1054. auto vim_emulation_setting_action = GUI::Action::create_checkable("&Vim Emulation", { Mod_Ctrl | Mod_Shift | Mod_Alt, Key_V }, [this](auto& action) {
  1055. if (action.is_checked()) {
  1056. for (auto& editor_wrapper : m_all_editor_wrappers)
  1057. editor_wrapper.editor().set_editing_engine(make<GUI::VimEditingEngine>());
  1058. } else {
  1059. for (auto& editor_wrapper : m_all_editor_wrappers)
  1060. editor_wrapper.editor().set_editing_engine(make<GUI::RegularEditingEngine>());
  1061. }
  1062. });
  1063. vim_emulation_setting_action->set_checked(false);
  1064. edit_menu.add_action(vim_emulation_setting_action);
  1065. }
  1066. void HackStudioWidget::create_build_menu(GUI::Window& window)
  1067. {
  1068. auto& build_menu = window.add_menu("&Build");
  1069. build_menu.add_action(*m_build_action);
  1070. build_menu.add_separator();
  1071. build_menu.add_action(*m_run_action);
  1072. build_menu.add_action(*m_stop_action);
  1073. build_menu.add_separator();
  1074. build_menu.add_action(*m_debug_action);
  1075. }
  1076. void HackStudioWidget::create_view_menu(GUI::Window& window)
  1077. {
  1078. auto hide_action_tabs_action = GUI::Action::create("&Hide Action Tabs", { Mod_Ctrl | Mod_Shift, Key_X }, [this](auto&) {
  1079. hide_action_tabs();
  1080. });
  1081. auto open_locator_action = GUI::Action::create("Open &Locator", { Mod_Ctrl, Key_K }, [this](auto&) {
  1082. m_locator->open();
  1083. });
  1084. auto show_dotfiles_action = GUI::Action::create_checkable("S&how Dotfiles", { Mod_Ctrl, Key_H }, [&](auto& checked) {
  1085. project().model().set_should_show_dotfiles(checked.is_checked());
  1086. });
  1087. auto& view_menu = window.add_menu("&View");
  1088. view_menu.add_action(hide_action_tabs_action);
  1089. view_menu.add_action(open_locator_action);
  1090. view_menu.add_action(show_dotfiles_action);
  1091. m_toggle_semantic_highlighting_action = create_toggle_syntax_highlighting_mode_action();
  1092. view_menu.add_action(*m_toggle_semantic_highlighting_action);
  1093. view_menu.add_separator();
  1094. m_wrapping_mode_actions.set_exclusive(true);
  1095. auto& wrapping_mode_menu = view_menu.add_submenu("&Wrapping Mode");
  1096. m_no_wrapping_action = GUI::Action::create_checkable("&No Wrapping", [&](auto&) {
  1097. for (auto& wrapper : m_all_editor_wrappers)
  1098. wrapper.editor().set_wrapping_mode(GUI::TextEditor::WrappingMode::NoWrap);
  1099. });
  1100. m_wrap_anywhere_action = GUI::Action::create_checkable("Wrap &Anywhere", [&](auto&) {
  1101. for (auto& wrapper : m_all_editor_wrappers)
  1102. wrapper.editor().set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAnywhere);
  1103. });
  1104. m_wrap_at_words_action = GUI::Action::create_checkable("Wrap at &Words", [&](auto&) {
  1105. for (auto& wrapper : m_all_editor_wrappers)
  1106. wrapper.editor().set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAtWords);
  1107. });
  1108. m_wrapping_mode_actions.add_action(*m_no_wrapping_action);
  1109. m_wrapping_mode_actions.add_action(*m_wrap_anywhere_action);
  1110. m_wrapping_mode_actions.add_action(*m_wrap_at_words_action);
  1111. wrapping_mode_menu.add_action(*m_no_wrapping_action);
  1112. wrapping_mode_menu.add_action(*m_wrap_anywhere_action);
  1113. wrapping_mode_menu.add_action(*m_wrap_at_words_action);
  1114. m_no_wrapping_action->set_checked(true);
  1115. 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(),
  1116. [&](auto&) {
  1117. auto picker = GUI::FontPicker::construct(&window, m_editor_font, false);
  1118. if (picker->exec() == GUI::Dialog::ExecOK) {
  1119. change_editor_font(picker->font());
  1120. }
  1121. });
  1122. view_menu.add_action(*m_editor_font_action);
  1123. view_menu.add_separator();
  1124. view_menu.add_action(*m_add_editor_action);
  1125. view_menu.add_action(*m_remove_current_editor_action);
  1126. view_menu.add_action(*m_add_terminal_action);
  1127. view_menu.add_action(*m_remove_current_terminal_action);
  1128. view_menu.add_separator();
  1129. create_location_history_actions();
  1130. view_menu.add_action(*m_locations_history_back_action);
  1131. view_menu.add_action(*m_locations_history_forward_action);
  1132. }
  1133. void HackStudioWidget::create_help_menu(GUI::Window& window)
  1134. {
  1135. auto& help_menu = window.add_menu("&Help");
  1136. help_menu.add_action(GUI::CommonActions::make_about_action("Hack Studio", GUI::Icon::default_icon("app-hack-studio"), &window));
  1137. }
  1138. NonnullRefPtr<GUI::Action> HackStudioWidget::create_stop_action()
  1139. {
  1140. 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&) {
  1141. if (!Debugger::the().session()) {
  1142. if (auto result = m_terminal_wrapper->kill_running_command(); result.is_error())
  1143. warnln("{}", result.error());
  1144. return;
  1145. }
  1146. Debugger::the().stop();
  1147. });
  1148. action->set_enabled(false);
  1149. return action;
  1150. }
  1151. void HackStudioWidget::initialize_menubar(GUI::Window& window)
  1152. {
  1153. create_file_menu(window);
  1154. create_edit_menu(window);
  1155. create_build_menu(window);
  1156. create_view_menu(window);
  1157. create_help_menu(window);
  1158. }
  1159. void HackStudioWidget::update_statusbar()
  1160. {
  1161. StringBuilder builder;
  1162. if (current_editor().has_selection()) {
  1163. String selected_text = current_editor().selected_text();
  1164. auto word_count = current_editor().number_of_selected_words();
  1165. builder.appendff("Selected: {} {} ({} {})", selected_text.length(), selected_text.length() == 1 ? "character" : "characters", word_count, word_count != 1 ? "words" : "word");
  1166. }
  1167. m_statusbar->set_text(0, builder.to_string());
  1168. m_statusbar->set_text(1, current_editor_wrapper().editor().code_document().language_name());
  1169. m_statusbar->set_text(2, String::formatted("Ln {}, Col {}", current_editor().cursor().line() + 1, current_editor().cursor().column()));
  1170. }
  1171. void HackStudioWidget::handle_external_file_deletion(const String& filepath)
  1172. {
  1173. close_file_in_all_editors(filepath);
  1174. }
  1175. void HackStudioWidget::stop_debugger_if_running()
  1176. {
  1177. if (!m_debugger_thread.is_null()) {
  1178. Debugger::the().stop();
  1179. dbgln("Waiting for debugger thread to terminate");
  1180. auto rc = m_debugger_thread->join();
  1181. if (rc.is_error()) {
  1182. warnln("pthread_join: {}", strerror(rc.error().value()));
  1183. dbgln("error joining debugger thread");
  1184. }
  1185. }
  1186. }
  1187. void HackStudioWidget::close_current_project()
  1188. {
  1189. m_editors_splitter->remove_all_children();
  1190. m_all_editor_wrappers.clear();
  1191. m_open_files.clear();
  1192. m_open_files_vector.clear();
  1193. add_new_editor(*m_editors_splitter);
  1194. m_find_in_files_widget->reset();
  1195. m_todo_entries_widget->clear();
  1196. m_terminal_wrapper->clear_including_history();
  1197. stop_debugger_if_running();
  1198. update_gml_preview();
  1199. }
  1200. HackStudioWidget::~HackStudioWidget()
  1201. {
  1202. stop_debugger_if_running();
  1203. }
  1204. HackStudioWidget::ContinueDecision HackStudioWidget::warn_unsaved_changes(const String& prompt)
  1205. {
  1206. if (!any_document_is_dirty())
  1207. return ContinueDecision::Yes;
  1208. auto result = GUI::MessageBox::show(window(), prompt, "Unsaved changes", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel);
  1209. if (result == GUI::MessageBox::ExecCancel)
  1210. return ContinueDecision::No;
  1211. if (result == GUI::MessageBox::ExecYes) {
  1212. for (auto& editor_wrapper : m_all_editor_wrappers) {
  1213. if (editor_wrapper.editor().document().is_modified()) {
  1214. editor_wrapper.save();
  1215. }
  1216. }
  1217. }
  1218. return ContinueDecision::Yes;
  1219. }
  1220. bool HackStudioWidget::any_document_is_dirty() const
  1221. {
  1222. return any_of(m_all_editor_wrappers, [](auto& editor_wrapper) {
  1223. return editor_wrapper.editor().document().is_modified();
  1224. });
  1225. }
  1226. void HackStudioWidget::update_gml_preview()
  1227. {
  1228. auto gml_content = current_editor_wrapper().filename().ends_with(".gml") ? current_editor_wrapper().editor().text() : "";
  1229. m_gml_preview_widget->load_gml(gml_content);
  1230. }
  1231. void HackStudioWidget::update_tree_view()
  1232. {
  1233. auto index = m_project->model().index(m_current_editor_wrapper->filename(), GUI::FileSystemModel::Column::Name);
  1234. if (index.is_valid()) {
  1235. m_project_tree_view->expand_all_parents_of(index);
  1236. m_project_tree_view->set_cursor(index, GUI::AbstractView::SelectionUpdate::Set);
  1237. }
  1238. }
  1239. void HackStudioWidget::update_window_title()
  1240. {
  1241. window()->set_title(String::formatted("{} - {} - Hack Studio", m_current_editor_wrapper->filename_label().text(), m_project->name()));
  1242. }
  1243. void HackStudioWidget::on_cursor_change()
  1244. {
  1245. update_statusbar();
  1246. if (current_editor_wrapper().filename().is_null())
  1247. return;
  1248. auto current_location = current_project_location();
  1249. if (m_locations_history_end_index != 0) {
  1250. auto last = m_locations_history[m_locations_history_end_index - 1];
  1251. if (current_location.filename == last.filename && current_location.line == last.line)
  1252. return;
  1253. }
  1254. // Clear "Go Forward" locations
  1255. VERIFY(m_locations_history_end_index <= m_locations_history.size());
  1256. m_locations_history.remove(m_locations_history_end_index, m_locations_history.size() - m_locations_history_end_index);
  1257. m_locations_history.append(current_location);
  1258. constexpr size_t max_locations = 30;
  1259. if (m_locations_history.size() > max_locations)
  1260. m_locations_history.take_first();
  1261. m_locations_history_end_index = m_locations_history.size();
  1262. update_history_actions();
  1263. }
  1264. void HackStudioWidget::create_location_history_actions()
  1265. {
  1266. 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&) {
  1267. if (m_locations_history_end_index <= 1)
  1268. return;
  1269. auto location = m_locations_history[m_locations_history_end_index - 2];
  1270. --m_locations_history_end_index;
  1271. m_locations_history_disabled = true;
  1272. open_file(location.filename, location.line, location.column);
  1273. m_locations_history_disabled = false;
  1274. update_history_actions();
  1275. });
  1276. 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&) {
  1277. if (m_locations_history_end_index == m_locations_history.size())
  1278. return;
  1279. auto location = m_locations_history[m_locations_history_end_index];
  1280. ++m_locations_history_end_index;
  1281. m_locations_history_disabled = true;
  1282. open_file(location.filename, location.line, location.column);
  1283. m_locations_history_disabled = false;
  1284. update_history_actions();
  1285. });
  1286. m_locations_history_forward_action->set_enabled(false);
  1287. }
  1288. HackStudioWidget::ProjectLocation HackStudioWidget::current_project_location() const
  1289. {
  1290. return ProjectLocation { current_editor_wrapper().filename(), current_editor().cursor().line(), current_editor().cursor().column() };
  1291. }
  1292. void HackStudioWidget::update_history_actions()
  1293. {
  1294. if (m_locations_history_end_index <= 1)
  1295. m_locations_history_back_action->set_enabled(false);
  1296. else
  1297. m_locations_history_back_action->set_enabled(true);
  1298. if (m_locations_history_end_index == m_locations_history.size())
  1299. m_locations_history_forward_action->set_enabled(false);
  1300. else
  1301. m_locations_history_forward_action->set_enabled(true);
  1302. }
  1303. RefPtr<Gfx::Font> HackStudioWidget::read_editor_font_from_config()
  1304. {
  1305. auto font_family = Config::read_string("HackStudio", "EditorFont", "Family", "Csilla");
  1306. auto font_variant = Config::read_string("HackStudio", "EditorFont", "Variant", "Regular");
  1307. auto font_size = Config::read_i32("HackStudio", "EditorFont", "Size", 10);
  1308. auto font = Gfx::FontDatabase::the().get(font_family, font_variant, font_size);
  1309. if (font.is_null())
  1310. return Gfx::FontDatabase::the().default_fixed_width_font();
  1311. return font;
  1312. }
  1313. void HackStudioWidget::change_editor_font(RefPtr<Gfx::Font> font)
  1314. {
  1315. m_editor_font = move(font);
  1316. for (auto& editor_wrapper : m_all_editor_wrappers) {
  1317. editor_wrapper.editor().set_font(*m_editor_font);
  1318. }
  1319. Config::write_string("HackStudio", "EditorFont", "Family", m_editor_font->family());
  1320. Config::write_string("HackStudio", "EditorFont", "Variant", m_editor_font->variant());
  1321. Config::write_i32("HackStudio", "EditorFont", "Size", m_editor_font->presentation_size());
  1322. }
  1323. void HackStudioWidget::open_coredump(String const& coredump_path)
  1324. {
  1325. open_project("/usr/src/serenity");
  1326. m_mode = Mode::Coredump;
  1327. m_coredump_inspector = Coredump::Inspector::create(coredump_path, [this](float progress) {
  1328. window()->set_progress(progress * 100);
  1329. });
  1330. window()->set_progress(0);
  1331. if (m_coredump_inspector) {
  1332. m_debug_info_widget->update_state(*m_coredump_inspector, m_coredump_inspector->get_registers());
  1333. reveal_action_tab(*m_debug_info_widget);
  1334. }
  1335. }
  1336. void HackStudioWidget::for_each_open_file(Function<void(ProjectFile const&)> func)
  1337. {
  1338. for (auto& open_file : m_open_files) {
  1339. func(*open_file.value);
  1340. }
  1341. }
  1342. NonnullRefPtr<GUI::Action> HackStudioWidget::create_toggle_syntax_highlighting_mode_action()
  1343. {
  1344. 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) {
  1345. for (auto& editor_wrapper : m_all_editor_wrappers)
  1346. editor_wrapper.editor().set_semantic_syntax_highlighting(action.is_checked());
  1347. });
  1348. return action;
  1349. }
  1350. bool HackStudioWidget::semantic_syntax_highlighting_is_enabled() const
  1351. {
  1352. return m_toggle_semantic_highlighting_action->is_checked();
  1353. }
  1354. }