HackStudioWidget.cpp 67 KB

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