code_edit.cpp 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566
  1. /**************************************************************************/
  2. /* code_edit.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "code_edit.h"
  31. #include "code_edit.compat.inc"
  32. #include "core/os/keyboard.h"
  33. #include "core/string/string_builder.h"
  34. #include "core/string/ustring.h"
  35. #include "scene/theme/theme_db.h"
  36. void CodeEdit::_notification(int p_what) {
  37. switch (p_what) {
  38. case NOTIFICATION_THEME_CHANGED: {
  39. set_gutter_width(main_gutter, get_line_height());
  40. set_gutter_width(line_number_gutter, (line_number_digits + 1) * theme_cache.font->get_char_size('0', theme_cache.font_size).width);
  41. set_gutter_width(fold_gutter, get_line_height() / 1.2);
  42. } break;
  43. case NOTIFICATION_DRAW: {
  44. RID ci = get_canvas_item();
  45. const Size2 size = get_size();
  46. const bool caret_visible = is_caret_visible();
  47. const bool rtl = is_layout_rtl();
  48. const int row_height = get_line_height();
  49. if (line_length_guideline_columns.size() > 0) {
  50. const int xmargin_beg = theme_cache.style_normal->get_margin(SIDE_LEFT) + get_total_gutter_width();
  51. const int xmargin_end = size.width - theme_cache.style_normal->get_margin(SIDE_RIGHT) - (is_drawing_minimap() ? get_minimap_width() : 0);
  52. const float char_size = theme_cache.font->get_char_size('0', theme_cache.font_size).width;
  53. for (int i = 0; i < line_length_guideline_columns.size(); i++) {
  54. const int xoffset = xmargin_beg + char_size * (int)line_length_guideline_columns[i] - get_h_scroll();
  55. if (xoffset > xmargin_beg && xoffset < xmargin_end) {
  56. Color guideline_color = (i == 0) ? theme_cache.line_length_guideline_color : theme_cache.line_length_guideline_color * Color(1, 1, 1, 0.5);
  57. if (rtl) {
  58. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(size.width - xoffset, 0), Point2(size.width - xoffset, size.height), guideline_color);
  59. continue;
  60. }
  61. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(xoffset, 0), Point2(xoffset, size.height), guideline_color);
  62. }
  63. }
  64. }
  65. bool code_completion_below = false;
  66. if (caret_visible && code_completion_active && code_completion_options.size() > 0) {
  67. const int code_completion_options_count = code_completion_options.size();
  68. const int lines = MIN(code_completion_options_count, theme_cache.code_completion_max_lines);
  69. const Size2 icon_area_size(row_height, row_height);
  70. code_completion_rect.size.width = code_completion_longest_line + theme_cache.code_completion_icon_separation + icon_area_size.width + 2;
  71. code_completion_rect.size.height = lines * row_height;
  72. const Point2 caret_pos = get_caret_draw_pos();
  73. const int total_height = theme_cache.code_completion_style->get_minimum_size().y + code_completion_rect.size.height;
  74. const bool can_fit_completion_above = (caret_pos.y - row_height > total_height);
  75. const bool can_fit_completion_below = (caret_pos.y + row_height + total_height <= get_size().height);
  76. if (!can_fit_completion_below && can_fit_completion_above) {
  77. code_completion_rect.position.y = (caret_pos.y - total_height - row_height) + theme_cache.line_spacing;
  78. } else {
  79. code_completion_rect.position.y = caret_pos.y + (theme_cache.line_spacing / 2.0f);
  80. code_completion_below = true;
  81. }
  82. const int scroll_width = code_completion_options_count > theme_cache.code_completion_max_lines ? theme_cache.code_completion_scroll_width : 0;
  83. const int code_completion_base_width = theme_cache.font->get_string_size(code_completion_base, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width;
  84. if (caret_pos.x - code_completion_base_width + code_completion_rect.size.width + scroll_width > get_size().width) {
  85. code_completion_rect.position.x = get_size().width - code_completion_rect.size.width - scroll_width;
  86. } else {
  87. code_completion_rect.position.x = caret_pos.x - code_completion_base_width;
  88. }
  89. draw_style_box(theme_cache.code_completion_style, Rect2(code_completion_rect.position - theme_cache.code_completion_style->get_offset(), code_completion_rect.size + theme_cache.code_completion_style->get_minimum_size() + Size2(scroll_width, 0)));
  90. if (theme_cache.code_completion_background_color.a > 0.01) {
  91. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(code_completion_rect.position, code_completion_rect.size + Size2(scroll_width, 0)), theme_cache.code_completion_background_color);
  92. }
  93. code_completion_scroll_rect.position = code_completion_rect.position + Vector2(code_completion_rect.size.width, 0);
  94. code_completion_scroll_rect.size = Vector2(scroll_width, code_completion_rect.size.height);
  95. code_completion_line_ofs = CLAMP((code_completion_force_item_center < 0 ? code_completion_current_selected : code_completion_force_item_center) - lines / 2, 0, code_completion_options_count - lines);
  96. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(code_completion_rect.position.x, code_completion_rect.position.y + (code_completion_current_selected - code_completion_line_ofs) * row_height), Size2(code_completion_rect.size.width, row_height)), theme_cache.code_completion_selected_color);
  97. for (int i = 0; i < lines; i++) {
  98. int l = code_completion_line_ofs + i;
  99. ERR_CONTINUE(l < 0 || l >= code_completion_options_count);
  100. Ref<TextLine> tl;
  101. tl.instantiate();
  102. tl->add_string(code_completion_options[l].display, theme_cache.font, theme_cache.font_size);
  103. int yofs = (row_height - tl->get_size().y) / 2;
  104. Point2 title_pos(code_completion_rect.position.x, code_completion_rect.position.y + i * row_height + yofs);
  105. /* Draw completion icon if it is valid. */
  106. const Ref<Texture2D> &icon = code_completion_options[l].icon;
  107. Rect2 icon_area(code_completion_rect.position.x, code_completion_rect.position.y + i * row_height, icon_area_size.width, icon_area_size.height);
  108. if (icon.is_valid()) {
  109. Size2 icon_size = icon_area.size * 0.7;
  110. icon->draw_rect(ci, Rect2(icon_area.position + (icon_area.size - icon_size) / 2, icon_size));
  111. }
  112. title_pos.x = icon_area.position.x + icon_area.size.width + theme_cache.code_completion_icon_separation;
  113. tl->set_width(code_completion_rect.size.width - (icon_area_size.x + theme_cache.code_completion_icon_separation));
  114. if (rtl) {
  115. if (code_completion_options[l].default_value.get_type() == Variant::COLOR) {
  116. draw_rect(Rect2(Point2(code_completion_rect.position.x, icon_area.position.y), icon_area_size), (Color)code_completion_options[l].default_value);
  117. }
  118. tl->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  119. } else {
  120. if (code_completion_options[l].default_value.get_type() == Variant::COLOR) {
  121. draw_rect(Rect2(Point2(code_completion_rect.position.x + code_completion_rect.size.width - icon_area_size.x, icon_area.position.y), icon_area_size), (Color)code_completion_options[l].default_value);
  122. }
  123. tl->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  124. }
  125. Point2 match_pos = Point2(code_completion_rect.position.x + icon_area_size.x + theme_cache.code_completion_icon_separation, code_completion_rect.position.y + i * row_height);
  126. for (int j = 0; j < code_completion_options[l].matches.size(); j++) {
  127. Pair<int, int> match_segment = code_completion_options[l].matches[j];
  128. int match_offset = theme_cache.font->get_string_size(code_completion_options[l].display.substr(0, match_segment.first), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width;
  129. int match_len = theme_cache.font->get_string_size(code_completion_options[l].display.substr(match_segment.first, match_segment.second), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width;
  130. draw_rect(Rect2(match_pos + Point2(match_offset, 0), Size2(match_len, row_height)), theme_cache.code_completion_existing_color);
  131. }
  132. tl->draw(ci, title_pos, code_completion_options[l].font_color);
  133. }
  134. /* Draw a small scroll rectangle to show a position in the options. */
  135. if (scroll_width) {
  136. Color scroll_color = is_code_completion_scroll_hovered || is_code_completion_scroll_pressed ? theme_cache.code_completion_scroll_hovered_color : theme_cache.code_completion_scroll_color;
  137. float r = (float)theme_cache.code_completion_max_lines / code_completion_options_count;
  138. float o = (float)code_completion_line_ofs / code_completion_options_count;
  139. draw_rect(Rect2(code_completion_rect.position.x + code_completion_rect.size.width, code_completion_rect.position.y + o * code_completion_rect.size.y, scroll_width, code_completion_rect.size.y * r), scroll_color);
  140. }
  141. }
  142. /* Code hint */
  143. if (caret_visible && !code_hint.is_empty() && (!code_completion_active || (code_completion_below != code_hint_draw_below))) {
  144. const int font_height = theme_cache.font->get_height(theme_cache.font_size);
  145. Vector<String> code_hint_lines = code_hint.split("\n");
  146. int line_count = code_hint_lines.size();
  147. int max_width = 0;
  148. for (int i = 0; i < line_count; i++) {
  149. max_width = MAX(max_width, theme_cache.font->get_string_size(code_hint_lines[i], HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x);
  150. }
  151. Size2 minsize = theme_cache.code_hint_style->get_minimum_size() + Size2(max_width, line_count * font_height + (theme_cache.line_spacing * line_count - 1));
  152. int offset = theme_cache.font->get_string_size(code_hint_lines[0].substr(0, code_hint_lines[0].find(String::chr(0xFFFF))), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x;
  153. if (code_hint_xpos == -0xFFFF) {
  154. code_hint_xpos = get_caret_draw_pos().x - offset;
  155. }
  156. Point2 hint_ofs = Vector2(code_hint_xpos, get_caret_draw_pos().y);
  157. if (code_hint_draw_below) {
  158. hint_ofs.y += theme_cache.line_spacing / 2.0f;
  159. } else {
  160. hint_ofs.y -= (minsize.y + row_height) - theme_cache.line_spacing;
  161. }
  162. draw_style_box(theme_cache.code_hint_style, Rect2(hint_ofs, minsize));
  163. int yofs = 0;
  164. for (int i = 0; i < line_count; i++) {
  165. const String &line = code_hint_lines[i];
  166. int begin = 0;
  167. int end = 0;
  168. if (line.contains(String::chr(0xFFFF))) {
  169. begin = theme_cache.font->get_string_size(line.substr(0, line.find(String::chr(0xFFFF))), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x;
  170. end = theme_cache.font->get_string_size(line.substr(0, line.rfind(String::chr(0xFFFF))), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x;
  171. }
  172. Point2 round_ofs = hint_ofs + theme_cache.code_hint_style->get_offset() + Vector2(0, theme_cache.font->get_ascent(theme_cache.font_size) + font_height * i + yofs);
  173. round_ofs = round_ofs.round();
  174. draw_string(theme_cache.font, round_ofs, line.replace(String::chr(0xFFFF), ""), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size, theme_cache.code_hint_color);
  175. if (end > 0) {
  176. // Draw an underline for the currently edited function parameter.
  177. const Vector2 b = hint_ofs + theme_cache.code_hint_style->get_offset() + Vector2(begin, font_height + font_height * i + yofs);
  178. draw_line(b, b + Vector2(end - begin, 0), theme_cache.code_hint_color, 2);
  179. // Draw a translucent text highlight as well.
  180. const Rect2 highlight_rect = Rect2(
  181. b - Vector2(0, font_height),
  182. Vector2(end - begin, font_height));
  183. draw_rect(highlight_rect, theme_cache.code_hint_color * Color(1, 1, 1, 0.2));
  184. }
  185. yofs += theme_cache.line_spacing;
  186. }
  187. }
  188. } break;
  189. case NOTIFICATION_DRAG_BEGIN: {
  190. cancel_code_completion();
  191. } break;
  192. case NOTIFICATION_MOUSE_EXIT: {
  193. queue_redraw();
  194. } break;
  195. }
  196. }
  197. void CodeEdit::gui_input(const Ref<InputEvent> &p_gui_input) {
  198. Ref<InputEventMouseButton> mb = p_gui_input;
  199. if (mb.is_valid()) {
  200. /* Ignore mouse clicks in IME input mode. */
  201. if (has_ime_text()) {
  202. return;
  203. }
  204. if (is_code_completion_scroll_pressed && mb->get_button_index() == MouseButton::LEFT) {
  205. is_code_completion_scroll_pressed = false;
  206. accept_event();
  207. queue_redraw();
  208. return;
  209. }
  210. if (is_code_completion_drag_started && !mb->is_pressed()) {
  211. is_code_completion_drag_started = false;
  212. accept_event();
  213. queue_redraw();
  214. return;
  215. }
  216. if (code_completion_active && code_completion_rect.has_point(mb->get_position())) {
  217. if (!mb->is_pressed()) {
  218. accept_event();
  219. return;
  220. }
  221. is_code_completion_drag_started = true;
  222. switch (mb->get_button_index()) {
  223. case MouseButton::WHEEL_UP: {
  224. if (code_completion_current_selected > 0) {
  225. code_completion_current_selected--;
  226. code_completion_force_item_center = -1;
  227. queue_redraw();
  228. }
  229. } break;
  230. case MouseButton::WHEEL_DOWN: {
  231. if (code_completion_current_selected < code_completion_options.size() - 1) {
  232. code_completion_current_selected++;
  233. code_completion_force_item_center = -1;
  234. queue_redraw();
  235. }
  236. } break;
  237. case MouseButton::LEFT: {
  238. if (code_completion_force_item_center == -1) {
  239. code_completion_force_item_center = code_completion_current_selected;
  240. }
  241. code_completion_current_selected = CLAMP(code_completion_line_ofs + (mb->get_position().y - code_completion_rect.position.y) / get_line_height(), 0, code_completion_options.size() - 1);
  242. if (mb->is_double_click()) {
  243. confirm_code_completion();
  244. }
  245. queue_redraw();
  246. } break;
  247. default:
  248. break;
  249. }
  250. accept_event();
  251. return;
  252. } else if (code_completion_active && code_completion_scroll_rect.has_point(mb->get_position())) {
  253. if (mb->get_button_index() != MouseButton::LEFT) {
  254. accept_event();
  255. return;
  256. }
  257. if (mb->is_pressed()) {
  258. is_code_completion_drag_started = true;
  259. is_code_completion_scroll_pressed = true;
  260. _update_scroll_selected_line(mb->get_position().y);
  261. queue_redraw();
  262. }
  263. accept_event();
  264. return;
  265. }
  266. cancel_code_completion();
  267. set_code_hint("");
  268. if (mb->is_pressed()) {
  269. Vector2i mpos = mb->get_position();
  270. if (is_layout_rtl()) {
  271. mpos.x = get_size().x - mpos.x;
  272. }
  273. Point2i pos = get_line_column_at_pos(mpos, false);
  274. int line = pos.y;
  275. int col = pos.x;
  276. if (line != -1 && mb->get_button_index() == MouseButton::LEFT) {
  277. if (is_line_folded(line)) {
  278. int wrap_index = get_line_wrap_index_at_column(line, col);
  279. if (wrap_index == get_line_wrap_count(line)) {
  280. int eol_icon_width = theme_cache.folded_eol_icon->get_width();
  281. int left_margin = get_total_gutter_width() + eol_icon_width + get_line_width(line, wrap_index) - get_h_scroll();
  282. if (mpos.x > left_margin && mpos.x <= left_margin + eol_icon_width + 3) {
  283. unfold_line(line);
  284. return;
  285. }
  286. }
  287. }
  288. }
  289. } else {
  290. if (mb->get_button_index() == MouseButton::LEFT) {
  291. if (mb->is_command_or_control_pressed() && !symbol_lookup_word.is_empty()) {
  292. Vector2i mpos = mb->get_position();
  293. if (is_layout_rtl()) {
  294. mpos.x = get_size().x - mpos.x;
  295. }
  296. Point2i pos = get_line_column_at_pos(mpos, false);
  297. int line = pos.y;
  298. int col = pos.x;
  299. if (line != -1) {
  300. emit_signal(SNAME("symbol_lookup"), symbol_lookup_word, line, col);
  301. }
  302. return;
  303. }
  304. }
  305. }
  306. }
  307. Ref<InputEventMouseMotion> mm = p_gui_input;
  308. if (mm.is_valid()) {
  309. Vector2i mpos = mm->get_position();
  310. if (is_layout_rtl()) {
  311. mpos.x = get_size().x - mpos.x;
  312. }
  313. if (symbol_lookup_on_click_enabled) {
  314. if (mm->is_command_or_control_pressed() && mm->get_button_mask().is_empty()) {
  315. symbol_lookup_pos = get_line_column_at_pos(mpos);
  316. symbol_lookup_new_word = get_word_at_pos(mpos);
  317. if (symbol_lookup_new_word != symbol_lookup_word) {
  318. emit_signal(SNAME("symbol_validate"), symbol_lookup_new_word);
  319. }
  320. } else if (!mm->is_command_or_control_pressed() || (!mm->get_button_mask().is_empty() && symbol_lookup_pos != get_line_column_at_pos(mpos))) {
  321. set_symbol_lookup_word_as_valid(false);
  322. }
  323. }
  324. bool scroll_hovered = code_completion_scroll_rect.has_point(mpos);
  325. if (is_code_completion_scroll_hovered != scroll_hovered) {
  326. is_code_completion_scroll_hovered = scroll_hovered;
  327. accept_event();
  328. queue_redraw();
  329. }
  330. if (is_code_completion_scroll_pressed) {
  331. _update_scroll_selected_line(mpos.y);
  332. accept_event();
  333. queue_redraw();
  334. return;
  335. }
  336. if (code_completion_active && code_completion_rect.has_point(mm->get_position())) {
  337. accept_event();
  338. return;
  339. }
  340. }
  341. Ref<InputEventKey> k = p_gui_input;
  342. if (TextEdit::alt_input(p_gui_input)) {
  343. accept_event();
  344. return;
  345. }
  346. bool update_code_completion = false;
  347. if (!k.is_valid()) {
  348. // MouseMotion events should not be handled by TextEdit logic if we're
  349. // currently clicking and dragging from the code completion panel.
  350. if (!mm.is_valid() || !is_code_completion_drag_started) {
  351. TextEdit::gui_input(p_gui_input);
  352. }
  353. return;
  354. }
  355. /* Ctrl + Hover symbols */
  356. bool mac_keys = OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios");
  357. if ((mac_keys && k->get_keycode() == Key::META) || (!mac_keys && k->get_keycode() == Key::CTRL)) {
  358. if (symbol_lookup_on_click_enabled) {
  359. if (k->is_pressed() && !is_dragging_cursor()) {
  360. symbol_lookup_new_word = get_word_at_pos(get_local_mouse_pos());
  361. if (symbol_lookup_new_word != symbol_lookup_word) {
  362. emit_signal(SNAME("symbol_validate"), symbol_lookup_new_word);
  363. }
  364. } else {
  365. set_symbol_lookup_word_as_valid(false);
  366. }
  367. }
  368. return;
  369. }
  370. /* If a modifier has been pressed, and nothing else, return. */
  371. if (!k->is_pressed() || k->get_keycode() == Key::CTRL || k->get_keycode() == Key::ALT || k->get_keycode() == Key::SHIFT || k->get_keycode() == Key::META || k->get_keycode() == Key::CAPSLOCK) {
  372. return;
  373. }
  374. // Allow unicode handling if:
  375. // No modifiers are pressed (except Shift and CapsLock)
  376. bool allow_unicode_handling = !(k->is_command_or_control_pressed() || k->is_ctrl_pressed() || k->is_alt_pressed() || k->is_meta_pressed());
  377. /* AUTO-COMPLETE */
  378. if (code_completion_enabled && k->is_action("ui_text_completion_query", true)) {
  379. request_code_completion(true);
  380. accept_event();
  381. return;
  382. }
  383. if (code_completion_active) {
  384. if (k->is_action("ui_up", true)) {
  385. if (code_completion_current_selected > 0) {
  386. code_completion_current_selected--;
  387. } else {
  388. code_completion_current_selected = code_completion_options.size() - 1;
  389. }
  390. code_completion_force_item_center = -1;
  391. queue_redraw();
  392. accept_event();
  393. return;
  394. }
  395. if (k->is_action("ui_down", true)) {
  396. if (code_completion_current_selected < code_completion_options.size() - 1) {
  397. code_completion_current_selected++;
  398. } else {
  399. code_completion_current_selected = 0;
  400. }
  401. code_completion_force_item_center = -1;
  402. queue_redraw();
  403. accept_event();
  404. return;
  405. }
  406. if (k->is_action("ui_page_up", true)) {
  407. code_completion_current_selected = MAX(0, code_completion_current_selected - theme_cache.code_completion_max_lines);
  408. code_completion_force_item_center = -1;
  409. queue_redraw();
  410. accept_event();
  411. return;
  412. }
  413. if (k->is_action("ui_page_down", true)) {
  414. code_completion_current_selected = MIN(code_completion_options.size() - 1, code_completion_current_selected + theme_cache.code_completion_max_lines);
  415. code_completion_force_item_center = -1;
  416. queue_redraw();
  417. accept_event();
  418. return;
  419. }
  420. if (k->is_action("ui_home", true)) {
  421. code_completion_current_selected = 0;
  422. code_completion_force_item_center = -1;
  423. queue_redraw();
  424. accept_event();
  425. return;
  426. }
  427. if (k->is_action("ui_end", true)) {
  428. code_completion_current_selected = code_completion_options.size() - 1;
  429. code_completion_force_item_center = -1;
  430. queue_redraw();
  431. accept_event();
  432. return;
  433. }
  434. if (k->is_action("ui_text_completion_replace", true) || k->is_action("ui_text_completion_accept", true)) {
  435. confirm_code_completion(k->is_action("ui_text_completion_replace", true));
  436. accept_event();
  437. return;
  438. }
  439. if (k->is_action("ui_cancel", true)) {
  440. cancel_code_completion();
  441. accept_event();
  442. return;
  443. }
  444. if (k->is_action("ui_text_backspace", true)) {
  445. backspace();
  446. _filter_code_completion_candidates_impl();
  447. accept_event();
  448. return;
  449. }
  450. if (k->is_action("ui_left", true) || k->is_action("ui_right", true)) {
  451. update_code_completion = true;
  452. } else {
  453. update_code_completion = (allow_unicode_handling && k->get_unicode() >= 32);
  454. }
  455. if (!update_code_completion) {
  456. cancel_code_completion();
  457. }
  458. }
  459. /* MISC */
  460. if (!code_hint.is_empty() && k->is_action("ui_cancel", true)) {
  461. set_code_hint("");
  462. accept_event();
  463. return;
  464. }
  465. if (allow_unicode_handling && k->get_unicode() == ')') {
  466. set_code_hint("");
  467. }
  468. /* Indentation */
  469. if (k->is_action("ui_text_indent", true)) {
  470. do_indent();
  471. accept_event();
  472. return;
  473. }
  474. if (k->is_action("ui_text_dedent", true)) {
  475. unindent_lines();
  476. accept_event();
  477. return;
  478. }
  479. // Override new line actions, for auto indent.
  480. if (k->is_action("ui_text_newline_above", true)) {
  481. _new_line(false, true);
  482. accept_event();
  483. return;
  484. }
  485. if (k->is_action("ui_text_newline_blank", true)) {
  486. _new_line(false);
  487. accept_event();
  488. return;
  489. }
  490. if (k->is_action("ui_text_newline", true)) {
  491. _new_line();
  492. accept_event();
  493. return;
  494. }
  495. // Remove shift, otherwise actions will not match.
  496. k = k->duplicate();
  497. k->set_shift_pressed(false);
  498. if (k->is_action("ui_text_caret_up", true) ||
  499. k->is_action("ui_text_caret_down", true) ||
  500. k->is_action("ui_text_caret_line_start", true) ||
  501. k->is_action("ui_text_caret_line_end", true) ||
  502. k->is_action("ui_text_caret_page_up", true) ||
  503. k->is_action("ui_text_caret_page_down", true)) {
  504. set_code_hint("");
  505. }
  506. TextEdit::gui_input(p_gui_input);
  507. if (update_code_completion) {
  508. _filter_code_completion_candidates_impl();
  509. }
  510. }
  511. /* General overrides */
  512. Control::CursorShape CodeEdit::get_cursor_shape(const Point2 &p_pos) const {
  513. if (!symbol_lookup_word.is_empty()) {
  514. return CURSOR_POINTING_HAND;
  515. }
  516. if ((code_completion_active && code_completion_rect.has_point(p_pos)) || (!is_editable() && (!is_selecting_enabled() || get_line_count() == 0))) {
  517. return CURSOR_ARROW;
  518. }
  519. if (code_completion_active && code_completion_scroll_rect.has_point(p_pos)) {
  520. return CURSOR_ARROW;
  521. }
  522. Point2i pos = get_line_column_at_pos(p_pos, false);
  523. int line = pos.y;
  524. int col = pos.x;
  525. if (line != -1 && is_line_folded(line)) {
  526. int wrap_index = get_line_wrap_index_at_column(line, col);
  527. if (wrap_index == get_line_wrap_count(line)) {
  528. int eol_icon_width = theme_cache.folded_eol_icon->get_width();
  529. int left_margin = get_total_gutter_width() + eol_icon_width + get_line_width(line, wrap_index) - get_h_scroll();
  530. if (p_pos.x > left_margin && p_pos.x <= left_margin + eol_icon_width + 3) {
  531. return CURSOR_POINTING_HAND;
  532. }
  533. }
  534. }
  535. return TextEdit::get_cursor_shape(p_pos);
  536. }
  537. /* Text manipulation */
  538. // Overridable actions
  539. void CodeEdit::_handle_unicode_input_internal(const uint32_t p_unicode, int p_caret) {
  540. start_action(EditAction::ACTION_TYPING);
  541. Vector<int> caret_edit_order = get_caret_index_edit_order();
  542. for (const int &i : caret_edit_order) {
  543. if (p_caret != -1 && p_caret != i) {
  544. continue;
  545. }
  546. bool had_selection = has_selection(i);
  547. String selection_text = (had_selection ? get_selected_text(i) : "");
  548. if (had_selection) {
  549. delete_selection(i);
  550. }
  551. // Remove the old character if in overtype mode and no selection.
  552. if (is_overtype_mode_enabled() && !had_selection) {
  553. // Make sure we don't try and remove empty space.
  554. if (get_caret_column(i) < get_line(get_caret_line(i)).length()) {
  555. remove_text(get_caret_line(i), get_caret_column(i), get_caret_line(i), get_caret_column(i) + 1);
  556. }
  557. }
  558. const char32_t chr[2] = { (char32_t)p_unicode, 0 };
  559. if (auto_brace_completion_enabled) {
  560. int cl = get_caret_line(i);
  561. int cc = get_caret_column(i);
  562. if (had_selection) {
  563. insert_text_at_caret(chr, i);
  564. String close_key = get_auto_brace_completion_close_key(chr);
  565. if (!close_key.is_empty()) {
  566. insert_text_at_caret(selection_text + close_key, i);
  567. set_caret_column(get_caret_column(i) - 1, i == 0, i);
  568. }
  569. } else {
  570. int caret_move_offset = 1;
  571. int post_brace_pair = cc < get_line(cl).length() ? _get_auto_brace_pair_close_at_pos(cl, cc) : -1;
  572. if (has_string_delimiter(chr) && cc > 0 && !is_symbol(get_line(cl)[cc - 1]) && post_brace_pair == -1) {
  573. insert_text_at_caret(chr, i);
  574. } else if (cc < get_line(cl).length() && !is_symbol(get_line(cl)[cc])) {
  575. insert_text_at_caret(chr, i);
  576. } else if (post_brace_pair != -1 && auto_brace_completion_pairs[post_brace_pair].close_key[0] == chr[0]) {
  577. caret_move_offset = auto_brace_completion_pairs[post_brace_pair].close_key.length();
  578. } else if (is_in_comment(cl, cc) != -1 || (is_in_string(cl, cc) != -1 && has_string_delimiter(chr))) {
  579. insert_text_at_caret(chr, i);
  580. } else {
  581. insert_text_at_caret(chr, i);
  582. int pre_brace_pair = _get_auto_brace_pair_open_at_pos(cl, cc + 1);
  583. if (pre_brace_pair != -1) {
  584. insert_text_at_caret(auto_brace_completion_pairs[pre_brace_pair].close_key, i);
  585. }
  586. }
  587. set_caret_column(cc + caret_move_offset, i == 0, i);
  588. }
  589. } else {
  590. insert_text_at_caret(chr, i);
  591. }
  592. }
  593. end_action();
  594. }
  595. void CodeEdit::_backspace_internal(int p_caret) {
  596. if (!is_editable()) {
  597. return;
  598. }
  599. if (has_selection(p_caret)) {
  600. delete_selection(p_caret);
  601. return;
  602. }
  603. begin_complex_operation();
  604. Vector<int> caret_edit_order = get_caret_index_edit_order();
  605. for (const int &i : caret_edit_order) {
  606. if (p_caret != -1 && p_caret != i) {
  607. continue;
  608. }
  609. int cc = get_caret_column(i);
  610. int cl = get_caret_line(i);
  611. if (cc == 0 && cl == 0) {
  612. continue;
  613. }
  614. if (cl > 0 && _is_line_hidden(cl - 1)) {
  615. unfold_line(get_caret_line(i) - 1);
  616. }
  617. int prev_line = cc ? cl : cl - 1;
  618. int prev_column = cc ? (cc - 1) : (get_line(cl - 1).length());
  619. merge_gutters(prev_line, cl);
  620. if (auto_brace_completion_enabled && cc > 0) {
  621. int idx = _get_auto_brace_pair_open_at_pos(cl, cc);
  622. if (idx != -1) {
  623. prev_column = cc - auto_brace_completion_pairs[idx].open_key.length();
  624. if (_get_auto_brace_pair_close_at_pos(cl, cc) == idx) {
  625. remove_text(prev_line, prev_column, cl, cc + auto_brace_completion_pairs[idx].close_key.length());
  626. } else {
  627. remove_text(prev_line, prev_column, cl, cc);
  628. }
  629. set_caret_line(prev_line, false, true, 0, i);
  630. set_caret_column(prev_column, i == 0, i);
  631. adjust_carets_after_edit(i, prev_line, prev_column, cl, cc + auto_brace_completion_pairs[idx].close_key.length());
  632. continue;
  633. }
  634. }
  635. // For space indentation we need to do a basic unindent if there are no chars to the left, acting the same way as tabs.
  636. if (indent_using_spaces && cc != 0) {
  637. if (get_first_non_whitespace_column(cl) >= cc) {
  638. prev_column = cc - _calculate_spaces_till_next_left_indent(cc);
  639. prev_line = cl;
  640. }
  641. }
  642. remove_text(prev_line, prev_column, cl, cc);
  643. set_caret_line(prev_line, false, true, 0, i);
  644. set_caret_column(prev_column, i == 0, i);
  645. adjust_carets_after_edit(i, prev_line, prev_column, cl, cc);
  646. }
  647. merge_overlapping_carets();
  648. end_complex_operation();
  649. }
  650. /* Indent management */
  651. void CodeEdit::set_indent_size(const int p_size) {
  652. ERR_FAIL_COND_MSG(p_size <= 0, "Indend size must be greater than 0.");
  653. if (indent_size == p_size) {
  654. return;
  655. }
  656. indent_size = p_size;
  657. if (indent_using_spaces) {
  658. indent_text = String(" ").repeat(p_size);
  659. } else {
  660. indent_text = "\t";
  661. }
  662. set_tab_size(p_size);
  663. }
  664. int CodeEdit::get_indent_size() const {
  665. return indent_size;
  666. }
  667. void CodeEdit::set_indent_using_spaces(const bool p_use_spaces) {
  668. indent_using_spaces = p_use_spaces;
  669. if (indent_using_spaces) {
  670. indent_text = String(" ").repeat(indent_size);
  671. } else {
  672. indent_text = "\t";
  673. }
  674. }
  675. bool CodeEdit::is_indent_using_spaces() const {
  676. return indent_using_spaces;
  677. }
  678. void CodeEdit::set_auto_indent_enabled(bool p_enabled) {
  679. auto_indent = p_enabled;
  680. }
  681. bool CodeEdit::is_auto_indent_enabled() const {
  682. return auto_indent;
  683. }
  684. void CodeEdit::set_auto_indent_prefixes(const TypedArray<String> &p_prefixes) {
  685. auto_indent_prefixes.clear();
  686. for (int i = 0; i < p_prefixes.size(); i++) {
  687. const String prefix = p_prefixes[i];
  688. auto_indent_prefixes.insert(prefix[0]);
  689. }
  690. }
  691. TypedArray<String> CodeEdit::get_auto_indent_prefixes() const {
  692. TypedArray<String> prefixes;
  693. for (const char32_t &E : auto_indent_prefixes) {
  694. prefixes.push_back(String::chr(E));
  695. }
  696. return prefixes;
  697. }
  698. void CodeEdit::do_indent() {
  699. if (!is_editable()) {
  700. return;
  701. }
  702. if (has_selection()) {
  703. indent_lines();
  704. return;
  705. }
  706. if (!indent_using_spaces) {
  707. insert_text_at_caret("\t");
  708. return;
  709. }
  710. begin_complex_operation();
  711. Vector<int> caret_edit_order = get_caret_index_edit_order();
  712. for (const int &i : caret_edit_order) {
  713. int spaces_to_add = _calculate_spaces_till_next_right_indent(get_caret_column(i));
  714. if (spaces_to_add > 0) {
  715. insert_text_at_caret(String(" ").repeat(spaces_to_add), i);
  716. }
  717. }
  718. end_complex_operation();
  719. }
  720. void CodeEdit::indent_lines() {
  721. if (!is_editable()) {
  722. return;
  723. }
  724. begin_complex_operation();
  725. Vector<int> caret_edit_order = get_caret_index_edit_order();
  726. for (const int &c : caret_edit_order) {
  727. // This value informs us by how much we changed selection position by indenting right.
  728. // Default is 1 for tab indentation.
  729. int selection_offset = 1;
  730. int start_line = get_caret_line(c);
  731. int end_line = start_line;
  732. if (has_selection(c)) {
  733. start_line = get_selection_from_line(c);
  734. end_line = get_selection_to_line(c);
  735. // Ignore the last line if the selection is not past the first column.
  736. if (get_selection_to_column(c) == 0) {
  737. selection_offset = 0;
  738. end_line--;
  739. }
  740. }
  741. for (int i = start_line; i <= end_line; i++) {
  742. const String line_text = get_line(i);
  743. if (line_text.size() == 0 && has_selection(c)) {
  744. continue;
  745. }
  746. if (!indent_using_spaces) {
  747. set_line(i, '\t' + line_text);
  748. continue;
  749. }
  750. // We don't really care where selection is - we just need to know indentation level at the beginning of the line.
  751. // Since we will add this many spaces, we want to move the whole selection and caret by this much.
  752. int spaces_to_add = _calculate_spaces_till_next_right_indent(get_first_non_whitespace_column(i));
  753. set_line(i, String(" ").repeat(spaces_to_add) + line_text);
  754. selection_offset = spaces_to_add;
  755. }
  756. // Fix selection and caret being off after shifting selection right.
  757. if (has_selection(c)) {
  758. select(start_line, get_selection_from_column(c) + selection_offset, get_selection_to_line(c), get_selection_to_column(c) + selection_offset, c);
  759. }
  760. set_caret_column(get_caret_column(c) + selection_offset, false, c);
  761. }
  762. end_complex_operation();
  763. queue_redraw();
  764. }
  765. void CodeEdit::unindent_lines() {
  766. if (!is_editable()) {
  767. return;
  768. }
  769. begin_complex_operation();
  770. Vector<int> caret_edit_order = get_caret_index_edit_order();
  771. for (const int &c : caret_edit_order) {
  772. // Moving caret and selection after unindenting can get tricky because
  773. // changing content of line can move caret and selection on its own (if new line ends before previous position of either)
  774. // therefore we just remember initial values and at the end of the operation offset them by number of removed characters.
  775. int removed_characters = 0;
  776. int initial_selection_end_column = 0;
  777. int initial_cursor_column = get_caret_column(c);
  778. int start_line = get_caret_line(c);
  779. int end_line = start_line;
  780. if (has_selection(c)) {
  781. start_line = get_selection_from_line(c);
  782. end_line = get_selection_to_line(c);
  783. // Ignore the last line if the selection is not past the first column.
  784. initial_selection_end_column = get_selection_to_column(c);
  785. if (initial_selection_end_column == 0) {
  786. end_line--;
  787. }
  788. }
  789. bool first_line_edited = false;
  790. bool last_line_edited = false;
  791. for (int i = start_line; i <= end_line; i++) {
  792. String line_text = get_line(i);
  793. if (line_text.begins_with("\t")) {
  794. line_text = line_text.substr(1, line_text.length());
  795. set_line(i, line_text);
  796. removed_characters = 1;
  797. first_line_edited = (i == start_line) ? true : first_line_edited;
  798. last_line_edited = (i == end_line) ? true : last_line_edited;
  799. continue;
  800. }
  801. if (line_text.begins_with(" ")) {
  802. // When unindenting we aim to remove spaces before line that has selection no matter what is selected.
  803. // Here we remove only enough spaces to align text to nearest full multiple of indentation_size.
  804. // In case where selection begins at the start of indentation_size multiple we remove whole indentation level.
  805. int spaces_to_remove = _calculate_spaces_till_next_left_indent(get_first_non_whitespace_column(i));
  806. line_text = line_text.substr(spaces_to_remove, line_text.length());
  807. set_line(i, line_text);
  808. removed_characters = spaces_to_remove;
  809. first_line_edited = (i == start_line) ? true : first_line_edited;
  810. last_line_edited = (i == end_line) ? true : last_line_edited;
  811. }
  812. }
  813. if (has_selection(c)) {
  814. // Fix selection being off by one on the first line.
  815. if (first_line_edited) {
  816. select(get_selection_from_line(c), get_selection_from_column(c) - removed_characters, get_selection_to_line(c), initial_selection_end_column, c);
  817. }
  818. // Fix selection being off by one on the last line.
  819. if (last_line_edited) {
  820. select(get_selection_from_line(c), get_selection_from_column(c), get_selection_to_line(c), initial_selection_end_column - removed_characters, c);
  821. }
  822. }
  823. set_caret_column(initial_cursor_column - removed_characters, false, c);
  824. }
  825. end_complex_operation();
  826. queue_redraw();
  827. }
  828. void CodeEdit::convert_indent(int p_from_line, int p_to_line) {
  829. if (!is_editable()) {
  830. return;
  831. }
  832. // Check line range.
  833. p_from_line = (p_from_line < 0) ? 0 : p_from_line;
  834. p_to_line = (p_to_line < 0) ? get_line_count() - 1 : p_to_line;
  835. ERR_FAIL_COND(p_from_line >= get_line_count());
  836. ERR_FAIL_COND(p_to_line >= get_line_count());
  837. ERR_FAIL_COND(p_to_line < p_from_line);
  838. // Store caret states.
  839. Vector<int> caret_columns;
  840. Vector<Pair<int, int>> from_selections;
  841. Vector<Pair<int, int>> to_selections;
  842. caret_columns.resize(get_caret_count());
  843. from_selections.resize(get_caret_count());
  844. to_selections.resize(get_caret_count());
  845. for (int c = 0; c < get_caret_count(); c++) {
  846. caret_columns.write[c] = get_caret_column(c);
  847. // Set "selection_from_line" to -1 to allow checking if there was a selection later.
  848. if (!has_selection(c)) {
  849. from_selections.write[c].first = -1;
  850. continue;
  851. }
  852. from_selections.write[c].first = get_selection_from_line(c);
  853. from_selections.write[c].second = get_selection_from_column(c);
  854. to_selections.write[c].first = get_selection_to_line(c);
  855. to_selections.write[c].second = get_selection_to_column(c);
  856. }
  857. // Check lines within range.
  858. const char32_t from_indent_char = indent_using_spaces ? '\t' : ' ';
  859. int size_diff = indent_using_spaces ? indent_size - 1 : -(indent_size - 1);
  860. bool changed_indentation = false;
  861. for (int i = p_from_line; i <= p_to_line; i++) {
  862. String line = get_line(i);
  863. if (line.length() <= 0) {
  864. continue;
  865. }
  866. // Check chars in the line.
  867. int j = 0;
  868. int space_count = 0;
  869. bool line_changed = false;
  870. while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
  871. if (line[j] != from_indent_char) {
  872. space_count = 0;
  873. j++;
  874. continue;
  875. }
  876. space_count++;
  877. if (!indent_using_spaces && space_count != indent_size) {
  878. j++;
  879. continue;
  880. }
  881. line_changed = true;
  882. if (!changed_indentation) {
  883. begin_complex_operation();
  884. changed_indentation = true;
  885. }
  886. // Calculate new caret state.
  887. for (int c = 0; c < get_caret_count(); c++) {
  888. if (get_caret_line(c) != i || caret_columns[c] <= j) {
  889. continue;
  890. }
  891. caret_columns.write[c] += size_diff;
  892. if (from_selections.write[c].first == -1) {
  893. continue;
  894. }
  895. from_selections.write[c].second = from_selections[c].first == i ? from_selections[c].second + size_diff : from_selections[c].second;
  896. to_selections.write[c].second = to_selections[c].first == i ? to_selections[c].second + size_diff : to_selections[c].second;
  897. }
  898. // Calculate new line.
  899. line = line.left(j + ((size_diff < 0) ? size_diff : 0)) + indent_text + line.substr(j + 1);
  900. space_count = 0;
  901. j += size_diff;
  902. }
  903. if (line_changed) {
  904. set_line(i, line);
  905. }
  906. }
  907. if (!changed_indentation) {
  908. return;
  909. }
  910. // Restore caret states.
  911. for (int c = 0; c < get_caret_count(); c++) {
  912. set_caret_column(caret_columns[c], c == 0, c);
  913. if (from_selections.write[c].first != -1) {
  914. select(from_selections.write[c].first, from_selections.write[c].second, to_selections.write[c].first, to_selections.write[c].second, c);
  915. }
  916. }
  917. merge_overlapping_carets();
  918. end_complex_operation();
  919. queue_redraw();
  920. }
  921. int CodeEdit::_calculate_spaces_till_next_left_indent(int p_column) const {
  922. int spaces_till_indent = p_column % indent_size;
  923. if (spaces_till_indent == 0) {
  924. spaces_till_indent = indent_size;
  925. }
  926. return spaces_till_indent;
  927. }
  928. int CodeEdit::_calculate_spaces_till_next_right_indent(int p_column) const {
  929. return indent_size - p_column % indent_size;
  930. }
  931. void CodeEdit::_new_line(bool p_split_current_line, bool p_above) {
  932. if (!is_editable()) {
  933. return;
  934. }
  935. begin_complex_operation();
  936. Vector<int> caret_edit_order = get_caret_index_edit_order();
  937. for (const int &i : caret_edit_order) {
  938. // When not splitting the line, we need to factor in indentation from the end of the current line.
  939. const int cc = p_split_current_line ? get_caret_column(i) : get_line(get_caret_line(i)).length();
  940. const int cl = get_caret_line(i);
  941. const String line = get_line(cl);
  942. String ins = "\n";
  943. // Append current indentation.
  944. int space_count = 0;
  945. int line_col = 0;
  946. for (; line_col < cc; line_col++) {
  947. if (line[line_col] == '\t') {
  948. ins += indent_text;
  949. space_count = 0;
  950. continue;
  951. }
  952. if (line[line_col] == ' ') {
  953. space_count++;
  954. if (space_count == indent_size) {
  955. ins += indent_text;
  956. space_count = 0;
  957. }
  958. continue;
  959. }
  960. break;
  961. }
  962. if (is_line_folded(cl)) {
  963. unfold_line(cl);
  964. }
  965. // Indent once again if the previous line needs it, ie ':'.
  966. // Then add an addition new line for any closing pairs aka '()'.
  967. // Skip this in comments or if we are going above.
  968. bool brace_indent = false;
  969. if (auto_indent && !p_above && cc > 0 && is_in_comment(cl) == -1) {
  970. bool should_indent = false;
  971. char32_t indent_char = ' ';
  972. for (; line_col < cc; line_col++) {
  973. char32_t c = line[line_col];
  974. if (auto_indent_prefixes.has(c) && is_in_comment(cl, line_col) == -1) {
  975. should_indent = true;
  976. indent_char = c;
  977. continue;
  978. }
  979. // Make sure this is the last char, trailing whitespace or comments are okay.
  980. // Increment column for comments because the delimiter (#) should be ignored.
  981. if (should_indent && (!is_whitespace(c) && is_in_comment(cl, line_col + 1) == -1)) {
  982. should_indent = false;
  983. }
  984. }
  985. if (should_indent) {
  986. ins += indent_text;
  987. String closing_pair = get_auto_brace_completion_close_key(String::chr(indent_char));
  988. if (!closing_pair.is_empty() && line.find(closing_pair, cc) == cc) {
  989. // No need to move the brace below if we are not taking the text with us.
  990. if (p_split_current_line) {
  991. brace_indent = true;
  992. ins += "\n" + ins.substr(indent_text.size(), ins.length() - 2);
  993. } else {
  994. brace_indent = false;
  995. ins = "\n" + ins.substr(indent_text.size(), ins.length() - 2);
  996. }
  997. }
  998. }
  999. }
  1000. bool first_line = false;
  1001. if (!p_split_current_line) {
  1002. deselect(i);
  1003. if (p_above) {
  1004. if (cl > 0) {
  1005. set_caret_line(cl - 1, false, true, 0, i);
  1006. set_caret_column(get_line(get_caret_line(i)).length(), i == 0, i);
  1007. } else {
  1008. set_caret_column(0, i == 0, i);
  1009. first_line = true;
  1010. }
  1011. } else {
  1012. set_caret_column(line.length(), i == 0, i);
  1013. }
  1014. }
  1015. insert_text_at_caret(ins, i);
  1016. if (first_line) {
  1017. set_caret_line(0, i == 0, true, 0, i);
  1018. } else if (brace_indent) {
  1019. set_caret_line(get_caret_line(i) - 1, false, true, 0, i);
  1020. set_caret_column(get_line(get_caret_line(i)).length(), i == 0, i);
  1021. }
  1022. }
  1023. end_complex_operation();
  1024. }
  1025. /* Auto brace completion */
  1026. void CodeEdit::set_auto_brace_completion_enabled(bool p_enabled) {
  1027. auto_brace_completion_enabled = p_enabled;
  1028. }
  1029. bool CodeEdit::is_auto_brace_completion_enabled() const {
  1030. return auto_brace_completion_enabled;
  1031. }
  1032. void CodeEdit::set_highlight_matching_braces_enabled(bool p_enabled) {
  1033. highlight_matching_braces_enabled = p_enabled;
  1034. queue_redraw();
  1035. }
  1036. bool CodeEdit::is_highlight_matching_braces_enabled() const {
  1037. return highlight_matching_braces_enabled;
  1038. }
  1039. void CodeEdit::add_auto_brace_completion_pair(const String &p_open_key, const String &p_close_key) {
  1040. ERR_FAIL_COND_MSG(p_open_key.is_empty(), "auto brace completion open key cannot be empty");
  1041. ERR_FAIL_COND_MSG(p_close_key.is_empty(), "auto brace completion close key cannot be empty");
  1042. for (int i = 0; i < p_open_key.length(); i++) {
  1043. ERR_FAIL_COND_MSG(!is_symbol(p_open_key[i]), "auto brace completion open key must be a symbol");
  1044. }
  1045. for (int i = 0; i < p_close_key.length(); i++) {
  1046. ERR_FAIL_COND_MSG(!is_symbol(p_close_key[i]), "auto brace completion close key must be a symbol");
  1047. }
  1048. int at = 0;
  1049. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  1050. ERR_FAIL_COND_MSG(auto_brace_completion_pairs[i].open_key == p_open_key, "auto brace completion open key '" + p_open_key + "' already exists.");
  1051. if (p_open_key.length() < auto_brace_completion_pairs[i].open_key.length()) {
  1052. at++;
  1053. }
  1054. }
  1055. BracePair brace_pair;
  1056. brace_pair.open_key = p_open_key;
  1057. brace_pair.close_key = p_close_key;
  1058. auto_brace_completion_pairs.insert(at, brace_pair);
  1059. }
  1060. void CodeEdit::set_auto_brace_completion_pairs(const Dictionary &p_auto_brace_completion_pairs) {
  1061. auto_brace_completion_pairs.clear();
  1062. Array keys = p_auto_brace_completion_pairs.keys();
  1063. for (int i = 0; i < keys.size(); i++) {
  1064. add_auto_brace_completion_pair(keys[i], p_auto_brace_completion_pairs[keys[i]]);
  1065. }
  1066. }
  1067. Dictionary CodeEdit::get_auto_brace_completion_pairs() const {
  1068. Dictionary brace_pairs;
  1069. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  1070. brace_pairs[auto_brace_completion_pairs[i].open_key] = auto_brace_completion_pairs[i].close_key;
  1071. }
  1072. return brace_pairs;
  1073. }
  1074. bool CodeEdit::has_auto_brace_completion_open_key(const String &p_open_key) const {
  1075. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  1076. if (auto_brace_completion_pairs[i].open_key == p_open_key) {
  1077. return true;
  1078. }
  1079. }
  1080. return false;
  1081. }
  1082. bool CodeEdit::has_auto_brace_completion_close_key(const String &p_close_key) const {
  1083. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  1084. if (auto_brace_completion_pairs[i].close_key == p_close_key) {
  1085. return true;
  1086. }
  1087. }
  1088. return false;
  1089. }
  1090. String CodeEdit::get_auto_brace_completion_close_key(const String &p_open_key) const {
  1091. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  1092. if (auto_brace_completion_pairs[i].open_key == p_open_key) {
  1093. return auto_brace_completion_pairs[i].close_key;
  1094. }
  1095. }
  1096. return String();
  1097. }
  1098. /* Main Gutter */
  1099. void CodeEdit::_update_draw_main_gutter() {
  1100. set_gutter_draw(main_gutter, draw_breakpoints || draw_bookmarks || draw_executing_lines);
  1101. }
  1102. void CodeEdit::set_draw_breakpoints_gutter(bool p_draw) {
  1103. draw_breakpoints = p_draw;
  1104. set_gutter_clickable(main_gutter, p_draw);
  1105. _update_draw_main_gutter();
  1106. }
  1107. bool CodeEdit::is_drawing_breakpoints_gutter() const {
  1108. return draw_breakpoints;
  1109. }
  1110. void CodeEdit::set_draw_bookmarks_gutter(bool p_draw) {
  1111. draw_bookmarks = p_draw;
  1112. _update_draw_main_gutter();
  1113. }
  1114. bool CodeEdit::is_drawing_bookmarks_gutter() const {
  1115. return draw_bookmarks;
  1116. }
  1117. void CodeEdit::set_draw_executing_lines_gutter(bool p_draw) {
  1118. draw_executing_lines = p_draw;
  1119. _update_draw_main_gutter();
  1120. }
  1121. bool CodeEdit::is_drawing_executing_lines_gutter() const {
  1122. return draw_executing_lines;
  1123. }
  1124. void CodeEdit::_main_gutter_draw_callback(int p_line, int p_gutter, const Rect2 &p_region) {
  1125. if (draw_breakpoints && theme_cache.breakpoint_icon.is_valid()) {
  1126. bool breakpointed = is_line_breakpointed(p_line);
  1127. bool hovering = p_region.has_point(get_local_mouse_pos());
  1128. bool shift_pressed = Input::get_singleton()->is_key_pressed(Key::SHIFT);
  1129. if (breakpointed || (hovering && !is_dragging_cursor() && !shift_pressed)) {
  1130. int padding = p_region.size.x / 6;
  1131. Color use_color = theme_cache.breakpoint_color;
  1132. if (hovering && !shift_pressed) {
  1133. use_color = breakpointed ? use_color.lightened(0.3) : use_color.darkened(0.5);
  1134. }
  1135. Rect2 icon_region = p_region;
  1136. icon_region.position += Point2(padding, padding);
  1137. icon_region.size -= Point2(padding, padding) * 2;
  1138. theme_cache.breakpoint_icon->draw_rect(get_canvas_item(), icon_region, false, use_color);
  1139. }
  1140. }
  1141. if (draw_bookmarks && theme_cache.bookmark_icon.is_valid()) {
  1142. bool bookmarked = is_line_bookmarked(p_line);
  1143. bool hovering = p_region.has_point(get_local_mouse_pos());
  1144. bool shift_pressed = Input::get_singleton()->is_key_pressed(Key::SHIFT);
  1145. if (bookmarked || (hovering && !is_dragging_cursor() && shift_pressed)) {
  1146. int horizontal_padding = p_region.size.x / 2;
  1147. int vertical_padding = p_region.size.y / 4;
  1148. Color use_color = theme_cache.bookmark_color;
  1149. if (hovering && shift_pressed) {
  1150. use_color = bookmarked ? use_color.lightened(0.3) : use_color.darkened(0.5);
  1151. }
  1152. Rect2 icon_region = p_region;
  1153. icon_region.position += Point2(horizontal_padding, 0);
  1154. icon_region.size -= Point2(horizontal_padding * 1.1, vertical_padding);
  1155. theme_cache.bookmark_icon->draw_rect(get_canvas_item(), icon_region, false, use_color);
  1156. }
  1157. }
  1158. if (draw_executing_lines && is_line_executing(p_line) && theme_cache.executing_line_icon.is_valid()) {
  1159. int horizontal_padding = p_region.size.x / 10;
  1160. int vertical_padding = p_region.size.y / 4;
  1161. Rect2 icon_region = p_region;
  1162. icon_region.position += Point2(horizontal_padding, vertical_padding);
  1163. icon_region.size -= Point2(horizontal_padding, vertical_padding) * 2;
  1164. theme_cache.executing_line_icon->draw_rect(get_canvas_item(), icon_region, false, theme_cache.executing_line_color);
  1165. }
  1166. }
  1167. // Breakpoints
  1168. void CodeEdit::set_line_as_breakpoint(int p_line, bool p_breakpointed) {
  1169. ERR_FAIL_INDEX(p_line, get_line_count());
  1170. int mask = get_line_gutter_metadata(p_line, main_gutter);
  1171. set_line_gutter_metadata(p_line, main_gutter, p_breakpointed ? mask | MAIN_GUTTER_BREAKPOINT : mask & ~MAIN_GUTTER_BREAKPOINT);
  1172. if (p_breakpointed) {
  1173. breakpointed_lines[p_line] = true;
  1174. } else if (breakpointed_lines.has(p_line)) {
  1175. breakpointed_lines.erase(p_line);
  1176. }
  1177. emit_signal(SNAME("breakpoint_toggled"), p_line);
  1178. queue_redraw();
  1179. }
  1180. bool CodeEdit::is_line_breakpointed(int p_line) const {
  1181. return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_BREAKPOINT;
  1182. }
  1183. void CodeEdit::clear_breakpointed_lines() {
  1184. for (int i = 0; i < get_line_count(); i++) {
  1185. if (is_line_breakpointed(i)) {
  1186. set_line_as_breakpoint(i, false);
  1187. }
  1188. }
  1189. }
  1190. PackedInt32Array CodeEdit::get_breakpointed_lines() const {
  1191. PackedInt32Array ret;
  1192. for (int i = 0; i < get_line_count(); i++) {
  1193. if (is_line_breakpointed(i)) {
  1194. ret.append(i);
  1195. }
  1196. }
  1197. return ret;
  1198. }
  1199. // Bookmarks
  1200. void CodeEdit::set_line_as_bookmarked(int p_line, bool p_bookmarked) {
  1201. int mask = get_line_gutter_metadata(p_line, main_gutter);
  1202. set_line_gutter_metadata(p_line, main_gutter, p_bookmarked ? mask | MAIN_GUTTER_BOOKMARK : mask & ~MAIN_GUTTER_BOOKMARK);
  1203. queue_redraw();
  1204. }
  1205. bool CodeEdit::is_line_bookmarked(int p_line) const {
  1206. return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_BOOKMARK;
  1207. }
  1208. void CodeEdit::clear_bookmarked_lines() {
  1209. for (int i = 0; i < get_line_count(); i++) {
  1210. if (is_line_bookmarked(i)) {
  1211. set_line_as_bookmarked(i, false);
  1212. }
  1213. }
  1214. }
  1215. PackedInt32Array CodeEdit::get_bookmarked_lines() const {
  1216. PackedInt32Array ret;
  1217. for (int i = 0; i < get_line_count(); i++) {
  1218. if (is_line_bookmarked(i)) {
  1219. ret.append(i);
  1220. }
  1221. }
  1222. return ret;
  1223. }
  1224. // Executing lines
  1225. void CodeEdit::set_line_as_executing(int p_line, bool p_executing) {
  1226. int mask = get_line_gutter_metadata(p_line, main_gutter);
  1227. set_line_gutter_metadata(p_line, main_gutter, p_executing ? mask | MAIN_GUTTER_EXECUTING : mask & ~MAIN_GUTTER_EXECUTING);
  1228. queue_redraw();
  1229. }
  1230. bool CodeEdit::is_line_executing(int p_line) const {
  1231. return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_EXECUTING;
  1232. }
  1233. void CodeEdit::clear_executing_lines() {
  1234. for (int i = 0; i < get_line_count(); i++) {
  1235. if (is_line_executing(i)) {
  1236. set_line_as_executing(i, false);
  1237. }
  1238. }
  1239. }
  1240. PackedInt32Array CodeEdit::get_executing_lines() const {
  1241. PackedInt32Array ret;
  1242. for (int i = 0; i < get_line_count(); i++) {
  1243. if (is_line_executing(i)) {
  1244. ret.append(i);
  1245. }
  1246. }
  1247. return ret;
  1248. }
  1249. /* Line numbers */
  1250. void CodeEdit::set_draw_line_numbers(bool p_draw) {
  1251. set_gutter_draw(line_number_gutter, p_draw);
  1252. }
  1253. bool CodeEdit::is_draw_line_numbers_enabled() const {
  1254. return is_gutter_drawn(line_number_gutter);
  1255. }
  1256. void CodeEdit::set_line_numbers_zero_padded(bool p_zero_padded) {
  1257. p_zero_padded ? line_number_padding = "0" : line_number_padding = " ";
  1258. queue_redraw();
  1259. }
  1260. bool CodeEdit::is_line_numbers_zero_padded() const {
  1261. return line_number_padding == "0";
  1262. }
  1263. void CodeEdit::_line_number_draw_callback(int p_line, int p_gutter, const Rect2 &p_region) {
  1264. String fc = String::num(p_line + 1).lpad(line_number_digits, line_number_padding);
  1265. if (is_localizing_numeral_system()) {
  1266. fc = TS->format_number(fc);
  1267. }
  1268. Ref<TextLine> tl;
  1269. tl.instantiate();
  1270. tl->add_string(fc, theme_cache.font, theme_cache.font_size);
  1271. int yofs = p_region.position.y + (get_line_height() - tl->get_size().y) / 2;
  1272. Color number_color = get_line_gutter_item_color(p_line, line_number_gutter);
  1273. if (number_color == Color(1, 1, 1)) {
  1274. number_color = theme_cache.line_number_color;
  1275. }
  1276. tl->draw(get_canvas_item(), Point2(p_region.position.x, yofs), number_color);
  1277. }
  1278. /* Fold Gutter */
  1279. void CodeEdit::set_draw_fold_gutter(bool p_draw) {
  1280. set_gutter_draw(fold_gutter, p_draw);
  1281. }
  1282. bool CodeEdit::is_drawing_fold_gutter() const {
  1283. return is_gutter_drawn(fold_gutter);
  1284. }
  1285. void CodeEdit::_fold_gutter_draw_callback(int p_line, int p_gutter, Rect2 p_region) {
  1286. if (!can_fold_line(p_line) && !is_line_folded(p_line)) {
  1287. set_line_gutter_clickable(p_line, fold_gutter, false);
  1288. return;
  1289. }
  1290. set_line_gutter_clickable(p_line, fold_gutter, true);
  1291. int horizontal_padding = p_region.size.x / 10;
  1292. int vertical_padding = p_region.size.y / 6;
  1293. p_region.position += Point2(horizontal_padding, vertical_padding);
  1294. p_region.size -= Point2(horizontal_padding, vertical_padding) * 2;
  1295. bool can_fold = can_fold_line(p_line);
  1296. if (is_line_code_region_start(p_line)) {
  1297. Color region_icon_color = theme_cache.folded_code_region_color;
  1298. region_icon_color.a = MAX(region_icon_color.a, 0.4f);
  1299. if (can_fold) {
  1300. theme_cache.can_fold_code_region_icon->draw_rect(get_canvas_item(), p_region, false, region_icon_color);
  1301. } else {
  1302. theme_cache.folded_code_region_icon->draw_rect(get_canvas_item(), p_region, false, region_icon_color);
  1303. }
  1304. return;
  1305. }
  1306. if (can_fold) {
  1307. theme_cache.can_fold_icon->draw_rect(get_canvas_item(), p_region, false, theme_cache.code_folding_color);
  1308. return;
  1309. }
  1310. theme_cache.folded_icon->draw_rect(get_canvas_item(), p_region, false, theme_cache.code_folding_color);
  1311. }
  1312. /* Line Folding */
  1313. void CodeEdit::set_line_folding_enabled(bool p_enabled) {
  1314. line_folding_enabled = p_enabled;
  1315. _set_hiding_enabled(p_enabled);
  1316. }
  1317. bool CodeEdit::is_line_folding_enabled() const {
  1318. return line_folding_enabled;
  1319. }
  1320. bool CodeEdit::can_fold_line(int p_line) const {
  1321. ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
  1322. if (!line_folding_enabled) {
  1323. return false;
  1324. }
  1325. if (p_line + 1 >= get_line_count() || get_line(p_line).strip_edges().size() == 0) {
  1326. return false;
  1327. }
  1328. if (_is_line_hidden(p_line) || is_line_folded(p_line)) {
  1329. return false;
  1330. }
  1331. // Check for code region.
  1332. if (is_line_code_region_end(p_line)) {
  1333. return false;
  1334. }
  1335. if (is_line_code_region_start(p_line)) {
  1336. int region_level = 0;
  1337. // Check if there is a valid end region tag.
  1338. for (int next_line = p_line + 1; next_line < get_line_count(); next_line++) {
  1339. if (is_line_code_region_end(next_line)) {
  1340. region_level -= 1;
  1341. if (region_level == -1) {
  1342. return true;
  1343. }
  1344. }
  1345. if (is_line_code_region_start(next_line)) {
  1346. region_level += 1;
  1347. }
  1348. }
  1349. return false;
  1350. }
  1351. /* Check for full multiline line or block strings / comments. */
  1352. int in_comment = is_in_comment(p_line);
  1353. int in_string = (in_comment == -1) ? is_in_string(p_line) : -1;
  1354. if (in_string != -1 || in_comment != -1) {
  1355. if (get_delimiter_start_position(p_line, get_line(p_line).size() - 1).y != p_line) {
  1356. return false;
  1357. }
  1358. int delimiter_end_line = get_delimiter_end_position(p_line, get_line(p_line).size() - 1).y;
  1359. /* No end line, therefore we have a multiline region over the rest of the file. */
  1360. if (delimiter_end_line == -1) {
  1361. return true;
  1362. }
  1363. /* End line is the same therefore we have a block. */
  1364. if (delimiter_end_line == p_line) {
  1365. /* Check we are the start of the block. */
  1366. if (p_line - 1 >= 0) {
  1367. if ((in_string != -1 && is_in_string(p_line - 1) != -1) || (in_comment != -1 && is_in_comment(p_line - 1) != -1)) {
  1368. return false;
  1369. }
  1370. }
  1371. /* Check it continues for at least one line. */
  1372. return ((in_string != -1 && is_in_string(p_line + 1) != -1) || (in_comment != -1 && is_in_comment(p_line + 1) != -1));
  1373. }
  1374. return ((in_string != -1 && is_in_string(delimiter_end_line) != -1) || (in_comment != -1 && is_in_comment(delimiter_end_line) != -1));
  1375. }
  1376. /* Otherwise check indent levels. */
  1377. int start_indent = get_indent_level(p_line);
  1378. for (int i = p_line + 1; i < get_line_count(); i++) {
  1379. if (is_in_string(i) != -1 || is_in_comment(i) != -1 || get_line(i).strip_edges().size() == 0) {
  1380. continue;
  1381. }
  1382. return (get_indent_level(i) > start_indent);
  1383. }
  1384. return false;
  1385. }
  1386. void CodeEdit::fold_line(int p_line) {
  1387. ERR_FAIL_INDEX(p_line, get_line_count());
  1388. if (!is_line_folding_enabled() || !can_fold_line(p_line)) {
  1389. return;
  1390. }
  1391. /* Find the last line to be hidden. */
  1392. const int line_count = get_line_count() - 1;
  1393. int end_line = line_count;
  1394. // Fold code region.
  1395. if (is_line_code_region_start(p_line)) {
  1396. int region_level = 0;
  1397. for (int endregion_line = p_line + 1; endregion_line < get_line_count(); endregion_line++) {
  1398. if (is_line_code_region_start(endregion_line)) {
  1399. region_level += 1;
  1400. }
  1401. if (is_line_code_region_end(endregion_line)) {
  1402. region_level -= 1;
  1403. if (region_level == -1) {
  1404. end_line = endregion_line;
  1405. break;
  1406. }
  1407. }
  1408. }
  1409. set_line_background_color(p_line, theme_cache.folded_code_region_color);
  1410. }
  1411. int in_comment = is_in_comment(p_line);
  1412. int in_string = (in_comment == -1) ? is_in_string(p_line) : -1;
  1413. if (!is_line_code_region_start(p_line)) {
  1414. if (in_string != -1 || in_comment != -1) {
  1415. end_line = get_delimiter_end_position(p_line, get_line(p_line).size() - 1).y;
  1416. // End line is the same therefore we have a block of single line delimiters.
  1417. if (end_line == p_line) {
  1418. for (int i = p_line + 1; i <= line_count; i++) {
  1419. if ((in_string != -1 && is_in_string(i) == -1) || (in_comment != -1 && is_in_comment(i) == -1)) {
  1420. break;
  1421. }
  1422. end_line = i;
  1423. }
  1424. }
  1425. } else {
  1426. int start_indent = get_indent_level(p_line);
  1427. for (int i = p_line + 1; i <= line_count; i++) {
  1428. if (get_line(i).strip_edges().size() == 0) {
  1429. continue;
  1430. }
  1431. if (get_indent_level(i) > start_indent) {
  1432. end_line = i;
  1433. continue;
  1434. }
  1435. if (is_in_string(i) == -1 && is_in_comment(i) == -1) {
  1436. break;
  1437. }
  1438. }
  1439. }
  1440. }
  1441. for (int i = p_line + 1; i <= end_line; i++) {
  1442. _set_line_as_hidden(i, true);
  1443. }
  1444. for (int i = 0; i < get_caret_count(); i++) {
  1445. // Fix selection.
  1446. if (has_selection(i)) {
  1447. if (_is_line_hidden(get_selection_from_line(i)) && _is_line_hidden(get_selection_to_line(i))) {
  1448. deselect(i);
  1449. } else if (_is_line_hidden(get_selection_from_line(i))) {
  1450. select(p_line, 9999, get_selection_to_line(i), get_selection_to_column(i), i);
  1451. } else if (_is_line_hidden(get_selection_to_line(i))) {
  1452. select(get_selection_from_line(i), get_selection_from_column(i), p_line, 9999, i);
  1453. }
  1454. }
  1455. // Reset caret.
  1456. if (_is_line_hidden(get_caret_line(i))) {
  1457. set_caret_line(p_line, false, false, 0, i);
  1458. set_caret_column(get_line(p_line).length(), false, i);
  1459. }
  1460. }
  1461. merge_overlapping_carets();
  1462. queue_redraw();
  1463. }
  1464. void CodeEdit::unfold_line(int p_line) {
  1465. ERR_FAIL_INDEX(p_line, get_line_count());
  1466. if (!is_line_folded(p_line) && !_is_line_hidden(p_line)) {
  1467. return;
  1468. }
  1469. int fold_start = p_line;
  1470. for (; fold_start > 0; fold_start--) {
  1471. if (is_line_folded(fold_start)) {
  1472. break;
  1473. }
  1474. }
  1475. fold_start = is_line_folded(fold_start) ? fold_start : p_line;
  1476. for (int i = fold_start + 1; i < get_line_count(); i++) {
  1477. if (!_is_line_hidden(i)) {
  1478. break;
  1479. }
  1480. _set_line_as_hidden(i, false);
  1481. if (is_line_code_region_start(i - 1)) {
  1482. set_line_background_color(i - 1, Color(0.0, 0.0, 0.0, 0.0));
  1483. }
  1484. }
  1485. queue_redraw();
  1486. }
  1487. void CodeEdit::fold_all_lines() {
  1488. for (int i = 0; i < get_line_count(); i++) {
  1489. fold_line(i);
  1490. }
  1491. queue_redraw();
  1492. }
  1493. void CodeEdit::unfold_all_lines() {
  1494. _unhide_all_lines();
  1495. }
  1496. void CodeEdit::toggle_foldable_line(int p_line) {
  1497. ERR_FAIL_INDEX(p_line, get_line_count());
  1498. if (is_line_folded(p_line)) {
  1499. unfold_line(p_line);
  1500. return;
  1501. }
  1502. fold_line(p_line);
  1503. }
  1504. bool CodeEdit::is_line_folded(int p_line) const {
  1505. ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
  1506. return p_line + 1 < get_line_count() && !_is_line_hidden(p_line) && _is_line_hidden(p_line + 1);
  1507. }
  1508. TypedArray<int> CodeEdit::get_folded_lines() const {
  1509. TypedArray<int> folded_lines;
  1510. for (int i = 0; i < get_line_count(); i++) {
  1511. if (is_line_folded(i)) {
  1512. folded_lines.push_back(i);
  1513. }
  1514. }
  1515. return folded_lines;
  1516. }
  1517. /* Code region */
  1518. void CodeEdit::create_code_region() {
  1519. // Abort if there is no selected text.
  1520. if (!has_selection()) {
  1521. return;
  1522. }
  1523. // Check that region tag find a comment delimiter and is valid.
  1524. if (code_region_start_string.is_empty()) {
  1525. WARN_PRINT_ONCE("Cannot create code region without any one line comment delimiters");
  1526. return;
  1527. }
  1528. begin_complex_operation();
  1529. // Merge selections if selection starts on the same line the previous one ends.
  1530. Vector<int> caret_edit_order = get_caret_index_edit_order();
  1531. Vector<int> carets_to_remove;
  1532. for (int i = 1; i < caret_edit_order.size(); i++) {
  1533. int current_caret = caret_edit_order[i - 1];
  1534. int next_caret = caret_edit_order[i];
  1535. if (get_selection_from_line(current_caret) == get_selection_to_line(next_caret)) {
  1536. select(get_selection_from_line(next_caret), get_selection_from_column(next_caret), get_selection_to_line(current_caret), get_selection_to_column(current_caret), next_caret);
  1537. carets_to_remove.append(current_caret);
  1538. }
  1539. }
  1540. // Sort and remove backwards to preserve indices.
  1541. carets_to_remove.sort();
  1542. for (int i = carets_to_remove.size() - 1; i >= 0; i--) {
  1543. remove_caret(carets_to_remove[i]);
  1544. }
  1545. // Adding start and end region tags.
  1546. int first_region_start = -1;
  1547. for (int caret_idx : get_caret_index_edit_order()) {
  1548. if (!has_selection(caret_idx)) {
  1549. continue;
  1550. }
  1551. int from_line = get_selection_from_line(caret_idx);
  1552. if (first_region_start == -1 || from_line < first_region_start) {
  1553. first_region_start = from_line;
  1554. }
  1555. int to_line = get_selection_to_line(caret_idx);
  1556. set_line(to_line, get_line(to_line) + "\n" + code_region_end_string);
  1557. insert_line_at(from_line, code_region_start_string + " " + RTR("New Code Region"));
  1558. fold_line(from_line);
  1559. }
  1560. // Select name of the first region to allow quick edit.
  1561. remove_secondary_carets();
  1562. set_caret_line(first_region_start);
  1563. int tag_length = code_region_start_string.length() + RTR("New Code Region").length() + 1;
  1564. set_caret_column(tag_length);
  1565. select(first_region_start, code_region_start_string.length() + 1, first_region_start, tag_length);
  1566. end_complex_operation();
  1567. queue_redraw();
  1568. }
  1569. String CodeEdit::get_code_region_start_tag() const {
  1570. return code_region_start_tag;
  1571. }
  1572. String CodeEdit::get_code_region_end_tag() const {
  1573. return code_region_end_tag;
  1574. }
  1575. void CodeEdit::set_code_region_tags(const String &p_start, const String &p_end) {
  1576. ERR_FAIL_COND_MSG(p_start == p_end, "Starting and ending region tags cannot be identical.");
  1577. ERR_FAIL_COND_MSG(p_start.is_empty(), "Starting region tag cannot be empty.");
  1578. ERR_FAIL_COND_MSG(p_end.is_empty(), "Ending region tag cannot be empty.");
  1579. code_region_start_tag = p_start;
  1580. code_region_end_tag = p_end;
  1581. _update_code_region_tags();
  1582. }
  1583. bool CodeEdit::is_line_code_region_start(int p_line) const {
  1584. ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
  1585. if (code_region_start_string.is_empty()) {
  1586. return false;
  1587. }
  1588. return get_line(p_line).strip_edges().begins_with(code_region_start_string);
  1589. }
  1590. bool CodeEdit::is_line_code_region_end(int p_line) const {
  1591. ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
  1592. if (code_region_start_string.is_empty()) {
  1593. return false;
  1594. }
  1595. return get_line(p_line).strip_edges().begins_with(code_region_end_string);
  1596. }
  1597. /* Delimiters */
  1598. // Strings
  1599. void CodeEdit::add_string_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only) {
  1600. _add_delimiter(p_start_key, p_end_key, p_line_only, TYPE_STRING);
  1601. }
  1602. void CodeEdit::remove_string_delimiter(const String &p_start_key) {
  1603. _remove_delimiter(p_start_key, TYPE_STRING);
  1604. }
  1605. bool CodeEdit::has_string_delimiter(const String &p_start_key) const {
  1606. return _has_delimiter(p_start_key, TYPE_STRING);
  1607. }
  1608. void CodeEdit::set_string_delimiters(const TypedArray<String> &p_string_delimiters) {
  1609. _set_delimiters(p_string_delimiters, TYPE_STRING);
  1610. }
  1611. void CodeEdit::clear_string_delimiters() {
  1612. _clear_delimiters(TYPE_STRING);
  1613. }
  1614. TypedArray<String> CodeEdit::get_string_delimiters() const {
  1615. return _get_delimiters(TYPE_STRING);
  1616. }
  1617. int CodeEdit::is_in_string(int p_line, int p_column) const {
  1618. return _is_in_delimiter(p_line, p_column, TYPE_STRING);
  1619. }
  1620. // Comments
  1621. void CodeEdit::add_comment_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only) {
  1622. _add_delimiter(p_start_key, p_end_key, p_line_only, TYPE_COMMENT);
  1623. }
  1624. void CodeEdit::remove_comment_delimiter(const String &p_start_key) {
  1625. _remove_delimiter(p_start_key, TYPE_COMMENT);
  1626. }
  1627. bool CodeEdit::has_comment_delimiter(const String &p_start_key) const {
  1628. return _has_delimiter(p_start_key, TYPE_COMMENT);
  1629. }
  1630. void CodeEdit::set_comment_delimiters(const TypedArray<String> &p_comment_delimiters) {
  1631. _set_delimiters(p_comment_delimiters, TYPE_COMMENT);
  1632. }
  1633. void CodeEdit::clear_comment_delimiters() {
  1634. _clear_delimiters(TYPE_COMMENT);
  1635. }
  1636. TypedArray<String> CodeEdit::get_comment_delimiters() const {
  1637. return _get_delimiters(TYPE_COMMENT);
  1638. }
  1639. int CodeEdit::is_in_comment(int p_line, int p_column) const {
  1640. return _is_in_delimiter(p_line, p_column, TYPE_COMMENT);
  1641. }
  1642. String CodeEdit::get_delimiter_start_key(int p_delimiter_idx) const {
  1643. ERR_FAIL_INDEX_V(p_delimiter_idx, delimiters.size(), "");
  1644. return delimiters[p_delimiter_idx].start_key;
  1645. }
  1646. String CodeEdit::get_delimiter_end_key(int p_delimiter_idx) const {
  1647. ERR_FAIL_INDEX_V(p_delimiter_idx, delimiters.size(), "");
  1648. return delimiters[p_delimiter_idx].end_key;
  1649. }
  1650. Point2 CodeEdit::get_delimiter_start_position(int p_line, int p_column) const {
  1651. if (delimiters.size() == 0) {
  1652. return Point2(-1, -1);
  1653. }
  1654. ERR_FAIL_INDEX_V(p_line, get_line_count(), Point2(-1, -1));
  1655. ERR_FAIL_COND_V(p_column - 1 > get_line(p_line).size(), Point2(-1, -1));
  1656. Point2 start_position;
  1657. start_position.y = -1;
  1658. start_position.x = -1;
  1659. bool in_region = ((p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->get()) != -1;
  1660. /* Check the keys for this line. */
  1661. for (const KeyValue<int, int> &E : delimiter_cache[p_line]) {
  1662. if (E.key > p_column) {
  1663. break;
  1664. }
  1665. in_region = E.value != -1;
  1666. start_position.x = in_region ? E.key : -1;
  1667. }
  1668. /* Region was found on this line and is not a multiline continuation. */
  1669. int line_length = get_line(p_line).length();
  1670. if (start_position.x != -1 && line_length > 0 && start_position.x != line_length + 1) {
  1671. start_position.y = p_line;
  1672. return start_position;
  1673. }
  1674. /* Not in a region */
  1675. if (!in_region) {
  1676. return start_position;
  1677. }
  1678. /* Region starts on a previous line */
  1679. for (int i = p_line - 1; i >= 0; i--) {
  1680. if (delimiter_cache[i].size() < 1) {
  1681. continue;
  1682. }
  1683. start_position.y = i;
  1684. start_position.x = delimiter_cache[i].back()->key();
  1685. /* Make sure it's not a multiline continuation. */
  1686. line_length = get_line(i).length();
  1687. if (line_length > 0 && start_position.x != line_length + 1) {
  1688. break;
  1689. }
  1690. }
  1691. return start_position;
  1692. }
  1693. Point2 CodeEdit::get_delimiter_end_position(int p_line, int p_column) const {
  1694. if (delimiters.size() == 0) {
  1695. return Point2(-1, -1);
  1696. }
  1697. ERR_FAIL_INDEX_V(p_line, get_line_count(), Point2(-1, -1));
  1698. ERR_FAIL_COND_V(p_column - 1 > get_line(p_line).size(), Point2(-1, -1));
  1699. Point2 end_position;
  1700. end_position.y = -1;
  1701. end_position.x = -1;
  1702. int region = (p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->value();
  1703. /* Check the keys for this line. */
  1704. for (const KeyValue<int, int> &E : delimiter_cache[p_line]) {
  1705. end_position.x = (E.value == -1) ? E.key : -1;
  1706. if (E.key > p_column) {
  1707. break;
  1708. }
  1709. region = E.value;
  1710. }
  1711. /* Region was found on this line and is not a multiline continuation. */
  1712. if (region != -1 && end_position.x != -1 && (delimiters[region].line_only || end_position.x != get_line(p_line).length() + 1)) {
  1713. end_position.y = p_line;
  1714. return end_position;
  1715. }
  1716. /* Not in a region */
  1717. if (region == -1) {
  1718. end_position.x = -1;
  1719. return end_position;
  1720. }
  1721. /* Region ends on a later line */
  1722. for (int i = p_line + 1; i < get_line_count(); i++) {
  1723. if (delimiter_cache[i].size() < 1 || delimiter_cache[i].front()->value() != -1) {
  1724. continue;
  1725. }
  1726. end_position.x = delimiter_cache[i].front()->key();
  1727. /* Make sure it's not a multiline continuation. */
  1728. if (get_line(i).length() > 0 && end_position.x != get_line(i).length() + 1) {
  1729. end_position.y = i;
  1730. break;
  1731. }
  1732. end_position.x = -1;
  1733. }
  1734. return end_position;
  1735. }
  1736. /* Code hint */
  1737. void CodeEdit::set_code_hint(const String &p_hint) {
  1738. code_hint = p_hint;
  1739. code_hint_xpos = -0xFFFF;
  1740. queue_redraw();
  1741. }
  1742. void CodeEdit::set_code_hint_draw_below(bool p_below) {
  1743. code_hint_draw_below = p_below;
  1744. queue_redraw();
  1745. }
  1746. /* Code Completion */
  1747. void CodeEdit::set_code_completion_enabled(bool p_enable) {
  1748. code_completion_enabled = p_enable;
  1749. }
  1750. bool CodeEdit::is_code_completion_enabled() const {
  1751. return code_completion_enabled;
  1752. }
  1753. void CodeEdit::set_code_completion_prefixes(const TypedArray<String> &p_prefixes) {
  1754. code_completion_prefixes.clear();
  1755. for (int i = 0; i < p_prefixes.size(); i++) {
  1756. const String prefix = p_prefixes[i];
  1757. ERR_CONTINUE_MSG(prefix.is_empty(), "Code completion prefix cannot be empty.");
  1758. code_completion_prefixes.insert(prefix[0]);
  1759. }
  1760. }
  1761. TypedArray<String> CodeEdit::get_code_completion_prefixes() const {
  1762. TypedArray<String> prefixes;
  1763. for (const char32_t &E : code_completion_prefixes) {
  1764. prefixes.push_back(String::chr(E));
  1765. }
  1766. return prefixes;
  1767. }
  1768. String CodeEdit::get_text_for_code_completion() const {
  1769. StringBuilder completion_text;
  1770. const int text_size = get_line_count();
  1771. for (int i = 0; i < text_size; i++) {
  1772. String line = get_line(i);
  1773. if (i == get_caret_line()) {
  1774. completion_text += line.substr(0, get_caret_column());
  1775. /* Not unicode, represents the caret. */
  1776. completion_text += String::chr(0xFFFF);
  1777. completion_text += line.substr(get_caret_column(), line.size());
  1778. } else {
  1779. completion_text += line;
  1780. }
  1781. if (i != text_size - 1) {
  1782. completion_text += "\n";
  1783. }
  1784. }
  1785. return completion_text.as_string();
  1786. }
  1787. void CodeEdit::request_code_completion(bool p_force) {
  1788. if (GDVIRTUAL_CALL(_request_code_completion, p_force)) {
  1789. return;
  1790. }
  1791. /* Don't re-query if all existing options are quoted types, eg path, signal. */
  1792. bool ignored = code_completion_active && !code_completion_options.is_empty();
  1793. if (ignored) {
  1794. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT;
  1795. const ScriptLanguage::CodeCompletionOption *previous_option = nullptr;
  1796. for (int i = 0; i < code_completion_options.size(); i++) {
  1797. const ScriptLanguage::CodeCompletionOption &current_option = code_completion_options[i];
  1798. if (!previous_option) {
  1799. previous_option = &current_option;
  1800. kind = current_option.kind;
  1801. }
  1802. if (previous_option->kind != current_option.kind) {
  1803. ignored = false;
  1804. break;
  1805. }
  1806. }
  1807. ignored = ignored && (kind == ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH || kind == ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH || kind == ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL);
  1808. }
  1809. if (ignored) {
  1810. return;
  1811. }
  1812. if (p_force) {
  1813. emit_signal(SNAME("code_completion_requested"));
  1814. return;
  1815. }
  1816. String line = get_line(get_caret_line());
  1817. int ofs = CLAMP(get_caret_column(), 0, line.length());
  1818. if (ofs > 0 && (is_in_string(get_caret_line(), ofs) != -1 || !is_symbol(line[ofs - 1]) || code_completion_prefixes.has(line[ofs - 1]))) {
  1819. emit_signal(SNAME("code_completion_requested"));
  1820. } else if (ofs > 1 && line[ofs - 1] == ' ' && code_completion_prefixes.has(line[ofs - 2])) {
  1821. emit_signal(SNAME("code_completion_requested"));
  1822. }
  1823. }
  1824. void CodeEdit::add_code_completion_option(CodeCompletionKind p_type, const String &p_display_text, const String &p_insert_text, const Color &p_text_color, const Ref<Resource> &p_icon, const Variant &p_value, int p_location) {
  1825. ScriptLanguage::CodeCompletionOption completion_option;
  1826. completion_option.kind = (ScriptLanguage::CodeCompletionKind)p_type;
  1827. completion_option.display = p_display_text;
  1828. completion_option.insert_text = p_insert_text;
  1829. completion_option.font_color = p_text_color;
  1830. completion_option.icon = p_icon;
  1831. completion_option.default_value = p_value;
  1832. completion_option.location = p_location;
  1833. code_completion_option_submitted.push_back(completion_option);
  1834. }
  1835. void CodeEdit::update_code_completion_options(bool p_forced) {
  1836. code_completion_forced = p_forced;
  1837. code_completion_option_sources = code_completion_option_submitted;
  1838. code_completion_option_submitted.clear();
  1839. _filter_code_completion_candidates_impl();
  1840. }
  1841. TypedArray<Dictionary> CodeEdit::get_code_completion_options() const {
  1842. if (!code_completion_active) {
  1843. return TypedArray<Dictionary>();
  1844. }
  1845. TypedArray<Dictionary> completion_options;
  1846. completion_options.resize(code_completion_options.size());
  1847. for (int i = 0; i < code_completion_options.size(); i++) {
  1848. Dictionary option;
  1849. option["kind"] = code_completion_options[i].kind;
  1850. option["display_text"] = code_completion_options[i].display;
  1851. option["insert_text"] = code_completion_options[i].insert_text;
  1852. option["font_color"] = code_completion_options[i].font_color;
  1853. option["icon"] = code_completion_options[i].icon;
  1854. option["location"] = code_completion_options[i].location;
  1855. option["default_value"] = code_completion_options[i].default_value;
  1856. completion_options[i] = option;
  1857. }
  1858. return completion_options;
  1859. }
  1860. Dictionary CodeEdit::get_code_completion_option(int p_index) const {
  1861. if (!code_completion_active) {
  1862. return Dictionary();
  1863. }
  1864. ERR_FAIL_INDEX_V(p_index, code_completion_options.size(), Dictionary());
  1865. Dictionary option;
  1866. option["kind"] = code_completion_options[p_index].kind;
  1867. option["display_text"] = code_completion_options[p_index].display;
  1868. option["insert_text"] = code_completion_options[p_index].insert_text;
  1869. option["font_color"] = code_completion_options[p_index].font_color;
  1870. option["icon"] = code_completion_options[p_index].icon;
  1871. option["location"] = code_completion_options[p_index].location;
  1872. option["default_value"] = code_completion_options[p_index].default_value;
  1873. return option;
  1874. }
  1875. int CodeEdit::get_code_completion_selected_index() const {
  1876. return (code_completion_active) ? code_completion_current_selected : -1;
  1877. }
  1878. void CodeEdit::set_code_completion_selected_index(int p_index) {
  1879. if (!code_completion_active) {
  1880. return;
  1881. }
  1882. ERR_FAIL_INDEX(p_index, code_completion_options.size());
  1883. code_completion_current_selected = p_index;
  1884. code_completion_force_item_center = -1;
  1885. queue_redraw();
  1886. }
  1887. void CodeEdit::confirm_code_completion(bool p_replace) {
  1888. if (!is_editable() || !code_completion_active) {
  1889. return;
  1890. }
  1891. if (GDVIRTUAL_CALL(_confirm_code_completion, p_replace)) {
  1892. return;
  1893. }
  1894. char32_t caret_last_completion_char = 0;
  1895. begin_complex_operation();
  1896. Vector<int> caret_edit_order = get_caret_index_edit_order();
  1897. for (const int &i : caret_edit_order) {
  1898. int caret_line = get_caret_line(i);
  1899. const String &insert_text = code_completion_options[code_completion_current_selected].insert_text;
  1900. const String &display_text = code_completion_options[code_completion_current_selected].display;
  1901. if (p_replace) {
  1902. // Find end of current section.
  1903. const String line = get_line(caret_line);
  1904. int caret_col = get_caret_column(i);
  1905. int caret_remove_line = caret_line;
  1906. bool merge_text = true;
  1907. int in_string = is_in_string(caret_line, caret_col);
  1908. if (in_string != -1) {
  1909. Point2 string_end = get_delimiter_end_position(caret_line, caret_col);
  1910. if (string_end.x != -1) {
  1911. merge_text = false;
  1912. caret_remove_line = string_end.y;
  1913. caret_col = string_end.x - 1;
  1914. }
  1915. }
  1916. if (merge_text) {
  1917. for (; caret_col < line.length(); caret_col++) {
  1918. if (is_symbol(line[caret_col])) {
  1919. break;
  1920. }
  1921. }
  1922. }
  1923. // Replace.
  1924. remove_text(caret_line, get_caret_column(i) - code_completion_base.length(), caret_remove_line, caret_col);
  1925. adjust_carets_after_edit(i, caret_line, caret_col - code_completion_base.length(), caret_remove_line, caret_col);
  1926. set_caret_column(get_caret_column(i) - code_completion_base.length(), false, i);
  1927. insert_text_at_caret(insert_text, i);
  1928. } else {
  1929. // Get first non-matching char.
  1930. const String line = get_line(caret_line);
  1931. int caret_col = get_caret_column(i);
  1932. int matching_chars = code_completion_base.length();
  1933. for (; matching_chars <= insert_text.length(); matching_chars++) {
  1934. if (caret_col >= line.length() || line[caret_col] != insert_text[matching_chars]) {
  1935. break;
  1936. }
  1937. caret_col++;
  1938. }
  1939. // Remove base completion text.
  1940. remove_text(caret_line, get_caret_column(i) - code_completion_base.length(), caret_line, get_caret_column(i));
  1941. adjust_carets_after_edit(i, caret_line, get_caret_column(i) - code_completion_base.length(), caret_line, get_caret_column(i));
  1942. set_caret_column(get_caret_column(i) - code_completion_base.length(), false, i);
  1943. // Merge with text.
  1944. insert_text_at_caret(insert_text.substr(0, code_completion_base.length()), i);
  1945. set_caret_column(caret_col, false, i);
  1946. insert_text_at_caret(insert_text.substr(matching_chars), i);
  1947. }
  1948. // Handle merging of symbols eg strings, brackets.
  1949. const String line = get_line(caret_line);
  1950. char32_t next_char = line[get_caret_column(i)];
  1951. char32_t last_completion_char = insert_text[insert_text.length() - 1];
  1952. if (i == 0) {
  1953. caret_last_completion_char = last_completion_char;
  1954. }
  1955. char32_t last_completion_char_display = display_text[display_text.length() - 1];
  1956. bool last_char_matches = (last_completion_char == next_char || last_completion_char_display == next_char);
  1957. int pre_brace_pair = get_caret_column(i) > 0 ? _get_auto_brace_pair_open_at_pos(caret_line, get_caret_column(i)) : -1;
  1958. int post_brace_pair = get_caret_column(i) < get_line(caret_line).length() ? _get_auto_brace_pair_close_at_pos(caret_line, get_caret_column(i)) : -1;
  1959. // Strings do not nest like brackets, so ensure we don't add an additional closing pair.
  1960. if (has_string_delimiter(String::chr(last_completion_char))) {
  1961. if (post_brace_pair != -1 && last_char_matches) {
  1962. remove_text(caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
  1963. adjust_carets_after_edit(i, caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
  1964. }
  1965. } else {
  1966. if (pre_brace_pair != -1 && pre_brace_pair != post_brace_pair && last_char_matches) {
  1967. remove_text(caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
  1968. adjust_carets_after_edit(i, caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
  1969. } else if (auto_brace_completion_enabled && pre_brace_pair != -1) {
  1970. insert_text_at_caret(auto_brace_completion_pairs[pre_brace_pair].close_key, i);
  1971. set_caret_column(get_caret_column(i) - auto_brace_completion_pairs[pre_brace_pair].close_key.length(), i == 0, i);
  1972. }
  1973. }
  1974. if (pre_brace_pair == -1 && post_brace_pair == -1 && get_caret_column(i) > 0 && get_caret_column(i) < get_line(caret_line).length()) {
  1975. pre_brace_pair = _get_auto_brace_pair_open_at_pos(caret_line, get_caret_column(i) + 1);
  1976. if (pre_brace_pair != -1 && pre_brace_pair == _get_auto_brace_pair_close_at_pos(caret_line, get_caret_column(i) - 1)) {
  1977. remove_text(caret_line, get_caret_column(i) - 2, caret_line, get_caret_column(i));
  1978. adjust_carets_after_edit(i, caret_line, get_caret_column(i) - 2, caret_line, get_caret_column(i));
  1979. if (_get_auto_brace_pair_close_at_pos(caret_line, get_caret_column(i) - 1) != pre_brace_pair) {
  1980. set_caret_column(get_caret_column(i) - 1, i == 0, i);
  1981. }
  1982. }
  1983. }
  1984. }
  1985. end_complex_operation();
  1986. cancel_code_completion();
  1987. if (code_completion_prefixes.has(caret_last_completion_char)) {
  1988. request_code_completion();
  1989. }
  1990. }
  1991. void CodeEdit::cancel_code_completion() {
  1992. if (!code_completion_active) {
  1993. return;
  1994. }
  1995. code_completion_forced = false;
  1996. code_completion_active = false;
  1997. is_code_completion_drag_started = false;
  1998. queue_redraw();
  1999. }
  2000. /* Line length guidelines */
  2001. void CodeEdit::set_line_length_guidelines(TypedArray<int> p_guideline_columns) {
  2002. line_length_guideline_columns = p_guideline_columns;
  2003. queue_redraw();
  2004. }
  2005. TypedArray<int> CodeEdit::get_line_length_guidelines() const {
  2006. return line_length_guideline_columns;
  2007. }
  2008. /* Symbol lookup */
  2009. void CodeEdit::set_symbol_lookup_on_click_enabled(bool p_enabled) {
  2010. symbol_lookup_on_click_enabled = p_enabled;
  2011. set_symbol_lookup_word_as_valid(false);
  2012. }
  2013. bool CodeEdit::is_symbol_lookup_on_click_enabled() const {
  2014. return symbol_lookup_on_click_enabled;
  2015. }
  2016. String CodeEdit::get_text_for_symbol_lookup() const {
  2017. Point2i mp = get_local_mouse_pos();
  2018. Point2i pos = get_line_column_at_pos(mp, false);
  2019. int line = pos.y;
  2020. int col = pos.x;
  2021. if (line == -1) {
  2022. return String();
  2023. }
  2024. return get_text_with_cursor_char(line, col);
  2025. }
  2026. String CodeEdit::get_text_with_cursor_char(int p_line, int p_column) const {
  2027. const int text_size = get_line_count();
  2028. StringBuilder result;
  2029. for (int i = 0; i < text_size; i++) {
  2030. String line_text = get_line(i);
  2031. if (i == p_line && p_column >= 0 && p_column <= line_text.size()) {
  2032. result += line_text.substr(0, p_column);
  2033. /* Not unicode, represents the cursor. */
  2034. result += String::chr(0xFFFF);
  2035. result += line_text.substr(p_column, line_text.size());
  2036. } else {
  2037. result += line_text;
  2038. }
  2039. if (i != text_size - 1) {
  2040. result += "\n";
  2041. }
  2042. }
  2043. return result.as_string();
  2044. }
  2045. void CodeEdit::set_symbol_lookup_word_as_valid(bool p_valid) {
  2046. symbol_lookup_word = p_valid ? symbol_lookup_new_word : "";
  2047. symbol_lookup_new_word = "";
  2048. if (lookup_symbol_word != symbol_lookup_word) {
  2049. _set_symbol_lookup_word(symbol_lookup_word);
  2050. }
  2051. }
  2052. /* Visual */
  2053. Color CodeEdit::_get_brace_mismatch_color() const {
  2054. return theme_cache.brace_mismatch_color;
  2055. }
  2056. Color CodeEdit::_get_code_folding_color() const {
  2057. return theme_cache.code_folding_color;
  2058. }
  2059. Ref<Texture2D> CodeEdit::_get_folded_eol_icon() const {
  2060. return theme_cache.folded_eol_icon;
  2061. }
  2062. void CodeEdit::_bind_methods() {
  2063. /* Indent management */
  2064. ClassDB::bind_method(D_METHOD("set_indent_size", "size"), &CodeEdit::set_indent_size);
  2065. ClassDB::bind_method(D_METHOD("get_indent_size"), &CodeEdit::get_indent_size);
  2066. ClassDB::bind_method(D_METHOD("set_indent_using_spaces", "use_spaces"), &CodeEdit::set_indent_using_spaces);
  2067. ClassDB::bind_method(D_METHOD("is_indent_using_spaces"), &CodeEdit::is_indent_using_spaces);
  2068. ClassDB::bind_method(D_METHOD("set_auto_indent_enabled", "enable"), &CodeEdit::set_auto_indent_enabled);
  2069. ClassDB::bind_method(D_METHOD("is_auto_indent_enabled"), &CodeEdit::is_auto_indent_enabled);
  2070. ClassDB::bind_method(D_METHOD("set_auto_indent_prefixes", "prefixes"), &CodeEdit::set_auto_indent_prefixes);
  2071. ClassDB::bind_method(D_METHOD("get_auto_indent_prefixes"), &CodeEdit::get_auto_indent_prefixes);
  2072. ClassDB::bind_method(D_METHOD("do_indent"), &CodeEdit::do_indent);
  2073. ClassDB::bind_method(D_METHOD("indent_lines"), &CodeEdit::indent_lines);
  2074. ClassDB::bind_method(D_METHOD("unindent_lines"), &CodeEdit::unindent_lines);
  2075. ClassDB::bind_method(D_METHOD("convert_indent", "from_line", "to_line"), &CodeEdit::convert_indent, DEFVAL(-1), DEFVAL(-1));
  2076. /* Auto brace completion */
  2077. ClassDB::bind_method(D_METHOD("set_auto_brace_completion_enabled", "enable"), &CodeEdit::set_auto_brace_completion_enabled);
  2078. ClassDB::bind_method(D_METHOD("is_auto_brace_completion_enabled"), &CodeEdit::is_auto_brace_completion_enabled);
  2079. ClassDB::bind_method(D_METHOD("set_highlight_matching_braces_enabled", "enable"), &CodeEdit::set_highlight_matching_braces_enabled);
  2080. ClassDB::bind_method(D_METHOD("is_highlight_matching_braces_enabled"), &CodeEdit::is_highlight_matching_braces_enabled);
  2081. ClassDB::bind_method(D_METHOD("add_auto_brace_completion_pair", "start_key", "end_key"), &CodeEdit::add_auto_brace_completion_pair);
  2082. ClassDB::bind_method(D_METHOD("set_auto_brace_completion_pairs", "pairs"), &CodeEdit::set_auto_brace_completion_pairs);
  2083. ClassDB::bind_method(D_METHOD("get_auto_brace_completion_pairs"), &CodeEdit::get_auto_brace_completion_pairs);
  2084. ClassDB::bind_method(D_METHOD("has_auto_brace_completion_open_key", "open_key"), &CodeEdit::has_auto_brace_completion_open_key);
  2085. ClassDB::bind_method(D_METHOD("has_auto_brace_completion_close_key", "close_key"), &CodeEdit::has_auto_brace_completion_close_key);
  2086. ClassDB::bind_method(D_METHOD("get_auto_brace_completion_close_key", "open_key"), &CodeEdit::get_auto_brace_completion_close_key);
  2087. /* Main Gutter */
  2088. ClassDB::bind_method(D_METHOD("set_draw_breakpoints_gutter", "enable"), &CodeEdit::set_draw_breakpoints_gutter);
  2089. ClassDB::bind_method(D_METHOD("is_drawing_breakpoints_gutter"), &CodeEdit::is_drawing_breakpoints_gutter);
  2090. ClassDB::bind_method(D_METHOD("set_draw_bookmarks_gutter", "enable"), &CodeEdit::set_draw_bookmarks_gutter);
  2091. ClassDB::bind_method(D_METHOD("is_drawing_bookmarks_gutter"), &CodeEdit::is_drawing_bookmarks_gutter);
  2092. ClassDB::bind_method(D_METHOD("set_draw_executing_lines_gutter", "enable"), &CodeEdit::set_draw_executing_lines_gutter);
  2093. ClassDB::bind_method(D_METHOD("is_drawing_executing_lines_gutter"), &CodeEdit::is_drawing_executing_lines_gutter);
  2094. // Breakpoints
  2095. ClassDB::bind_method(D_METHOD("set_line_as_breakpoint", "line", "breakpointed"), &CodeEdit::set_line_as_breakpoint);
  2096. ClassDB::bind_method(D_METHOD("is_line_breakpointed", "line"), &CodeEdit::is_line_breakpointed);
  2097. ClassDB::bind_method(D_METHOD("clear_breakpointed_lines"), &CodeEdit::clear_breakpointed_lines);
  2098. ClassDB::bind_method(D_METHOD("get_breakpointed_lines"), &CodeEdit::get_breakpointed_lines);
  2099. // Bookmarks
  2100. ClassDB::bind_method(D_METHOD("set_line_as_bookmarked", "line", "bookmarked"), &CodeEdit::set_line_as_bookmarked);
  2101. ClassDB::bind_method(D_METHOD("is_line_bookmarked", "line"), &CodeEdit::is_line_bookmarked);
  2102. ClassDB::bind_method(D_METHOD("clear_bookmarked_lines"), &CodeEdit::clear_bookmarked_lines);
  2103. ClassDB::bind_method(D_METHOD("get_bookmarked_lines"), &CodeEdit::get_bookmarked_lines);
  2104. // Executing lines
  2105. ClassDB::bind_method(D_METHOD("set_line_as_executing", "line", "executing"), &CodeEdit::set_line_as_executing);
  2106. ClassDB::bind_method(D_METHOD("is_line_executing", "line"), &CodeEdit::is_line_executing);
  2107. ClassDB::bind_method(D_METHOD("clear_executing_lines"), &CodeEdit::clear_executing_lines);
  2108. ClassDB::bind_method(D_METHOD("get_executing_lines"), &CodeEdit::get_executing_lines);
  2109. /* Line numbers */
  2110. ClassDB::bind_method(D_METHOD("set_draw_line_numbers", "enable"), &CodeEdit::set_draw_line_numbers);
  2111. ClassDB::bind_method(D_METHOD("is_draw_line_numbers_enabled"), &CodeEdit::is_draw_line_numbers_enabled);
  2112. ClassDB::bind_method(D_METHOD("set_line_numbers_zero_padded", "enable"), &CodeEdit::set_line_numbers_zero_padded);
  2113. ClassDB::bind_method(D_METHOD("is_line_numbers_zero_padded"), &CodeEdit::is_line_numbers_zero_padded);
  2114. /* Fold Gutter */
  2115. ClassDB::bind_method(D_METHOD("set_draw_fold_gutter", "enable"), &CodeEdit::set_draw_fold_gutter);
  2116. ClassDB::bind_method(D_METHOD("is_drawing_fold_gutter"), &CodeEdit::is_drawing_fold_gutter);
  2117. /* Line folding */
  2118. ClassDB::bind_method(D_METHOD("set_line_folding_enabled", "enabled"), &CodeEdit::set_line_folding_enabled);
  2119. ClassDB::bind_method(D_METHOD("is_line_folding_enabled"), &CodeEdit::is_line_folding_enabled);
  2120. ClassDB::bind_method(D_METHOD("can_fold_line", "line"), &CodeEdit::can_fold_line);
  2121. ClassDB::bind_method(D_METHOD("fold_line", "line"), &CodeEdit::fold_line);
  2122. ClassDB::bind_method(D_METHOD("unfold_line", "line"), &CodeEdit::unfold_line);
  2123. ClassDB::bind_method(D_METHOD("fold_all_lines"), &CodeEdit::fold_all_lines);
  2124. ClassDB::bind_method(D_METHOD("unfold_all_lines"), &CodeEdit::unfold_all_lines);
  2125. ClassDB::bind_method(D_METHOD("toggle_foldable_line", "line"), &CodeEdit::toggle_foldable_line);
  2126. ClassDB::bind_method(D_METHOD("is_line_folded", "line"), &CodeEdit::is_line_folded);
  2127. ClassDB::bind_method(D_METHOD("get_folded_lines"), &CodeEdit::get_folded_lines);
  2128. /* Code region */
  2129. ClassDB::bind_method(D_METHOD("create_code_region"), &CodeEdit::create_code_region);
  2130. ClassDB::bind_method(D_METHOD("get_code_region_start_tag"), &CodeEdit::get_code_region_start_tag);
  2131. ClassDB::bind_method(D_METHOD("get_code_region_end_tag"), &CodeEdit::get_code_region_end_tag);
  2132. ClassDB::bind_method(D_METHOD("set_code_region_tags", "start", "end"), &CodeEdit::set_code_region_tags, DEFVAL("region"), DEFVAL("endregion"));
  2133. ClassDB::bind_method(D_METHOD("is_line_code_region_start", "line"), &CodeEdit::is_line_code_region_start);
  2134. ClassDB::bind_method(D_METHOD("is_line_code_region_end", "line"), &CodeEdit::is_line_code_region_end);
  2135. /* Delimiters */
  2136. // Strings
  2137. ClassDB::bind_method(D_METHOD("add_string_delimiter", "start_key", "end_key", "line_only"), &CodeEdit::add_string_delimiter, DEFVAL(false));
  2138. ClassDB::bind_method(D_METHOD("remove_string_delimiter", "start_key"), &CodeEdit::remove_string_delimiter);
  2139. ClassDB::bind_method(D_METHOD("has_string_delimiter", "start_key"), &CodeEdit::has_string_delimiter);
  2140. ClassDB::bind_method(D_METHOD("set_string_delimiters", "string_delimiters"), &CodeEdit::set_string_delimiters);
  2141. ClassDB::bind_method(D_METHOD("clear_string_delimiters"), &CodeEdit::clear_string_delimiters);
  2142. ClassDB::bind_method(D_METHOD("get_string_delimiters"), &CodeEdit::get_string_delimiters);
  2143. ClassDB::bind_method(D_METHOD("is_in_string", "line", "column"), &CodeEdit::is_in_string, DEFVAL(-1));
  2144. // Comments
  2145. ClassDB::bind_method(D_METHOD("add_comment_delimiter", "start_key", "end_key", "line_only"), &CodeEdit::add_comment_delimiter, DEFVAL(false));
  2146. ClassDB::bind_method(D_METHOD("remove_comment_delimiter", "start_key"), &CodeEdit::remove_comment_delimiter);
  2147. ClassDB::bind_method(D_METHOD("has_comment_delimiter", "start_key"), &CodeEdit::has_comment_delimiter);
  2148. ClassDB::bind_method(D_METHOD("set_comment_delimiters", "comment_delimiters"), &CodeEdit::set_comment_delimiters);
  2149. ClassDB::bind_method(D_METHOD("clear_comment_delimiters"), &CodeEdit::clear_comment_delimiters);
  2150. ClassDB::bind_method(D_METHOD("get_comment_delimiters"), &CodeEdit::get_comment_delimiters);
  2151. ClassDB::bind_method(D_METHOD("is_in_comment", "line", "column"), &CodeEdit::is_in_comment, DEFVAL(-1));
  2152. // Util
  2153. ClassDB::bind_method(D_METHOD("get_delimiter_start_key", "delimiter_index"), &CodeEdit::get_delimiter_start_key);
  2154. ClassDB::bind_method(D_METHOD("get_delimiter_end_key", "delimiter_index"), &CodeEdit::get_delimiter_end_key);
  2155. ClassDB::bind_method(D_METHOD("get_delimiter_start_position", "line", "column"), &CodeEdit::get_delimiter_start_position);
  2156. ClassDB::bind_method(D_METHOD("get_delimiter_end_position", "line", "column"), &CodeEdit::get_delimiter_end_position);
  2157. /* Code hint */
  2158. ClassDB::bind_method(D_METHOD("set_code_hint", "code_hint"), &CodeEdit::set_code_hint);
  2159. ClassDB::bind_method(D_METHOD("set_code_hint_draw_below", "draw_below"), &CodeEdit::set_code_hint_draw_below);
  2160. /* Code Completion */
  2161. BIND_ENUM_CONSTANT(KIND_CLASS);
  2162. BIND_ENUM_CONSTANT(KIND_FUNCTION);
  2163. BIND_ENUM_CONSTANT(KIND_SIGNAL);
  2164. BIND_ENUM_CONSTANT(KIND_VARIABLE);
  2165. BIND_ENUM_CONSTANT(KIND_MEMBER);
  2166. BIND_ENUM_CONSTANT(KIND_ENUM);
  2167. BIND_ENUM_CONSTANT(KIND_CONSTANT);
  2168. BIND_ENUM_CONSTANT(KIND_NODE_PATH);
  2169. BIND_ENUM_CONSTANT(KIND_FILE_PATH);
  2170. BIND_ENUM_CONSTANT(KIND_PLAIN_TEXT);
  2171. BIND_ENUM_CONSTANT(LOCATION_LOCAL);
  2172. BIND_ENUM_CONSTANT(LOCATION_PARENT_MASK);
  2173. BIND_ENUM_CONSTANT(LOCATION_OTHER_USER_CODE)
  2174. BIND_ENUM_CONSTANT(LOCATION_OTHER);
  2175. ClassDB::bind_method(D_METHOD("get_text_for_code_completion"), &CodeEdit::get_text_for_code_completion);
  2176. ClassDB::bind_method(D_METHOD("request_code_completion", "force"), &CodeEdit::request_code_completion, DEFVAL(false));
  2177. ClassDB::bind_method(D_METHOD("add_code_completion_option", "type", "display_text", "insert_text", "text_color", "icon", "value", "location"), &CodeEdit::add_code_completion_option, DEFVAL(Color(1, 1, 1)), DEFVAL(Ref<Resource>()), DEFVAL(Variant::NIL), DEFVAL(LOCATION_OTHER));
  2178. ClassDB::bind_method(D_METHOD("update_code_completion_options", "force"), &CodeEdit::update_code_completion_options);
  2179. ClassDB::bind_method(D_METHOD("get_code_completion_options"), &CodeEdit::get_code_completion_options);
  2180. ClassDB::bind_method(D_METHOD("get_code_completion_option", "index"), &CodeEdit::get_code_completion_option);
  2181. ClassDB::bind_method(D_METHOD("get_code_completion_selected_index"), &CodeEdit::get_code_completion_selected_index);
  2182. ClassDB::bind_method(D_METHOD("set_code_completion_selected_index", "index"), &CodeEdit::set_code_completion_selected_index);
  2183. ClassDB::bind_method(D_METHOD("confirm_code_completion", "replace"), &CodeEdit::confirm_code_completion, DEFVAL(false));
  2184. ClassDB::bind_method(D_METHOD("cancel_code_completion"), &CodeEdit::cancel_code_completion);
  2185. ClassDB::bind_method(D_METHOD("set_code_completion_enabled", "enable"), &CodeEdit::set_code_completion_enabled);
  2186. ClassDB::bind_method(D_METHOD("is_code_completion_enabled"), &CodeEdit::is_code_completion_enabled);
  2187. ClassDB::bind_method(D_METHOD("set_code_completion_prefixes", "prefixes"), &CodeEdit::set_code_completion_prefixes);
  2188. ClassDB::bind_method(D_METHOD("get_code_completion_prefixes"), &CodeEdit::get_code_completion_prefixes);
  2189. // Overridable
  2190. GDVIRTUAL_BIND(_confirm_code_completion, "replace")
  2191. GDVIRTUAL_BIND(_request_code_completion, "force")
  2192. GDVIRTUAL_BIND(_filter_code_completion_candidates, "candidates")
  2193. /* Line length guidelines */
  2194. ClassDB::bind_method(D_METHOD("set_line_length_guidelines", "guideline_columns"), &CodeEdit::set_line_length_guidelines);
  2195. ClassDB::bind_method(D_METHOD("get_line_length_guidelines"), &CodeEdit::get_line_length_guidelines);
  2196. /* Symbol lookup */
  2197. ClassDB::bind_method(D_METHOD("set_symbol_lookup_on_click_enabled", "enable"), &CodeEdit::set_symbol_lookup_on_click_enabled);
  2198. ClassDB::bind_method(D_METHOD("is_symbol_lookup_on_click_enabled"), &CodeEdit::is_symbol_lookup_on_click_enabled);
  2199. ClassDB::bind_method(D_METHOD("get_text_for_symbol_lookup"), &CodeEdit::get_text_for_symbol_lookup);
  2200. ClassDB::bind_method(D_METHOD("get_text_with_cursor_char", "line", "column"), &CodeEdit::get_text_with_cursor_char);
  2201. ClassDB::bind_method(D_METHOD("set_symbol_lookup_word_as_valid", "valid"), &CodeEdit::set_symbol_lookup_word_as_valid);
  2202. /* Inspector */
  2203. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "symbol_lookup_on_click"), "set_symbol_lookup_on_click_enabled", "is_symbol_lookup_on_click_enabled");
  2204. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "line_folding"), "set_line_folding_enabled", "is_line_folding_enabled");
  2205. ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "line_length_guidelines"), "set_line_length_guidelines", "get_line_length_guidelines");
  2206. ADD_GROUP("Gutters", "gutters_");
  2207. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_draw_breakpoints_gutter"), "set_draw_breakpoints_gutter", "is_drawing_breakpoints_gutter");
  2208. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_draw_bookmarks"), "set_draw_bookmarks_gutter", "is_drawing_bookmarks_gutter");
  2209. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_draw_executing_lines"), "set_draw_executing_lines_gutter", "is_drawing_executing_lines_gutter");
  2210. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_draw_line_numbers"), "set_draw_line_numbers", "is_draw_line_numbers_enabled");
  2211. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_zero_pad_line_numbers"), "set_line_numbers_zero_padded", "is_line_numbers_zero_padded");
  2212. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_draw_fold_gutter"), "set_draw_fold_gutter", "is_drawing_fold_gutter");
  2213. ADD_GROUP("Delimiters", "delimiter_");
  2214. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "delimiter_strings"), "set_string_delimiters", "get_string_delimiters");
  2215. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "delimiter_comments"), "set_comment_delimiters", "get_comment_delimiters");
  2216. ADD_GROUP("Code Completion", "code_completion_");
  2217. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "code_completion_enabled"), "set_code_completion_enabled", "is_code_completion_enabled");
  2218. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "code_completion_prefixes"), "set_code_completion_prefixes", "get_code_completion_prefixes");
  2219. ADD_GROUP("Indentation", "indent_");
  2220. ADD_PROPERTY(PropertyInfo(Variant::INT, "indent_size"), "set_indent_size", "get_indent_size");
  2221. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indent_use_spaces"), "set_indent_using_spaces", "is_indent_using_spaces");
  2222. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indent_automatic"), "set_auto_indent_enabled", "is_auto_indent_enabled");
  2223. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "indent_automatic_prefixes"), "set_auto_indent_prefixes", "get_auto_indent_prefixes");
  2224. ADD_GROUP("Auto Brace Completion", "auto_brace_completion_");
  2225. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_brace_completion_enabled"), "set_auto_brace_completion_enabled", "is_auto_brace_completion_enabled");
  2226. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_brace_completion_highlight_matching"), "set_highlight_matching_braces_enabled", "is_highlight_matching_braces_enabled");
  2227. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "auto_brace_completion_pairs"), "set_auto_brace_completion_pairs", "get_auto_brace_completion_pairs");
  2228. /* Signals */
  2229. /* Gutters */
  2230. ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo(Variant::INT, "line")));
  2231. /* Code Completion */
  2232. ADD_SIGNAL(MethodInfo("code_completion_requested"));
  2233. /* Symbol lookup */
  2234. ADD_SIGNAL(MethodInfo("symbol_lookup", PropertyInfo(Variant::STRING, "symbol"), PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::INT, "column")));
  2235. ADD_SIGNAL(MethodInfo("symbol_validate", PropertyInfo(Variant::STRING, "symbol")));
  2236. /* Theme items */
  2237. /* Gutters */
  2238. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, code_folding_color);
  2239. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, folded_code_region_color);
  2240. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, can_fold_icon, "can_fold");
  2241. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, folded_icon, "folded");
  2242. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, can_fold_code_region_icon, "can_fold_code_region");
  2243. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, folded_code_region_icon, "folded_code_region");
  2244. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CodeEdit, folded_eol_icon);
  2245. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, breakpoint_color);
  2246. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, breakpoint_icon, "breakpoint");
  2247. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, bookmark_color);
  2248. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, bookmark_icon, "bookmark");
  2249. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, executing_line_color);
  2250. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, executing_line_icon, "executing_line");
  2251. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, line_number_color);
  2252. /* Code Completion */
  2253. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, CodeEdit, code_completion_style, "completion");
  2254. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_CONSTANT, CodeEdit, code_completion_icon_separation, "h_separation", "ItemList");
  2255. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, CodeEdit, code_completion_max_width, "completion_max_width");
  2256. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, CodeEdit, code_completion_max_lines, "completion_lines");
  2257. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, CodeEdit, code_completion_scroll_width, "completion_scroll_width");
  2258. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, CodeEdit, code_completion_scroll_color, "completion_scroll_color");
  2259. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, CodeEdit, code_completion_scroll_hovered_color, "completion_scroll_hovered_color");
  2260. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, CodeEdit, code_completion_background_color, "completion_background_color");
  2261. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, CodeEdit, code_completion_selected_color, "completion_selected_color");
  2262. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, CodeEdit, code_completion_existing_color, "completion_existing_color");
  2263. /* Code hint */
  2264. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, CodeEdit, code_hint_style, "panel", "TooltipPanel");
  2265. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, CodeEdit, code_hint_color, "font_color", "TooltipLabel");
  2266. /* Line length guideline */
  2267. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, line_length_guideline_color);
  2268. /* Other visuals */
  2269. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, CodeEdit, style_normal, "normal");
  2270. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, brace_mismatch_color);
  2271. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, CodeEdit, font);
  2272. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, CodeEdit, font_size);
  2273. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, CodeEdit, line_spacing);
  2274. }
  2275. /* Auto brace completion */
  2276. int CodeEdit::_get_auto_brace_pair_open_at_pos(int p_line, int p_col) {
  2277. const String &line = get_line(p_line);
  2278. /* Should be fast enough, expecting low amount of pairs... */
  2279. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  2280. const String &open_key = auto_brace_completion_pairs[i].open_key;
  2281. if (p_col - open_key.length() < 0) {
  2282. continue;
  2283. }
  2284. bool is_match = true;
  2285. for (int j = 0; j < open_key.length(); j++) {
  2286. if (line[(p_col - 1) - j] != open_key[(open_key.length() - 1) - j]) {
  2287. is_match = false;
  2288. break;
  2289. }
  2290. }
  2291. if (is_match) {
  2292. return i;
  2293. }
  2294. }
  2295. return -1;
  2296. }
  2297. int CodeEdit::_get_auto_brace_pair_close_at_pos(int p_line, int p_col) {
  2298. const String &line = get_line(p_line);
  2299. /* Should be fast enough, expecting low amount of pairs... */
  2300. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  2301. if (p_col + auto_brace_completion_pairs[i].close_key.length() > line.length()) {
  2302. continue;
  2303. }
  2304. bool is_match = true;
  2305. for (int j = 0; j < auto_brace_completion_pairs[i].close_key.length(); j++) {
  2306. if (line[p_col + j] != auto_brace_completion_pairs[i].close_key[j]) {
  2307. is_match = false;
  2308. break;
  2309. }
  2310. }
  2311. if (is_match) {
  2312. return i;
  2313. }
  2314. }
  2315. return -1;
  2316. }
  2317. /* Gutters */
  2318. void CodeEdit::_gutter_clicked(int p_line, int p_gutter) {
  2319. bool shift_pressed = Input::get_singleton()->is_key_pressed(Key::SHIFT);
  2320. if (p_gutter == main_gutter) {
  2321. if (draw_breakpoints && !shift_pressed) {
  2322. set_line_as_breakpoint(p_line, !is_line_breakpointed(p_line));
  2323. } else if (draw_bookmarks && shift_pressed) {
  2324. set_line_as_bookmarked(p_line, !is_line_bookmarked(p_line));
  2325. }
  2326. return;
  2327. }
  2328. if (p_gutter == line_number_gutter) {
  2329. remove_secondary_carets();
  2330. set_selection_mode(TextEdit::SelectionMode::SELECTION_MODE_LINE, p_line, 0);
  2331. select(p_line, 0, p_line + 1, 0);
  2332. set_caret_line(p_line + 1);
  2333. set_caret_column(0);
  2334. return;
  2335. }
  2336. if (p_gutter == fold_gutter) {
  2337. if (is_line_folded(p_line)) {
  2338. unfold_line(p_line);
  2339. } else if (can_fold_line(p_line)) {
  2340. fold_line(p_line);
  2341. }
  2342. return;
  2343. }
  2344. }
  2345. void CodeEdit::_update_gutter_indexes() {
  2346. for (int i = 0; i < get_gutter_count(); i++) {
  2347. if (get_gutter_name(i) == "main_gutter") {
  2348. main_gutter = i;
  2349. continue;
  2350. }
  2351. if (get_gutter_name(i) == "line_numbers") {
  2352. line_number_gutter = i;
  2353. continue;
  2354. }
  2355. if (get_gutter_name(i) == "fold_gutter") {
  2356. fold_gutter = i;
  2357. continue;
  2358. }
  2359. }
  2360. }
  2361. /* Code Region */
  2362. void CodeEdit::_update_code_region_tags() {
  2363. code_region_start_string = "";
  2364. code_region_end_string = "";
  2365. if (code_region_start_tag.is_empty() || code_region_end_tag.is_empty()) {
  2366. return;
  2367. }
  2368. for (int i = 0; i < delimiters.size(); i++) {
  2369. if (delimiters[i].type != DelimiterType::TYPE_COMMENT) {
  2370. continue;
  2371. }
  2372. if (delimiters[i].end_key.is_empty() && delimiters[i].line_only == true) {
  2373. code_region_start_string = delimiters[i].start_key + code_region_start_tag;
  2374. code_region_end_string = delimiters[i].start_key + code_region_end_tag;
  2375. return;
  2376. }
  2377. }
  2378. }
  2379. /* Delimiters */
  2380. void CodeEdit::_update_delimiter_cache(int p_from_line, int p_to_line) {
  2381. if (delimiters.size() == 0) {
  2382. return;
  2383. }
  2384. int line_count = get_line_count();
  2385. if (p_to_line == -1) {
  2386. p_to_line = line_count;
  2387. }
  2388. int start_line = MIN(p_from_line, p_to_line);
  2389. int end_line = MAX(p_from_line, p_to_line);
  2390. /* Make sure delimiter_cache has all the lines. */
  2391. if (start_line != end_line) {
  2392. if (p_to_line < p_from_line) {
  2393. for (int i = end_line; i > start_line; i--) {
  2394. delimiter_cache.remove_at(i);
  2395. }
  2396. } else {
  2397. for (int i = start_line; i < end_line; i++) {
  2398. delimiter_cache.insert(i, RBMap<int, int>());
  2399. }
  2400. }
  2401. }
  2402. int in_region = -1;
  2403. for (int i = start_line; i < MIN(end_line + 1, line_count); i++) {
  2404. int current_end_region = (i <= 0 || delimiter_cache[i].size() < 1) ? -1 : delimiter_cache[i].back()->value();
  2405. in_region = (i <= 0 || delimiter_cache[i - 1].size() < 1) ? -1 : delimiter_cache[i - 1].back()->value();
  2406. const String &str = get_line(i);
  2407. const int line_length = str.length();
  2408. delimiter_cache.write[i].clear();
  2409. if (str.length() == 0) {
  2410. if (in_region != -1) {
  2411. delimiter_cache.write[i][0] = in_region;
  2412. }
  2413. if (i == end_line && current_end_region != in_region) {
  2414. end_line++;
  2415. end_line = MIN(end_line, line_count);
  2416. }
  2417. continue;
  2418. }
  2419. int end_region = -1;
  2420. for (int j = 0; j < line_length; j++) {
  2421. int from = j;
  2422. for (; from < line_length; from++) {
  2423. if (str[from] == '\\') {
  2424. from++;
  2425. continue;
  2426. }
  2427. break;
  2428. }
  2429. /* check if we are in entering a region */
  2430. bool same_line = false;
  2431. if (in_region == -1) {
  2432. for (int d = 0; d < delimiters.size(); d++) {
  2433. /* check there is enough room */
  2434. int chars_left = line_length - from;
  2435. int start_key_length = delimiters[d].start_key.length();
  2436. int end_key_length = delimiters[d].end_key.length();
  2437. if (chars_left < start_key_length) {
  2438. continue;
  2439. }
  2440. /* search the line */
  2441. bool match = true;
  2442. const char32_t *start_key = delimiters[d].start_key.get_data();
  2443. for (int k = 0; k < start_key_length; k++) {
  2444. if (start_key[k] != str[from + k]) {
  2445. match = false;
  2446. break;
  2447. }
  2448. }
  2449. if (!match) {
  2450. continue;
  2451. }
  2452. same_line = true;
  2453. in_region = d;
  2454. delimiter_cache.write[i][from + 1] = d;
  2455. from += start_key_length;
  2456. /* check if it's the whole line */
  2457. if (end_key_length == 0 || delimiters[d].line_only || from + end_key_length > line_length) {
  2458. j = line_length;
  2459. if (delimiters[d].line_only) {
  2460. delimiter_cache.write[i][line_length + 1] = -1;
  2461. } else {
  2462. end_region = in_region;
  2463. }
  2464. }
  2465. break;
  2466. }
  2467. if (j == line_length || in_region == -1) {
  2468. continue;
  2469. }
  2470. }
  2471. /* if we are in one find the end key */
  2472. /* search the line */
  2473. int region_end_index = -1;
  2474. int end_key_length = delimiters[in_region].end_key.length();
  2475. const char32_t *end_key = delimiters[in_region].end_key.get_data();
  2476. for (; from < line_length; from++) {
  2477. if (line_length - from < end_key_length) {
  2478. break;
  2479. }
  2480. if (!is_symbol(str[from])) {
  2481. continue;
  2482. }
  2483. if (str[from] == '\\') {
  2484. from++;
  2485. continue;
  2486. }
  2487. region_end_index = from;
  2488. for (int k = 0; k < end_key_length; k++) {
  2489. if (end_key[k] != str[from + k]) {
  2490. region_end_index = -1;
  2491. break;
  2492. }
  2493. }
  2494. if (region_end_index != -1) {
  2495. break;
  2496. }
  2497. }
  2498. j = from + (end_key_length - 1);
  2499. end_region = (region_end_index == -1) ? in_region : -1;
  2500. if (!same_line || region_end_index != -1) {
  2501. delimiter_cache.write[i][j + 1] = end_region;
  2502. }
  2503. in_region = -1;
  2504. }
  2505. if (i == end_line && current_end_region != end_region) {
  2506. end_line++;
  2507. end_line = MIN(end_line, line_count);
  2508. }
  2509. }
  2510. }
  2511. int CodeEdit::_is_in_delimiter(int p_line, int p_column, DelimiterType p_type) const {
  2512. if (delimiters.size() == 0) {
  2513. return -1;
  2514. }
  2515. ERR_FAIL_INDEX_V(p_line, get_line_count(), 0);
  2516. int region = (p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->value();
  2517. bool in_region = region != -1 && delimiters[region].type == p_type;
  2518. for (RBMap<int, int>::Element *E = delimiter_cache[p_line].front(); E; E = E->next()) {
  2519. /* If column is specified, loop until the key is larger then the column. */
  2520. if (p_column != -1) {
  2521. if (E->key() > p_column) {
  2522. break;
  2523. }
  2524. in_region = E->value() != -1 && delimiters[E->value()].type == p_type;
  2525. region = in_region ? E->value() : -1;
  2526. continue;
  2527. }
  2528. /* If no column, calculate if the entire line is a region */
  2529. /* excluding whitespace. */
  2530. const String line = get_line(p_line);
  2531. if (!in_region) {
  2532. if (E->value() == -1 || delimiters[E->value()].type != p_type) {
  2533. break;
  2534. }
  2535. region = E->value();
  2536. in_region = true;
  2537. for (int i = E->key() - 2; i >= 0; i--) {
  2538. if (!is_whitespace(line[i])) {
  2539. return -1;
  2540. }
  2541. }
  2542. }
  2543. if (delimiters[region].line_only) {
  2544. return region;
  2545. }
  2546. int end_col = E->key();
  2547. if (E->value() != -1) {
  2548. if (!E->next()) {
  2549. return region;
  2550. }
  2551. end_col = E->next()->key();
  2552. }
  2553. for (int i = end_col; i < line.length(); i++) {
  2554. if (!is_whitespace(line[i])) {
  2555. return -1;
  2556. }
  2557. }
  2558. return region;
  2559. }
  2560. return in_region ? region : -1;
  2561. }
  2562. void CodeEdit::_add_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only, DelimiterType p_type) {
  2563. // If we are the editor allow "null" as a valid start key, otherwise users cannot add delimiters via the inspector.
  2564. if (!(Engine::get_singleton()->is_editor_hint() && p_start_key == "null")) {
  2565. ERR_FAIL_COND_MSG(p_start_key.is_empty(), "delimiter start key cannot be empty");
  2566. for (int i = 0; i < p_start_key.length(); i++) {
  2567. ERR_FAIL_COND_MSG(!is_symbol(p_start_key[i]), "delimiter must start with a symbol");
  2568. }
  2569. }
  2570. if (p_end_key.length() > 0) {
  2571. for (int i = 0; i < p_end_key.length(); i++) {
  2572. ERR_FAIL_COND_MSG(!is_symbol(p_end_key[i]), "delimiter must end with a symbol");
  2573. }
  2574. }
  2575. int at = 0;
  2576. for (int i = 0; i < delimiters.size(); i++) {
  2577. ERR_FAIL_COND_MSG(delimiters[i].start_key == p_start_key, "delimiter with start key '" + p_start_key + "' already exists.");
  2578. if (p_start_key.length() < delimiters[i].start_key.length()) {
  2579. at++;
  2580. }
  2581. }
  2582. Delimiter delimiter;
  2583. delimiter.type = p_type;
  2584. delimiter.start_key = p_start_key;
  2585. delimiter.end_key = p_end_key;
  2586. delimiter.line_only = p_line_only || p_end_key.is_empty();
  2587. delimiters.insert(at, delimiter);
  2588. if (!setting_delimiters) {
  2589. delimiter_cache.clear();
  2590. _update_delimiter_cache();
  2591. }
  2592. if (p_type == DelimiterType::TYPE_COMMENT) {
  2593. _update_code_region_tags();
  2594. }
  2595. }
  2596. void CodeEdit::_remove_delimiter(const String &p_start_key, DelimiterType p_type) {
  2597. for (int i = 0; i < delimiters.size(); i++) {
  2598. if (delimiters[i].start_key != p_start_key) {
  2599. continue;
  2600. }
  2601. if (delimiters[i].type != p_type) {
  2602. break;
  2603. }
  2604. delimiters.remove_at(i);
  2605. if (!setting_delimiters) {
  2606. delimiter_cache.clear();
  2607. _update_delimiter_cache();
  2608. }
  2609. if (p_type == DelimiterType::TYPE_COMMENT) {
  2610. _update_code_region_tags();
  2611. }
  2612. break;
  2613. }
  2614. }
  2615. bool CodeEdit::_has_delimiter(const String &p_start_key, DelimiterType p_type) const {
  2616. for (int i = 0; i < delimiters.size(); i++) {
  2617. if (delimiters[i].start_key == p_start_key) {
  2618. return delimiters[i].type == p_type;
  2619. }
  2620. }
  2621. return false;
  2622. }
  2623. void CodeEdit::_set_delimiters(const TypedArray<String> &p_delimiters, DelimiterType p_type) {
  2624. setting_delimiters = true;
  2625. _clear_delimiters(p_type);
  2626. for (int i = 0; i < p_delimiters.size(); i++) {
  2627. String key = p_delimiters[i];
  2628. if (key.is_empty()) {
  2629. continue;
  2630. }
  2631. const String start_key = key.get_slice(" ", 0);
  2632. const String end_key = key.get_slice_count(" ") > 1 ? key.get_slice(" ", 1) : String();
  2633. _add_delimiter(start_key, end_key, end_key.is_empty(), p_type);
  2634. }
  2635. setting_delimiters = false;
  2636. _update_delimiter_cache();
  2637. }
  2638. void CodeEdit::_clear_delimiters(DelimiterType p_type) {
  2639. for (int i = delimiters.size() - 1; i >= 0; i--) {
  2640. if (delimiters[i].type == p_type) {
  2641. delimiters.remove_at(i);
  2642. }
  2643. }
  2644. delimiter_cache.clear();
  2645. if (!setting_delimiters) {
  2646. _update_delimiter_cache();
  2647. }
  2648. if (p_type == DelimiterType::TYPE_COMMENT) {
  2649. _update_code_region_tags();
  2650. }
  2651. }
  2652. TypedArray<String> CodeEdit::_get_delimiters(DelimiterType p_type) const {
  2653. TypedArray<String> r_delimiters;
  2654. for (int i = 0; i < delimiters.size(); i++) {
  2655. if (delimiters[i].type != p_type) {
  2656. continue;
  2657. }
  2658. r_delimiters.push_back(delimiters[i].start_key + (delimiters[i].end_key.is_empty() ? "" : " " + delimiters[i].end_key));
  2659. }
  2660. return r_delimiters;
  2661. }
  2662. /* Code Completion */
  2663. void CodeEdit::_update_scroll_selected_line(float p_mouse_y) {
  2664. float percent = (float)(p_mouse_y - code_completion_scroll_rect.position.y) / code_completion_scroll_rect.size.height;
  2665. percent = CLAMP(percent, 0.0f, 1.0f);
  2666. code_completion_current_selected = (int)(percent * (code_completion_options.size() - 1));
  2667. code_completion_force_item_center = -1;
  2668. }
  2669. void CodeEdit::_filter_code_completion_candidates_impl() {
  2670. int line_height = get_line_height();
  2671. if (GDVIRTUAL_IS_OVERRIDDEN(_filter_code_completion_candidates)) {
  2672. code_completion_options.clear();
  2673. code_completion_base = "";
  2674. /* Build options argument. */
  2675. TypedArray<Dictionary> completion_options_sources;
  2676. completion_options_sources.resize(code_completion_option_sources.size());
  2677. int i = 0;
  2678. for (const ScriptLanguage::CodeCompletionOption &E : code_completion_option_sources) {
  2679. Dictionary option;
  2680. option["kind"] = E.kind;
  2681. option["display_text"] = E.display;
  2682. option["insert_text"] = E.insert_text;
  2683. option["font_color"] = E.font_color;
  2684. option["icon"] = E.icon;
  2685. option["default_value"] = E.default_value;
  2686. option["location"] = E.location;
  2687. completion_options_sources[i] = option;
  2688. i++;
  2689. }
  2690. TypedArray<Dictionary> completion_options;
  2691. GDVIRTUAL_CALL(_filter_code_completion_candidates, completion_options_sources, completion_options);
  2692. /* No options to complete, cancel. */
  2693. if (completion_options.size() == 0) {
  2694. cancel_code_completion();
  2695. return;
  2696. }
  2697. /* Convert back into options. */
  2698. int max_width = 0;
  2699. for (i = 0; i < completion_options.size(); i++) {
  2700. ScriptLanguage::CodeCompletionOption option;
  2701. option.kind = (ScriptLanguage::CodeCompletionKind)(int)completion_options[i].get("kind");
  2702. option.display = completion_options[i].get("display_text");
  2703. option.insert_text = completion_options[i].get("insert_text");
  2704. option.font_color = completion_options[i].get("font_color");
  2705. option.icon = completion_options[i].get("icon");
  2706. option.location = completion_options[i].get("location");
  2707. option.default_value = completion_options[i].get("default_value");
  2708. int offset = 0;
  2709. if (option.default_value.get_type() == Variant::COLOR) {
  2710. offset = line_height;
  2711. }
  2712. if (theme_cache.font.is_valid()) {
  2713. max_width = MAX(max_width, theme_cache.font->get_string_size(option.display, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width + offset);
  2714. }
  2715. code_completion_options.push_back(option);
  2716. }
  2717. code_completion_longest_line = MIN(max_width, theme_cache.code_completion_max_width * theme_cache.font_size);
  2718. code_completion_current_selected = 0;
  2719. code_completion_force_item_center = -1;
  2720. code_completion_active = true;
  2721. queue_redraw();
  2722. return;
  2723. }
  2724. const int caret_line = get_caret_line();
  2725. const int caret_column = get_caret_column();
  2726. const String line = get_line(caret_line);
  2727. ERR_FAIL_INDEX_MSG(caret_column, line.length() + 1, "Caret column exceeds line length.");
  2728. if (caret_column > 0 && line[caret_column - 1] == '(' && !code_completion_forced) {
  2729. cancel_code_completion();
  2730. return;
  2731. }
  2732. /* Get string status, are we in one or at the close. */
  2733. int in_string = is_in_string(caret_line, caret_column);
  2734. int first_quote_col = -1;
  2735. if (in_string != -1) {
  2736. Point2 string_start_pos = get_delimiter_start_position(caret_line, caret_column);
  2737. first_quote_col = (string_start_pos.y == caret_line) ? string_start_pos.x : -1;
  2738. } else if (caret_column > 0) {
  2739. if (is_in_string(caret_line, caret_column - 1) != -1) {
  2740. first_quote_col = caret_column - 1;
  2741. }
  2742. }
  2743. int cofs = caret_column;
  2744. String string_to_complete;
  2745. bool prev_is_word = false;
  2746. /* Cancel if we are at the close of a string. */
  2747. if (caret_column > 0 && in_string == -1 && first_quote_col == cofs - 1) {
  2748. cancel_code_completion();
  2749. return;
  2750. /* In a string, therefore we are trying to complete the string text. */
  2751. } else if (in_string != -1 && first_quote_col != -1) {
  2752. int key_length = delimiters[in_string].start_key.length();
  2753. string_to_complete = line.substr(first_quote_col - key_length, (cofs - first_quote_col) + key_length);
  2754. /* If we have a space, previous word might be a keyword. eg "func |". */
  2755. } else if (cofs > 0 && line[cofs - 1] == ' ') {
  2756. int ofs = cofs - 1;
  2757. while (ofs > 0 && line[ofs] == ' ') {
  2758. ofs--;
  2759. }
  2760. prev_is_word = !is_symbol(line[ofs]);
  2761. /* Otherwise get current word and set cofs to the start. */
  2762. } else {
  2763. int start_cofs = cofs;
  2764. while (cofs > 0 && line[cofs - 1] > 32 && (line[cofs - 1] == '/' || !is_symbol(line[cofs - 1]))) {
  2765. cofs--;
  2766. }
  2767. string_to_complete = line.substr(cofs, start_cofs - cofs);
  2768. }
  2769. /* If all else fails, check for a prefix. */
  2770. /* Single space between caret and prefix is okay. */
  2771. bool prev_is_prefix = false;
  2772. if (cofs > 0 && code_completion_prefixes.has(line[cofs - 1])) {
  2773. prev_is_prefix = true;
  2774. } else if (cofs > 1 && line[cofs - 1] == ' ' && code_completion_prefixes.has(line[cofs - 2])) {
  2775. prev_is_prefix = true;
  2776. }
  2777. if (!prev_is_word && string_to_complete.is_empty() && (cofs == 0 || !prev_is_prefix)) {
  2778. cancel_code_completion();
  2779. return;
  2780. }
  2781. /* Filter Options. */
  2782. /* For now handle only traditional quoted strings. */
  2783. bool single_quote = in_string != -1 && first_quote_col > 0 && delimiters[in_string].start_key == "'";
  2784. code_completion_options.clear();
  2785. code_completion_base = string_to_complete;
  2786. /* Don't autocomplete setting numerical values. */
  2787. if (code_completion_base.is_numeric()) {
  2788. cancel_code_completion();
  2789. return;
  2790. }
  2791. int max_width = 0;
  2792. String string_to_complete_lower = string_to_complete.to_lower();
  2793. for (ScriptLanguage::CodeCompletionOption &option : code_completion_option_sources) {
  2794. option.matches.clear();
  2795. if (single_quote && option.display.is_quoted()) {
  2796. option.display = option.display.unquote().quote("'");
  2797. }
  2798. int offset = option.default_value.get_type() == Variant::COLOR ? line_height : 0;
  2799. if (in_string != -1) {
  2800. String quote = single_quote ? "'" : "\"";
  2801. option.display = option.display.unquote().quote(quote);
  2802. option.insert_text = option.insert_text.unquote().quote(quote);
  2803. }
  2804. if (option.display.length() == 0) {
  2805. continue;
  2806. }
  2807. if (string_to_complete.length() == 0) {
  2808. option.get_option_characteristics(string_to_complete);
  2809. code_completion_options.push_back(option);
  2810. if (theme_cache.font.is_valid()) {
  2811. max_width = MAX(max_width, theme_cache.font->get_string_size(option.display, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width + offset);
  2812. }
  2813. continue;
  2814. }
  2815. String target_lower = option.display.to_lower();
  2816. int long_option = target_lower.size() > 50;
  2817. const char32_t *string_to_complete_char_lower = &string_to_complete_lower[0];
  2818. const char32_t *target_char_lower = &target_lower[0];
  2819. Vector<Vector<Pair<int, int>>> all_possible_subsequence_matches;
  2820. for (int i = 0; *target_char_lower; i++, target_char_lower++) {
  2821. if (*target_char_lower == *string_to_complete_char_lower) {
  2822. all_possible_subsequence_matches.push_back({ { i, 1 } });
  2823. }
  2824. }
  2825. string_to_complete_char_lower++;
  2826. for (int i = 1; *string_to_complete_char_lower && (all_possible_subsequence_matches.size() > 0); i++, string_to_complete_char_lower++) {
  2827. // find all occurrences of ssq_lower to avoid looking everywhere each time
  2828. Vector<int> all_ocurence;
  2829. if (long_option) {
  2830. all_ocurence.push_back(target_lower.find_char(*string_to_complete_char_lower));
  2831. } else {
  2832. for (int j = i; j < target_lower.length(); j++) {
  2833. if (target_lower[j] == *string_to_complete_char_lower) {
  2834. all_ocurence.push_back(j);
  2835. }
  2836. }
  2837. }
  2838. Vector<Vector<Pair<int, int>>> next_subsequence_matches;
  2839. for (Vector<Pair<int, int>> &subsequence_match : all_possible_subsequence_matches) {
  2840. Pair<int, int> match_last_segment = subsequence_match[subsequence_match.size() - 1];
  2841. int next_index = match_last_segment.first + match_last_segment.second;
  2842. // get the last index from current sequence
  2843. // and look for next char starting from that index
  2844. if (target_lower[next_index] == *string_to_complete_char_lower) {
  2845. Vector<Pair<int, int>> new_match = subsequence_match;
  2846. new_match.write[new_match.size() - 1].second++;
  2847. next_subsequence_matches.push_back(new_match);
  2848. if (long_option) {
  2849. continue;
  2850. }
  2851. }
  2852. for (int index : all_ocurence) {
  2853. if (index > next_index) {
  2854. Vector<Pair<int, int>> new_match = subsequence_match;
  2855. new_match.push_back({ index, 1 });
  2856. next_subsequence_matches.push_back(new_match);
  2857. }
  2858. }
  2859. }
  2860. all_possible_subsequence_matches = next_subsequence_matches;
  2861. }
  2862. // go through all possible matches to get the best one as defined by CodeCompletionOptionCompare
  2863. if (all_possible_subsequence_matches.size() > 0) {
  2864. option.matches = all_possible_subsequence_matches[0];
  2865. option.get_option_characteristics(string_to_complete);
  2866. all_possible_subsequence_matches = all_possible_subsequence_matches.slice(1);
  2867. if (all_possible_subsequence_matches.size() > 0) {
  2868. CodeCompletionOptionCompare compare;
  2869. ScriptLanguage::CodeCompletionOption compared_option = option;
  2870. compared_option.clear_characteristics();
  2871. for (Vector<Pair<int, int>> &matches : all_possible_subsequence_matches) {
  2872. compared_option.matches = matches;
  2873. compared_option.get_option_characteristics(string_to_complete);
  2874. if (compare(compared_option, option)) {
  2875. option = compared_option;
  2876. compared_option.clear_characteristics();
  2877. }
  2878. }
  2879. }
  2880. code_completion_options.push_back(option);
  2881. if (theme_cache.font.is_valid()) {
  2882. max_width = MAX(max_width, theme_cache.font->get_string_size(option.display, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width + offset);
  2883. }
  2884. }
  2885. }
  2886. /* No options to complete, cancel. */
  2887. if (code_completion_options.size() == 0) {
  2888. cancel_code_completion();
  2889. return;
  2890. }
  2891. /* A perfect match, stop completion. */
  2892. if (code_completion_options.size() == 1 && string_to_complete == code_completion_options[0].display) {
  2893. cancel_code_completion();
  2894. return;
  2895. }
  2896. code_completion_options.sort_custom<CodeCompletionOptionCompare>();
  2897. code_completion_longest_line = MIN(max_width, theme_cache.code_completion_max_width * theme_cache.font_size);
  2898. code_completion_current_selected = 0;
  2899. code_completion_force_item_center = -1;
  2900. code_completion_active = true;
  2901. queue_redraw();
  2902. }
  2903. void CodeEdit::_lines_edited_from(int p_from_line, int p_to_line) {
  2904. _update_delimiter_cache(p_from_line, p_to_line);
  2905. if (p_from_line == p_to_line) {
  2906. return;
  2907. }
  2908. lines_edited_changed += p_to_line - p_from_line;
  2909. lines_edited_from = (lines_edited_from == -1) ? MIN(p_from_line, p_to_line) : MIN(lines_edited_from, MIN(p_from_line, p_to_line));
  2910. lines_edited_to = (lines_edited_to == -1) ? MAX(p_from_line, p_to_line) : MAX(lines_edited_from, MAX(p_from_line, p_to_line));
  2911. }
  2912. void CodeEdit::_text_set() {
  2913. lines_edited_from = 0;
  2914. lines_edited_to = 9999;
  2915. _text_changed();
  2916. }
  2917. void CodeEdit::_text_changed() {
  2918. if (lines_edited_from == -1) {
  2919. return;
  2920. }
  2921. int lc = get_line_count();
  2922. line_number_digits = 1;
  2923. while (lc /= 10) {
  2924. line_number_digits++;
  2925. }
  2926. if (theme_cache.font.is_valid()) {
  2927. set_gutter_width(line_number_gutter, (line_number_digits + 1) * theme_cache.font->get_char_size('0', theme_cache.font_size).width);
  2928. }
  2929. lc = get_line_count();
  2930. List<int> breakpoints;
  2931. for (const KeyValue<int, bool> &E : breakpointed_lines) {
  2932. breakpoints.push_back(E.key);
  2933. }
  2934. for (const int &line : breakpoints) {
  2935. if (line < lines_edited_from || (line < lc && is_line_breakpointed(line))) {
  2936. continue;
  2937. }
  2938. breakpointed_lines.erase(line);
  2939. emit_signal(SNAME("breakpoint_toggled"), line);
  2940. int next_line = line + lines_edited_changed;
  2941. if (next_line > -1 && next_line < lc && is_line_breakpointed(next_line)) {
  2942. emit_signal(SNAME("breakpoint_toggled"), next_line);
  2943. breakpointed_lines[next_line] = true;
  2944. continue;
  2945. }
  2946. }
  2947. lines_edited_from = -1;
  2948. lines_edited_to = -1;
  2949. lines_edited_changed = 0;
  2950. }
  2951. CodeEdit::CodeEdit() {
  2952. /* Indent management */
  2953. auto_indent_prefixes.insert(':');
  2954. auto_indent_prefixes.insert('{');
  2955. auto_indent_prefixes.insert('[');
  2956. auto_indent_prefixes.insert('(');
  2957. /* Auto brace completion */
  2958. add_auto_brace_completion_pair("(", ")");
  2959. add_auto_brace_completion_pair("{", "}");
  2960. add_auto_brace_completion_pair("[", "]");
  2961. add_auto_brace_completion_pair("\"", "\"");
  2962. add_auto_brace_completion_pair("\'", "\'");
  2963. /* Delimiter tracking */
  2964. add_string_delimiter("\"", "\"", false);
  2965. add_string_delimiter("\'", "\'", false);
  2966. /* Text Direction */
  2967. set_layout_direction(LAYOUT_DIRECTION_LTR);
  2968. set_text_direction(TEXT_DIRECTION_LTR);
  2969. /* Gutters */
  2970. int gutter_idx = 0;
  2971. /* Main Gutter */
  2972. add_gutter();
  2973. set_gutter_name(gutter_idx, "main_gutter");
  2974. set_gutter_draw(gutter_idx, false);
  2975. set_gutter_overwritable(gutter_idx, true);
  2976. set_gutter_type(gutter_idx, GUTTER_TYPE_CUSTOM);
  2977. set_gutter_custom_draw(gutter_idx, callable_mp(this, &CodeEdit::_main_gutter_draw_callback));
  2978. gutter_idx++;
  2979. /* Line numbers */
  2980. add_gutter();
  2981. set_gutter_name(gutter_idx, "line_numbers");
  2982. set_gutter_draw(gutter_idx, false);
  2983. set_gutter_type(gutter_idx, GUTTER_TYPE_CUSTOM);
  2984. set_gutter_custom_draw(gutter_idx, callable_mp(this, &CodeEdit::_line_number_draw_callback));
  2985. gutter_idx++;
  2986. /* Fold Gutter */
  2987. add_gutter();
  2988. set_gutter_name(gutter_idx, "fold_gutter");
  2989. set_gutter_draw(gutter_idx, false);
  2990. set_gutter_type(gutter_idx, GUTTER_TYPE_CUSTOM);
  2991. set_gutter_custom_draw(gutter_idx, callable_mp(this, &CodeEdit::_fold_gutter_draw_callback));
  2992. gutter_idx++;
  2993. connect("lines_edited_from", callable_mp(this, &CodeEdit::_lines_edited_from));
  2994. connect("text_set", callable_mp(this, &CodeEdit::_text_set));
  2995. connect("text_changed", callable_mp(this, &CodeEdit::_text_changed));
  2996. connect("gutter_clicked", callable_mp(this, &CodeEdit::_gutter_clicked));
  2997. connect("gutter_added", callable_mp(this, &CodeEdit::_update_gutter_indexes));
  2998. connect("gutter_removed", callable_mp(this, &CodeEdit::_update_gutter_indexes));
  2999. _update_gutter_indexes();
  3000. }
  3001. CodeEdit::~CodeEdit() {
  3002. }
  3003. // Return true if l should come before r
  3004. bool CodeCompletionOptionCompare::operator()(const ScriptLanguage::CodeCompletionOption &l, const ScriptLanguage::CodeCompletionOption &r) const {
  3005. TypedArray<int> lcharac = l.get_option_cached_characteristics();
  3006. TypedArray<int> rcharac = r.get_option_cached_characteristics();
  3007. if (lcharac != rcharac) {
  3008. return lcharac < rcharac;
  3009. }
  3010. // to get here they need to have the same size so we can take the size of whichever we want
  3011. for (int i = 0; i < l.matches.size(); ++i) {
  3012. if (l.matches[i].first != r.matches[i].first) {
  3013. return l.matches[i].first < r.matches[i].first;
  3014. }
  3015. if (l.matches[i].second != r.matches[i].second) {
  3016. return l.matches[i].second > r.matches[i].second;
  3017. }
  3018. }
  3019. return l.display.naturalnocasecmp_to(r.display) < 0;
  3020. }