CE Find.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. namespace Edit{
  5. /******************************************************************************/
  6. #define RESULT_SEPARATOR "-------------------------------------------"
  7. /******************************************************************************/
  8. Bool Find::FilterScope(Source *source, UInt scope)
  9. {
  10. //Find::OPENED : return source->opened;
  11. if(source)return (scope&CUR_FILE) && CE.cur()==source
  12. || (scope&ACT_APP ) && source->used() && !source->ee_header
  13. || (scope&ESENTHEL) && source->ee_header;
  14. return false;
  15. }
  16. /******************************************************************************/
  17. static Bool FilterText(C Str &t, Memc<Str> &texts, Bool case_sensitive, Bool whole_words)
  18. {
  19. REPA(texts)if(!TextPos(t, texts[i], case_sensitive, whole_words))return false;
  20. return true;
  21. }
  22. /******************************************************************************/
  23. static void FindChanged(Find &find) {find.find ();}
  24. static void FindClose (Find &find) {find.hide ();}
  25. void FindPrev (Find &find) {find.findPrev();}
  26. void FindNext (Find &find) {find.findNext();}
  27. /******************************************************************************/
  28. void Find::find()
  29. {
  30. result.find(visible() ? text() : S, case_sensitive(), whole_words(), (MODE)mode(), scope(), skip_ee());
  31. }
  32. /******************************************************************************/
  33. void Find::findPrev()
  34. {
  35. if(CE.cur())CE.cur()->clearSuggestions();
  36. Memt<VecI2> res;
  37. if(text().is())
  38. {
  39. Memc<Source*> sources; Int offset=CE.curI(); FREPA(CE.sources){Source &s=CE.sources[Mod(i+offset, CE.sources.elms())]; if(FilterScope(&s, scope()))sources.add(&s);} // list all possible sources
  40. if(sources.elms())
  41. for(Int s=sources.elms(); s>=0; s--)
  42. {
  43. Source &src =*sources[Mod(s, sources.elms())];
  44. Bool cur =(CE.cur()==&src),
  45. cur_start=(cur && s==sources.elms()), // if we're starting search
  46. cur_end =(cur && s== 0); // if we're ending search
  47. Int y_from =(cur_start ? src.cur.y : src.lines.elms()-1),
  48. y_to =(cur_end ? src.cur.y : 0);
  49. for(Int y=y_from; y>=y_to; y--)if(InRange(y, src.lines))
  50. {
  51. res.clear();
  52. CChar *line=src.lines[y]; for(Int offset=0; ; )
  53. {
  54. Int match_length, pos=TextPosSkipSpaceI(line+offset, text(), match_length, case_sensitive(), whole_words());
  55. if(pos<0)break; offset+=pos; res.add(VecI2(offset, match_length)); offset+=match_length; // add all results to container
  56. }
  57. REPA(res) // go from last one
  58. {
  59. VecI2 r=res[i];
  60. if(cur_start && y==src.cur.y) // if we're starting search
  61. {
  62. Int cur_x=((src.sel.y==src.cur.y && src.sel.x>=0) ? src.sel.x : src.cur.x); // use 'sel' instead of 'cur' if possible
  63. if(r.x>=cur_x)continue; // if result is completely after cursor then ignore
  64. }
  65. src.sel.set(r.x , y);
  66. src.cur.set(r.x+r.y, y);
  67. if(!CE.view_mode())src.makeCurVisible(true, !cur);
  68. else src.highlight (y , !cur);
  69. CE.cur(&src);
  70. return;
  71. }
  72. }
  73. }
  74. }
  75. }
  76. /******************************************************************************/
  77. void Find::findNext()
  78. {
  79. if(CE.cur())CE.cur()->clearSuggestions();
  80. if(text().is())
  81. {
  82. Memc<Source*> sources; Int offset=CE.curI(); FREPA(CE.sources){Source &s=CE.sources[Mod(i+offset, CE.sources.elms())]; if(FilterScope(&s, scope()))sources.add(&s);} // list all possible sources
  83. if(sources.elms())
  84. for(Int s=0; s<sources.elms()+1; s++)
  85. {
  86. Source &src =*sources[Mod(s, sources.elms())];
  87. Bool cur =(CE.cur()==&src),
  88. cur_start=(cur && s== 0), // if we're starting search
  89. cur_end =(cur && s==sources.elms()); // if we're ending search
  90. Int y_from =(cur_start ? src.cur.y : 0),
  91. y_to =(cur_end ? src.cur.y : src.lines.elms()-1);
  92. for(Int y=y_from; y<=y_to; y++)if(InRange(y, src.lines))
  93. {
  94. CChar *line=src.lines[y]; for(Int offset=0; ; )
  95. {
  96. Int match_length, pos=TextPosSkipSpaceI(line+offset, text(), match_length, case_sensitive(), whole_words());
  97. if(pos<0)break; offset+=pos;
  98. if(cur_start && y==src.cur.y) // if we're starting search
  99. if(offset+match_length<=src.cur.x){offset+=match_length; continue;} // if result is completely before cursor then ignore
  100. src.sel.set(offset , y);
  101. src.cur.set(offset+match_length, y);
  102. if(!CE.view_mode())src.makeCurVisible(true, !cur);
  103. else src.highlight (y , !cur);
  104. CE.cur(&src);
  105. return;
  106. }
  107. }
  108. }
  109. }
  110. }
  111. /******************************************************************************/
  112. void Find::toggle() {if(Gui.ms()==&CE.b_find)visibleToggleActivate();else if(Gui.kb()!=&text)activate();else hide();}
  113. /******************************************************************************/
  114. void Find::create()
  115. {
  116. CChar8 *mode_t[]=
  117. {
  118. "Symbol (exact)",
  119. "Symbol (nearest)",
  120. "Text in Files",
  121. };
  122. Gui+=super::create().hide();
  123. T+=text .create( ).func(FindChanged, T).desc("Use Up/Down Arrow Keys to use previous searches");
  124. T+=prev .create("<").func(FindPrev , T).focusable(false).desc(S+"Previous Search Result\nKeyboard Shortcut: "+Kb.ctrlCmdName()+"+Shift+D");
  125. T+=next .create(">").func(FindNext , T).focusable(false).desc(S+"Next Search Result\nKeyboard Shortcut: "+Kb.ctrlCmdName()+"+D" );
  126. T+=close .create( ).func(FindClose , T).focusable(false); close.image="Gui/close.img"; close.skin=&EmptyGuiSkin;
  127. T+=case_sensitive.create("case sensitive" ).func(FindChanged, T).focusable(false).desc("Keyboard Shortcut: Alt+C" ); case_sensitive.mode=BUTTON_TOGGLE;
  128. T+=whole_words .create("whole words" ).func(FindChanged, T).focusable(false).desc("Keyboard Shortcut: Alt+W" ); whole_words .mode=BUTTON_TOGGLE;
  129. T+=mode .create(mode_t, Elms(mode_t)).set(0).valid(true).func(FindChanged, T); mode.tab(0).desc("Keyboard Shortcut: Alt+S"); mode.tab(1).desc("Keyboard Shortcut: Alt+N"); mode.tab(2).desc("Keyboard Shortcut: Alt+T");
  130. T+=cur_file .create("Current File" ).func(FindChanged, T).focusable(false).desc("If perform the search in current file\nKeyboard Shortcut: Alt+F" ); cur_file .mode=BUTTON_TOGGLE; cur_file .set(true, QUIET);
  131. T+=active_app .create("Active Application" ).func(FindChanged, T).focusable(false).desc("If perform the search in active application\nKeyboard Shortcut: Alt+A"); active_app .mode=BUTTON_TOGGLE; active_app.set(true, QUIET);
  132. T+=engine .create("Engine" ).func(FindChanged, T).focusable(false).desc("If perform the search in the engine headers\nKeyboard Shortcut: Alt+E"); engine .mode=BUTTON_TOGGLE; engine .set(true, QUIET);
  133. T+=skip_ee .create("Skip \"EE\" namespace").func(FindChanged, T).focusable(false); skip_ee.mode=BUTTON_TOGGLE; skip_ee.set(true);
  134. slidebar[0].del(); slidebar[1].del(); view.del();
  135. result.create();
  136. history_pos=0;
  137. }
  138. /******************************************************************************/
  139. void Find::resize()
  140. {
  141. Flt x=0.0025f, //D.pixelToScreenSize().x,
  142. y=0.0025f, //D.pixelToScreenSize().y,
  143. b=y*4 ,
  144. h=y*17;
  145. rect(Rect_RU(Vec2(D.w()-0.12f, D.h()), x*270, h*5 + b*5 + b));
  146. close .rect(Rect_RU( rect().w() , -b, h , h));
  147. next .rect(Rect_RU(close.rect().min.x-b, -b, h*0.9f, h));
  148. prev .rect(Rect_RU(next .rect().min.x , -b, h*0.9f, h));
  149. text .rect(Rect (b, -b-h, prev.rect().min.x-b, -b));
  150. case_sensitive.rect(Rect (b, -b-h-b-h, rect().w()/2-b/2, -b-h-b));
  151. whole_words .rect(Rect (rect().w()/2+b/2, -b-h-b-h, rect().w()-b, -b-h-b));
  152. mode .rect(Rect (b, -b-h-b-h-b-h, rect().w()-b, -b-h-b-h-b), 0, true);
  153. cur_file .rect(Rect (b, -b-h-b-h-b-h-b-h, rect().w()*0.3f, -b-h-b-h-b-h-b));
  154. engine .rect(Rect (rect().w()*0.76f, -b-h-b-h-b-h-b-h, rect().w()-b, -b-h-b-h-b-h-b));
  155. active_app .rect(Rect (cur_file.rect().max.x, -b-h-b-h-b-h-b-h, engine.rect().min.x, -b-h-b-h-b-h-b));
  156. skip_ee .rect(Rect (rect().w()/2+b/2, -b-h-b-h-b-h-b-h-b-h, rect().w()-b, -b-h-b-h-b-h-b-h-b));
  157. result.resize();
  158. }
  159. /******************************************************************************/
  160. Find& Find::hide()
  161. {
  162. Bool visible=T.visible();
  163. super::hide();
  164. result.hide();
  165. if(visible)FindChanged(T); // this will clear results when hidden
  166. return T;
  167. }
  168. /******************************************************************************/
  169. Find& Find::show()
  170. {
  171. Bool hidden=T.hidden();
  172. super::show();
  173. if(hidden){text.selectAll().activate(); FindChanged(T);}
  174. return T;
  175. }
  176. /******************************************************************************/
  177. static Bool SelectedText(Str &t, Bool &force)
  178. {
  179. if(Source *s=CE.cur())
  180. if(InRange(s->cur.y, s->lines))
  181. {
  182. Line &line=s->lines[s->cur.y];
  183. Int from, to;
  184. if(force=(s->sel.x>=0 && s->sel.y==s->cur.y))
  185. {
  186. from=Min(s->sel.x, s->cur.x);
  187. to =Max(s->sel.x, s->cur.x);
  188. }else
  189. {
  190. from=line.wordBegin(s->cur.x);
  191. to =line.wordEnd ( from);
  192. }
  193. for(; from<to; from++)t+=line[from];
  194. return true;
  195. }
  196. return false;
  197. }
  198. Find& Find::activate()
  199. {
  200. Str t; Bool force;
  201. // copy text from cursor position to find box
  202. if(hidden())
  203. if(SelectedText(t, force))
  204. {
  205. if(!force)
  206. {
  207. if(t=='.')t.clear(); // clear if the cursor is at '.' because searching for that means that all symbols will be listed which may take some time
  208. t=SkipWhiteChars(t); // if selection consists only of spaces then clear it
  209. }
  210. if(t.is())
  211. {
  212. text.set(t); // if there's some text specified then use it for search, otherwise keep the old search
  213. historyAdd(t);
  214. }
  215. }
  216. super::activate();
  217. text.selectAll().activate();
  218. return T;
  219. }
  220. /******************************************************************************/
  221. void Find::historyAdd(C Str &text)
  222. {
  223. if(history.elms()>=64)history.remove(0, true);
  224. REPA(history)if(Equal(history[i], text))history.remove(i, true);
  225. history.add(text);
  226. history_pos=history.elms()-1;
  227. }
  228. void Find::historySet(Int delta)
  229. {
  230. Clamp(history_pos+=delta, 0, history.elms()-1);
  231. if(InRange(history_pos, history))text.set(history[history_pos]).selectAll();
  232. }
  233. void Find::update(C GuiPC &gpc)
  234. {
  235. super::update(gpc);
  236. if(visible())
  237. {
  238. if(contains(Gui.kb()))
  239. {
  240. if((Kb.k(KB_ESC ) || Kb.k(KB_NAV_BACK)) && Kb.k.first()){hide(); Kb.eatKey();}else
  241. if((Kb.k(KB_ENTER) || Kb.k(KB_NPENTER )) && Kb.k.first())
  242. {
  243. CE.markCurPos();
  244. historyAdd(text());
  245. findNext();
  246. hide();
  247. Kb.eatKey();
  248. }else
  249. if(Kb.k(KB_UP ))historySet(+1);else
  250. if(Kb.k(KB_DOWN))historySet(-1);
  251. if(Kb.k(KB_C) && Kb.k.alt()){case_sensitive.push(); Kb.eatKey();}
  252. if(Kb.k(KB_W) && Kb.k.alt()){whole_words .push(); Kb.eatKey();}
  253. if(Kb.k(KB_S) && Kb.k.alt()){mode .set(0); Kb.eatKey();}
  254. if(Kb.k(KB_N) && Kb.k.alt()){mode .set(1); Kb.eatKey();}
  255. if(Kb.k(KB_T) && Kb.k.alt()){mode .set(2); Kb.eatKey();}
  256. if(Kb.k(KB_F) && Kb.k.alt()){cur_file .push(); Kb.eatKey();}
  257. if(Kb.k(KB_A) && Kb.k.alt()){active_app .push(); Kb.eatKey();}
  258. if(Kb.k(KB_E) && Kb.k.alt()){engine .push(); Kb.eatKey();}
  259. }else
  260. if(result.contains(Gui.kb()))
  261. if((Kb.k(KB_ESC) || Kb.k(KB_NAV_BACK)) && Kb.k.first()){hide(); Kb.eatKey();}
  262. Bool ms=(contains(Gui.ms()) || result.contains(Gui.ms())),
  263. kb=(contains(Gui.kb()) || result.contains(Gui.kb()));
  264. result.visible(ms || kb);
  265. if(ms && Ms.bp(2))hide();
  266. }
  267. }
  268. /******************************************************************************/
  269. void Find::ResultRegion::update(C GuiPC &gpc)
  270. {
  271. super::update(gpc);
  272. Bool sel=(Gui.ms()==&list && Ms.bp(0)); REPA(Touches)if(Touches[i].guiObj()==&list && Touches[i].rs())sel=true;
  273. if( sel)
  274. if(Result *result=list())
  275. {
  276. if(result->symbol )CE.jumpTo(result->symbol());else
  277. if(result->source_loc.is())
  278. {
  279. Source *cur=CE.cur();
  280. if(CE.load(result->source_loc))
  281. if(result->line>=0)
  282. CE.cur()->highlight(result->line, cur!=CE.cur());
  283. }else
  284. {
  285. result->opened^=1;
  286. setData();
  287. }
  288. }
  289. }
  290. /******************************************************************************/
  291. void Find::ResultRegion::create()
  292. {
  293. Gui+=super::create().hide();
  294. ListColumn lc[]=
  295. {
  296. ListColumn(MEMBER(Result, text), LCW_DATA, "text"),
  297. };
  298. T+=list.create(lc, Elms(lc), true).skin(&CE.find_result_skin); list.cur_mode=LCM_ALWAYS;
  299. }
  300. /******************************************************************************/
  301. void Find::ResultRegion::resize()
  302. {
  303. rect(Rect(D.w()*0.27f, -D.h()*0.85f, D.w()-0.07f, CE.find.rect().min.y));
  304. if(TextStyle *text_style=list.getTextStyle())list.elmHeight(text_style->lineHeight()).textSize(text_style->size.y);
  305. }
  306. /******************************************************************************/
  307. static Int CompareSymbolPath(C Str &a, C Str &b)
  308. {
  309. Str ap=SymbolToPath(a),
  310. bp=SymbolToPath(b);
  311. if(Int c=ComparePath(GetPath(ap), GetPath(bp)))return c;
  312. return ComparePath( ap , bp );
  313. }
  314. /******************************************************************************/
  315. static Int CompareResult (C Find::ResultRegion::Result &a, C Find::ResultRegion::Result &b) {return CompareSymbolPath(a.text , b.text );}
  316. static Int CompareResultPriority(C Find::ResultRegion::Result &a, C Find::ResultRegion::Result &b) {return Compare (b.priority, a.priority);}
  317. static Int CompareResultAlphabet(C Find::ResultRegion::Result &a, C Find::ResultRegion::Result &b) {return Compare (a.text , b.text );}
  318. /******************************************************************************/
  319. static void SetVisible(Memt<Bool> &visible, Memx<Find::ResultRegion::Result> &results, Bool parent_visible=true)
  320. {
  321. FREPA(results)
  322. {
  323. Find::ResultRegion::Result &r=results[i];
  324. visible.add(parent_visible);
  325. SetVisible(visible, r.children, parent_visible && r.opened);
  326. }
  327. }
  328. /******************************************************************************/
  329. void Find::ResultRegion::setData()
  330. {
  331. Memt<Bool> visible; SetVisible(visible, data);
  332. list.setDataNode(data, visible);
  333. }
  334. /******************************************************************************/
  335. void Find::ResultRegion::find(C Str &text, Bool case_sensitive, Bool whole_words, MODE mode, UInt scope, Bool skip_ee)
  336. {
  337. data.clear();
  338. Str t =SymbolToPath(Replace(Replace(text, "::", "."), DIV, '\0'));
  339. Bool full_name=Contains (t, SEP);
  340. if(mode==EXACT)
  341. {
  342. Memc<Result> temp;
  343. Memc<Str > texts;
  344. texts=Split(text, L' '); REPA(texts)if(!texts[i].is())texts.remove(i);
  345. if(texts.elms())
  346. {
  347. // files
  348. temp.clear(); FREPA(CE.sources)if(FilterScope(&CE.sources[i], scope) && FilterText(CE.sources[i].loc.base_name, texts, case_sensitive, whole_words))
  349. {
  350. if(!temp.elms()){if(data.elms())data.New(); data.New().text=RESULT_SEPARATOR; data.New(); data.New().text="FILES:"; data.New();}
  351. temp.New().setText(CE.sources[i].loc.asText(), CE.sources[i].loc);
  352. }
  353. temp.sort(CompareResult); REPA(temp)if(i)if(!Equal(GetPath(temp[i].text), GetPath(temp[i-1].text), true))temp.NewAt(i); FREPA(temp)Swap(data.New(), temp[i]);
  354. }
  355. texts=Split(t, L' '); REPA(texts)if(!texts[i].is())texts.remove(i);
  356. if(texts.elms())
  357. {
  358. // macros
  359. temp.clear(); FREPA(ProjectMacros)
  360. {
  361. Macro &macro=ProjectMacros[i];
  362. if(FilterScope(macro.source, scope) && FilterText(macro.name, texts, case_sensitive, whole_words))
  363. {
  364. if(!temp.elms()){if(data.elms())data.New(); data.New().text=RESULT_SEPARATOR; data.New(); data.New().text="MACROS:"; data.New();}
  365. temp.New().setText(macro.name, macro.source ? macro.source->loc : SourceLoc(), macro.line);
  366. }
  367. }
  368. temp.sort(CompareResultAlphabet); FREPA(temp)Swap(data.New(), temp[i]);
  369. // typedefs
  370. temp.clear(); FREPA(Symbols){Symbol &symbol=Symbols.lockedData(i); if(symbol.type==Symbol::TYPEDEF && FilterScope(symbol.source, scope) && FilterText(full_name ? symbol.full_name : symbol, texts, case_sensitive, whole_words) && !symbol.insideFunc())
  371. {
  372. if(!temp.elms()){if(data.elms())data.New(); data.New().text=RESULT_SEPARATOR; data.New(); data.New().text="TYPEDEFS:"; data.New();}
  373. temp.New().set(skip_ee, symbol.full_name, &symbol);
  374. }}
  375. temp.sort(CompareResult); REPA(temp)if(i)if(!Equal(SymbolGetPath(temp[i].text), SymbolGetPath(temp[i-1].text), true))temp.NewAt(i); FREPA(temp)Swap(data.New(), temp[i]);
  376. // enums
  377. temp.clear(); FREPA(Symbols){Symbol &symbol=Symbols.lockedData(i); if((symbol.type==Symbol::ENUM || symbol.type==Symbol::ENUM_ELM) && !(symbol.modifiers&Symbol::MODIF_NAMELESS) && FilterScope(symbol.source, scope) && FilterText(full_name ? symbol.full_name : symbol, texts, case_sensitive, whole_words) && !symbol.insideFunc())
  378. {
  379. if(!temp.elms()){if(data.elms())data.New(); data.New().text=RESULT_SEPARATOR; data.New(); data.New().text="ENUMS:"; data.New();}
  380. temp.New().set(skip_ee, symbol.full_name, &symbol);
  381. }}
  382. temp.sort(CompareResult); REPA(temp)if(i)if(!Equal(SymbolGetPath(temp[i].text), SymbolGetPath(temp[i-1].text), true))temp.NewAt(i); FREPA(temp)Swap(data.New(), temp[i]);
  383. // namespaces
  384. temp.clear(); FREPA(Symbols){Symbol &symbol=Symbols.lockedData(i); if(symbol.type==Symbol::NAMESPACE && !(symbol.modifiers&Symbol::MODIF_NAMELESS) && FilterText(full_name ? symbol.full_name : symbol, texts, case_sensitive, whole_words) && !symbol.insideFunc())
  385. {
  386. if(!temp.elms()){if(data.elms())data.New(); data.New().text=RESULT_SEPARATOR; data.New(); data.New().text="NAMESPACES:"; data.New();}
  387. temp.New().set(skip_ee, symbol.full_name, &symbol);
  388. }}
  389. temp.sort(CompareResult); REPA(temp)if(i)if(!Equal(SymbolGetPath(temp[i].text), SymbolGetPath(temp[i-1].text), true))temp.NewAt(i); FREPA(temp)Swap(data.New(), temp[i]);
  390. // classes
  391. temp.clear(); FREPA(Symbols){Symbol &symbol=Symbols.lockedData(i); if(symbol.type==Symbol::CLASS && !(symbol.modifiers&Symbol::MODIF_NAMELESS) && FilterScope(symbol.source, scope) && FilterText(full_name ? symbol.full_name : symbol, texts, case_sensitive, whole_words))
  392. {
  393. if(!temp.elms()){if(data.elms())data.New(); data.New().text=RESULT_SEPARATOR; data.New(); data.New().text="CLASSES:"; data.New();}
  394. temp.New().set(skip_ee, symbol.full_name, &symbol);
  395. }}
  396. temp.sort(CompareResult); REPA(temp)if(i)if(!Equal(SymbolGetPath(temp[i].text), SymbolGetPath(temp[i-1].text), true))temp.NewAt(i); FREPA(temp)Swap(data.New(), temp[i]);
  397. // classes extending
  398. temp.clear(); FREPA(Symbols){Symbol &symbol=Symbols.lockedData(i); if(symbol.type==Symbol::CLASS && !(symbol.modifiers&Symbol::MODIF_NAMELESS) && FilterScope(symbol.source, scope))
  399. {
  400. FREPA(symbol.base)if(FilterText(full_name ? symbol.base[i]->full_name : *symbol.base[i], texts, case_sensitive, whole_words))
  401. {
  402. if(!temp.elms()){if(data.elms())data.New(); data.New().text=RESULT_SEPARATOR; data.New(); data.New().text=S+"CLASSES EXTENDING \""+text+"\":"; data.New();}
  403. temp.New().set(skip_ee, symbol.full_name, &symbol);
  404. break;
  405. }
  406. }}
  407. temp.sort(CompareResult); REPA(temp)if(i)if(!Equal(SymbolGetPath(temp[i].text), SymbolGetPath(temp[i-1].text), true))temp.NewAt(i); FREPA(temp)Swap(data.New(), temp[i]);
  408. // functions
  409. temp.clear(); FREPA(Symbols){Symbol &symbol=Symbols.lockedData(i); if(symbol.type==Symbol::FUNC_LIST && !(symbol.modifiers&Symbol::MODIF_CTOR_DTOR) && FilterText(full_name ? symbol.full_name : symbol, texts, case_sensitive, whole_words) && !symbol.insideFunc())
  410. {
  411. FREPA(symbol.funcs)if(FilterScope(symbol.funcs[i]->source, scope)) // if at least one function is in scope
  412. {
  413. if(!temp.elms()){if(data.elms())data.New(); data.New().text=RESULT_SEPARATOR; data.New(); data.New().text="FUNCTIONS:"; data.New();}
  414. // add all functions within scope
  415. Memc<SymbolPtr> funcs; FREPA(symbol.funcs)if(FilterScope(symbol.funcs[i]->source, scope))funcs.add(symbol.funcs[i]);
  416. // remove double functions (definition/declaration)
  417. REPA(funcs){Symbol *a=funcs[i](); REPD(j, i)if(a->sameFunc(*funcs[j])){funcs.remove((a->modifiers&Symbol::MODIF_FUNC_BODY) ? i : j, true); break;}} // remove the one with function body (because it doesn't have default parameters listed, and full class path)
  418. if(funcs.elms()==1)temp.New().set(false, SkipWhiteChars(funcs[0]->definition()), funcs[0]);else // if only one function
  419. {
  420. // add functions list and its children
  421. Result &func_list=temp.New().set(skip_ee, symbol.full_name+ELLIPSIS, null);
  422. FREPA(funcs)func_list.children.New().set(false, S+" "+SkipWhiteChars(funcs[i]->definition()), funcs[i]);
  423. }
  424. break;
  425. }
  426. }}
  427. temp.sort(CompareResult); REPA(temp)if(i)if(!Equal(SymbolGetPath(temp[i].text), SymbolGetPath(temp[i-1].text), true))temp.NewAt(i); FREPA(temp)Swap(data.New(), temp[i]);
  428. // variables
  429. temp.clear(); FREPA(Symbols){Symbol &symbol=Symbols.lockedData(i); if(symbol.type==Symbol::VAR && FilterScope(symbol.source, scope) && FilterText(full_name ? symbol.full_name : symbol, texts, case_sensitive, whole_words) && !symbol.insideFunc())
  430. {
  431. if(!temp.elms()){if(data.elms())data.New(); data.New().text=RESULT_SEPARATOR; data.New(); data.New().text="VARIABLES:"; data.New();}
  432. temp.New().set(skip_ee, symbol.full_name, &symbol);
  433. }}
  434. temp.sort(CompareResult); REPA(temp)if(i)if(!Equal(SymbolGetPath(temp[i].text), SymbolGetPath(temp[i-1].text), true))temp.NewAt(i); FREPA(temp)Swap(data.New(), temp[i]);
  435. }
  436. }else
  437. if(mode==NEAREST)
  438. {
  439. if(t.is())
  440. {
  441. // symbols
  442. REPA(Symbols)
  443. {
  444. Symbol &symbol=Symbols.lockedData(i);
  445. if(!(symbol.modifiers&Symbol::MODIF_SKIP_SUGGESTIONS) && symbol.valid && symbol.type!=Symbol::PREPROC && symbol.type!=Symbol::TYPENAME && !symbol.insideFunc())
  446. if(Int p=SuggestionsPriority(full_name ? symbol.full_name : symbol, t, FlagTest(symbol.modifiers, Symbol::MODIF_ALL_UP_CASE)))
  447. {
  448. if(symbol.type==Symbol::FUNC_LIST)
  449. {
  450. FREPA(symbol.funcs)if(FilterScope(symbol.funcs[i]->source, scope)) // if at least one function is in scope
  451. {
  452. // add all functions within scope
  453. Memc<SymbolPtr> funcs; FREPA(symbol.funcs)if(FilterScope(symbol.funcs[i]->source, scope))funcs.add(symbol.funcs[i]);
  454. // remove double functions (definition/declaration)
  455. REPA(funcs){Symbol *a=funcs[i](); REPD(j, i)if(a->sameFunc(*funcs[j])){funcs.remove((a->modifiers&Symbol::MODIF_FUNC_BODY) ? i : j, true); break;}} // remove the one with function body (because it doesn't have default parameters listed, and full class path)
  456. if(funcs.elms()==1)data.New().set(false, SkipWhiteChars(funcs[0]->definition()), funcs[0], p);else // if only one function
  457. {
  458. // add functions list and its children
  459. Result &func_list=data.New().set(skip_ee, symbol.full_name+ELLIPSIS, null, p);
  460. FREPA(funcs)func_list.children.New().set(false, S+" "+SkipWhiteChars(funcs[i]->definition()), funcs[i]);
  461. }
  462. break;
  463. }
  464. }else
  465. if(FilterScope(symbol.source, scope))
  466. {
  467. data.New().set(skip_ee, symbol.full_name, &symbol, p);
  468. }
  469. }
  470. }
  471. // macros
  472. REPA(ProjectMacros)
  473. {
  474. Macro &macro=ProjectMacros[i];
  475. if(FilterScope(macro.source, scope))
  476. if(Int p=SuggestionsPriority(macro.name, t, macro.all_up_case))
  477. data.New().setText(macro.name, macro.source ? macro.source->loc : SourceLoc(), macro.line).priority=p;
  478. }
  479. }
  480. data.sort(CompareResultPriority);
  481. }else
  482. if(mode==FILES)
  483. {
  484. Source *last_source=null;
  485. FREPA(CE.sources)
  486. {
  487. Source &s=CE.sources[i]; if(FilterScope(&s, scope))FREPA(s.lines)
  488. {
  489. if(Contains(s.lines[i], text, case_sensitive, whole_words))
  490. {
  491. if(last_source!=&s){last_source=&s; if(data.elms())data.New(); data.New().setText(S+'"'+s.loc.asText()+"\":", s.loc);} // add "FILE:"
  492. data.New().setText(S+" "+SkipWhiteChars(s.lines[i]), s.loc, i);
  493. }
  494. }
  495. }
  496. }
  497. setData();
  498. }
  499. /******************************************************************************/
  500. static CChar8 *scope_t[]=
  501. {
  502. "Selection",
  503. "Current File",
  504. };
  505. enum REPLACE_SCOPE
  506. {
  507. RS_SEL,
  508. RS_CUR_FILE,
  509. };
  510. void ReplaceProcess(ReplaceText &r) {r.process();}
  511. void ReplaceText::process()
  512. {
  513. if(Source *src=CE.cur())
  514. {
  515. if(src->Const){Gui.msgBox(S, "This file can't be changed"); return;}
  516. Str start, text, end;
  517. if(src->lines.elms())
  518. {
  519. Int last_line_length=-1;
  520. VecI2 min, max;
  521. if(scope()==RS_SEL)
  522. {
  523. if(src->sel.x<0){Gui.msgBox(S, "Selection is empty"); return;}
  524. src->curSel(min, max);
  525. src->writeAll(start, VecI2(0), min);
  526. src->writeAll(text , min , max);
  527. src->writeAll(end , max , VecI2(src->lines.last().length(), src->lines.elms()-1));
  528. if(InRange(max.y, src->lines))last_line_length=src->lines[max.y].length();
  529. }else text=src->asText();
  530. text=Replace(text, T.src(), T.dest(), case_sensitive(), whole_words());
  531. src->setUndo();
  532. src->fromText(start+text+end);
  533. // adjust cursor/selection
  534. if(last_line_length>=0 && InRange(max.y, src->lines))
  535. {
  536. Int delta=src->lines[max.y].length()-last_line_length;
  537. ((src->cur==max) ? src->cur : src->sel).x+=delta;
  538. }
  539. }
  540. }else Gui.msgBox(S, "No file opened");
  541. }
  542. static void CaseSensitive(ReplaceText &r) {r.case_sensitive.push();}
  543. static void WholeWords (ReplaceText &r) {r.whole_words .push();}
  544. void ReplaceText::create()
  545. {
  546. Flt y=-0.04f, h=0.055f, s=h+0.01f;
  547. Gui+=super::create(Rect_C(0, 0, 1.05f, 0.47f), "Replace").hide(); button[2].func(CodeEditor::HideAndFocusCE, SCAST(GuiObj, T)).show();
  548. T+=t_src .create(Vec2(0.09f, y), "Find" ); T+=src .create(Rect_L(0.18f, y, 0.82f, h)); y-=s;
  549. T+=t_dest .create(Vec2(0.09f, y), "Replace"); T+=dest .create(Rect_L(0.18f, y, 0.82f, h)); y-=s;
  550. T+=t_scope.create(Vec2(0.09f, y), "Scope" ); T+=scope.create(Rect_L(0.18f, y, 0.82f, h), scope_t, Elms(scope_t)); y-=s;
  551. T+=case_sensitive.create(Rect_R(clientWidth()/2-0.03f, y, 0.3f, h), "case sensitive").desc("Keyboard Shortcut: Alt+C"); case_sensitive.mode=BUTTON_TOGGLE;
  552. T+=whole_words .create(Rect_L(clientWidth()/2+0.03f, y, 0.3f, h), "whole words" ).desc("Keyboard Shortcut: Alt+W"); whole_words .mode=BUTTON_TOGGLE; y-=s;
  553. y-=s/2;
  554. T+=replace .create(Rect_C(clientWidth()*1.0f/4, y, 0.3f, 0.06f), "Replace All").func(ReplaceProcess, T).desc("Keyboard Shortcut: Enter");
  555. T+=cancel .create(Rect_C(clientWidth()*3.0f/4, y, 0.3f, 0.06f), "Cancel").func(button[2].func(), button[2].funcUser()); y-=s;
  556. Node<MenuElm> node;
  557. node.New().create("Case Sensitive", CaseSensitive , T).kbsc(KbSc(KB_C, KBSC_ALT));
  558. node.New().create("Whole Words" , WholeWords , T).kbsc(KbSc(KB_W, KBSC_ALT));
  559. node.New().create("Replace" , ReplaceProcess, T).kbsc(KbSc(KB_ENTER));
  560. T+=menu.create(node);
  561. }
  562. ReplaceText& ReplaceText::show()
  563. {
  564. if(hidden())
  565. {
  566. super::show();
  567. Str t; Bool force; if(!src().is() && SelectedText(t, force))src.set(t);
  568. src.selectAll().kbSet();
  569. Source *src=CE.cur();
  570. scope.set((src && src->sel.x>=0) ? RS_SEL : RS_CUR_FILE);
  571. }
  572. return T;
  573. }
  574. /******************************************************************************/
  575. }}
  576. /******************************************************************************/