TextEditor.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/CharacterTypes.h>
  7. #include <AK/Debug.h>
  8. #include <AK/ScopeGuard.h>
  9. #include <AK/StringBuilder.h>
  10. #include <AK/TemporaryChange.h>
  11. #include <LibCore/Timer.h>
  12. #include <LibGUI/Action.h>
  13. #include <LibGUI/AutocompleteProvider.h>
  14. #include <LibGUI/Clipboard.h>
  15. #include <LibGUI/EditingEngine.h>
  16. #include <LibGUI/InputBox.h>
  17. #include <LibGUI/Menu.h>
  18. #include <LibGUI/Painter.h>
  19. #include <LibGUI/RegularEditingEngine.h>
  20. #include <LibGUI/Scrollbar.h>
  21. #include <LibGUI/TextEditor.h>
  22. #include <LibGUI/Window.h>
  23. #include <LibGfx/Bitmap.h>
  24. #include <LibGfx/Font.h>
  25. #include <LibGfx/FontDatabase.h>
  26. #include <LibGfx/Palette.h>
  27. #include <LibSyntax/Highlighter.h>
  28. #include <fcntl.h>
  29. #include <math.h>
  30. #include <stdio.h>
  31. #include <unistd.h>
  32. REGISTER_WIDGET(GUI, TextEditor)
  33. namespace GUI {
  34. TextEditor::TextEditor(Type type)
  35. : m_type(type)
  36. {
  37. REGISTER_STRING_PROPERTY("text", text, set_text);
  38. REGISTER_STRING_PROPERTY("placeholder", placeholder, set_placeholder);
  39. REGISTER_ENUM_PROPERTY("mode", mode, set_mode, Mode,
  40. { Editable, "Editable" },
  41. { ReadOnly, "ReadOnly" },
  42. { DisplayOnly, "DisplayOnly" });
  43. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  44. set_accepts_emoji_input(true);
  45. set_override_cursor(Gfx::StandardCursor::IBeam);
  46. set_background_role(ColorRole::Base);
  47. set_foreground_role(ColorRole::BaseText);
  48. set_document(TextDocument::create());
  49. if (is_single_line())
  50. set_visualize_trailing_whitespace(false);
  51. set_scrollbars_enabled(is_multi_line());
  52. if (is_multi_line())
  53. set_font(Gfx::FontDatabase::default_fixed_width_font());
  54. vertical_scrollbar().set_step(line_height());
  55. m_cursor = { 0, 0 };
  56. m_automatic_selection_scroll_timer = add<Core::Timer>(100, [this] {
  57. automatic_selection_scroll_timer_fired();
  58. });
  59. m_automatic_selection_scroll_timer->stop();
  60. create_actions();
  61. set_editing_engine(make<RegularEditingEngine>());
  62. }
  63. TextEditor::~TextEditor()
  64. {
  65. if (m_document)
  66. m_document->unregister_client(*this);
  67. }
  68. void TextEditor::create_actions()
  69. {
  70. m_undo_action = CommonActions::make_undo_action([&](auto&) { undo(); }, this);
  71. m_redo_action = CommonActions::make_redo_action([&](auto&) { redo(); }, this);
  72. m_undo_action->set_enabled(false);
  73. m_redo_action->set_enabled(false);
  74. m_cut_action = CommonActions::make_cut_action([&](auto&) { cut(); }, this);
  75. m_copy_action = CommonActions::make_copy_action([&](auto&) { copy(); }, this);
  76. m_paste_action = CommonActions::make_paste_action([&](auto&) { paste(); }, this);
  77. m_delete_action = CommonActions::make_delete_action([&](auto&) { do_delete(); }, this);
  78. if (is_multi_line()) {
  79. m_go_to_line_action = Action::create(
  80. "Go to line...", { Mod_Ctrl, Key_L }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), [this](auto&) {
  81. String value;
  82. if (InputBox::show(window(), value, "Line:", "Go to line") == InputBox::ExecOK) {
  83. auto line_target = value.to_uint();
  84. if (line_target.has_value()) {
  85. set_cursor_and_focus_line(line_target.value() - 1, 0);
  86. }
  87. }
  88. },
  89. this);
  90. }
  91. m_select_all_action = CommonActions::make_select_all_action([this](auto&) { select_all(); }, this);
  92. }
  93. void TextEditor::set_text(const StringView& text)
  94. {
  95. m_selection.clear();
  96. document().set_text(text);
  97. update_content_size();
  98. recompute_all_visual_lines();
  99. if (is_single_line())
  100. set_cursor(0, line(0).length());
  101. else
  102. set_cursor(0, 0);
  103. did_update_selection();
  104. update();
  105. }
  106. void TextEditor::update_content_size()
  107. {
  108. int content_width = 0;
  109. int content_height = 0;
  110. for (auto& line : m_line_visual_data) {
  111. content_width = max(line.visual_rect.width(), content_width);
  112. content_height += line.visual_rect.height();
  113. }
  114. content_width += m_horizontal_content_padding * 2;
  115. if (is_right_text_alignment(m_text_alignment))
  116. content_width = max(frame_inner_rect().width(), content_width);
  117. set_content_size({ content_width, content_height });
  118. set_size_occupied_by_fixed_elements({ ruler_width() + gutter_width(), 0 });
  119. }
  120. TextPosition TextEditor::text_position_at_content_position(const Gfx::IntPoint& content_position) const
  121. {
  122. auto position = content_position;
  123. if (is_single_line() && icon())
  124. position.translate_by(-(icon_size() + icon_padding()), 0);
  125. size_t line_index = 0;
  126. if (position.y() >= 0) {
  127. if (is_wrapping_enabled()) {
  128. for (size_t i = 0; i < line_count(); ++i) {
  129. auto& rect = m_line_visual_data[i].visual_rect;
  130. if (position.y() >= rect.top() && position.y() <= rect.bottom()) {
  131. line_index = i;
  132. break;
  133. }
  134. if (position.y() > rect.bottom())
  135. line_index = line_count() - 1;
  136. }
  137. } else {
  138. line_index = (size_t)(position.y() / line_height());
  139. }
  140. line_index = max((size_t)0, min(line_index, line_count() - 1));
  141. }
  142. size_t column_index = 0;
  143. switch (m_text_alignment) {
  144. case Gfx::TextAlignment::CenterLeft:
  145. for_each_visual_line(line_index, [&](const Gfx::IntRect& rect, auto& view, size_t start_of_line, [[maybe_unused]] bool is_last_visual_line) {
  146. if (is_multi_line() && !rect.contains_vertically(position.y()) && !is_last_visual_line)
  147. return IterationDecision::Continue;
  148. column_index = start_of_line;
  149. if (position.x() <= 0) {
  150. // We're outside the text on the left side, put cursor at column 0 on this visual line.
  151. } else {
  152. int glyph_x = 0;
  153. size_t i = 0;
  154. for (; i < view.length(); ++i) {
  155. int advance = font().glyph_width(view.code_points()[i]) + font().glyph_spacing();
  156. if ((glyph_x + (advance / 2)) >= position.x())
  157. break;
  158. glyph_x += advance;
  159. }
  160. column_index += i;
  161. }
  162. return IterationDecision::Break;
  163. });
  164. break;
  165. case Gfx::TextAlignment::CenterRight:
  166. // FIXME: Support right-aligned line wrapping, I guess.
  167. VERIFY(!is_wrapping_enabled());
  168. column_index = (position.x() - content_x_for_position({ line_index, 0 }) + fixed_glyph_width() / 2) / fixed_glyph_width();
  169. break;
  170. default:
  171. VERIFY_NOT_REACHED();
  172. }
  173. column_index = max((size_t)0, min(column_index, line(line_index).length()));
  174. return { line_index, column_index };
  175. }
  176. TextPosition TextEditor::text_position_at(const Gfx::IntPoint& widget_position) const
  177. {
  178. auto content_position = widget_position;
  179. content_position.translate_by(horizontal_scrollbar().value(), vertical_scrollbar().value());
  180. content_position.translate_by(-(m_horizontal_content_padding + ruler_width() + gutter_width()), 0);
  181. content_position.translate_by(-frame_thickness(), -frame_thickness());
  182. return text_position_at_content_position(content_position);
  183. }
  184. void TextEditor::doubleclick_event(MouseEvent& event)
  185. {
  186. if (event.button() != MouseButton::Left)
  187. return;
  188. if (is_displayonly())
  189. return;
  190. if (!current_line().can_select())
  191. return;
  192. rehighlight_if_needed();
  193. m_triple_click_timer.start();
  194. m_in_drag_select = false;
  195. auto position = text_position_at(event.position());
  196. if (document().has_spans()) {
  197. for (auto& span : document().spans()) {
  198. if (span.range.contains(position)) {
  199. m_selection = span.range;
  200. break;
  201. }
  202. }
  203. } else {
  204. m_selection.set_start(document().first_word_break_before(position, false));
  205. m_selection.set_end(document().first_word_break_after(position));
  206. }
  207. set_cursor(m_selection.end());
  208. update();
  209. did_update_selection();
  210. }
  211. void TextEditor::mousedown_event(MouseEvent& event)
  212. {
  213. if (event.button() != MouseButton::Left) {
  214. return;
  215. }
  216. if (on_mousedown)
  217. on_mousedown();
  218. if (is_displayonly())
  219. return;
  220. if (m_triple_click_timer.is_valid() && m_triple_click_timer.elapsed() < 250) {
  221. m_triple_click_timer = Core::ElapsedTimer();
  222. m_selection = document().range_for_entire_line(m_cursor.line());
  223. set_cursor(m_selection.end());
  224. update();
  225. did_update_selection();
  226. return;
  227. }
  228. if (event.modifiers() & Mod_Shift) {
  229. if (!has_selection())
  230. m_selection.set(m_cursor, {});
  231. } else {
  232. m_selection.clear();
  233. }
  234. m_in_drag_select = true;
  235. m_automatic_selection_scroll_timer->start();
  236. set_cursor(text_position_at(event.position()));
  237. if (!(event.modifiers() & Mod_Shift)) {
  238. if (!has_selection())
  239. m_selection.set(m_cursor, {});
  240. }
  241. if (m_selection.start().is_valid() && m_selection.start() != m_cursor)
  242. m_selection.set_end(m_cursor);
  243. // FIXME: Only update the relevant rects.
  244. update();
  245. did_update_selection();
  246. }
  247. void TextEditor::mouseup_event(MouseEvent& event)
  248. {
  249. if (event.button() == MouseButton::Left) {
  250. if (m_in_drag_select) {
  251. m_in_drag_select = false;
  252. }
  253. return;
  254. }
  255. }
  256. void TextEditor::mousemove_event(MouseEvent& event)
  257. {
  258. m_last_mousemove_position = event.position();
  259. if (m_in_drag_select && (rect().contains(event.position()) || !m_automatic_selection_scroll_timer->is_active())) {
  260. set_cursor(text_position_at(event.position()));
  261. m_selection.set_end(m_cursor);
  262. did_update_selection();
  263. update();
  264. return;
  265. }
  266. }
  267. void TextEditor::automatic_selection_scroll_timer_fired()
  268. {
  269. if (!m_in_drag_select) {
  270. m_automatic_selection_scroll_timer->stop();
  271. return;
  272. }
  273. set_cursor(text_position_at(m_last_mousemove_position));
  274. m_selection.set_end(m_cursor);
  275. did_update_selection();
  276. update();
  277. }
  278. int TextEditor::ruler_width() const
  279. {
  280. if (!m_ruler_visible)
  281. return 0;
  282. int line_count_digits = static_cast<int>(log10(line_count())) + 1;
  283. constexpr size_t padding = 5;
  284. return line_count() < 10 ? (line_count_digits + 1) * font().glyph_width('x') + padding : line_count_digits * font().glyph_width('x') + padding;
  285. }
  286. int TextEditor::gutter_width() const
  287. {
  288. if (!m_gutter_visible)
  289. return 0;
  290. return line_height(); // square gutter
  291. }
  292. Gfx::IntRect TextEditor::ruler_content_rect(size_t line_index) const
  293. {
  294. if (!m_ruler_visible)
  295. return {};
  296. return {
  297. 0 - ruler_width() + horizontal_scrollbar().value(),
  298. line_content_rect(line_index).y(),
  299. ruler_width(),
  300. line_content_rect(line_index).height()
  301. };
  302. }
  303. Gfx::IntRect TextEditor::gutter_content_rect(size_t line_index) const
  304. {
  305. if (!m_gutter_visible)
  306. return {};
  307. return {
  308. 0 - ruler_width() - gutter_width() + horizontal_scrollbar().value(),
  309. line_content_rect(line_index).y(),
  310. gutter_width(),
  311. line_content_rect(line_index).height()
  312. };
  313. }
  314. Gfx::IntRect TextEditor::ruler_rect_in_inner_coordinates() const
  315. {
  316. return { gutter_width(), 0, ruler_width(), height() - height_occupied_by_horizontal_scrollbar() };
  317. }
  318. Gfx::IntRect TextEditor::gutter_rect_in_inner_coordinates() const
  319. {
  320. return { 0, 0, gutter_width(), height() - height_occupied_by_horizontal_scrollbar() };
  321. }
  322. Gfx::IntRect TextEditor::visible_text_rect_in_inner_coordinates() const
  323. {
  324. return {
  325. m_horizontal_content_padding + (m_ruler_visible ? (ruler_rect_in_inner_coordinates().right() + 1) : 0),
  326. 0,
  327. frame_inner_rect().width() - (m_horizontal_content_padding * 2) - width_occupied_by_vertical_scrollbar() - ruler_width(),
  328. frame_inner_rect().height() - height_occupied_by_horizontal_scrollbar()
  329. };
  330. }
  331. void TextEditor::paint_event(PaintEvent& event)
  332. {
  333. Color widget_background_color = palette().color(is_enabled() ? background_role() : Gfx::ColorRole::Window);
  334. rehighlight_if_needed();
  335. Frame::paint_event(event);
  336. Painter painter(*this);
  337. painter.add_clip_rect(widget_inner_rect());
  338. painter.add_clip_rect(event.rect());
  339. painter.fill_rect(event.rect(), widget_background_color);
  340. if (is_displayonly() && is_focused()) {
  341. widget_background_color = palette().selection();
  342. Gfx::IntRect display_rect {
  343. widget_inner_rect().x() + 1,
  344. widget_inner_rect().y() + 1,
  345. widget_inner_rect().width() - 2,
  346. widget_inner_rect().height() - 2
  347. };
  348. painter.add_clip_rect(display_rect);
  349. painter.add_clip_rect(event.rect());
  350. painter.fill_rect(event.rect(), widget_background_color);
  351. }
  352. painter.translate(frame_thickness(), frame_thickness());
  353. if (m_gutter_visible) {
  354. auto gutter_rect = gutter_rect_in_inner_coordinates();
  355. painter.fill_rect(gutter_rect, palette().gutter());
  356. if (!m_ruler_visible)
  357. painter.draw_line(gutter_rect.top_right(), gutter_rect.bottom_right(), palette().gutter_border());
  358. }
  359. if (m_ruler_visible) {
  360. auto ruler_rect = ruler_rect_in_inner_coordinates();
  361. painter.fill_rect(ruler_rect, palette().ruler());
  362. painter.draw_line(ruler_rect.top_right(), ruler_rect.bottom_right(), palette().ruler_border());
  363. }
  364. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  365. painter.translate(gutter_width(), 0);
  366. painter.translate(ruler_width(), 0);
  367. size_t first_visible_line = text_position_at(event.rect().top_left()).line();
  368. size_t last_visible_line = text_position_at(event.rect().bottom_right()).line();
  369. auto selection = normalized_selection();
  370. bool has_selection = selection.is_valid();
  371. if (m_ruler_visible) {
  372. for (size_t i = first_visible_line; i <= last_visible_line; ++i) {
  373. bool is_current_line = i == m_cursor.line();
  374. auto ruler_line_rect = ruler_content_rect(i);
  375. painter.draw_text(
  376. ruler_line_rect.shrunken(2, 0).translated(0, m_line_spacing / 2),
  377. String::number(i + 1),
  378. is_current_line ? font().bold_variant() : font(),
  379. Gfx::TextAlignment::TopRight,
  380. is_current_line ? palette().ruler_active_text() : palette().ruler_inactive_text());
  381. }
  382. }
  383. auto text_left = 0;
  384. if (m_ruler_visible)
  385. text_left = ruler_rect_in_inner_coordinates().right() + 1;
  386. else if (m_gutter_visible)
  387. text_left = gutter_rect_in_inner_coordinates().right() + 1;
  388. text_left += frame_thickness();
  389. Gfx::IntRect text_clip_rect {
  390. 0,
  391. frame_thickness(),
  392. width() - width_occupied_by_vertical_scrollbar() - text_left,
  393. height() - height_occupied_by_horizontal_scrollbar()
  394. };
  395. text_clip_rect.translate_by(horizontal_scrollbar().value(), vertical_scrollbar().value());
  396. painter.add_clip_rect(text_clip_rect);
  397. size_t span_index = 0;
  398. if (document().has_spans()) {
  399. for (;;) {
  400. if (span_index >= document().spans().size() || document().spans()[span_index].range.end().line() >= first_visible_line) {
  401. break;
  402. }
  403. ++span_index;
  404. }
  405. }
  406. for (size_t line_index = first_visible_line; line_index <= last_visible_line; ++line_index) {
  407. auto& line = this->line(line_index);
  408. bool physical_line_has_selection = has_selection && line_index >= selection.start().line() && line_index <= selection.end().line();
  409. size_t first_visual_line_with_selection = 0;
  410. size_t last_visual_line_with_selection = 0;
  411. if (physical_line_has_selection) {
  412. if (selection.start().line() < line_index)
  413. first_visual_line_with_selection = 0;
  414. else
  415. first_visual_line_with_selection = visual_line_containing(line_index, selection.start().column());
  416. if (selection.end().line() > line_index)
  417. last_visual_line_with_selection = m_line_visual_data[line_index].visual_line_breaks.size();
  418. else
  419. last_visual_line_with_selection = visual_line_containing(line_index, selection.end().column());
  420. }
  421. size_t selection_start_column_within_line = selection.start().line() == line_index ? selection.start().column() : 0;
  422. size_t selection_end_column_within_line = selection.end().line() == line_index ? selection.end().column() : line.length();
  423. size_t visual_line_index = 0;
  424. for_each_visual_line(line_index, [&](const Gfx::IntRect& visual_line_rect, auto& visual_line_text, size_t start_of_visual_line, [[maybe_unused]] bool is_last_visual_line) {
  425. if (is_multi_line() && line_index == m_cursor.line())
  426. painter.fill_rect(visual_line_rect, widget_background_color.darkened(0.9f));
  427. if constexpr (TEXTEDITOR_DEBUG)
  428. painter.draw_rect(visual_line_rect, Color::Cyan);
  429. if (!placeholder().is_empty() && document().is_empty() && !is_focused() && line_index == 0) {
  430. auto line_rect = visual_line_rect;
  431. line_rect.set_width(font().width(placeholder()));
  432. painter.draw_text(line_rect, placeholder(), m_text_alignment, palette().color(Gfx::ColorRole::PlaceholderText));
  433. } else if (!document().has_spans()) {
  434. // Fast-path for plain text
  435. auto color = palette().color(is_enabled() ? foreground_role() : Gfx::ColorRole::DisabledText);
  436. if (is_displayonly() && is_focused())
  437. color = palette().color(is_enabled() ? Gfx::ColorRole::SelectionText : Gfx::ColorRole::DisabledText);
  438. painter.draw_text(visual_line_rect, visual_line_text, m_text_alignment, color);
  439. } else {
  440. auto unspanned_color = palette().color(is_enabled() ? foreground_role() : Gfx::ColorRole::DisabledText);
  441. if (is_displayonly() && is_focused())
  442. unspanned_color = palette().color(is_enabled() ? Gfx::ColorRole::SelectionText : Gfx::ColorRole::DisabledText);
  443. RefPtr<Gfx::Font> unspanned_font = this->font();
  444. size_t next_column = 0;
  445. Gfx::IntRect span_rect = { visual_line_rect.location(), { 0, line_height() } };
  446. auto draw_text_helper = [&](size_t start, size_t end, RefPtr<Gfx::Font>& font, Color& color, Optional<Color> background_color = {}, bool underline = false) {
  447. size_t length = end - start;
  448. if (length == 0)
  449. return;
  450. auto text = visual_line_text.substring_view(start, length);
  451. span_rect.set_width(font->width(text));
  452. if (background_color.has_value()) {
  453. painter.fill_rect(span_rect, background_color.value());
  454. }
  455. painter.draw_text(span_rect, text, *font, m_text_alignment, color);
  456. if (underline) {
  457. painter.draw_line(span_rect.bottom_left().translated(0, 1), span_rect.bottom_right().translated(0, 1), color);
  458. }
  459. span_rect.translate_by(span_rect.width(), 0);
  460. };
  461. for (;;) {
  462. if (span_index >= document().spans().size()) {
  463. break;
  464. }
  465. auto& span = document().spans()[span_index];
  466. if (!span.range.is_valid()) {
  467. ++span_index;
  468. continue;
  469. }
  470. if (span.range.end().line() < line_index) {
  471. dbgln_if(TEXTEDITOR_DEBUG, "spans not sorted (span end {}:{} is before current line {}) => ignoring", span.range.end().line(), span.range.end().column(), line_index);
  472. ++span_index;
  473. continue;
  474. }
  475. if (span.range.start().line() > line_index
  476. || (span.range.start().line() == line_index && span.range.start().column() >= start_of_visual_line + visual_line_text.length())) {
  477. // no more spans in this line, moving on
  478. break;
  479. }
  480. if (span.range.start().line() == span.range.end().line() && span.range.end().column() < span.range.start().column()) {
  481. dbgln_if(TEXTEDITOR_DEBUG, "span form {}:{} to {}:{} has negative length => ignoring", span.range.start().line(), span.range.start().column(), span.range.end().line(), span.range.end().column());
  482. ++span_index;
  483. continue;
  484. }
  485. if (span.range.end().line() == line_index && span.range.end().column() < start_of_visual_line + next_column) {
  486. dbgln_if(TEXTEDITOR_DEBUG, "spans not sorted (span end {}:{} is before current position {}:{}) => ignoring",
  487. span.range.end().line(), span.range.end().column(), line_index, start_of_visual_line + next_column);
  488. ++span_index;
  489. continue;
  490. }
  491. size_t span_start;
  492. if (span.range.start().line() < line_index || span.range.start().column() < start_of_visual_line) {
  493. span_start = 0;
  494. } else {
  495. span_start = span.range.start().column() - start_of_visual_line;
  496. }
  497. if (span_start < next_column) {
  498. dbgln_if(TEXTEDITOR_DEBUG, "span started before the current position, maybe two spans overlap? (span start {} is before current position {}) => ignoring", span_start, next_column);
  499. ++span_index;
  500. continue;
  501. }
  502. size_t span_end;
  503. bool span_consumned;
  504. if (span.range.end().line() > line_index || span.range.end().column() > start_of_visual_line + visual_line_text.length()) {
  505. span_end = visual_line_text.length();
  506. span_consumned = false;
  507. } else {
  508. span_end = span.range.end().column() - start_of_visual_line;
  509. span_consumned = true;
  510. }
  511. if (span_start != next_column) {
  512. // draw unspanned text between spans
  513. draw_text_helper(next_column, span_start, unspanned_font, unspanned_color);
  514. }
  515. auto font = unspanned_font;
  516. if (span.attributes.bold) {
  517. if (auto bold_font = Gfx::FontDatabase::the().get(font->family(), font->presentation_size(), 700))
  518. font = bold_font;
  519. }
  520. draw_text_helper(span_start, span_end, font, span.attributes.color, span.attributes.background_color, span.attributes.underline);
  521. next_column = span_end;
  522. if (!span_consumned) {
  523. // continue with same span on next line
  524. break;
  525. } else {
  526. ++span_index;
  527. }
  528. }
  529. // draw unspanned text after last span
  530. if (next_column < visual_line_text.length()) {
  531. draw_text_helper(next_column, visual_line_text.length(), unspanned_font, unspanned_color);
  532. }
  533. // consume all spans that should end this line
  534. // this is necessary since the spans can include the new line character
  535. while (is_last_visual_line && span_index < document().spans().size()) {
  536. auto& span = document().spans()[span_index];
  537. if (span.range.end().line() == line_index) {
  538. ++span_index;
  539. } else {
  540. break;
  541. }
  542. }
  543. }
  544. if (m_visualize_trailing_whitespace && line.ends_in_whitespace()) {
  545. size_t physical_column;
  546. auto last_non_whitespace_column = line.last_non_whitespace_column();
  547. if (last_non_whitespace_column.has_value())
  548. physical_column = last_non_whitespace_column.value() + 1;
  549. else
  550. physical_column = 0;
  551. size_t end_of_visual_line = (start_of_visual_line + visual_line_text.length());
  552. if (physical_column < end_of_visual_line) {
  553. size_t visual_column = physical_column > start_of_visual_line ? (physical_column - start_of_visual_line) : 0;
  554. Gfx::IntRect whitespace_rect {
  555. content_x_for_position({ line_index, visual_column }),
  556. visual_line_rect.y(),
  557. font().width(visual_line_text.substring_view(visual_column, visual_line_text.length() - visual_column)),
  558. visual_line_rect.height()
  559. };
  560. painter.fill_rect_with_dither_pattern(whitespace_rect, Color(), Color(255, 192, 192));
  561. }
  562. }
  563. if (m_visualize_leading_whitespace && line.leading_spaces() > 0) {
  564. size_t physical_column = line.leading_spaces();
  565. size_t end_of_leading_whitespace = (start_of_visual_line + physical_column);
  566. size_t end_of_visual_line = (start_of_visual_line + visual_line_text.length());
  567. if (end_of_leading_whitespace < end_of_visual_line) {
  568. Gfx::IntRect whitespace_rect {
  569. content_x_for_position({ line_index, start_of_visual_line }),
  570. visual_line_rect.y(),
  571. font().width(visual_line_text.substring_view(0, end_of_leading_whitespace)),
  572. visual_line_rect.height()
  573. };
  574. painter.fill_rect_with_dither_pattern(whitespace_rect, Color(), Color(192, 255, 192));
  575. }
  576. }
  577. if (physical_line_has_selection) {
  578. size_t start_of_selection_within_visual_line = (size_t)max(0, (int)selection_start_column_within_line - (int)start_of_visual_line);
  579. size_t end_of_selection_within_visual_line = selection_end_column_within_line - start_of_visual_line;
  580. bool current_visual_line_has_selection = start_of_selection_within_visual_line != end_of_selection_within_visual_line
  581. && ((line_index != selection.start().line() && line_index != selection.end().line())
  582. || (visual_line_index >= first_visual_line_with_selection && visual_line_index <= last_visual_line_with_selection));
  583. if (current_visual_line_has_selection) {
  584. bool selection_begins_on_current_visual_line = visual_line_index == first_visual_line_with_selection;
  585. bool selection_ends_on_current_visual_line = visual_line_index == last_visual_line_with_selection;
  586. int selection_left = selection_begins_on_current_visual_line
  587. ? content_x_for_position({ line_index, (size_t)selection_start_column_within_line })
  588. : m_horizontal_content_padding;
  589. int selection_right = selection_ends_on_current_visual_line
  590. ? content_x_for_position({ line_index, (size_t)selection_end_column_within_line })
  591. : visual_line_rect.right() + 1;
  592. Gfx::IntRect selection_rect {
  593. selection_left,
  594. visual_line_rect.y(),
  595. selection_right - selection_left,
  596. visual_line_rect.height()
  597. };
  598. Color background_color = is_focused() ? palette().selection() : palette().inactive_selection();
  599. Color text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
  600. painter.fill_rect(selection_rect, background_color);
  601. if (visual_line_text.code_points()) {
  602. Utf32View visual_selected_text {
  603. visual_line_text.code_points() + start_of_selection_within_visual_line,
  604. end_of_selection_within_visual_line - start_of_selection_within_visual_line
  605. };
  606. painter.draw_text(selection_rect, visual_selected_text, Gfx::TextAlignment::CenterLeft, text_color);
  607. }
  608. }
  609. }
  610. ++visual_line_index;
  611. return IterationDecision::Continue;
  612. });
  613. }
  614. if (!is_multi_line() && m_icon) {
  615. Gfx::IntRect icon_rect { icon_padding(), 1, icon_size(), icon_size() };
  616. painter.draw_scaled_bitmap(icon_rect, *m_icon, m_icon->rect());
  617. }
  618. if (is_focused() && m_cursor_state && !is_displayonly())
  619. painter.fill_rect(cursor_content_rect(), palette().text_cursor());
  620. }
  621. void TextEditor::select_all()
  622. {
  623. TextPosition start_of_document { 0, 0 };
  624. TextPosition end_of_document { line_count() - 1, line(line_count() - 1).length() };
  625. m_selection.set(end_of_document, start_of_document);
  626. did_update_selection();
  627. set_cursor(start_of_document);
  628. update();
  629. }
  630. void TextEditor::keydown_event(KeyEvent& event)
  631. {
  632. TemporaryChange change { m_should_keep_autocomplete_box, true };
  633. if (m_autocomplete_box && m_autocomplete_box->is_visible() && (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Tab)) {
  634. m_autocomplete_box->apply_suggestion();
  635. m_autocomplete_box->close();
  636. return;
  637. }
  638. if (m_autocomplete_box && m_autocomplete_box->is_visible() && event.key() == KeyCode::Key_Escape) {
  639. m_autocomplete_box->close();
  640. return;
  641. }
  642. if (m_autocomplete_box && m_autocomplete_box->is_visible() && event.key() == KeyCode::Key_Up) {
  643. m_autocomplete_box->previous_suggestion();
  644. return;
  645. }
  646. if (m_autocomplete_box && m_autocomplete_box->is_visible() && event.key() == KeyCode::Key_Down) {
  647. m_autocomplete_box->next_suggestion();
  648. return;
  649. }
  650. if (is_single_line()) {
  651. if (event.key() == KeyCode::Key_Tab)
  652. return AbstractScrollableWidget::keydown_event(event);
  653. if (event.modifiers() == KeyModifier::Mod_Shift && event.key() == KeyCode::Key_Return) {
  654. if (on_shift_return_pressed)
  655. on_shift_return_pressed();
  656. return;
  657. }
  658. if (event.key() == KeyCode::Key_Return) {
  659. if (on_return_pressed)
  660. on_return_pressed();
  661. return;
  662. }
  663. if (event.key() == KeyCode::Key_Up) {
  664. if (on_up_pressed)
  665. on_up_pressed();
  666. return;
  667. }
  668. if (event.key() == KeyCode::Key_Down) {
  669. if (on_down_pressed)
  670. on_down_pressed();
  671. return;
  672. }
  673. if (event.key() == KeyCode::Key_PageUp) {
  674. if (on_pageup_pressed)
  675. on_pageup_pressed();
  676. return;
  677. }
  678. if (event.key() == KeyCode::Key_PageDown) {
  679. if (on_pagedown_pressed)
  680. on_pagedown_pressed();
  681. return;
  682. }
  683. } else if (!is_multi_line()) {
  684. VERIFY_NOT_REACHED();
  685. }
  686. ArmedScopeGuard update_autocomplete { [&] {
  687. if (m_autocomplete_box && m_autocomplete_box->is_visible()) {
  688. m_autocomplete_provider->provide_completions([&](auto completions) {
  689. m_autocomplete_box->update_suggestions(move(completions));
  690. });
  691. }
  692. } };
  693. if (is_multi_line() && !event.shift() && !event.alt() && event.ctrl() && event.key() == KeyCode::Key_Space) {
  694. if (m_autocomplete_provider) {
  695. try_show_autocomplete();
  696. update_autocomplete.disarm();
  697. return;
  698. }
  699. }
  700. if (m_editing_engine->on_key(event))
  701. return;
  702. if (event.key() == KeyCode::Key_Escape) {
  703. if (on_escape_pressed)
  704. on_escape_pressed();
  705. return;
  706. }
  707. if (event.modifiers() == Mod_Shift && event.key() == KeyCode::Key_Delete) {
  708. if (m_autocomplete_box)
  709. m_autocomplete_box->close();
  710. return;
  711. }
  712. if (event.key() == KeyCode::Key_Delete) {
  713. if (m_autocomplete_box)
  714. m_autocomplete_box->close();
  715. return;
  716. }
  717. if (event.key() == KeyCode::Key_Backspace) {
  718. if (!is_editable())
  719. return;
  720. if (m_autocomplete_box)
  721. m_autocomplete_box->close();
  722. if (has_selection()) {
  723. delete_selection();
  724. did_update_selection();
  725. return;
  726. }
  727. if (m_cursor.column() > 0) {
  728. int erase_count = 1;
  729. if (event.modifiers() == Mod_Ctrl) {
  730. auto word_break_pos = document().first_word_break_before(m_cursor, true);
  731. erase_count = m_cursor.column() - word_break_pos.column();
  732. } else if (current_line().first_non_whitespace_column() >= m_cursor.column()) {
  733. int new_column;
  734. if (m_cursor.column() % m_soft_tab_width == 0)
  735. new_column = m_cursor.column() - m_soft_tab_width;
  736. else
  737. new_column = (m_cursor.column() / m_soft_tab_width) * m_soft_tab_width;
  738. erase_count = m_cursor.column() - new_column;
  739. }
  740. // Backspace within line
  741. TextRange erased_range({ m_cursor.line(), m_cursor.column() - erase_count }, m_cursor);
  742. auto erased_text = document().text_in_range(erased_range);
  743. execute<RemoveTextCommand>(erased_text, erased_range);
  744. return;
  745. }
  746. if (m_cursor.column() == 0 && m_cursor.line() != 0) {
  747. // Backspace at column 0; merge with previous line
  748. size_t previous_length = line(m_cursor.line() - 1).length();
  749. TextRange erased_range({ m_cursor.line() - 1, previous_length }, m_cursor);
  750. execute<RemoveTextCommand>("\n", erased_range);
  751. return;
  752. }
  753. return;
  754. }
  755. if (!event.ctrl() && !event.alt() && event.code_point() != 0) {
  756. add_code_point(event.code_point());
  757. return;
  758. }
  759. event.ignore();
  760. }
  761. void TextEditor::delete_current_line()
  762. {
  763. if (has_selection())
  764. return delete_selection();
  765. TextPosition start;
  766. TextPosition end;
  767. if (m_cursor.line() == 0 && line_count() == 1) {
  768. start = { 0, 0 };
  769. end = { 0, line(0).length() };
  770. } else if (m_cursor.line() == line_count() - 1) {
  771. start = { m_cursor.line() - 1, line(m_cursor.line() - 1).length() };
  772. end = { m_cursor.line(), line(m_cursor.line()).length() };
  773. } else {
  774. start = { m_cursor.line(), 0 };
  775. end = { m_cursor.line() + 1, 0 };
  776. }
  777. TextRange erased_range(start, end);
  778. execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
  779. }
  780. void TextEditor::do_delete()
  781. {
  782. if (!is_editable())
  783. return;
  784. if (has_selection())
  785. return delete_selection();
  786. if (m_cursor.column() < current_line().length()) {
  787. // Delete within line
  788. TextRange erased_range(m_cursor, { m_cursor.line(), m_cursor.column() + 1 });
  789. execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
  790. return;
  791. }
  792. if (m_cursor.column() == current_line().length() && m_cursor.line() != line_count() - 1) {
  793. // Delete at end of line; merge with next line
  794. TextRange erased_range(m_cursor, { m_cursor.line() + 1, 0 });
  795. execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
  796. return;
  797. }
  798. }
  799. void TextEditor::add_code_point(u32 code_point)
  800. {
  801. if (!is_editable())
  802. return;
  803. StringBuilder sb;
  804. sb.append_code_point(code_point);
  805. if (should_autocomplete_automatically()) {
  806. if (sb.string_view().is_whitespace())
  807. m_autocomplete_timer->stop();
  808. else
  809. m_autocomplete_timer->start();
  810. }
  811. insert_at_cursor_or_replace_selection(sb.to_string());
  812. };
  813. void TextEditor::reset_cursor_blink()
  814. {
  815. m_cursor_state = true;
  816. update_cursor();
  817. stop_timer();
  818. start_timer(500);
  819. }
  820. void TextEditor::update_selection(bool is_selecting)
  821. {
  822. if (is_selecting && !selection()->is_valid()) {
  823. selection()->set(cursor(), {});
  824. did_update_selection();
  825. update();
  826. return;
  827. }
  828. if (!is_selecting && selection()->is_valid()) {
  829. selection()->clear();
  830. did_update_selection();
  831. update();
  832. return;
  833. }
  834. if (is_selecting && selection()->start().is_valid()) {
  835. selection()->set_end(cursor());
  836. did_update_selection();
  837. update();
  838. return;
  839. }
  840. }
  841. int TextEditor::content_x_for_position(const TextPosition& position) const
  842. {
  843. auto& line = this->line(position.line());
  844. int x_offset = 0;
  845. switch (m_text_alignment) {
  846. case Gfx::TextAlignment::CenterLeft:
  847. for_each_visual_line(position.line(), [&](const Gfx::IntRect&, auto& visual_line_view, size_t start_of_visual_line, bool is_last_visual_line) {
  848. size_t offset_in_visual_line = position.column() - start_of_visual_line;
  849. auto before_line_end = is_last_visual_line ? (offset_in_visual_line <= visual_line_view.length()) : (offset_in_visual_line < visual_line_view.length());
  850. if (position.column() >= start_of_visual_line && before_line_end) {
  851. if (offset_in_visual_line == 0) {
  852. x_offset = 0;
  853. } else {
  854. x_offset = font().width(visual_line_view.substring_view(0, offset_in_visual_line));
  855. x_offset += font().glyph_spacing();
  856. }
  857. return IterationDecision::Break;
  858. }
  859. return IterationDecision::Continue;
  860. });
  861. return m_horizontal_content_padding + ((is_single_line() && icon()) ? (icon_size() + icon_padding()) : 0) + x_offset;
  862. case Gfx::TextAlignment::CenterRight:
  863. // FIXME
  864. VERIFY(!is_wrapping_enabled());
  865. return content_width() - m_horizontal_content_padding - (line.length() * fixed_glyph_width()) + (position.column() * fixed_glyph_width());
  866. default:
  867. VERIFY_NOT_REACHED();
  868. }
  869. }
  870. Gfx::IntRect TextEditor::content_rect_for_position(const TextPosition& position) const
  871. {
  872. if (!position.is_valid())
  873. return {};
  874. VERIFY(!lines().is_empty());
  875. VERIFY(position.column() <= (current_line().length() + 1));
  876. int x = content_x_for_position(position);
  877. if (is_single_line()) {
  878. Gfx::IntRect rect { x, 0, 1, line_height() };
  879. rect.center_vertically_within({ {}, frame_inner_rect().size() });
  880. return rect;
  881. }
  882. Gfx::IntRect rect;
  883. for_each_visual_line(position.line(), [&](const Gfx::IntRect& visual_line_rect, auto& view, size_t start_of_visual_line, bool is_last_visual_line) {
  884. auto before_line_end = is_last_visual_line ? ((position.column() - start_of_visual_line) <= view.length()) : ((position.column() - start_of_visual_line) < view.length());
  885. if (position.column() >= start_of_visual_line && before_line_end) {
  886. // NOTE: We have to subtract the horizontal padding here since it's part of the visual line rect
  887. // *and* included in what we get from content_x_for_position().
  888. rect = {
  889. visual_line_rect.x() + x - (m_horizontal_content_padding),
  890. visual_line_rect.y(),
  891. m_editing_engine->cursor_width() == CursorWidth::WIDE ? 7 : 1,
  892. line_height()
  893. };
  894. return IterationDecision::Break;
  895. }
  896. return IterationDecision::Continue;
  897. });
  898. return rect;
  899. }
  900. Gfx::IntRect TextEditor::cursor_content_rect() const
  901. {
  902. return content_rect_for_position(m_cursor);
  903. }
  904. Gfx::IntRect TextEditor::line_widget_rect(size_t line_index) const
  905. {
  906. auto rect = line_content_rect(line_index);
  907. rect.set_x(frame_thickness());
  908. rect.set_width(frame_inner_rect().width());
  909. rect.translate_by(0, -(vertical_scrollbar().value()));
  910. rect.translate_by(0, frame_thickness());
  911. rect.intersect(frame_inner_rect());
  912. return rect;
  913. }
  914. void TextEditor::scroll_position_into_view(const TextPosition& position)
  915. {
  916. auto rect = content_rect_for_position(position);
  917. if (position.column() == 0)
  918. rect.set_x(content_x_for_position({ position.line(), 0 }) - 2);
  919. else if (position.column() == line(position.line()).length())
  920. rect.set_x(content_x_for_position({ position.line(), line(position.line()).length() }) + 2);
  921. scroll_into_view(rect, true, true);
  922. }
  923. void TextEditor::scroll_cursor_into_view()
  924. {
  925. if (!m_reflow_deferred)
  926. scroll_position_into_view(m_cursor);
  927. }
  928. Gfx::IntRect TextEditor::line_content_rect(size_t line_index) const
  929. {
  930. auto& line = this->line(line_index);
  931. if (is_single_line()) {
  932. Gfx::IntRect line_rect = { content_x_for_position({ line_index, 0 }), 0, font().width(line.view()), font().glyph_height() + 4 };
  933. line_rect.center_vertically_within({ {}, frame_inner_rect().size() });
  934. return line_rect;
  935. }
  936. if (is_wrapping_enabled())
  937. return m_line_visual_data[line_index].visual_rect;
  938. return {
  939. content_x_for_position({ line_index, 0 }),
  940. (int)line_index * line_height(),
  941. font().width(line.view()),
  942. line_height()
  943. };
  944. }
  945. void TextEditor::set_cursor_and_focus_line(size_t line, size_t column)
  946. {
  947. u_int index_max = line_count() - 1;
  948. set_cursor(line, column);
  949. if (line > 1 && line < index_max) {
  950. int headroom = frame_inner_rect().height() / 3;
  951. do {
  952. auto line_data = m_line_visual_data[line];
  953. headroom -= line_data.visual_rect.height();
  954. line--;
  955. } while (line > 0 && headroom > 0);
  956. Gfx::IntRect rect = { 0, line_content_rect(line).y(),
  957. 1, frame_inner_rect().height() };
  958. scroll_into_view(rect, false, true);
  959. }
  960. }
  961. void TextEditor::update_cursor()
  962. {
  963. update(line_widget_rect(m_cursor.line()));
  964. }
  965. void TextEditor::set_cursor(size_t line, size_t column)
  966. {
  967. set_cursor({ line, column });
  968. }
  969. void TextEditor::set_cursor(const TextPosition& a_position)
  970. {
  971. VERIFY(!lines().is_empty());
  972. TextPosition position = a_position;
  973. if (position.line() >= line_count())
  974. position.set_line(line_count() - 1);
  975. if (position.column() > lines()[position.line()].length())
  976. position.set_column(lines()[position.line()].length());
  977. if (m_cursor != position && is_visual_data_up_to_date()) {
  978. // NOTE: If the old cursor is no longer valid, repaint everything just in case.
  979. auto old_cursor_line_rect = m_cursor.line() < line_count()
  980. ? line_widget_rect(m_cursor.line())
  981. : rect();
  982. m_cursor = position;
  983. m_cursor_state = true;
  984. scroll_cursor_into_view();
  985. update(old_cursor_line_rect);
  986. update_cursor();
  987. } else if (m_cursor != position) {
  988. m_cursor = position;
  989. m_cursor_state = true;
  990. }
  991. cursor_did_change();
  992. if (on_cursor_change)
  993. on_cursor_change();
  994. if (m_highlighter)
  995. m_highlighter->cursor_did_change();
  996. }
  997. void TextEditor::focusin_event(FocusEvent& event)
  998. {
  999. if (event.source() == FocusSource::Keyboard)
  1000. select_all();
  1001. m_cursor_state = true;
  1002. update_cursor();
  1003. stop_timer();
  1004. start_timer(500);
  1005. if (on_focusin)
  1006. on_focusin();
  1007. }
  1008. void TextEditor::focusout_event(FocusEvent&)
  1009. {
  1010. if (is_displayonly() && has_selection())
  1011. m_selection.clear();
  1012. stop_timer();
  1013. if (on_focusout)
  1014. on_focusout();
  1015. }
  1016. void TextEditor::timer_event(Core::TimerEvent&)
  1017. {
  1018. m_cursor_state = !m_cursor_state;
  1019. if (is_focused())
  1020. update_cursor();
  1021. }
  1022. bool TextEditor::write_to_file(const String& path)
  1023. {
  1024. int fd = open(path.characters(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
  1025. if (fd < 0) {
  1026. perror("open");
  1027. return false;
  1028. }
  1029. ScopeGuard fd_guard = [fd] { close(fd); };
  1030. off_t file_size = 0;
  1031. if (line_count() == 1 && line(0).is_empty()) {
  1032. // Truncate to zero.
  1033. } else {
  1034. // Compute the final file size and ftruncate() to make writing fast.
  1035. // FIXME: Remove this once the kernel is smart enough to do this instead.
  1036. for (size_t i = 0; i < line_count(); ++i)
  1037. file_size += line(i).length();
  1038. file_size += line_count();
  1039. }
  1040. if (ftruncate(fd, file_size) < 0) {
  1041. perror("ftruncate");
  1042. return false;
  1043. }
  1044. if (file_size == 0) {
  1045. // A size 0 file doesn't need a data copy.
  1046. } else {
  1047. for (size_t i = 0; i < line_count(); ++i) {
  1048. auto& line = this->line(i);
  1049. if (line.length()) {
  1050. auto line_as_utf8 = line.to_utf8();
  1051. ssize_t nwritten = write(fd, line_as_utf8.characters(), line_as_utf8.length());
  1052. if (nwritten < 0) {
  1053. perror("write");
  1054. return false;
  1055. }
  1056. }
  1057. char ch = '\n';
  1058. ssize_t nwritten = write(fd, &ch, 1);
  1059. if (nwritten != 1) {
  1060. perror("write");
  1061. return false;
  1062. }
  1063. }
  1064. }
  1065. document().set_unmodified();
  1066. return true;
  1067. }
  1068. String TextEditor::text() const
  1069. {
  1070. return document().text();
  1071. }
  1072. void TextEditor::clear()
  1073. {
  1074. document().remove_all_lines();
  1075. document().append_line(make<TextDocumentLine>(document()));
  1076. m_selection.clear();
  1077. did_update_selection();
  1078. set_cursor(0, 0);
  1079. update();
  1080. }
  1081. String TextEditor::selected_text() const
  1082. {
  1083. if (!has_selection())
  1084. return {};
  1085. return document().text_in_range(m_selection);
  1086. }
  1087. size_t TextEditor::number_of_selected_words() const
  1088. {
  1089. if (!has_selection())
  1090. return 0;
  1091. size_t word_count = 0;
  1092. bool in_word = false;
  1093. auto selected_text = this->selected_text();
  1094. for (char c : selected_text) {
  1095. if (in_word && is_ascii_space(c)) {
  1096. in_word = false;
  1097. word_count++;
  1098. continue;
  1099. }
  1100. if (!in_word && !is_ascii_space(c))
  1101. in_word = true;
  1102. }
  1103. if (in_word)
  1104. word_count++;
  1105. return word_count;
  1106. }
  1107. void TextEditor::delete_selection()
  1108. {
  1109. auto selection = normalized_selection();
  1110. auto selected = selected_text();
  1111. m_selection.clear();
  1112. execute<RemoveTextCommand>(selected, selection);
  1113. did_update_selection();
  1114. did_change();
  1115. set_cursor(selection.start());
  1116. update();
  1117. }
  1118. void TextEditor::delete_text_range(TextRange range)
  1119. {
  1120. auto normalized_range = range.normalized();
  1121. execute<RemoveTextCommand>(document().text_in_range(normalized_range), normalized_range);
  1122. did_change();
  1123. set_cursor(normalized_range.start());
  1124. update();
  1125. }
  1126. void TextEditor::insert_at_cursor_or_replace_selection(const StringView& text)
  1127. {
  1128. ReflowDeferrer defer(*this);
  1129. VERIFY(is_editable());
  1130. if (has_selection())
  1131. delete_selection();
  1132. // Check if adding a newline leaves the previous line as just whitespace.
  1133. auto const clear_length = m_cursor.column();
  1134. auto const should_clear_last_line = text == "\n"
  1135. && clear_length > 0
  1136. && current_line().leading_spaces() == clear_length;
  1137. execute<InsertTextCommand>(text, m_cursor);
  1138. if (should_clear_last_line) { // If it does leave just whitespace, clear it.
  1139. auto const original_cursor_position = cursor();
  1140. TextPosition start(original_cursor_position.line() - 1, 0);
  1141. TextPosition end(original_cursor_position.line() - 1, clear_length);
  1142. TextRange erased_range(start, end);
  1143. execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
  1144. set_cursor(original_cursor_position);
  1145. }
  1146. }
  1147. void TextEditor::cut()
  1148. {
  1149. if (!is_editable())
  1150. return;
  1151. auto selected_text = this->selected_text();
  1152. dbgln_if(TEXTEDITOR_DEBUG, "Cut: \"{}\"", selected_text);
  1153. Clipboard::the().set_plain_text(selected_text);
  1154. delete_selection();
  1155. }
  1156. void TextEditor::copy()
  1157. {
  1158. auto selected_text = this->selected_text();
  1159. dbgln_if(TEXTEDITOR_DEBUG, "Copy: \"{}\"\n", selected_text);
  1160. Clipboard::the().set_plain_text(selected_text);
  1161. }
  1162. void TextEditor::paste()
  1163. {
  1164. if (!is_editable())
  1165. return;
  1166. auto paste_text = Clipboard::the().data();
  1167. if (paste_text.is_empty())
  1168. return;
  1169. dbgln_if(TEXTEDITOR_DEBUG, "Paste: \"{}\"", String::copy(paste_text));
  1170. TemporaryChange change(m_automatic_indentation_enabled, false);
  1171. insert_at_cursor_or_replace_selection(paste_text);
  1172. }
  1173. void TextEditor::defer_reflow()
  1174. {
  1175. ++m_reflow_deferred;
  1176. }
  1177. void TextEditor::undefer_reflow()
  1178. {
  1179. VERIFY(m_reflow_deferred);
  1180. if (!--m_reflow_deferred) {
  1181. if (m_reflow_requested) {
  1182. recompute_all_visual_lines();
  1183. scroll_cursor_into_view();
  1184. }
  1185. }
  1186. }
  1187. void TextEditor::try_show_autocomplete()
  1188. {
  1189. if (m_autocomplete_provider) {
  1190. m_autocomplete_provider->provide_completions([&](auto completions) {
  1191. auto has_completions = !completions.is_empty();
  1192. m_autocomplete_box->update_suggestions(move(completions));
  1193. auto position = content_rect_for_position(cursor()).translated(0, -visible_content_rect().y()).bottom_right().translated(screen_relative_rect().top_left().translated(ruler_width(), 0).translated(10, 5));
  1194. if (has_completions)
  1195. m_autocomplete_box->show(position);
  1196. });
  1197. }
  1198. }
  1199. void TextEditor::enter_event(Core::Event&)
  1200. {
  1201. m_automatic_selection_scroll_timer->stop();
  1202. }
  1203. void TextEditor::leave_event(Core::Event&)
  1204. {
  1205. if (m_in_drag_select)
  1206. m_automatic_selection_scroll_timer->start();
  1207. }
  1208. void TextEditor::did_change()
  1209. {
  1210. update_content_size();
  1211. recompute_all_visual_lines();
  1212. if (m_autocomplete_box && !m_should_keep_autocomplete_box) {
  1213. m_autocomplete_box->close();
  1214. if (m_autocomplete_timer)
  1215. m_autocomplete_timer->stop();
  1216. }
  1217. m_needs_rehighlight = true;
  1218. if (!m_has_pending_change_notification) {
  1219. m_has_pending_change_notification = true;
  1220. deferred_invoke([this](auto&) {
  1221. m_has_pending_change_notification = false;
  1222. if (on_change)
  1223. on_change();
  1224. });
  1225. }
  1226. }
  1227. void TextEditor::set_mode(const Mode mode)
  1228. {
  1229. if (m_mode == mode)
  1230. return;
  1231. m_mode = mode;
  1232. switch (mode) {
  1233. case Editable:
  1234. m_cut_action->set_enabled(true && has_selection());
  1235. m_delete_action->set_enabled(true);
  1236. m_paste_action->set_enabled(true);
  1237. set_accepts_emoji_input(true);
  1238. break;
  1239. case DisplayOnly:
  1240. case ReadOnly:
  1241. m_cut_action->set_enabled(false && has_selection());
  1242. m_delete_action->set_enabled(false);
  1243. m_paste_action->set_enabled(false);
  1244. set_accepts_emoji_input(false);
  1245. break;
  1246. default:
  1247. VERIFY_NOT_REACHED();
  1248. }
  1249. if (!is_displayonly())
  1250. set_override_cursor(Gfx::StandardCursor::IBeam);
  1251. else
  1252. set_override_cursor(Gfx::StandardCursor::None);
  1253. }
  1254. void TextEditor::did_update_selection()
  1255. {
  1256. m_cut_action->set_enabled(is_editable() && has_selection());
  1257. m_copy_action->set_enabled(has_selection());
  1258. if (on_selection_change)
  1259. on_selection_change();
  1260. if (is_wrapping_enabled()) {
  1261. // FIXME: Try to repaint less.
  1262. update();
  1263. }
  1264. }
  1265. void TextEditor::context_menu_event(ContextMenuEvent& event)
  1266. {
  1267. if (is_displayonly())
  1268. return;
  1269. if (!m_context_menu) {
  1270. m_context_menu = Menu::construct();
  1271. m_context_menu->add_action(undo_action());
  1272. m_context_menu->add_action(redo_action());
  1273. m_context_menu->add_separator();
  1274. m_context_menu->add_action(cut_action());
  1275. m_context_menu->add_action(copy_action());
  1276. m_context_menu->add_action(paste_action());
  1277. m_context_menu->add_action(delete_action());
  1278. m_context_menu->add_separator();
  1279. m_context_menu->add_action(select_all_action());
  1280. if (is_multi_line()) {
  1281. m_context_menu->add_separator();
  1282. m_context_menu->add_action(go_to_line_action());
  1283. }
  1284. if (!m_custom_context_menu_actions.is_empty()) {
  1285. m_context_menu->add_separator();
  1286. for (auto& action : m_custom_context_menu_actions) {
  1287. m_context_menu->add_action(action);
  1288. }
  1289. }
  1290. }
  1291. m_context_menu->popup(event.screen_position());
  1292. }
  1293. void TextEditor::set_text_alignment(Gfx::TextAlignment alignment)
  1294. {
  1295. if (m_text_alignment == alignment)
  1296. return;
  1297. m_text_alignment = alignment;
  1298. update();
  1299. }
  1300. void TextEditor::resize_event(ResizeEvent& event)
  1301. {
  1302. AbstractScrollableWidget::resize_event(event);
  1303. update_content_size();
  1304. recompute_all_visual_lines();
  1305. }
  1306. void TextEditor::theme_change_event(ThemeChangeEvent& event)
  1307. {
  1308. AbstractScrollableWidget::theme_change_event(event);
  1309. m_needs_rehighlight = true;
  1310. }
  1311. void TextEditor::set_selection(const TextRange& selection)
  1312. {
  1313. if (m_selection == selection)
  1314. return;
  1315. m_selection = selection;
  1316. set_cursor(m_selection.end());
  1317. scroll_position_into_view(normalized_selection().start());
  1318. update();
  1319. }
  1320. void TextEditor::clear_selection()
  1321. {
  1322. if (!has_selection())
  1323. return;
  1324. m_selection.clear();
  1325. update();
  1326. }
  1327. void TextEditor::recompute_all_visual_lines()
  1328. {
  1329. if (m_reflow_deferred) {
  1330. m_reflow_requested = true;
  1331. return;
  1332. }
  1333. m_reflow_requested = false;
  1334. int y_offset = 0;
  1335. for (size_t line_index = 0; line_index < line_count(); ++line_index) {
  1336. recompute_visual_lines(line_index);
  1337. m_line_visual_data[line_index].visual_rect.set_y(y_offset);
  1338. y_offset += m_line_visual_data[line_index].visual_rect.height();
  1339. }
  1340. update_content_size();
  1341. }
  1342. void TextEditor::ensure_cursor_is_valid()
  1343. {
  1344. auto new_cursor = m_cursor;
  1345. if (new_cursor.line() >= line_count())
  1346. new_cursor.set_line(line_count() - 1);
  1347. if (new_cursor.column() > line(new_cursor.line()).length())
  1348. new_cursor.set_column(line(new_cursor.line()).length());
  1349. if (m_cursor != new_cursor)
  1350. set_cursor(new_cursor);
  1351. }
  1352. size_t TextEditor::visual_line_containing(size_t line_index, size_t column) const
  1353. {
  1354. size_t visual_line_index = 0;
  1355. for_each_visual_line(line_index, [&](const Gfx::IntRect&, auto& view, size_t start_of_visual_line, [[maybe_unused]] bool is_last_visual_line) {
  1356. if (column >= start_of_visual_line && ((column - start_of_visual_line) < view.length()))
  1357. return IterationDecision::Break;
  1358. ++visual_line_index;
  1359. return IterationDecision::Continue;
  1360. });
  1361. return visual_line_index;
  1362. }
  1363. void TextEditor::recompute_visual_lines(size_t line_index)
  1364. {
  1365. auto& line = document().line(line_index);
  1366. auto& visual_data = m_line_visual_data[line_index];
  1367. visual_data.visual_line_breaks.clear_with_capacity();
  1368. int available_width = visible_text_rect_in_inner_coordinates().width();
  1369. if (is_wrapping_enabled()) {
  1370. int line_width_so_far = 0;
  1371. size_t last_whitespace_index = 0;
  1372. size_t line_width_since_last_whitespace = 0;
  1373. auto glyph_spacing = font().glyph_spacing();
  1374. for (size_t i = 0; i < line.length(); ++i) {
  1375. auto code_point = line.code_points()[i];
  1376. if (is_ascii_space(code_point)) {
  1377. last_whitespace_index = i;
  1378. line_width_since_last_whitespace = 0;
  1379. }
  1380. auto glyph_width = font().glyph_or_emoji_width(code_point);
  1381. line_width_since_last_whitespace += glyph_width + glyph_spacing;
  1382. if ((line_width_so_far + glyph_width + glyph_spacing) > available_width) {
  1383. if (m_wrapping_mode == WrappingMode::WrapAtWords && last_whitespace_index != 0) {
  1384. // Plus 1 to get the first letter of the word.
  1385. visual_data.visual_line_breaks.append(last_whitespace_index + 1);
  1386. line_width_so_far = line_width_since_last_whitespace;
  1387. last_whitespace_index = 0;
  1388. line_width_since_last_whitespace = 0;
  1389. } else {
  1390. visual_data.visual_line_breaks.append(i);
  1391. line_width_so_far = glyph_width + glyph_spacing;
  1392. }
  1393. continue;
  1394. }
  1395. line_width_so_far += glyph_width + glyph_spacing;
  1396. }
  1397. }
  1398. visual_data.visual_line_breaks.append(line.length());
  1399. if (is_wrapping_enabled())
  1400. visual_data.visual_rect = { m_horizontal_content_padding, 0, available_width, static_cast<int>(visual_data.visual_line_breaks.size()) * line_height() };
  1401. else
  1402. visual_data.visual_rect = { m_horizontal_content_padding, 0, font().width(line.view()), line_height() };
  1403. }
  1404. template<typename Callback>
  1405. void TextEditor::for_each_visual_line(size_t line_index, Callback callback) const
  1406. {
  1407. auto editor_visible_text_rect = visible_text_rect_in_inner_coordinates();
  1408. size_t start_of_line = 0;
  1409. size_t visual_line_index = 0;
  1410. auto& line = document().line(line_index);
  1411. auto& visual_data = m_line_visual_data[line_index];
  1412. for (auto visual_line_break : visual_data.visual_line_breaks) {
  1413. auto visual_line_view = Utf32View(line.code_points() + start_of_line, visual_line_break - start_of_line);
  1414. Gfx::IntRect visual_line_rect {
  1415. visual_data.visual_rect.x(),
  1416. visual_data.visual_rect.y() + ((int)visual_line_index * line_height()),
  1417. font().width(visual_line_view) + font().glyph_spacing(),
  1418. line_height()
  1419. };
  1420. if (is_right_text_alignment(text_alignment()))
  1421. visual_line_rect.set_right_without_resize(editor_visible_text_rect.right());
  1422. if (is_single_line()) {
  1423. visual_line_rect.center_vertically_within(editor_visible_text_rect);
  1424. if (m_icon)
  1425. visual_line_rect.translate_by(icon_size() + icon_padding(), 0);
  1426. }
  1427. if (callback(visual_line_rect, visual_line_view, start_of_line, visual_line_index == visual_data.visual_line_breaks.size() - 1) == IterationDecision::Break)
  1428. break;
  1429. start_of_line = visual_line_break;
  1430. ++visual_line_index;
  1431. }
  1432. }
  1433. void TextEditor::set_wrapping_mode(WrappingMode mode)
  1434. {
  1435. if (m_wrapping_mode == mode)
  1436. return;
  1437. m_wrapping_mode = mode;
  1438. horizontal_scrollbar().set_visible(m_wrapping_mode == WrappingMode::NoWrap);
  1439. update_content_size();
  1440. recompute_all_visual_lines();
  1441. update();
  1442. }
  1443. void TextEditor::add_custom_context_menu_action(Action& action)
  1444. {
  1445. m_custom_context_menu_actions.append(action);
  1446. }
  1447. void TextEditor::did_change_font()
  1448. {
  1449. vertical_scrollbar().set_step(line_height());
  1450. recompute_all_visual_lines();
  1451. update();
  1452. AbstractScrollableWidget::did_change_font();
  1453. }
  1454. void TextEditor::document_did_append_line()
  1455. {
  1456. m_line_visual_data.append(make<LineVisualData>());
  1457. recompute_all_visual_lines();
  1458. update();
  1459. }
  1460. void TextEditor::document_did_remove_line(size_t line_index)
  1461. {
  1462. m_line_visual_data.remove(line_index);
  1463. recompute_all_visual_lines();
  1464. update();
  1465. }
  1466. void TextEditor::document_did_remove_all_lines()
  1467. {
  1468. m_line_visual_data.clear();
  1469. recompute_all_visual_lines();
  1470. update();
  1471. }
  1472. void TextEditor::document_did_insert_line(size_t line_index)
  1473. {
  1474. m_line_visual_data.insert(line_index, make<LineVisualData>());
  1475. recompute_all_visual_lines();
  1476. update();
  1477. }
  1478. void TextEditor::document_did_change()
  1479. {
  1480. did_change();
  1481. update();
  1482. }
  1483. void TextEditor::document_did_update_undo_stack()
  1484. {
  1485. auto make_action_text = [](auto prefix, auto suffix) {
  1486. StringBuilder builder;
  1487. builder.append(prefix);
  1488. if (suffix.has_value()) {
  1489. builder.append(' ');
  1490. builder.append(suffix.value());
  1491. }
  1492. return builder.to_string();
  1493. };
  1494. m_undo_action->set_enabled(can_undo());
  1495. m_redo_action->set_enabled(can_redo());
  1496. m_undo_action->set_text(make_action_text("&Undo", document().undo_stack().undo_action_text()));
  1497. m_redo_action->set_text(make_action_text("&Redo", document().undo_stack().redo_action_text()));
  1498. // FIXME: This is currently firing more often than it should.
  1499. // Ideally we'd only send this out when the undo stack modified state actually changes.
  1500. if (on_modified_change)
  1501. on_modified_change(document().is_modified());
  1502. }
  1503. void TextEditor::document_did_set_text()
  1504. {
  1505. m_line_visual_data.clear();
  1506. for (size_t i = 0; i < m_document->line_count(); ++i)
  1507. m_line_visual_data.append(make<LineVisualData>());
  1508. document_did_change();
  1509. }
  1510. void TextEditor::document_did_set_cursor(const TextPosition& position)
  1511. {
  1512. set_cursor(position);
  1513. }
  1514. void TextEditor::set_document(TextDocument& document)
  1515. {
  1516. if (m_document.ptr() == &document)
  1517. return;
  1518. if (m_document)
  1519. m_document->unregister_client(*this);
  1520. m_document = document;
  1521. m_line_visual_data.clear();
  1522. for (size_t i = 0; i < m_document->line_count(); ++i) {
  1523. m_line_visual_data.append(make<LineVisualData>());
  1524. }
  1525. set_cursor(0, 0);
  1526. if (has_selection())
  1527. m_selection.clear();
  1528. recompute_all_visual_lines();
  1529. update();
  1530. m_document->register_client(*this);
  1531. }
  1532. void TextEditor::rehighlight_if_needed()
  1533. {
  1534. if (!m_needs_rehighlight)
  1535. return;
  1536. if (m_highlighter)
  1537. m_highlighter->rehighlight(palette());
  1538. m_needs_rehighlight = false;
  1539. }
  1540. const Syntax::Highlighter* TextEditor::syntax_highlighter() const
  1541. {
  1542. return m_highlighter.ptr();
  1543. }
  1544. void TextEditor::set_syntax_highlighter(OwnPtr<Syntax::Highlighter> highlighter)
  1545. {
  1546. if (m_highlighter)
  1547. m_highlighter->detach();
  1548. m_highlighter = move(highlighter);
  1549. if (m_highlighter) {
  1550. m_highlighter->attach(*this);
  1551. m_needs_rehighlight = true;
  1552. } else
  1553. document().set_spans({});
  1554. }
  1555. const AutocompleteProvider* TextEditor::autocomplete_provider() const
  1556. {
  1557. return m_autocomplete_provider.ptr();
  1558. }
  1559. void TextEditor::set_autocomplete_provider(OwnPtr<AutocompleteProvider>&& provider)
  1560. {
  1561. if (m_autocomplete_provider)
  1562. m_autocomplete_provider->detach();
  1563. m_autocomplete_provider = move(provider);
  1564. if (m_autocomplete_provider) {
  1565. m_autocomplete_provider->attach(*this);
  1566. if (!m_autocomplete_box)
  1567. m_autocomplete_box = make<AutocompleteBox>(*this);
  1568. }
  1569. if (m_autocomplete_box)
  1570. m_autocomplete_box->close();
  1571. }
  1572. const EditingEngine* TextEditor::editing_engine() const
  1573. {
  1574. return m_editing_engine.ptr();
  1575. }
  1576. void TextEditor::set_editing_engine(OwnPtr<EditingEngine> editing_engine)
  1577. {
  1578. if (m_editing_engine)
  1579. m_editing_engine->detach();
  1580. m_editing_engine = move(editing_engine);
  1581. VERIFY(m_editing_engine);
  1582. m_editing_engine->attach(*this);
  1583. m_cursor_state = true;
  1584. update_cursor();
  1585. stop_timer();
  1586. start_timer(500);
  1587. }
  1588. int TextEditor::line_height() const
  1589. {
  1590. return font().glyph_height() + m_line_spacing;
  1591. }
  1592. int TextEditor::fixed_glyph_width() const
  1593. {
  1594. VERIFY(font().is_fixed_width());
  1595. return font().glyph_width(' ');
  1596. }
  1597. void TextEditor::set_icon(const Gfx::Bitmap* icon)
  1598. {
  1599. if (m_icon == icon)
  1600. return;
  1601. m_icon = icon;
  1602. update();
  1603. }
  1604. void TextEditor::set_visualize_trailing_whitespace(bool enabled)
  1605. {
  1606. if (m_visualize_trailing_whitespace == enabled)
  1607. return;
  1608. m_visualize_trailing_whitespace = enabled;
  1609. update();
  1610. }
  1611. void TextEditor::set_visualize_leading_whitespace(bool enabled)
  1612. {
  1613. if (m_visualize_leading_whitespace == enabled)
  1614. return;
  1615. m_visualize_leading_whitespace = enabled;
  1616. update();
  1617. }
  1618. void TextEditor::set_should_autocomplete_automatically(bool value)
  1619. {
  1620. if (value == should_autocomplete_automatically())
  1621. return;
  1622. if (value) {
  1623. VERIFY(m_autocomplete_provider);
  1624. m_autocomplete_timer = Core::Timer::create_single_shot(m_automatic_autocomplete_delay_ms, [this] { try_show_autocomplete(); });
  1625. return;
  1626. }
  1627. remove_child(*m_autocomplete_timer);
  1628. m_autocomplete_timer = nullptr;
  1629. }
  1630. int TextEditor::number_of_visible_lines() const
  1631. {
  1632. return visible_content_rect().height() / line_height();
  1633. }
  1634. void TextEditor::set_ruler_visible(bool visible)
  1635. {
  1636. if (m_ruler_visible == visible)
  1637. return;
  1638. m_ruler_visible = visible;
  1639. recompute_all_visual_lines();
  1640. update();
  1641. }
  1642. void TextEditor::set_gutter_visible(bool visible)
  1643. {
  1644. if (m_gutter_visible == visible)
  1645. return;
  1646. m_gutter_visible = visible;
  1647. recompute_all_visual_lines();
  1648. update();
  1649. }
  1650. void TextEditor::undo()
  1651. {
  1652. clear_selection();
  1653. document().undo();
  1654. }
  1655. void TextEditor::redo()
  1656. {
  1657. clear_selection();
  1658. document().redo();
  1659. }
  1660. }