HackStudioWidget.cpp 41 KB

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