TextEditor.cpp 64 KB

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