HackStudioWidget.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  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. reveal_action_tab(terminal_wrapper);
  704. update_actions();
  705. terminal_wrapper.terminal().set_focus(true);
  706. });
  707. }
  708. void HackStudioWidget::reveal_action_tab(GUI::Widget& widget)
  709. {
  710. if (m_action_tab_widget->min_height() < 200)
  711. m_action_tab_widget->set_fixed_height(200);
  712. m_action_tab_widget->set_active_widget(&widget);
  713. }
  714. NonnullRefPtr<GUI::Action> HackStudioWidget::create_debug_action()
  715. {
  716. 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&) {
  717. if (!Core::File::exists(get_project_executable_path())) {
  718. GUI::MessageBox::show(window(), String::formatted("Could not find file: {}. (did you build the project?)", get_project_executable_path()), "Error", GUI::MessageBox::Type::Error);
  719. return;
  720. }
  721. if (Debugger::the().session()) {
  722. GUI::MessageBox::show(window(), "Debugger is already running", "Error", GUI::MessageBox::Type::Error);
  723. return;
  724. }
  725. Debugger::the().set_executable_path(get_project_executable_path());
  726. m_terminal_wrapper->clear_including_history();
  727. // The debugger calls wait() on the debugee, so the TerminalWrapper can't do that.
  728. auto ptm_res = m_terminal_wrapper->setup_master_pseudoterminal(TerminalWrapper::WaitForChildOnExit::No);
  729. if (ptm_res.is_error()) {
  730. perror("setup_master_pseudoterminal");
  731. return;
  732. }
  733. Debugger::the().set_child_setup_callback([this, ptm_res]() {
  734. return m_terminal_wrapper->setup_slave_pseudoterminal(ptm_res.value());
  735. });
  736. m_debugger_thread = Threading::Thread::construct(Debugger::start_static);
  737. m_debugger_thread->start();
  738. m_stop_action->set_enabled(true);
  739. m_run_action->set_enabled(false);
  740. for (auto& editor_wrapper : m_all_editor_wrappers) {
  741. editor_wrapper.set_debug_mode(true);
  742. }
  743. });
  744. }
  745. void HackStudioWidget::initialize_debugger()
  746. {
  747. Debugger::initialize(
  748. m_project->root_path(),
  749. [this](const PtraceRegisters& regs) {
  750. VERIFY(Debugger::the().session());
  751. const auto& debug_session = *Debugger::the().session();
  752. auto source_position = debug_session.get_source_position(regs.ip());
  753. if (!source_position.has_value()) {
  754. dbgln("Could not find source position for address: {:p}", regs.ip());
  755. return Debugger::HasControlPassedToUser::No;
  756. }
  757. dbgln("Debugger stopped at source position: {}:{}", source_position.value().file_path, source_position.value().line_number);
  758. deferred_invoke([this, source_position, &regs] {
  759. m_current_editor_in_execution = get_editor_of_file(source_position.value().file_path);
  760. if (m_current_editor_in_execution)
  761. m_current_editor_in_execution->editor().set_execution_position(source_position.value().line_number - 1);
  762. m_debug_info_widget->update_state(*Debugger::the().session(), regs);
  763. m_debug_info_widget->set_debug_actions_enabled(true);
  764. m_disassembly_widget->update_state(*Debugger::the().session(), regs);
  765. HackStudioWidget::reveal_action_tab(*m_debug_info_widget);
  766. });
  767. Core::EventLoop::wake_current();
  768. return Debugger::HasControlPassedToUser::Yes;
  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. });
  776. Core::EventLoop::wake_current();
  777. },
  778. [this]() {
  779. deferred_invoke([this] {
  780. m_debug_info_widget->set_debug_actions_enabled(false);
  781. if (m_current_editor_in_execution)
  782. m_current_editor_in_execution->editor().clear_execution_position();
  783. m_debug_info_widget->program_stopped();
  784. m_disassembly_widget->program_stopped();
  785. m_stop_action->set_enabled(false);
  786. m_run_action->set_enabled(true);
  787. m_debugger_thread.clear();
  788. for (auto& editor_wrapper : m_all_editor_wrappers) {
  789. editor_wrapper.set_debug_mode(false);
  790. }
  791. HackStudioWidget::hide_action_tabs();
  792. GUI::MessageBox::show(window(), "Program Exited", "Debugger", GUI::MessageBox::Type::Information);
  793. });
  794. Core::EventLoop::wake_current();
  795. });
  796. }
  797. String HackStudioWidget::get_full_path_of_serenity_source(const String& file)
  798. {
  799. auto path_parts = LexicalPath(file).parts();
  800. while (!path_parts.is_empty() && path_parts[0] == "..") {
  801. path_parts.remove(0);
  802. }
  803. StringBuilder relative_path_builder;
  804. relative_path_builder.join("/", path_parts);
  805. constexpr char SERENITY_LIBS_PREFIX[] = "/usr/src/serenity";
  806. LexicalPath serenity_sources_base(SERENITY_LIBS_PREFIX);
  807. return String::formatted("{}/{}", serenity_sources_base, relative_path_builder.to_string());
  808. }
  809. String HackStudioWidget::get_absolute_path(const String& path) const
  810. {
  811. // TODO: We can probably do a more specific condition here, something like
  812. // "if (file.starts_with("../Libraries/") || file.starts_with("../AK/"))"
  813. if (path.starts_with("..")) {
  814. return get_full_path_of_serenity_source(path);
  815. }
  816. return m_project->to_absolute_path(path);
  817. }
  818. RefPtr<EditorWrapper> HackStudioWidget::get_editor_of_file(const String& filename)
  819. {
  820. String file_path = filename;
  821. if (filename.starts_with("../")) {
  822. file_path = get_full_path_of_serenity_source(filename);
  823. }
  824. if (!open_file(file_path))
  825. return nullptr;
  826. return current_editor_wrapper();
  827. }
  828. String HackStudioWidget::get_project_executable_path() const
  829. {
  830. // FIXME: Dumb heuristic ahead!
  831. // e.g /my/project => /my/project/project
  832. // TODO: Perhaps a Makefile rule for getting the value of $(PROGRAM) would be better?
  833. return String::formatted("{}/{}", m_project->root_path(), LexicalPath::basename(m_project->root_path()));
  834. }
  835. void HackStudioWidget::build()
  836. {
  837. auto result = m_project_builder->build(active_file());
  838. if (result.is_error()) {
  839. GUI::MessageBox::show(window(), String::formatted("{}", result.error()), "Build failed", GUI::MessageBox::Type::Error);
  840. }
  841. }
  842. void HackStudioWidget::run()
  843. {
  844. auto result = m_project_builder->run(active_file());
  845. if (result.is_error()) {
  846. GUI::MessageBox::show(window(), String::formatted("{}", result.error()), "Run failed", GUI::MessageBox::Type::Error);
  847. }
  848. }
  849. void HackStudioWidget::hide_action_tabs()
  850. {
  851. m_action_tab_widget->set_fixed_height(24);
  852. };
  853. Project& HackStudioWidget::project()
  854. {
  855. return *m_project;
  856. }
  857. void HackStudioWidget::set_current_editor_wrapper(RefPtr<EditorWrapper> editor_wrapper)
  858. {
  859. m_current_editor_wrapper = editor_wrapper;
  860. update_window_title();
  861. update_tree_view();
  862. }
  863. void HackStudioWidget::file_renamed(String const& old_name, String const& new_name)
  864. {
  865. auto editor_or_none = m_all_editor_wrappers.first_matching([&old_name](auto const& editor) {
  866. return editor->filename() == old_name;
  867. });
  868. if (editor_or_none.has_value()) {
  869. (*editor_or_none)->set_filename(new_name);
  870. (*editor_or_none)->set_name(new_name);
  871. }
  872. if (m_open_files.contains(old_name)) {
  873. VERIFY(m_open_files_vector.remove_first_matching([&old_name](auto const& file) { return file == old_name; }));
  874. m_open_files_vector.append(new_name);
  875. ProjectFile* f = m_open_files.get(old_name).release_value();
  876. m_open_files.set(new_name, *f);
  877. m_open_files.remove(old_name);
  878. m_open_files_view->model()->invalidate();
  879. }
  880. if (m_file_watcher->is_watching(old_name)) {
  881. VERIFY(!m_file_watcher->remove_watch(old_name).is_error());
  882. VERIFY(!m_file_watcher->add_watch(new_name, Core::FileWatcherEvent::Type::Deleted).is_error());
  883. }
  884. }
  885. void HackStudioWidget::configure_project_tree_view()
  886. {
  887. m_project_tree_view->set_model(m_project->model());
  888. m_project_tree_view->set_selection_mode(GUI::AbstractView::SelectionMode::MultiSelection);
  889. m_project_tree_view->set_editable(true);
  890. m_project_tree_view->aid_create_editing_delegate = [](auto&) {
  891. return make<GUI::StringModelEditingDelegate>();
  892. };
  893. for (int column_index = 0; column_index < m_project->model().column_count(); ++column_index)
  894. m_project_tree_view->set_column_visible(column_index, false);
  895. m_project_tree_view->set_column_visible(GUI::FileSystemModel::Column::Name, true);
  896. m_project_tree_view->on_context_menu_request = [this](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  897. if (index.is_valid()) {
  898. m_project_tree_view_context_menu->popup(event.screen_position(), m_open_selected_action);
  899. }
  900. };
  901. m_project_tree_view->on_selection_change = [this] {
  902. m_open_selected_action->set_enabled(!m_project_tree_view->selection().is_empty());
  903. auto selections = m_project_tree_view->selection().indices();
  904. auto it = selections.find_if([&](auto selected_file) {
  905. return access(m_project->model().full_path(selected_file.parent()).characters(), W_OK) == 0;
  906. });
  907. bool has_permissions = it != selections.end();
  908. m_tree_view_rename_action->set_enabled(has_permissions);
  909. m_delete_action->set_enabled(has_permissions);
  910. };
  911. m_project_tree_view->on_activation = [this](auto& index) {
  912. auto full_path_to_file = m_project->model().full_path(index);
  913. open_file(full_path_to_file);
  914. };
  915. }
  916. void HackStudioWidget::create_open_files_view(GUI::Widget& parent)
  917. {
  918. m_open_files_view = parent.add<GUI::ListView>();
  919. auto open_files_model = GUI::ItemListModel<String>::create(m_open_files_vector);
  920. m_open_files_view->set_model(open_files_model);
  921. m_open_files_view->on_activation = [this](auto& index) {
  922. open_file(index.data().to_string());
  923. };
  924. }
  925. void HackStudioWidget::create_toolbar(GUI::Widget& parent)
  926. {
  927. auto& toolbar = parent.add<GUI::Toolbar>();
  928. toolbar.add_action(*m_new_plain_file_action);
  929. toolbar.add_action(*m_new_directory_action);
  930. toolbar.add_action(*m_save_action);
  931. toolbar.add_action(*m_delete_action);
  932. toolbar.add_separator();
  933. toolbar.add_action(GUI::CommonActions::make_cut_action([this](auto&) { current_editor().cut_action().activate(); }));
  934. toolbar.add_action(GUI::CommonActions::make_copy_action([this](auto&) { current_editor().copy_action().activate(); }));
  935. toolbar.add_action(GUI::CommonActions::make_paste_action([this](auto&) { current_editor().paste_action().activate(); }));
  936. toolbar.add_separator();
  937. toolbar.add_action(GUI::CommonActions::make_undo_action([this](auto&) { current_editor().undo_action().activate(); }));
  938. toolbar.add_action(GUI::CommonActions::make_redo_action([this](auto&) { current_editor().redo_action().activate(); }));
  939. toolbar.add_separator();
  940. toolbar.add_action(*m_build_action);
  941. toolbar.add_separator();
  942. toolbar.add_action(*m_run_action);
  943. toolbar.add_action(*m_stop_action);
  944. toolbar.add_separator();
  945. toolbar.add_action(*m_debug_action);
  946. }
  947. NonnullRefPtr<GUI::Action> HackStudioWidget::create_build_action()
  948. {
  949. 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&) {
  950. if (warn_unsaved_changes("There are unsaved changes, do you want to save before building?") == ContinueDecision::No)
  951. return;
  952. reveal_action_tab(*m_terminal_wrapper);
  953. build();
  954. m_stop_action->set_enabled(true);
  955. });
  956. }
  957. NonnullRefPtr<GUI::Action> HackStudioWidget::create_run_action()
  958. {
  959. 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&) {
  960. reveal_action_tab(*m_terminal_wrapper);
  961. run();
  962. m_stop_action->set_enabled(true);
  963. });
  964. }
  965. void HackStudioWidget::create_action_tab(GUI::Widget& parent)
  966. {
  967. m_action_tab_widget = parent.add<GUI::TabWidget>();
  968. m_action_tab_widget->set_fixed_height(24);
  969. m_action_tab_widget->on_change = [this](auto&) {
  970. on_action_tab_change();
  971. static bool first_time = true;
  972. if (!first_time)
  973. m_action_tab_widget->set_fixed_height(200);
  974. first_time = false;
  975. };
  976. m_find_in_files_widget = m_action_tab_widget->add_tab<FindInFilesWidget>("Find in files");
  977. m_todo_entries_widget = m_action_tab_widget->add_tab<ToDoEntriesWidget>("TODO");
  978. m_terminal_wrapper = m_action_tab_widget->add_tab<TerminalWrapper>("Console", false);
  979. m_debug_info_widget = m_action_tab_widget->add_tab<DebugInfoWidget>("Debug");
  980. m_debug_info_widget->on_backtrace_frame_selection = [this](Debug::DebugInfo::SourcePosition const& source_position) {
  981. open_file(get_absolute_path(source_position.file_path), source_position.line_number - 1);
  982. };
  983. m_disassembly_widget = m_action_tab_widget->add_tab<DisassemblyWidget>("Disassembly");
  984. m_git_widget = m_action_tab_widget->add_tab<GitWidget>("Git", m_project->root_path());
  985. m_git_widget->set_view_diff_callback([this](const auto& original_content, const auto& diff) {
  986. m_diff_viewer->set_content(original_content, diff);
  987. set_edit_mode(EditMode::Diff);
  988. });
  989. m_gml_preview_widget = m_action_tab_widget->add_tab<GMLPreviewWidget>("GML Preview", "");
  990. ToDoEntries::the().on_update = [this]() {
  991. m_todo_entries_widget->refresh();
  992. };
  993. }
  994. void HackStudioWidget::create_project_tab(GUI::Widget& parent)
  995. {
  996. m_project_tab = parent.add<GUI::TabWidget>();
  997. m_project_tab->set_tab_position(GUI::TabWidget::TabPosition::Bottom);
  998. auto& tree_view_container = m_project_tab->add_tab<GUI::Widget>("Files");
  999. tree_view_container.set_layout<GUI::VerticalBoxLayout>();
  1000. tree_view_container.layout()->set_margins(2);
  1001. m_project_tree_view = tree_view_container.add<GUI::TreeView>();
  1002. configure_project_tree_view();
  1003. auto& class_view_container = m_project_tab->add_tab<GUI::Widget>("Classes");
  1004. class_view_container.set_layout<GUI::VerticalBoxLayout>();
  1005. class_view_container.layout()->set_margins(2);
  1006. m_class_view = class_view_container.add<ClassViewWidget>();
  1007. ProjectDeclarations::the().on_update = [this]() {
  1008. m_class_view->refresh();
  1009. };
  1010. }
  1011. void HackStudioWidget::update_recent_projects_submenu()
  1012. {
  1013. if (!m_recent_projects_submenu)
  1014. return;
  1015. m_recent_projects_submenu->remove_all_actions();
  1016. auto recent_projects = read_recent_projects();
  1017. if (recent_projects.size() <= 1) {
  1018. auto empty_action = GUI::Action::create("Empty...", [](auto&) {});
  1019. empty_action->set_enabled(false);
  1020. m_recent_projects_submenu->add_action(empty_action);
  1021. return;
  1022. }
  1023. for (size_t i = 1; i < recent_projects.size(); i++) {
  1024. auto project_path = recent_projects[i];
  1025. m_recent_projects_submenu->add_action(GUI::Action::create(recent_projects[i], [this, project_path](auto&) {
  1026. open_project(project_path);
  1027. }));
  1028. }
  1029. }
  1030. void HackStudioWidget::create_file_menu(GUI::Window& window)
  1031. {
  1032. auto& file_menu = window.add_menu("&File");
  1033. auto& new_submenu = file_menu.add_submenu("&New...");
  1034. new_submenu.add_action(*m_new_project_action);
  1035. new_submenu.add_separator();
  1036. for (auto& new_file_action : m_new_file_actions) {
  1037. new_submenu.add_action(new_file_action);
  1038. }
  1039. new_submenu.set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/new.png").release_value_but_fixme_should_propagate_errors());
  1040. new_submenu.add_action(*m_new_plain_file_action);
  1041. new_submenu.add_separator();
  1042. new_submenu.add_action(*m_new_directory_action);
  1043. file_menu.add_action(*m_open_action);
  1044. m_recent_projects_submenu = &file_menu.add_submenu("Open &Recent");
  1045. 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());
  1046. update_recent_projects_submenu();
  1047. file_menu.add_action(*m_save_action);
  1048. file_menu.add_action(*m_save_as_action);
  1049. file_menu.add_separator();
  1050. file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  1051. GUI::Application::the()->quit();
  1052. }));
  1053. }
  1054. void HackStudioWidget::create_edit_menu(GUI::Window& window)
  1055. {
  1056. auto& edit_menu = window.add_menu("&Edit");
  1057. 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&) {
  1058. reveal_action_tab(*m_find_in_files_widget);
  1059. m_find_in_files_widget->focus_textbox_and_select_all();
  1060. }));
  1061. edit_menu.add_separator();
  1062. auto vim_emulation_setting_action = GUI::Action::create_checkable("&Vim Emulation", { Mod_Ctrl | Mod_Shift | Mod_Alt, Key_V }, [this](auto& action) {
  1063. if (action.is_checked()) {
  1064. for (auto& editor_wrapper : m_all_editor_wrappers)
  1065. editor_wrapper.editor().set_editing_engine(make<GUI::VimEditingEngine>());
  1066. } else {
  1067. for (auto& editor_wrapper : m_all_editor_wrappers)
  1068. editor_wrapper.editor().set_editing_engine(make<GUI::RegularEditingEngine>());
  1069. }
  1070. });
  1071. vim_emulation_setting_action->set_checked(false);
  1072. edit_menu.add_action(vim_emulation_setting_action);
  1073. }
  1074. void HackStudioWidget::create_build_menu(GUI::Window& window)
  1075. {
  1076. auto& build_menu = window.add_menu("&Build");
  1077. build_menu.add_action(*m_build_action);
  1078. build_menu.add_separator();
  1079. build_menu.add_action(*m_run_action);
  1080. build_menu.add_action(*m_stop_action);
  1081. build_menu.add_separator();
  1082. build_menu.add_action(*m_debug_action);
  1083. }
  1084. void HackStudioWidget::create_view_menu(GUI::Window& window)
  1085. {
  1086. auto hide_action_tabs_action = GUI::Action::create("&Hide Action Tabs", { Mod_Ctrl | Mod_Shift, Key_X }, [this](auto&) {
  1087. hide_action_tabs();
  1088. });
  1089. auto open_locator_action = GUI::Action::create("Open &Locator", { Mod_Ctrl, Key_K }, [this](auto&) {
  1090. m_locator->open();
  1091. });
  1092. auto show_dotfiles_action = GUI::Action::create_checkable("S&how Dotfiles", { Mod_Ctrl, Key_H }, [&](auto& checked) {
  1093. project().model().set_should_show_dotfiles(checked.is_checked());
  1094. });
  1095. auto& view_menu = window.add_menu("&View");
  1096. view_menu.add_action(hide_action_tabs_action);
  1097. view_menu.add_action(open_locator_action);
  1098. view_menu.add_action(show_dotfiles_action);
  1099. m_toggle_semantic_highlighting_action = create_toggle_syntax_highlighting_mode_action();
  1100. view_menu.add_action(*m_toggle_semantic_highlighting_action);
  1101. view_menu.add_separator();
  1102. m_wrapping_mode_actions.set_exclusive(true);
  1103. auto& wrapping_mode_menu = view_menu.add_submenu("&Wrapping Mode");
  1104. m_no_wrapping_action = GUI::Action::create_checkable("&No Wrapping", [&](auto&) {
  1105. for (auto& wrapper : m_all_editor_wrappers)
  1106. wrapper.editor().set_wrapping_mode(GUI::TextEditor::WrappingMode::NoWrap);
  1107. });
  1108. m_wrap_anywhere_action = GUI::Action::create_checkable("Wrap &Anywhere", [&](auto&) {
  1109. for (auto& wrapper : m_all_editor_wrappers)
  1110. wrapper.editor().set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAnywhere);
  1111. });
  1112. m_wrap_at_words_action = GUI::Action::create_checkable("Wrap at &Words", [&](auto&) {
  1113. for (auto& wrapper : m_all_editor_wrappers)
  1114. wrapper.editor().set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAtWords);
  1115. });
  1116. m_wrapping_mode_actions.add_action(*m_no_wrapping_action);
  1117. m_wrapping_mode_actions.add_action(*m_wrap_anywhere_action);
  1118. m_wrapping_mode_actions.add_action(*m_wrap_at_words_action);
  1119. wrapping_mode_menu.add_action(*m_no_wrapping_action);
  1120. wrapping_mode_menu.add_action(*m_wrap_anywhere_action);
  1121. wrapping_mode_menu.add_action(*m_wrap_at_words_action);
  1122. m_no_wrapping_action->set_checked(true);
  1123. 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(),
  1124. [&](auto&) {
  1125. auto picker = GUI::FontPicker::construct(&window, m_editor_font, false);
  1126. if (picker->exec() == GUI::Dialog::ExecOK) {
  1127. change_editor_font(picker->font());
  1128. }
  1129. });
  1130. view_menu.add_action(*m_editor_font_action);
  1131. view_menu.add_separator();
  1132. view_menu.add_action(*m_add_editor_action);
  1133. view_menu.add_action(*m_remove_current_editor_action);
  1134. view_menu.add_action(*m_add_terminal_action);
  1135. view_menu.add_action(*m_remove_current_terminal_action);
  1136. view_menu.add_separator();
  1137. create_location_history_actions();
  1138. view_menu.add_action(*m_locations_history_back_action);
  1139. view_menu.add_action(*m_locations_history_forward_action);
  1140. }
  1141. void HackStudioWidget::create_help_menu(GUI::Window& window)
  1142. {
  1143. auto& help_menu = window.add_menu("&Help");
  1144. help_menu.add_action(GUI::CommonActions::make_about_action("Hack Studio", GUI::Icon::default_icon("app-hack-studio"), &window));
  1145. }
  1146. NonnullRefPtr<GUI::Action> HackStudioWidget::create_stop_action()
  1147. {
  1148. 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&) {
  1149. if (!Debugger::the().session()) {
  1150. if (auto result = m_terminal_wrapper->kill_running_command(); result.is_error())
  1151. warnln("{}", result.error());
  1152. return;
  1153. }
  1154. Debugger::the().stop();
  1155. });
  1156. action->set_enabled(false);
  1157. return action;
  1158. }
  1159. void HackStudioWidget::initialize_menubar(GUI::Window& window)
  1160. {
  1161. create_file_menu(window);
  1162. create_edit_menu(window);
  1163. create_build_menu(window);
  1164. create_view_menu(window);
  1165. create_help_menu(window);
  1166. }
  1167. void HackStudioWidget::update_statusbar()
  1168. {
  1169. StringBuilder builder;
  1170. if (current_editor().has_selection()) {
  1171. String selected_text = current_editor().selected_text();
  1172. auto word_count = current_editor().number_of_selected_words();
  1173. builder.appendff("Selected: {} {} ({} {})", selected_text.length(), selected_text.length() == 1 ? "character" : "characters", word_count, word_count != 1 ? "words" : "word");
  1174. }
  1175. m_statusbar->set_text(0, builder.to_string());
  1176. m_statusbar->set_text(1, current_editor_wrapper().editor().code_document().language_name());
  1177. m_statusbar->set_text(2, String::formatted("Ln {}, Col {}", current_editor().cursor().line() + 1, current_editor().cursor().column()));
  1178. }
  1179. void HackStudioWidget::handle_external_file_deletion(const String& filepath)
  1180. {
  1181. close_file_in_all_editors(filepath);
  1182. }
  1183. void HackStudioWidget::stop_debugger_if_running()
  1184. {
  1185. if (!m_debugger_thread.is_null()) {
  1186. Debugger::the().stop();
  1187. dbgln("Waiting for debugger thread to terminate");
  1188. auto rc = m_debugger_thread->join();
  1189. if (rc.is_error()) {
  1190. warnln("pthread_join: {}", strerror(rc.error().value()));
  1191. dbgln("error joining debugger thread");
  1192. }
  1193. }
  1194. }
  1195. void HackStudioWidget::close_current_project()
  1196. {
  1197. m_editors_splitter->remove_all_children();
  1198. m_all_editor_wrappers.clear();
  1199. m_open_files.clear();
  1200. m_open_files_vector.clear();
  1201. add_new_editor(*m_editors_splitter);
  1202. m_find_in_files_widget->reset();
  1203. m_todo_entries_widget->clear();
  1204. m_terminal_wrapper->clear_including_history();
  1205. stop_debugger_if_running();
  1206. update_gml_preview();
  1207. }
  1208. HackStudioWidget::~HackStudioWidget()
  1209. {
  1210. stop_debugger_if_running();
  1211. }
  1212. HackStudioWidget::ContinueDecision HackStudioWidget::warn_unsaved_changes(const String& prompt)
  1213. {
  1214. if (!any_document_is_dirty())
  1215. return ContinueDecision::Yes;
  1216. auto result = GUI::MessageBox::show(window(), prompt, "Unsaved changes", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel);
  1217. if (result == GUI::MessageBox::ExecCancel)
  1218. return ContinueDecision::No;
  1219. if (result == GUI::MessageBox::ExecYes) {
  1220. for (auto& editor_wrapper : m_all_editor_wrappers) {
  1221. if (editor_wrapper.editor().document().is_modified()) {
  1222. editor_wrapper.save();
  1223. }
  1224. }
  1225. }
  1226. return ContinueDecision::Yes;
  1227. }
  1228. bool HackStudioWidget::any_document_is_dirty() const
  1229. {
  1230. return any_of(m_all_editor_wrappers, [](auto& editor_wrapper) {
  1231. return editor_wrapper.editor().document().is_modified();
  1232. });
  1233. }
  1234. void HackStudioWidget::update_gml_preview()
  1235. {
  1236. auto gml_content = current_editor_wrapper().filename().ends_with(".gml") ? current_editor_wrapper().editor().text() : "";
  1237. m_gml_preview_widget->load_gml(gml_content);
  1238. }
  1239. void HackStudioWidget::update_tree_view()
  1240. {
  1241. auto index = m_project->model().index(m_current_editor_wrapper->filename(), GUI::FileSystemModel::Column::Name);
  1242. if (index.is_valid()) {
  1243. m_project_tree_view->expand_all_parents_of(index);
  1244. m_project_tree_view->set_cursor(index, GUI::AbstractView::SelectionUpdate::Set);
  1245. }
  1246. }
  1247. void HackStudioWidget::update_window_title()
  1248. {
  1249. window()->set_title(String::formatted("{} - {} - Hack Studio", m_current_editor_wrapper->filename_label().text(), m_project->name()));
  1250. }
  1251. void HackStudioWidget::on_cursor_change()
  1252. {
  1253. update_statusbar();
  1254. if (current_editor_wrapper().filename().is_null())
  1255. return;
  1256. auto current_location = current_project_location();
  1257. if (m_locations_history_end_index != 0) {
  1258. auto last = m_locations_history[m_locations_history_end_index - 1];
  1259. if (current_location.filename == last.filename && current_location.line == last.line)
  1260. return;
  1261. }
  1262. // Clear "Go Forward" locations
  1263. VERIFY(m_locations_history_end_index <= m_locations_history.size());
  1264. m_locations_history.remove(m_locations_history_end_index, m_locations_history.size() - m_locations_history_end_index);
  1265. m_locations_history.append(current_location);
  1266. constexpr size_t max_locations = 30;
  1267. if (m_locations_history.size() > max_locations)
  1268. m_locations_history.take_first();
  1269. m_locations_history_end_index = m_locations_history.size();
  1270. update_history_actions();
  1271. }
  1272. void HackStudioWidget::create_location_history_actions()
  1273. {
  1274. 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&) {
  1275. if (m_locations_history_end_index <= 1)
  1276. return;
  1277. auto location = m_locations_history[m_locations_history_end_index - 2];
  1278. --m_locations_history_end_index;
  1279. m_locations_history_disabled = true;
  1280. open_file(location.filename, location.line, location.column);
  1281. m_locations_history_disabled = false;
  1282. update_history_actions();
  1283. });
  1284. 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&) {
  1285. if (m_locations_history_end_index == m_locations_history.size())
  1286. return;
  1287. auto location = m_locations_history[m_locations_history_end_index];
  1288. ++m_locations_history_end_index;
  1289. m_locations_history_disabled = true;
  1290. open_file(location.filename, location.line, location.column);
  1291. m_locations_history_disabled = false;
  1292. update_history_actions();
  1293. });
  1294. m_locations_history_forward_action->set_enabled(false);
  1295. }
  1296. HackStudioWidget::ProjectLocation HackStudioWidget::current_project_location() const
  1297. {
  1298. return ProjectLocation { current_editor_wrapper().filename(), current_editor().cursor().line(), current_editor().cursor().column() };
  1299. }
  1300. void HackStudioWidget::update_history_actions()
  1301. {
  1302. if (m_locations_history_end_index <= 1)
  1303. m_locations_history_back_action->set_enabled(false);
  1304. else
  1305. m_locations_history_back_action->set_enabled(true);
  1306. if (m_locations_history_end_index == m_locations_history.size())
  1307. m_locations_history_forward_action->set_enabled(false);
  1308. else
  1309. m_locations_history_forward_action->set_enabled(true);
  1310. }
  1311. RefPtr<Gfx::Font> HackStudioWidget::read_editor_font_from_config()
  1312. {
  1313. auto font_family = Config::read_string("HackStudio", "EditorFont", "Family", "Csilla");
  1314. auto font_variant = Config::read_string("HackStudio", "EditorFont", "Variant", "Regular");
  1315. auto font_size = Config::read_i32("HackStudio", "EditorFont", "Size", 10);
  1316. auto font = Gfx::FontDatabase::the().get(font_family, font_variant, font_size);
  1317. if (font.is_null())
  1318. return Gfx::FontDatabase::the().default_fixed_width_font();
  1319. return font;
  1320. }
  1321. void HackStudioWidget::change_editor_font(RefPtr<Gfx::Font> font)
  1322. {
  1323. m_editor_font = move(font);
  1324. for (auto& editor_wrapper : m_all_editor_wrappers) {
  1325. editor_wrapper.editor().set_font(*m_editor_font);
  1326. }
  1327. Config::write_string("HackStudio", "EditorFont", "Family", m_editor_font->family());
  1328. Config::write_string("HackStudio", "EditorFont", "Variant", m_editor_font->variant());
  1329. Config::write_i32("HackStudio", "EditorFont", "Size", m_editor_font->presentation_size());
  1330. }
  1331. void HackStudioWidget::open_coredump(String const& coredump_path)
  1332. {
  1333. open_project("/usr/src/serenity");
  1334. m_mode = Mode::Coredump;
  1335. m_coredump_inspector = Coredump::Inspector::create(coredump_path, [this](float progress) {
  1336. window()->set_progress(progress * 100);
  1337. });
  1338. window()->set_progress(0);
  1339. if (m_coredump_inspector) {
  1340. m_debug_info_widget->update_state(*m_coredump_inspector, m_coredump_inspector->get_registers());
  1341. reveal_action_tab(*m_debug_info_widget);
  1342. }
  1343. }
  1344. void HackStudioWidget::for_each_open_file(Function<void(ProjectFile const&)> func)
  1345. {
  1346. for (auto& open_file : m_open_files) {
  1347. func(*open_file.value);
  1348. }
  1349. }
  1350. NonnullRefPtr<GUI::Action> HackStudioWidget::create_toggle_syntax_highlighting_mode_action()
  1351. {
  1352. 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) {
  1353. for (auto& editor_wrapper : m_all_editor_wrappers)
  1354. editor_wrapper.editor().set_semantic_syntax_highlighting(action.is_checked());
  1355. });
  1356. return action;
  1357. }
  1358. bool HackStudioWidget::semantic_syntax_highlighting_is_enabled() const
  1359. {
  1360. return m_toggle_semantic_highlighting_action->is_checked();
  1361. }
  1362. }