2
0

TextModel.cs 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. namespace Terminal.Gui.Views;
  2. /// <summary>
  3. /// Represents the underlying data model for managing and manipulating multi-line text in a <see cref="TextView"/>.
  4. /// </summary>
  5. /// <remarks>
  6. /// The <see cref="TextModel"/> class provides functionality for storing, retrieving, and modifying lines of text,
  7. /// as well as supporting operations like word navigation, text search, and file loading. It is used internally
  8. /// by text input controls such as <see cref="TextView"/> to manage text content.
  9. /// </remarks>
  10. internal class TextModel
  11. {
  12. private List<List<Cell>> _lines = new ();
  13. private (Point startPointToFind, Point currentPointToFind, bool found) _toFind;
  14. /// <summary>The number of text lines in the model</summary>
  15. public int Count => _lines.Count;
  16. public string? FilePath { get; set; }
  17. /// <summary>Adds a line to the model at the specified position.</summary>
  18. /// <param name="pos">Line number where the line will be inserted.</param>
  19. /// <param name="cells">The line of text and color, as a List of Cell.</param>
  20. public void AddLine (int pos, List<Cell> cells) { _lines.Insert (pos, cells); }
  21. public bool CloseFile ()
  22. {
  23. if (FilePath is null)
  24. {
  25. throw new ArgumentNullException (nameof (FilePath));
  26. }
  27. FilePath = null;
  28. _lines = new ();
  29. return true;
  30. }
  31. public List<List<Cell>> GetAllLines () { return _lines; }
  32. /// <summary>Returns the specified line as a List of Rune</summary>
  33. /// <returns>The line.</returns>
  34. /// <param name="line">Line number to retrieve.</param>
  35. public List<Cell> GetLine (int line)
  36. {
  37. if (_lines.Count > 0)
  38. {
  39. if (line < Count)
  40. {
  41. return _lines [line];
  42. }
  43. return _lines [Count - 1];
  44. }
  45. _lines.Add (new ());
  46. return _lines [0];
  47. }
  48. /// <summary>Returns the maximum line length of the visible lines.</summary>
  49. /// <param name="first">The first line.</param>
  50. /// <param name="last">The last line.</param>
  51. /// <param name="tabWidth">The tab width.</param>
  52. public int GetMaxVisibleLine (int first, int last, int tabWidth)
  53. {
  54. var maxLength = 0;
  55. last = last < _lines.Count ? last : _lines.Count;
  56. for (int i = first; i < last; i++)
  57. {
  58. List<Cell> line = GetLine (i);
  59. int tabSum = line.Sum (c => c.Grapheme == "\t" ? Math.Max (tabWidth - 1, 0) : 0);
  60. int l = line.Count + tabSum;
  61. if (l > maxLength)
  62. {
  63. maxLength = l;
  64. }
  65. }
  66. return maxLength;
  67. }
  68. public event EventHandler? LinesLoaded;
  69. public bool LoadFile (string file)
  70. {
  71. FilePath = file ?? throw new ArgumentNullException (nameof (file));
  72. using (FileStream stream = File.OpenRead (file))
  73. {
  74. LoadStream (stream);
  75. return true;
  76. }
  77. }
  78. public void LoadListCells (List<List<Cell>> cellsList, Attribute? attribute)
  79. {
  80. _lines = cellsList;
  81. SetAttributes (attribute);
  82. OnLinesLoaded ();
  83. }
  84. public void LoadCells (List<Cell> cells, Attribute? attribute)
  85. {
  86. _lines = Cell.ToCells (cells);
  87. SetAttributes (attribute);
  88. OnLinesLoaded ();
  89. }
  90. public void LoadStream (Stream input)
  91. {
  92. if (input is null)
  93. {
  94. throw new ArgumentNullException (nameof (input));
  95. }
  96. _lines = new ();
  97. var buff = new BufferedStream (input);
  98. int v;
  99. List<byte> line = new ();
  100. var wasNewLine = false;
  101. while ((v = buff.ReadByte ()) != -1)
  102. {
  103. if (v == 13)
  104. {
  105. continue;
  106. }
  107. if (v == 10)
  108. {
  109. Append (line);
  110. line.Clear ();
  111. wasNewLine = true;
  112. continue;
  113. }
  114. line.Add ((byte)v);
  115. wasNewLine = false;
  116. }
  117. if (line.Count > 0 || wasNewLine)
  118. {
  119. Append (line);
  120. }
  121. buff.Dispose ();
  122. OnLinesLoaded ();
  123. }
  124. public void LoadString (string content)
  125. {
  126. _lines = Cell.StringToLinesOfCells (content);
  127. OnLinesLoaded ();
  128. }
  129. /// <summary>Removes the line at the specified position</summary>
  130. /// <param name="pos">Position.</param>
  131. public void RemoveLine (int pos)
  132. {
  133. if (_lines.Count > 0)
  134. {
  135. if (_lines.Count == 1 && _lines [0].Count == 0)
  136. {
  137. return;
  138. }
  139. _lines.RemoveAt (pos);
  140. }
  141. }
  142. public void ReplaceLine (int pos, List<Cell> runes)
  143. {
  144. if (_lines.Count > 0 && pos < _lines.Count)
  145. {
  146. _lines [pos] = [.. runes];
  147. }
  148. else if (_lines.Count == 0 || (_lines.Count > 0 && pos >= _lines.Count))
  149. {
  150. _lines.Add (runes);
  151. }
  152. }
  153. public override string ToString ()
  154. {
  155. var sb = new StringBuilder ();
  156. for (var i = 0; i < _lines.Count; i++)
  157. {
  158. sb.Append (Cell.ToString (_lines [i]));
  159. if (i + 1 < _lines.Count)
  160. {
  161. sb.AppendLine ();
  162. }
  163. }
  164. return sb.ToString ();
  165. }
  166. public (int col, int row)? WordBackward (int fromCol, int fromRow, bool useSameRuneType)
  167. {
  168. if (fromRow == 0 && fromCol == 0)
  169. {
  170. return null;
  171. }
  172. int col = Math.Max (fromCol - 1, 0);
  173. int row = fromRow;
  174. try
  175. {
  176. Cell? cell = RuneAt (col, row);
  177. Rune rune;
  178. if (cell is { })
  179. {
  180. rune = Rune.GetRuneAt (cell.Value.Grapheme, 0);
  181. }
  182. else
  183. {
  184. if (col > 0)
  185. {
  186. return (col, row);
  187. }
  188. if (col == 0 && row > 0)
  189. {
  190. row--;
  191. List<Cell> line = GetLine (row);
  192. return (line.Count, row);
  193. }
  194. return null;
  195. }
  196. RuneType runeType = GetRuneType (rune);
  197. int lastValidCol = IsSameRuneType (rune, runeType, useSameRuneType) && (Rune.IsLetterOrDigit (rune) || Rune.IsPunctuation (rune) || Rune.IsSymbol (rune))
  198. ? col
  199. : -1;
  200. void ProcMovePrev (ref int nCol, ref int nRow, Rune nRune)
  201. {
  202. if (Rune.IsWhiteSpace (nRune))
  203. {
  204. while (MovePrev (ref nCol, ref nRow, out nRune, useSameRuneType))
  205. {
  206. lastValidCol = nCol;
  207. if (Rune.IsLetterOrDigit (nRune) || Rune.IsPunctuation (nRune) || Rune.IsSymbol (nRune))
  208. {
  209. rune = nRune;
  210. runeType = GetRuneType (nRune);
  211. }
  212. }
  213. if (lastValidCol > -1)
  214. {
  215. nCol = lastValidCol;
  216. nRow = fromRow;
  217. }
  218. if ((!Rune.IsWhiteSpace (nRune) && Rune.IsWhiteSpace (rune))
  219. || (Rune.IsWhiteSpace (nRune) && !Rune.IsWhiteSpace (rune)))
  220. {
  221. return;
  222. }
  223. if (nRow != fromRow && (Rune.IsLetterOrDigit (nRune) || Rune.IsPunctuation (nRune) || Rune.IsSymbol (nRune)))
  224. {
  225. List<Cell> line = GetLine (nRow);
  226. if (lastValidCol > -1)
  227. {
  228. nCol = lastValidCol + Math.Max (lastValidCol, line.Count);
  229. }
  230. }
  231. }
  232. else
  233. {
  234. if (!MovePrev (ref nCol, ref nRow, out nRune, useSameRuneType))
  235. {
  236. if (lastValidCol > -1)
  237. {
  238. nCol = lastValidCol;
  239. nRow = fromRow;
  240. }
  241. return;
  242. }
  243. List<Cell> line = GetLine (nRow);
  244. Rune firstRune = Rune.GetRuneAt (line [0].Grapheme, 0);
  245. if (nCol == 0
  246. && nRow == fromRow
  247. && (Rune.IsLetterOrDigit (firstRune) || Rune.IsPunctuation (firstRune) || Rune.IsSymbol (firstRune)))
  248. {
  249. return;
  250. }
  251. lastValidCol =
  252. (IsSameRuneType (nRune, runeType, useSameRuneType) && Rune.IsLetterOrDigit (nRune)) || Rune.IsPunctuation (nRune) || Rune.IsSymbol (nRune)
  253. ? nCol
  254. : lastValidCol;
  255. if (lastValidCol > -1 && Rune.IsWhiteSpace (nRune))
  256. {
  257. nCol = lastValidCol;
  258. return;
  259. }
  260. if (fromRow != nRow)
  261. {
  262. nCol = line.Count;
  263. return;
  264. }
  265. ProcMovePrev (ref nCol, ref nRow, nRune);
  266. }
  267. }
  268. ProcMovePrev (ref col, ref row, rune);
  269. if (fromCol != col || fromRow != row)
  270. {
  271. return (col, row);
  272. }
  273. if (fromCol == col && fromRow == row && row > 0)
  274. {
  275. row--;
  276. List<Cell> line = GetLine (row);
  277. col = line.Count;
  278. return (col, row);
  279. }
  280. return null;
  281. }
  282. catch (Exception)
  283. {
  284. return null;
  285. }
  286. }
  287. public (int col, int row)? WordForward (int fromCol, int fromRow, bool useSameRuneType)
  288. {
  289. if (fromRow == _lines.Count - 1 && fromCol == GetLine (_lines.Count - 1).Count)
  290. {
  291. return null;
  292. }
  293. int col = fromCol;
  294. int row = fromRow;
  295. try
  296. {
  297. Rune rune = _lines [row].Count > 0 ? Rune.GetRuneAt (RuneAt (col, row)!.Value.Grapheme, 0) : default (Rune);
  298. RuneType runeType = GetRuneType (rune);
  299. int lastValidCol = IsSameRuneType (rune, runeType, useSameRuneType) && (Rune.IsLetterOrDigit (rune) || Rune.IsPunctuation (rune) || Rune.IsSymbol (rune))
  300. ? col
  301. : -1;
  302. void ProcMoveNext (ref int nCol, ref int nRow, Rune nRune)
  303. {
  304. if (Rune.IsWhiteSpace (nRune))
  305. {
  306. while (MoveNext (ref nCol, ref nRow, out nRune, useSameRuneType))
  307. {
  308. lastValidCol = nCol;
  309. if (Rune.IsLetterOrDigit (nRune) || Rune.IsPunctuation (nRune) || Rune.IsSymbol (nRune))
  310. {
  311. return;
  312. }
  313. }
  314. lastValidCol = nCol;
  315. if (!Rune.IsWhiteSpace (nRune) && Rune.IsWhiteSpace (rune))
  316. {
  317. return;
  318. }
  319. if (nRow != fromRow && (Rune.IsLetterOrDigit (nRune) || Rune.IsPunctuation (nRune) || Rune.IsSymbol (nRune)))
  320. {
  321. if (lastValidCol > -1)
  322. {
  323. nCol = lastValidCol;
  324. }
  325. return;
  326. }
  327. if (lastValidCol > -1)
  328. {
  329. nCol = lastValidCol;
  330. nRow = fromRow;
  331. }
  332. }
  333. else
  334. {
  335. if (!MoveNext (ref nCol, ref nRow, out nRune, useSameRuneType))
  336. {
  337. return;
  338. }
  339. lastValidCol = nCol;
  340. if (!IsSameRuneType (nRune, runeType, useSameRuneType) && !Rune.IsWhiteSpace (nRune))
  341. {
  342. return;
  343. }
  344. List<Cell> line = GetLine (nRow);
  345. Rune firstRune = Rune.GetRuneAt (line [0].Grapheme, 0);
  346. if (nCol == line.Count
  347. && nRow == fromRow
  348. && (Rune.IsLetterOrDigit (firstRune) || Rune.IsPunctuation (firstRune) || Rune.IsSymbol (firstRune)))
  349. {
  350. return;
  351. }
  352. if (fromRow != nRow)
  353. {
  354. nCol = 0;
  355. return;
  356. }
  357. ProcMoveNext (ref nCol, ref nRow, nRune);
  358. }
  359. }
  360. ProcMoveNext (ref col, ref row, rune);
  361. if (fromCol != col || fromRow != row)
  362. {
  363. return (col, row);
  364. }
  365. return null;
  366. }
  367. catch (Exception)
  368. {
  369. return null;
  370. }
  371. }
  372. public (int startCol, int col, int row)? ProcessDoubleClickSelection (int fromStartCol, int fromCol, int fromRow, bool useSameRuneType, bool selectWordOnly)
  373. {
  374. List<Cell> line = GetLine (fromRow);
  375. int startCol = fromStartCol;
  376. int col = fromCol;
  377. int row = fromRow;
  378. (int col, int row)? newPos = WordForward (col, row, useSameRuneType);
  379. if (newPos.HasValue)
  380. {
  381. col = row == newPos.Value.row ? newPos.Value.col : 0;
  382. }
  383. if (startCol > 0
  384. && StringExtensions.ToString (line.GetRange (startCol, col - startCol).Select (c => c.Grapheme).ToList ()).Trim () == ""
  385. && (col - startCol > 1 || (col - startCol > 0 && line [startCol - 1].Grapheme == " ")))
  386. {
  387. while (startCol > 0 && line [startCol - 1].Grapheme == " ")
  388. {
  389. startCol--;
  390. }
  391. }
  392. else
  393. {
  394. newPos = WordBackward (col, row, useSameRuneType);
  395. if (newPos is { })
  396. {
  397. startCol = row == newPos.Value.row ? newPos.Value.col : line.Count;
  398. }
  399. }
  400. if (selectWordOnly)
  401. {
  402. List<string> selText = line.GetRange (startCol, col - startCol).Select (c => c.Grapheme).ToList ();
  403. if (StringExtensions.ToString (selText).Trim () != "")
  404. {
  405. for (int i = selText.Count - 1; i > -1; i--)
  406. {
  407. if (selText [i] == " ")
  408. {
  409. col--;
  410. }
  411. }
  412. }
  413. }
  414. if (fromStartCol != startCol || fromCol != col || fromRow != row)
  415. {
  416. return (startCol, col, row);
  417. }
  418. return null;
  419. }
  420. internal static int CalculateLeftColumn (List<Cell> t, int start, int end, int width, int tabWidth = 0)
  421. {
  422. List<string> strings = new ();
  423. foreach (Cell cell in t)
  424. {
  425. strings.Add (cell.Grapheme);
  426. }
  427. return CalculateLeftColumn (strings, start, end, width, tabWidth);
  428. }
  429. // Returns the left column in a range of the string.
  430. internal static int CalculateLeftColumn (List<string> t, int start, int end, int width, int tabWidth = 0)
  431. {
  432. if (t is null || t.Count == 0)
  433. {
  434. return 0;
  435. }
  436. var size = 0;
  437. int tCount = end > t.Count - 1 ? t.Count - 1 : end;
  438. var col = 0;
  439. for (int i = tCount; i >= 0; i--)
  440. {
  441. string text = t [i];
  442. size += text.GetColumns (false);
  443. if (text == "\t")
  444. {
  445. size += tabWidth + 1;
  446. }
  447. if (size > width)
  448. {
  449. if (col + width == end)
  450. {
  451. col++;
  452. }
  453. break;
  454. }
  455. if ((end < t.Count && col > 0 && start < end && col == start) || end - col == width - 1)
  456. {
  457. break;
  458. }
  459. col = i;
  460. }
  461. return col;
  462. }
  463. internal static (int size, int length) DisplaySize (
  464. List<Cell> t,
  465. int start = -1,
  466. int end = -1,
  467. bool checkNextText = true,
  468. int tabWidth = 0
  469. )
  470. {
  471. List<string> strings = new ();
  472. foreach (Cell cell in t)
  473. {
  474. strings.Add (cell.Grapheme);
  475. }
  476. return DisplaySize (strings, start, end, checkNextText, tabWidth);
  477. }
  478. // Returns the size and length in a range of the string.
  479. internal static (int size, int length) DisplaySize (
  480. List<string> t,
  481. int start = -1,
  482. int end = -1,
  483. bool checkNextRune = true,
  484. int tabWidth = 0
  485. )
  486. {
  487. if (t is null || t.Count == 0)
  488. {
  489. return (0, 0);
  490. }
  491. var size = 0;
  492. var len = 0;
  493. int tCount = end == -1 ? t.Count :
  494. end > t.Count ? t.Count : end;
  495. int i = start == -1 ? 0 : start;
  496. for (; i < tCount; i++)
  497. {
  498. string text = t [i];
  499. size += text.GetColumns (false);
  500. len += text.Length;
  501. if (text == "\t")
  502. {
  503. size += tabWidth + 1;
  504. len += tabWidth - 1;
  505. }
  506. if (checkNextRune && i == tCount - 1 && t.Count > tCount && IsWideText (t [i + 1], tabWidth, out int s, out int l))
  507. {
  508. size += s;
  509. len += l;
  510. }
  511. }
  512. bool IsWideText (string s1, int tWidth, out int s, out int l)
  513. {
  514. s = s1.GetColumns ();
  515. l = Encoding.Unicode.GetByteCount (s1);
  516. if (s1 == "\t")
  517. {
  518. s += tWidth + 1;
  519. l += tWidth - 1;
  520. }
  521. return s > 1;
  522. }
  523. return (size, len);
  524. }
  525. internal Size GetDisplaySize ()
  526. {
  527. var size = Size.Empty;
  528. return size;
  529. }
  530. internal (Point current, bool found) FindNextText (
  531. string text,
  532. out bool gaveFullTurn,
  533. bool matchCase = false,
  534. bool matchWholeWord = false
  535. )
  536. {
  537. if (text is null || _lines.Count == 0)
  538. {
  539. gaveFullTurn = false;
  540. return (Point.Empty, false);
  541. }
  542. if (_toFind.found)
  543. {
  544. _toFind.currentPointToFind.X++;
  545. }
  546. (Point current, bool found) foundPos = GetFoundNextTextPoint (
  547. text,
  548. _lines.Count,
  549. matchCase,
  550. matchWholeWord,
  551. _toFind.currentPointToFind
  552. );
  553. if (!foundPos.found && _toFind.currentPointToFind != _toFind.startPointToFind)
  554. {
  555. foundPos = GetFoundNextTextPoint (
  556. text,
  557. _toFind.startPointToFind.Y + 1,
  558. matchCase,
  559. matchWholeWord,
  560. Point.Empty
  561. );
  562. }
  563. gaveFullTurn = ApplyToFind (foundPos);
  564. return foundPos;
  565. }
  566. internal (Point current, bool found) FindPreviousText (
  567. string text,
  568. out bool gaveFullTurn,
  569. bool matchCase = false,
  570. bool matchWholeWord = false
  571. )
  572. {
  573. if (text is null || _lines.Count == 0)
  574. {
  575. gaveFullTurn = false;
  576. return (Point.Empty, false);
  577. }
  578. if (_toFind.found)
  579. {
  580. _toFind.currentPointToFind.X++;
  581. }
  582. int linesCount = _toFind.currentPointToFind.IsEmpty ? _lines.Count - 1 : _toFind.currentPointToFind.Y;
  583. (Point current, bool found) foundPos = GetFoundPreviousTextPoint (
  584. text,
  585. linesCount,
  586. matchCase,
  587. matchWholeWord,
  588. _toFind.currentPointToFind
  589. );
  590. if (!foundPos.found && _toFind.currentPointToFind != _toFind.startPointToFind)
  591. {
  592. foundPos = GetFoundPreviousTextPoint (
  593. text,
  594. _lines.Count - 1,
  595. matchCase,
  596. matchWholeWord,
  597. new (_lines [_lines.Count - 1].Count, _lines.Count)
  598. );
  599. }
  600. gaveFullTurn = ApplyToFind (foundPos);
  601. return foundPos;
  602. }
  603. internal static int GetColFromX (List<Cell> t, int start, int x, int tabWidth = 0)
  604. {
  605. List<string> strings = new ();
  606. foreach (Cell cell in t)
  607. {
  608. strings.Add (cell.Grapheme);
  609. }
  610. return GetColFromX (strings, start, x, tabWidth);
  611. }
  612. internal static int GetColFromX (List<string> t, int start, int x, int tabWidth = 0)
  613. {
  614. if (x < 0)
  615. {
  616. return x;
  617. }
  618. int size = start;
  619. int pX = x + start;
  620. for (int i = start; i < t.Count; i++)
  621. {
  622. string s = t [i];
  623. size += s.GetColumns ();
  624. if (s == "\t")
  625. {
  626. size += tabWidth + 1;
  627. }
  628. if (i == pX || size > pX)
  629. {
  630. return i - start;
  631. }
  632. }
  633. return t.Count - start;
  634. }
  635. internal (Point current, bool found) ReplaceAllText (
  636. string text,
  637. bool matchCase = false,
  638. bool matchWholeWord = false,
  639. string? textToReplace = null
  640. )
  641. {
  642. var found = false;
  643. var pos = Point.Empty;
  644. for (var i = 0; i < _lines.Count; i++)
  645. {
  646. List<Cell> x = _lines [i];
  647. string txt = GetText (x);
  648. string matchText = !matchCase ? text.ToUpper () : text;
  649. int col = txt.IndexOf (matchText);
  650. while (col > -1)
  651. {
  652. if (matchWholeWord && !MatchWholeWord (txt, matchText, col))
  653. {
  654. if (col + 1 > txt.Length)
  655. {
  656. break;
  657. }
  658. col = txt.IndexOf (matchText, col + 1);
  659. continue;
  660. }
  661. if (col > -1)
  662. {
  663. if (!found)
  664. {
  665. found = true;
  666. }
  667. _lines [i] = Cell.ToCellList (ReplaceText (x, textToReplace!, matchText, col));
  668. x = _lines [i];
  669. txt = GetText (x);
  670. pos = new (col, i);
  671. col += textToReplace!.Length - matchText.Length;
  672. }
  673. if (col < 0 || col + 1 > txt.Length)
  674. {
  675. break;
  676. }
  677. col = txt.IndexOf (matchText, col + 1);
  678. }
  679. }
  680. string GetText (List<Cell> x)
  681. {
  682. var txt = Cell.ToString (x);
  683. if (!matchCase)
  684. {
  685. txt = txt.ToUpper ();
  686. }
  687. return txt;
  688. }
  689. return (pos, found);
  690. }
  691. /// <summary>Redefine column and line tracking.</summary>
  692. /// <param name="point">Contains the column and line.</param>
  693. internal void ResetContinuousFind (Point point)
  694. {
  695. _toFind.startPointToFind = _toFind.currentPointToFind = point;
  696. _toFind.found = false;
  697. }
  698. internal static bool SetCol (ref int col, int width, int cols)
  699. {
  700. if (col + cols <= width)
  701. {
  702. col += cols;
  703. return true;
  704. }
  705. return false;
  706. }
  707. private void Append (List<byte> line)
  708. {
  709. var str = StringExtensions.ToString (line.ToArray ());
  710. _lines.Add (Cell.StringToCells (str));
  711. }
  712. private bool ApplyToFind ((Point current, bool found) foundPos)
  713. {
  714. var gaveFullTurn = false;
  715. if (foundPos.found)
  716. {
  717. _toFind.currentPointToFind = foundPos.current;
  718. if (_toFind.found && _toFind.currentPointToFind == _toFind.startPointToFind)
  719. {
  720. gaveFullTurn = true;
  721. }
  722. if (!_toFind.found)
  723. {
  724. _toFind.startPointToFind = _toFind.currentPointToFind = foundPos.current;
  725. _toFind.found = foundPos.found;
  726. }
  727. }
  728. return gaveFullTurn;
  729. }
  730. private (Point current, bool found) GetFoundNextTextPoint (
  731. string text,
  732. int linesCount,
  733. bool matchCase,
  734. bool matchWholeWord,
  735. Point start
  736. )
  737. {
  738. for (int i = start.Y; i < linesCount; i++)
  739. {
  740. List<Cell> x = _lines [i];
  741. var txt = Cell.ToString (x);
  742. if (!matchCase)
  743. {
  744. txt = txt.ToUpper ();
  745. }
  746. string matchText = !matchCase ? text.ToUpper () : text;
  747. int col = txt.IndexOf (matchText, Math.Min (start.X, txt.Length));
  748. if (col > -1 && matchWholeWord && !MatchWholeWord (txt, matchText, col))
  749. {
  750. continue;
  751. }
  752. if (col > -1 && ((i == start.Y && col >= start.X) || i > start.Y) && txt.Contains (matchText))
  753. {
  754. return (new (col, i), true);
  755. }
  756. if (col == -1 && start.X > 0)
  757. {
  758. start.X = 0;
  759. }
  760. }
  761. return (Point.Empty, false);
  762. }
  763. private (Point current, bool found) GetFoundPreviousTextPoint (
  764. string text,
  765. int linesCount,
  766. bool matchCase,
  767. bool matchWholeWord,
  768. Point start
  769. )
  770. {
  771. for (int i = linesCount; i >= 0; i--)
  772. {
  773. List<Cell> x = _lines [i];
  774. var txt = Cell.ToString (x);
  775. if (!matchCase)
  776. {
  777. txt = txt.ToUpper ();
  778. }
  779. if (start.Y != i)
  780. {
  781. start.X = Math.Max (x.Count - 1, 0);
  782. }
  783. string matchText = !matchCase ? text.ToUpper () : text;
  784. int col = txt.LastIndexOf (matchText, _toFind.found ? start.X - 1 : start.X);
  785. if (col > -1 && matchWholeWord && !MatchWholeWord (txt, matchText, col))
  786. {
  787. continue;
  788. }
  789. if (col > -1 && ((i <= linesCount && col <= start.X) || i < start.Y) && txt.Contains (matchText))
  790. {
  791. return (new (col, i), true);
  792. }
  793. }
  794. return (Point.Empty, false);
  795. }
  796. private RuneType GetRuneType (Rune rune)
  797. {
  798. if (Rune.IsSymbol (rune))
  799. {
  800. return RuneType.IsSymbol;
  801. }
  802. if (Rune.IsWhiteSpace (rune))
  803. {
  804. return RuneType.IsWhiteSpace;
  805. }
  806. if (Rune.IsLetterOrDigit (rune))
  807. {
  808. return RuneType.IsLetterOrDigit;
  809. }
  810. if (Rune.IsPunctuation (rune))
  811. {
  812. return RuneType.IsPunctuation;
  813. }
  814. return RuneType.IsUnknown;
  815. }
  816. private bool IsSameRuneType (Rune newRune, RuneType runeType, bool useSameRuneType)
  817. {
  818. RuneType rt = GetRuneType (newRune);
  819. if (useSameRuneType)
  820. {
  821. return rt == runeType;
  822. }
  823. switch (runeType)
  824. {
  825. case RuneType.IsSymbol:
  826. case RuneType.IsPunctuation:
  827. return rt is RuneType.IsSymbol or RuneType.IsPunctuation;
  828. case RuneType.IsWhiteSpace:
  829. case RuneType.IsLetterOrDigit:
  830. case RuneType.IsUnknown:
  831. return rt == runeType;
  832. default:
  833. throw new ArgumentOutOfRangeException (nameof (runeType), runeType, null);
  834. }
  835. }
  836. private bool MatchWholeWord (string source, string matchText, int index = 0)
  837. {
  838. if (string.IsNullOrEmpty (source) || string.IsNullOrEmpty (matchText))
  839. {
  840. return false;
  841. }
  842. string txt = matchText.Trim ();
  843. int start = index > 0 ? index - 1 : 0;
  844. int end = index + txt.Length;
  845. if ((start == 0 || Rune.IsWhiteSpace ((Rune)source [start])) && (end == source.Length || Rune.IsWhiteSpace ((Rune)source [end])))
  846. {
  847. return true;
  848. }
  849. return false;
  850. }
  851. private bool MoveNext (ref int col, ref int row, out Rune rune, bool useSameRuneType)
  852. {
  853. List<Cell> line = GetLine (row);
  854. if (col + 1 < line.Count)
  855. {
  856. col++;
  857. rune = Rune.GetRuneAt (line [col].Grapheme, 0);
  858. Rune prevRune = Rune.GetRuneAt (line [col - 1].Grapheme, 0);
  859. if (col + 1 == line.Count
  860. && !Rune.IsLetterOrDigit (rune)
  861. && !Rune.IsWhiteSpace (prevRune)
  862. && IsSameRuneType (prevRune, GetRuneType (rune), useSameRuneType))
  863. {
  864. col++;
  865. }
  866. prevRune = Rune.GetRuneAt (line [col - 1].Grapheme, 0);
  867. if (!Rune.IsWhiteSpace (rune)
  868. && (Rune.IsWhiteSpace (prevRune) || !IsSameRuneType (prevRune, GetRuneType (rune), useSameRuneType)))
  869. {
  870. return false;
  871. }
  872. return true;
  873. }
  874. if (col + 1 == line.Count)
  875. {
  876. col++;
  877. rune = default (Rune);
  878. return false;
  879. }
  880. // End of line
  881. col = 0;
  882. row++;
  883. rune = default (Rune);
  884. return false;
  885. }
  886. private bool MovePrev (ref int col, ref int row, out Rune rune, bool useSameRuneType)
  887. {
  888. List<Cell> line = GetLine (row);
  889. if (col > 0)
  890. {
  891. col--;
  892. rune = Rune.GetRuneAt (line [col].Grapheme, 0);
  893. Rune nextRune = Rune.GetRuneAt (line [col + 1].Grapheme, 0);
  894. if ((!Rune.IsWhiteSpace (rune)
  895. && !Rune.IsWhiteSpace (nextRune)
  896. && !IsSameRuneType (nextRune, GetRuneType (rune), useSameRuneType))
  897. || (Rune.IsWhiteSpace (rune) && !Rune.IsWhiteSpace (nextRune)))
  898. {
  899. return false;
  900. }
  901. return true;
  902. }
  903. rune = default (Rune);
  904. return false;
  905. }
  906. private void OnLinesLoaded () { LinesLoaded?.Invoke (this, EventArgs.Empty); }
  907. private string ReplaceText (List<Cell> source, string textToReplace, string matchText, int col)
  908. {
  909. var origTxt = Cell.ToString (source);
  910. (_, int len) = DisplaySize (source, 0, col, false);
  911. (_, int len2) = DisplaySize (source, col, col + matchText.Length, false);
  912. (_, int len3) = DisplaySize (source, col + matchText.Length, origTxt.GetRuneCount (), false);
  913. return origTxt [..len] + textToReplace + origTxt.Substring (len + len2, len3);
  914. }
  915. private Cell? RuneAt (int col, int row)
  916. {
  917. List<Cell> line = GetLine (row);
  918. if (line.Count > 0)
  919. {
  920. return line [col > line.Count - 1 ? line.Count - 1 : col];
  921. }
  922. return null;
  923. }
  924. private void SetAttributes (Attribute? attribute)
  925. {
  926. foreach (List<Cell> line in _lines)
  927. {
  928. for (var i = 0; i < line.Count; i++)
  929. {
  930. Cell cell = line [i];
  931. cell.Attribute ??= attribute;
  932. line [i] = cell;
  933. }
  934. }
  935. }
  936. private enum RuneType
  937. {
  938. IsSymbol,
  939. IsWhiteSpace,
  940. IsLetterOrDigit,
  941. IsPunctuation,
  942. IsUnknown
  943. }
  944. }