main.cpp 23 KB

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