TextView.Insert.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. namespace Terminal.Gui.Views;
  2. public partial class TextView
  3. {
  4. /// <summary>
  5. /// Inserts the given <paramref name="toAdd"/> text at the current cursor position exactly as if the user had just
  6. /// typed it
  7. /// </summary>
  8. /// <param name="toAdd">Text to add</param>
  9. public void InsertText (string toAdd)
  10. {
  11. foreach (char ch in toAdd)
  12. {
  13. Key key;
  14. try
  15. {
  16. key = new (ch);
  17. }
  18. catch (Exception)
  19. {
  20. throw new ArgumentException (
  21. $"Cannot insert character '{ch}' because it does not map to a Key"
  22. );
  23. }
  24. InsertText (key);
  25. if (NeedsDraw)
  26. {
  27. AdjustScrollPosition ();
  28. }
  29. else
  30. {
  31. PositionCursor ();
  32. }
  33. }
  34. }
  35. private void Insert (Cell cell)
  36. {
  37. List<Cell> line = GetCurrentLine ();
  38. if (Used)
  39. {
  40. line.Insert (Math.Min (CurrentColumn, line.Count), cell);
  41. }
  42. else
  43. {
  44. if (CurrentColumn < line.Count)
  45. {
  46. line.RemoveAt (CurrentColumn);
  47. }
  48. line.Insert (Math.Min (CurrentColumn, line.Count), cell);
  49. }
  50. int prow = CurrentRow - _topRow;
  51. if (!_wrapNeeded)
  52. {
  53. // BUGBUG: customized rect aren't supported now because the Redraw isn't using the Intersect method.
  54. //SetNeedsDraw (new (0, prow, Math.Max (Viewport.Width, 0), Math.Max (prow + 1, 0)));
  55. SetNeedsDraw ();
  56. }
  57. }
  58. private void InsertAllText (string text, bool fromClipboard = false)
  59. {
  60. if (string.IsNullOrEmpty (text))
  61. {
  62. return;
  63. }
  64. List<List<Cell>> lines;
  65. if (fromClipboard && text == _copiedText)
  66. {
  67. lines = _copiedCellsList;
  68. }
  69. else
  70. {
  71. // Get selected attribute
  72. Attribute? attribute = GetSelectedAttribute (CurrentRow, CurrentColumn);
  73. lines = Cell.StringToLinesOfCells (text, attribute);
  74. }
  75. if (lines.Count == 0)
  76. {
  77. return;
  78. }
  79. SetWrapModel ();
  80. List<Cell> line = GetCurrentLine ();
  81. _historyText.Add ([new (line)], CursorPosition);
  82. // Optimize single line
  83. if (lines.Count == 1)
  84. {
  85. line.InsertRange (CurrentColumn, lines [0]);
  86. CurrentColumn += lines [0].Count;
  87. _historyText.Add (
  88. [new (line)],
  89. CursorPosition,
  90. TextEditingLineStatus.Replaced
  91. );
  92. if (!_wordWrap && CurrentColumn - _leftColumn > Viewport.Width)
  93. {
  94. _leftColumn = Math.Max (CurrentColumn - Viewport.Width + 1, 0);
  95. }
  96. if (_wordWrap)
  97. {
  98. SetNeedsDraw ();
  99. }
  100. else
  101. {
  102. // BUGBUG: customized rect aren't supported now because the Redraw isn't using the Intersect method.
  103. //SetNeedsDraw (new (0, currentRow - topRow, Viewport.Width, Math.Max (currentRow - topRow + 1, 0)));
  104. SetNeedsDraw ();
  105. }
  106. UpdateWrapModel ();
  107. OnContentsChanged ();
  108. return;
  109. }
  110. List<Cell>? rest = null;
  111. var lastPosition = 0;
  112. if (_model.Count > 0 && line.Count > 0 && !_copyWithoutSelection)
  113. {
  114. // Keep a copy of the rest of the line
  115. int restCount = line.Count - CurrentColumn;
  116. rest = line.GetRange (CurrentColumn, restCount);
  117. line.RemoveRange (CurrentColumn, restCount);
  118. }
  119. // First line is inserted at the current location, the rest is appended
  120. line.InsertRange (CurrentColumn, lines [0]);
  121. //model.AddLine (currentRow, lines [0]);
  122. List<List<Cell>> addedLines = [new (line)];
  123. for (var i = 1; i < lines.Count; i++)
  124. {
  125. _model.AddLine (CurrentRow + i, lines [i]);
  126. addedLines.Add ([.. lines [i]]);
  127. }
  128. if (rest is { })
  129. {
  130. List<Cell> last = _model.GetLine (CurrentRow + lines.Count - 1);
  131. lastPosition = last.Count;
  132. last.InsertRange (last.Count, rest);
  133. addedLines.Last ().InsertRange (addedLines.Last ().Count, rest);
  134. }
  135. _historyText.Add (addedLines, CursorPosition, TextEditingLineStatus.Added);
  136. // Now adjust column and row positions
  137. CurrentRow += lines.Count - 1;
  138. CurrentColumn = rest is { } ? lastPosition : lines [^1].Count;
  139. AdjustScrollPosition ();
  140. _historyText.Add (
  141. [new (line)],
  142. CursorPosition,
  143. TextEditingLineStatus.Replaced
  144. );
  145. UpdateWrapModel ();
  146. OnContentsChanged ();
  147. }
  148. private bool InsertText (Key a, Attribute? attribute = null)
  149. {
  150. //So that special keys like tab can be processed
  151. if (_isReadOnly)
  152. {
  153. return true;
  154. }
  155. SetWrapModel ();
  156. _historyText.Add ([new (GetCurrentLine ())], CursorPosition);
  157. if (IsSelecting)
  158. {
  159. ClearSelectedRegion ();
  160. }
  161. if ((uint)a.KeyCode == '\n')
  162. {
  163. _model.AddLine (CurrentRow + 1, []);
  164. CurrentRow++;
  165. CurrentColumn = 0;
  166. }
  167. else if ((uint)a.KeyCode == '\r')
  168. {
  169. CurrentColumn = 0;
  170. }
  171. else
  172. {
  173. if (Used)
  174. {
  175. Insert (new () { Grapheme = a.AsRune.ToString (), Attribute = attribute });
  176. CurrentColumn++;
  177. if (CurrentColumn >= _leftColumn + Viewport.Width)
  178. {
  179. _leftColumn++;
  180. SetNeedsDraw ();
  181. }
  182. }
  183. else
  184. {
  185. Insert (new () { Grapheme = a.AsRune.ToString (), Attribute = attribute });
  186. CurrentColumn++;
  187. }
  188. }
  189. _historyText.Add (
  190. [new (GetCurrentLine ())],
  191. CursorPosition,
  192. TextEditingLineStatus.Replaced
  193. );
  194. UpdateWrapModel ();
  195. OnContentsChanged ();
  196. return true;
  197. }
  198. #region History Event Handlers
  199. private void HistoryText_ChangeText (object sender, HistoryTextItemEventArgs obj)
  200. {
  201. SetWrapModel ();
  202. if (obj is { })
  203. {
  204. int startLine = obj.CursorPosition.Y;
  205. if (obj.RemovedOnAdded is { })
  206. {
  207. int offset;
  208. if (obj.IsUndoing)
  209. {
  210. offset = Math.Max (obj.RemovedOnAdded.Lines.Count - obj.Lines.Count, 1);
  211. }
  212. else
  213. {
  214. offset = obj.RemovedOnAdded.Lines.Count - 1;
  215. }
  216. for (var i = 0; i < offset; i++)
  217. {
  218. if (Lines > obj.RemovedOnAdded.CursorPosition.Y)
  219. {
  220. _model.RemoveLine (obj.RemovedOnAdded.CursorPosition.Y);
  221. }
  222. else
  223. {
  224. break;
  225. }
  226. }
  227. }
  228. for (var i = 0; i < obj.Lines.Count; i++)
  229. {
  230. if (i == 0 || obj.LineStatus == TextEditingLineStatus.Original || obj.LineStatus == TextEditingLineStatus.Attribute)
  231. {
  232. _model.ReplaceLine (startLine, obj.Lines [i]);
  233. }
  234. else if (obj is { IsUndoing: true, LineStatus: TextEditingLineStatus.Removed }
  235. or { IsUndoing: false, LineStatus: TextEditingLineStatus.Added })
  236. {
  237. _model.AddLine (startLine, obj.Lines [i]);
  238. }
  239. else if (Lines > obj.CursorPosition.Y + 1)
  240. {
  241. _model.RemoveLine (obj.CursorPosition.Y + 1);
  242. }
  243. startLine++;
  244. }
  245. CursorPosition = obj.FinalCursorPosition;
  246. }
  247. UpdateWrapModel ();
  248. AdjustScrollPosition ();
  249. OnContentsChanged ();
  250. }
  251. #endregion
  252. }