TextEditor.cpp 64 KB

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