main.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. #include "CppLexer.h"
  2. #include "CursorTool.h"
  3. #include "Editor.h"
  4. #include "EditorWrapper.h"
  5. #include "FindInFilesWidget.h"
  6. #include "FormEditorWidget.h"
  7. #include "FormWidget.h"
  8. #include "Locator.h"
  9. #include "Project.h"
  10. #include "TerminalWrapper.h"
  11. #include "WidgetTool.h"
  12. #include "WidgetTreeModel.h"
  13. #include <LibCore/CFile.h>
  14. #include <LibGUI/GAboutDialog.h>
  15. #include <LibGUI/GAction.h>
  16. #include <LibGUI/GActionGroup.h>
  17. #include <LibGUI/GApplication.h>
  18. #include <LibGUI/GBoxLayout.h>
  19. #include <LibGUI/GButton.h>
  20. #include <LibGUI/GFilePicker.h>
  21. #include <LibGUI/GInputBox.h>
  22. #include <LibGUI/GLabel.h>
  23. #include <LibGUI/GMenu.h>
  24. #include <LibGUI/GMenuBar.h>
  25. #include <LibGUI/GMessageBox.h>
  26. #include <LibGUI/GSplitter.h>
  27. #include <LibGUI/GStackWidget.h>
  28. #include <LibGUI/GTabWidget.h>
  29. #include <LibGUI/GTableView.h>
  30. #include <LibGUI/GTextBox.h>
  31. #include <LibGUI/GTextEditor.h>
  32. #include <LibGUI/GToolBar.h>
  33. #include <LibGUI/GTreeView.h>
  34. #include <LibGUI/GWidget.h>
  35. #include <LibGUI/GWindow.h>
  36. #include <stdio.h>
  37. #include <sys/wait.h>
  38. #include <unistd.h>
  39. NonnullRefPtrVector<EditorWrapper> g_all_editor_wrappers;
  40. RefPtr<EditorWrapper> g_current_editor_wrapper;
  41. String g_currently_open_file;
  42. OwnPtr<Project> g_project;
  43. RefPtr<GWindow> g_window;
  44. RefPtr<GTreeView> g_project_tree_view;
  45. RefPtr<GStackWidget> g_right_hand_stack;
  46. RefPtr<GSplitter> g_text_inner_splitter;
  47. RefPtr<GWidget> g_form_inner_container;
  48. RefPtr<FormEditorWidget> g_form_editor_widget;
  49. static RefPtr<GTabWidget> s_action_tab_widget;
  50. void add_new_editor(GWidget& parent)
  51. {
  52. auto wrapper = EditorWrapper::construct(nullptr);
  53. if (s_action_tab_widget) {
  54. parent.insert_child_before(wrapper, *s_action_tab_widget);
  55. } else {
  56. parent.add_child(wrapper);
  57. }
  58. g_current_editor_wrapper = wrapper;
  59. g_all_editor_wrappers.append(wrapper);
  60. wrapper->editor().set_focus(true);
  61. }
  62. enum class EditMode {
  63. Text,
  64. Form,
  65. };
  66. void set_edit_mode(EditMode mode)
  67. {
  68. if (mode == EditMode::Text) {
  69. g_right_hand_stack->set_active_widget(g_text_inner_splitter);
  70. } else if (mode == EditMode::Form) {
  71. g_right_hand_stack->set_active_widget(g_form_inner_container);
  72. }
  73. }
  74. EditorWrapper& current_editor_wrapper()
  75. {
  76. ASSERT(g_current_editor_wrapper);
  77. return *g_current_editor_wrapper;
  78. }
  79. Editor& current_editor()
  80. {
  81. return current_editor_wrapper().editor();
  82. }
  83. static void build(TerminalWrapper&);
  84. static void run(TerminalWrapper&);
  85. void open_file(const String&);
  86. bool make_is_available();
  87. int main(int argc, char** argv)
  88. {
  89. if (pledge("stdio tty rpath cpath wpath shared_buffer proc unix fattr", nullptr) < 0) {
  90. perror("pledge");
  91. return 1;
  92. }
  93. GApplication app(argc, argv);
  94. Function<void()> update_actions;
  95. g_window = GWindow::construct();
  96. g_window->set_rect(90, 90, 840, 600);
  97. g_window->set_title("HackStudio");
  98. auto widget = GWidget::construct();
  99. g_window->set_main_widget(widget);
  100. widget->set_fill_with_background_color(true);
  101. widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
  102. widget->layout()->set_spacing(0);
  103. if (!make_is_available())
  104. GMessageBox::show("The 'make' command is not available. You probably want to install the binutils, gcc, and make ports from the root of the Serenity repository.", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, g_window);
  105. if (chdir("/home/anon/little") < 0) {
  106. perror("chdir");
  107. return 1;
  108. }
  109. g_project = Project::load_from_file("little.files");
  110. ASSERT(g_project);
  111. auto toolbar = GToolBar::construct(widget);
  112. auto selected_file_names = [&] {
  113. Vector<String> files;
  114. g_project_tree_view->selection().for_each_index([&](const GModelIndex& index) {
  115. files.append(g_project->model().data(index).as_string());
  116. });
  117. return files;
  118. };
  119. auto new_action = GAction::create("Add new file to project...", { Mod_Ctrl, Key_N }, GraphicsBitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GAction&) {
  120. auto input_box = GInputBox::construct("Enter name of new file:", "Add new file to project", g_window);
  121. if (input_box->exec() == GInputBox::ExecCancel)
  122. return;
  123. auto filename = input_box->text_value();
  124. auto file = CFile::construct(filename);
  125. if (!file->open((CIODevice::OpenMode)(CIODevice::WriteOnly | CIODevice::MustBeNew))) {
  126. GMessageBox::show(String::format("Failed to create '%s'", filename.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, g_window);
  127. return;
  128. }
  129. if (!g_project->add_file(filename)) {
  130. GMessageBox::show(String::format("Failed to add '%s' to project", filename.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, g_window);
  131. // FIXME: Should we unlink the file here maybe?
  132. return;
  133. }
  134. open_file(filename);
  135. });
  136. auto add_existing_file_action = GAction::create("Add existing file to project...", GraphicsBitmap::load_from_file("/res/icons/16x16/open.png"), [&](auto&) {
  137. auto result = GFilePicker::get_open_filepath("Add existing file to project");
  138. if (!result.has_value())
  139. return;
  140. auto& filename = result.value();
  141. if (!g_project->add_file(filename)) {
  142. GMessageBox::show(String::format("Failed to add '%s' to project", filename.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, g_window);
  143. return;
  144. }
  145. open_file(filename);
  146. });
  147. auto delete_action = GCommonActions::make_delete_action([&](const GAction& action) {
  148. (void)action;
  149. auto files = selected_file_names();
  150. if (files.is_empty())
  151. return;
  152. String message;
  153. if (files.size() == 1) {
  154. message = String::format("Really remove %s from the project?", FileSystemPath(files[0]).basename().characters());
  155. } else {
  156. message = String::format("Really remove %d files from the project?", files.size());
  157. }
  158. auto result = GMessageBox::show(
  159. message,
  160. "Confirm deletion",
  161. GMessageBox::Type::Warning,
  162. GMessageBox::InputType::OKCancel,
  163. g_window);
  164. if (result == GMessageBox::ExecCancel)
  165. return;
  166. for (auto& file : files) {
  167. if (!g_project->remove_file(file)) {
  168. GMessageBox::show(
  169. String::format("Removing file %s from the project failed.", file.characters()),
  170. "Removal failed",
  171. GMessageBox::Type::Error,
  172. GMessageBox::InputType::OK,
  173. g_window);
  174. break;
  175. }
  176. }
  177. });
  178. delete_action->set_enabled(false);
  179. auto project_tree_view_context_menu = GMenu::construct("Project Files");
  180. project_tree_view_context_menu->add_action(new_action);
  181. project_tree_view_context_menu->add_action(add_existing_file_action);
  182. project_tree_view_context_menu->add_action(delete_action);
  183. auto outer_splitter = GSplitter::construct(Orientation::Horizontal, widget);
  184. g_project_tree_view = GTreeView::construct(outer_splitter);
  185. g_project_tree_view->set_model(g_project->model());
  186. g_project_tree_view->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  187. g_project_tree_view->set_preferred_size(140, 0);
  188. g_project_tree_view->on_context_menu_request = [&](const GModelIndex& index, const GContextMenuEvent& event) {
  189. if (index.is_valid()) {
  190. project_tree_view_context_menu->popup(event.screen_position());
  191. }
  192. };
  193. g_project_tree_view->on_selection_change = [&] {
  194. delete_action->set_enabled(!g_project_tree_view->selection().is_empty());
  195. };
  196. g_right_hand_stack = GStackWidget::construct(outer_splitter);
  197. g_form_inner_container = GWidget::construct(g_right_hand_stack);
  198. g_form_inner_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  199. auto form_widgets_toolbar = GToolBar::construct(Orientation::Vertical, 26, g_form_inner_container);
  200. form_widgets_toolbar->set_preferred_size(38, 0);
  201. GActionGroup tool_actions;
  202. tool_actions.set_exclusive(true);
  203. auto cursor_tool_action = GAction::create("Cursor", GraphicsBitmap::load_from_file("/res/icons/widgets/Cursor.png"), [&](auto&) {
  204. g_form_editor_widget->set_tool(make<CursorTool>(*g_form_editor_widget));
  205. });
  206. cursor_tool_action->set_checkable(true);
  207. cursor_tool_action->set_checked(true);
  208. tool_actions.add_action(cursor_tool_action);
  209. form_widgets_toolbar->add_action(cursor_tool_action);
  210. GWidgetClassRegistration::for_each([&](const GWidgetClassRegistration& reg) {
  211. auto icon_path = String::format("/res/icons/widgets/%s.png", reg.class_name().characters());
  212. auto action = GAction::create(reg.class_name(), GraphicsBitmap::load_from_file(icon_path), [&reg](auto&) {
  213. g_form_editor_widget->set_tool(make<WidgetTool>(*g_form_editor_widget, reg));
  214. auto widget = reg.construct(&g_form_editor_widget->form_widget());
  215. widget->set_relative_rect(30, 30, 30, 30);
  216. g_form_editor_widget->model().update();
  217. });
  218. action->set_checkable(true);
  219. action->set_checked(false);
  220. tool_actions.add_action(action);
  221. form_widgets_toolbar->add_action(move(action));
  222. });
  223. auto form_editor_inner_splitter = GSplitter::construct(Orientation::Horizontal, g_form_inner_container);
  224. g_form_editor_widget = FormEditorWidget::construct(form_editor_inner_splitter);
  225. auto form_editing_pane_container = GSplitter::construct(Orientation::Vertical, form_editor_inner_splitter);
  226. form_editing_pane_container->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  227. form_editing_pane_container->set_preferred_size(190, 0);
  228. form_editing_pane_container->set_layout(make<GBoxLayout>(Orientation::Vertical));
  229. auto add_properties_pane = [&](auto& text, auto pane_widget) {
  230. auto wrapper = GWidget::construct(form_editing_pane_container.ptr());
  231. wrapper->set_layout(make<GBoxLayout>(Orientation::Vertical));
  232. auto label = GLabel::construct(text, wrapper);
  233. label->set_fill_with_background_color(true);
  234. label->set_text_alignment(TextAlignment::CenterLeft);
  235. label->set_font(Font::default_bold_font());
  236. label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  237. label->set_preferred_size(0, 16);
  238. wrapper->add_child(pane_widget);
  239. };
  240. auto form_widget_tree_view = GTreeView::construct(nullptr);
  241. form_widget_tree_view->set_model(g_form_editor_widget->model());
  242. form_widget_tree_view->on_selection_change = [&] {
  243. g_form_editor_widget->selection().disable_hooks();
  244. g_form_editor_widget->selection().clear();
  245. form_widget_tree_view->selection().for_each_index([&](auto& index) {
  246. // NOTE: Make sure we don't add the FormWidget itself to the selection,
  247. // since that would allow you to drag-move the FormWidget.
  248. if (index.internal_data() != &g_form_editor_widget->form_widget())
  249. g_form_editor_widget->selection().add(*(GWidget*)index.internal_data());
  250. });
  251. g_form_editor_widget->update();
  252. g_form_editor_widget->selection().enable_hooks();
  253. };
  254. g_form_editor_widget->selection().on_add = [&](auto& widget) {
  255. form_widget_tree_view->selection().add(g_form_editor_widget->model().index_for_widget(widget));
  256. };
  257. g_form_editor_widget->selection().on_remove = [&](auto& widget) {
  258. form_widget_tree_view->selection().remove(g_form_editor_widget->model().index_for_widget(widget));
  259. };
  260. g_form_editor_widget->selection().on_clear = [&] {
  261. form_widget_tree_view->selection().clear();
  262. };
  263. add_properties_pane("Form widget tree:", form_widget_tree_view);
  264. add_properties_pane("Widget properties:", GTableView::construct(nullptr));
  265. g_text_inner_splitter = GSplitter::construct(Orientation::Vertical, g_right_hand_stack);
  266. g_text_inner_splitter->layout()->set_margins({ 0, 3, 0, 0 });
  267. add_new_editor(*g_text_inner_splitter);
  268. auto switch_to_next_editor = GAction::create("Switch to next editor", { Mod_Ctrl, Key_E }, [&](auto&) {
  269. if (g_all_editor_wrappers.size() <= 1)
  270. return;
  271. Vector<EditorWrapper*> wrappers;
  272. g_text_inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
  273. wrappers.append(&child);
  274. return IterationDecision::Continue;
  275. });
  276. for (int i = 0; i < wrappers.size(); ++i) {
  277. if (g_current_editor_wrapper.ptr() == wrappers[i]) {
  278. if (i == wrappers.size() - 1)
  279. wrappers[0]->editor().set_focus(true);
  280. else
  281. wrappers[i + 1]->editor().set_focus(true);
  282. }
  283. }
  284. });
  285. auto switch_to_previous_editor = GAction::create("Switch to previous editor", { Mod_Ctrl | Mod_Shift, Key_E }, [&](auto&) {
  286. if (g_all_editor_wrappers.size() <= 1)
  287. return;
  288. Vector<EditorWrapper*> wrappers;
  289. g_text_inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
  290. wrappers.append(&child);
  291. return IterationDecision::Continue;
  292. });
  293. for (int i = wrappers.size() - 1; i >= 0; --i) {
  294. if (g_current_editor_wrapper.ptr() == wrappers[i]) {
  295. if (i == 0)
  296. wrappers.last()->editor().set_focus(true);
  297. else
  298. wrappers[i - 1]->editor().set_focus(true);
  299. }
  300. }
  301. });
  302. auto remove_current_editor_action = GAction::create("Remove current editor", { Mod_Alt | Mod_Shift, Key_E }, [&](auto&) {
  303. if (g_all_editor_wrappers.size() <= 1)
  304. return;
  305. auto wrapper = g_current_editor_wrapper;
  306. switch_to_next_editor->activate();
  307. g_text_inner_splitter->remove_child(*wrapper);
  308. g_all_editor_wrappers.remove_first_matching([&](auto& entry) { return entry == wrapper.ptr(); });
  309. update_actions();
  310. });
  311. auto save_action = GAction::create("Save", { Mod_Ctrl, Key_S }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [&](auto&) {
  312. if (g_currently_open_file.is_empty())
  313. return;
  314. current_editor().write_to_file(g_currently_open_file);
  315. });
  316. toolbar->add_action(new_action);
  317. toolbar->add_action(add_existing_file_action);
  318. toolbar->add_action(save_action);
  319. toolbar->add_action(delete_action);
  320. toolbar->add_separator();
  321. toolbar->add_action(GCommonActions::make_cut_action([&](auto&) { current_editor().cut_action().activate(); }));
  322. toolbar->add_action(GCommonActions::make_copy_action([&](auto&) { current_editor().copy_action().activate(); }));
  323. toolbar->add_action(GCommonActions::make_paste_action([&](auto&) { current_editor().paste_action().activate(); }));
  324. toolbar->add_separator();
  325. toolbar->add_action(GCommonActions::make_undo_action([&](auto&) { current_editor().undo_action().activate(); }));
  326. toolbar->add_action(GCommonActions::make_redo_action([&](auto&) { current_editor().redo_action().activate(); }));
  327. toolbar->add_separator();
  328. g_project_tree_view->on_activation = [&](auto& index) {
  329. auto filename = g_project_tree_view->model()->data(index, GModel::Role::Custom).to_string();
  330. open_file(filename);
  331. };
  332. s_action_tab_widget = GTabWidget::construct(g_text_inner_splitter);
  333. s_action_tab_widget->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  334. s_action_tab_widget->set_preferred_size(0, 24);
  335. auto reveal_action_tab = [&](auto& widget) {
  336. if (s_action_tab_widget->preferred_size().height() < 200)
  337. s_action_tab_widget->set_preferred_size(0, 200);
  338. s_action_tab_widget->set_active_widget(widget);
  339. };
  340. auto hide_action_tabs = [&] {
  341. s_action_tab_widget->set_preferred_size(0, 24);
  342. };
  343. auto hide_action_tabs_action = GAction::create("Hide action tabs", { Mod_Ctrl | Mod_Shift, Key_X }, [&](auto&) {
  344. hide_action_tabs();
  345. });
  346. auto add_editor_action = GAction::create("Add new editor", { Mod_Ctrl | Mod_Alt, Key_E }, [&](auto&) {
  347. add_new_editor(*g_text_inner_splitter);
  348. update_actions();
  349. });
  350. auto find_in_files_widget = FindInFilesWidget::construct(nullptr);
  351. s_action_tab_widget->add_widget("Find in files", find_in_files_widget);
  352. auto terminal_wrapper = TerminalWrapper::construct(nullptr);
  353. s_action_tab_widget->add_widget("Console", terminal_wrapper);
  354. auto locator = Locator::construct(widget);
  355. auto open_locator_action = GAction::create("Open Locator...", { Mod_Ctrl, Key_K }, [&](auto&) {
  356. locator->open();
  357. });
  358. auto menubar = make<GMenuBar>();
  359. auto app_menu = GMenu::construct("HackStudio");
  360. app_menu->add_action(save_action);
  361. app_menu->add_action(GCommonActions::make_quit_action([&](auto&) {
  362. app.quit();
  363. }));
  364. menubar->add_menu(move(app_menu));
  365. auto project_menu = GMenu::construct("Project");
  366. project_menu->add_action(new_action);
  367. project_menu->add_action(add_existing_file_action);
  368. menubar->add_menu(move(project_menu));
  369. auto edit_menu = GMenu::construct("Edit");
  370. edit_menu->add_action(GAction::create("Find in files...", { Mod_Ctrl | Mod_Shift, Key_F }, [&](auto&) {
  371. reveal_action_tab(find_in_files_widget);
  372. find_in_files_widget->focus_textbox_and_select_all();
  373. }));
  374. menubar->add_menu(move(edit_menu));
  375. auto stop_action = GAction::create("Stop", GraphicsBitmap::load_from_file("/res/icons/16x16/stop.png"), [&](auto&) {
  376. terminal_wrapper->kill_running_command();
  377. });
  378. stop_action->set_enabled(false);
  379. terminal_wrapper->on_command_exit = [&] {
  380. stop_action->set_enabled(false);
  381. };
  382. auto build_action = GAction::create("Build", { Mod_Ctrl, Key_B }, GraphicsBitmap::load_from_file("/res/icons/16x16/build.png"), [&](auto&) {
  383. reveal_action_tab(terminal_wrapper);
  384. build(terminal_wrapper);
  385. stop_action->set_enabled(true);
  386. });
  387. toolbar->add_action(build_action);
  388. auto run_action = GAction::create("Run", { Mod_Ctrl, Key_R }, GraphicsBitmap::load_from_file("/res/icons/16x16/play.png"), [&](auto&) {
  389. reveal_action_tab(terminal_wrapper);
  390. run(terminal_wrapper);
  391. stop_action->set_enabled(true);
  392. });
  393. toolbar->add_action(run_action);
  394. toolbar->add_action(stop_action);
  395. auto build_menu = GMenu::construct("Build");
  396. build_menu->add_action(build_action);
  397. build_menu->add_action(run_action);
  398. build_menu->add_action(stop_action);
  399. menubar->add_menu(move(build_menu));
  400. auto view_menu = GMenu::construct("View");
  401. view_menu->add_action(hide_action_tabs_action);
  402. view_menu->add_action(open_locator_action);
  403. view_menu->add_separator();
  404. view_menu->add_action(add_editor_action);
  405. view_menu->add_action(remove_current_editor_action);
  406. menubar->add_menu(move(view_menu));
  407. auto small_icon = GraphicsBitmap::load_from_file("/res/icons/16x16/app-hack-studio.png");
  408. auto help_menu = GMenu::construct("Help");
  409. help_menu->add_action(GAction::create("About", [&](auto&) {
  410. GAboutDialog::show("HackStudio", small_icon, g_window);
  411. }));
  412. menubar->add_menu(move(help_menu));
  413. app.set_menubar(move(menubar));
  414. g_window->set_icon(small_icon);
  415. g_window->show();
  416. update_actions = [&]() {
  417. remove_current_editor_action->set_enabled(g_all_editor_wrappers.size() > 1);
  418. };
  419. open_file("main.cpp");
  420. update_actions();
  421. return app.exec();
  422. }
  423. void build(TerminalWrapper& wrapper)
  424. {
  425. wrapper.run_command("make");
  426. }
  427. void run(TerminalWrapper& wrapper)
  428. {
  429. wrapper.run_command("make run");
  430. }
  431. struct TextStyle {
  432. Color color;
  433. const Font* font { nullptr };
  434. };
  435. static TextStyle style_for_token_type(CppToken::Type type)
  436. {
  437. switch (type) {
  438. case CppToken::Type::Keyword:
  439. return { Color::Black, &Font::default_bold_fixed_width_font() };
  440. case CppToken::Type::KnownType:
  441. return { Color::from_rgb(0x929200), &Font::default_bold_fixed_width_font() };
  442. case CppToken::Type::Identifier:
  443. return { Color::from_rgb(0x000092) };
  444. case CppToken::Type::DoubleQuotedString:
  445. case CppToken::Type::SingleQuotedString:
  446. case CppToken::Type::Number:
  447. return { Color::from_rgb(0x920000) };
  448. case CppToken::Type::PreprocessorStatement:
  449. return { Color::from_rgb(0x009292) };
  450. case CppToken::Type::Comment:
  451. return { Color::from_rgb(0x009200) };
  452. default:
  453. return { Color::Black };
  454. }
  455. }
  456. static void rehighlight()
  457. {
  458. auto text = current_editor().text();
  459. CppLexer lexer(text);
  460. auto tokens = lexer.lex();
  461. Vector<GTextDocumentSpan> spans;
  462. for (auto& token : tokens) {
  463. #ifdef DEBUG_SYNTAX_HIGHLIGHTING
  464. dbg() << token.to_string() << " @ " << token.m_start.line << ":" << token.m_start.column << " - " << token.m_end.line << ":" << token.m_end.column;
  465. #endif
  466. GTextDocumentSpan span;
  467. span.range.set_start({ token.m_start.line, token.m_start.column });
  468. span.range.set_end({ token.m_end.line, token.m_end.column });
  469. auto style = style_for_token_type(token.m_type);
  470. span.color = style.color;
  471. span.font = style.font;
  472. span.is_skippable = token.m_type == CppToken::Type::Whitespace;
  473. span.data = (void*)token.m_type;
  474. spans.append(span);
  475. }
  476. current_editor().document().set_spans(spans);
  477. static_cast<Editor&>(current_editor()).notify_did_rehighlight();
  478. current_editor().update();
  479. }
  480. void open_file(const String& filename)
  481. {
  482. auto file = g_project->get_file(filename);
  483. current_editor().set_document(const_cast<GTextDocument&>(file->document()));
  484. if (filename.ends_with(".cpp") || filename.ends_with(".h")) {
  485. current_editor().on_change = [] { rehighlight(); };
  486. rehighlight();
  487. } else {
  488. current_editor().on_change = nullptr;
  489. }
  490. if (filename.ends_with(".frm")) {
  491. set_edit_mode(EditMode::Form);
  492. } else {
  493. set_edit_mode(EditMode::Text);
  494. }
  495. g_currently_open_file = filename;
  496. g_window->set_title(String::format("%s - HackStudio", g_currently_open_file.characters()));
  497. g_project_tree_view->update();
  498. current_editor_wrapper().filename_label().set_text(filename);
  499. current_editor().set_focus(true);
  500. }
  501. bool make_is_available()
  502. {
  503. auto pid = fork();
  504. if (pid < 0)
  505. return false;
  506. if (!pid) {
  507. int rc = execlp("make", "make", "--version", nullptr);
  508. ASSERT(rc < 0);
  509. perror("execl");
  510. exit(127);
  511. }
  512. int wstatus;
  513. waitpid(pid, &wstatus, 0);
  514. return WEXITSTATUS(wstatus) == 0;
  515. }