Autocomplete.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  6. using Rune = System.Rune;
  7. namespace Terminal.Gui {
  8. /// <summary>
  9. /// Renders an overlay on another view at a given point that allows selecting
  10. /// from a range of 'autocomplete' options.
  11. /// </summary>
  12. public abstract class Autocomplete : IAutocomplete {
  13. private class Popup : View {
  14. Autocomplete autocomplete;
  15. public Popup (Autocomplete autocomplete)
  16. {
  17. this.autocomplete = autocomplete;
  18. CanFocus = true;
  19. WantMousePositionReports = true;
  20. }
  21. public override Rect Frame {
  22. get => base.Frame;
  23. set {
  24. base.Frame = value;
  25. X = value.X;
  26. Y = value.Y;
  27. Width = value.Width;
  28. Height = value.Height;
  29. }
  30. }
  31. public override void Redraw (Rect bounds)
  32. {
  33. if (autocomplete.LastPopupPos == null) {
  34. return;
  35. }
  36. autocomplete.RenderOverlay ((Point)autocomplete.LastPopupPos);
  37. }
  38. public override bool MouseEvent (MouseEvent mouseEvent)
  39. {
  40. return autocomplete.MouseEvent (mouseEvent);
  41. }
  42. }
  43. private View top, popup;
  44. private bool closed;
  45. int toRenderLength;
  46. private Point? LastPopupPos { get; set; }
  47. private ColorScheme colorScheme;
  48. private View hostControl;
  49. /// <summary>
  50. /// The host control to handle.
  51. /// </summary>
  52. public virtual View HostControl {
  53. get => hostControl;
  54. set {
  55. hostControl = value;
  56. top = hostControl.SuperView;
  57. if (top != null) {
  58. top.DrawContent += Top_DrawContent;
  59. top.DrawContentComplete += Top_DrawContentComplete;
  60. top.Removed += Top_Removed;
  61. }
  62. }
  63. }
  64. private void Top_Removed (View obj)
  65. {
  66. Visible = false;
  67. ManipulatePopup ();
  68. }
  69. private void Top_DrawContentComplete (Rect obj)
  70. {
  71. ManipulatePopup ();
  72. }
  73. private void Top_DrawContent (Rect obj)
  74. {
  75. if (!closed) {
  76. ReopenSuggestions ();
  77. }
  78. ManipulatePopup ();
  79. if (Visible) {
  80. top.BringSubviewToFront (popup);
  81. }
  82. }
  83. private void ManipulatePopup ()
  84. {
  85. if (Visible && popup == null) {
  86. popup = new Popup (this) {
  87. Frame = Rect.Empty
  88. };
  89. top?.Add (popup);
  90. }
  91. if (!Visible && popup != null) {
  92. top?.Remove (popup);
  93. popup.Dispose ();
  94. popup = null;
  95. }
  96. }
  97. /// <summary>
  98. /// Gets or sets If the popup is displayed inside or outside the host limits.
  99. /// </summary>
  100. public bool PopupInsideContainer { get; set; } = true;
  101. /// <summary>
  102. /// The maximum width of the autocomplete dropdown
  103. /// </summary>
  104. public virtual int MaxWidth { get; set; } = 10;
  105. /// <summary>
  106. /// The maximum number of visible rows in the autocomplete dropdown to render
  107. /// </summary>
  108. public virtual int MaxHeight { get; set; } = 6;
  109. /// <summary>
  110. /// True if the autocomplete should be considered open and visible
  111. /// </summary>
  112. public virtual bool Visible { get; set; }
  113. /// <summary>
  114. /// The strings that form the current list of suggestions to render
  115. /// based on what the user has typed so far.
  116. /// </summary>
  117. public virtual ReadOnlyCollection<string> Suggestions { get; set; } = new ReadOnlyCollection<string> (new string [0]);
  118. /// <summary>
  119. /// The full set of all strings that can be suggested.
  120. /// </summary>
  121. /// <returns></returns>
  122. public virtual List<string> AllSuggestions { get; set; } = new List<string> ();
  123. /// <summary>
  124. /// The currently selected index into <see cref="Suggestions"/> that the user has highlighted
  125. /// </summary>
  126. public virtual int SelectedIdx { get; set; }
  127. /// <summary>
  128. /// When more suggestions are available than can be rendered the user
  129. /// can scroll down the dropdown list. This indicates how far down they
  130. /// have gone
  131. /// </summary>
  132. public virtual int ScrollOffset { get; set; }
  133. /// <summary>
  134. /// The colors to use to render the overlay. Accessing this property before
  135. /// the Application has been initialized will cause an error
  136. /// </summary>
  137. public virtual ColorScheme ColorScheme {
  138. get {
  139. if (colorScheme == null) {
  140. colorScheme = Colors.Menu;
  141. }
  142. return colorScheme;
  143. }
  144. set {
  145. colorScheme = value;
  146. }
  147. }
  148. /// <summary>
  149. /// The key that the user must press to accept the currently selected autocomplete suggestion
  150. /// </summary>
  151. public virtual Key SelectionKey { get; set; } = Key.Enter;
  152. /// <summary>
  153. /// The key that the user can press to close the currently popped autocomplete menu
  154. /// </summary>
  155. public virtual Key CloseKey { get; set; } = Key.Esc;
  156. /// <summary>
  157. /// The key that the user can press to reopen the currently popped autocomplete menu
  158. /// </summary>
  159. public virtual Key Reopen { get; set; } = Key.Space | Key.CtrlMask | Key.AltMask;
  160. /// <summary>
  161. /// Renders the autocomplete dialog inside or outside the given <see cref="HostControl"/> at the
  162. /// given point.
  163. /// </summary>
  164. /// <param name="renderAt"></param>
  165. public virtual void RenderOverlay (Point renderAt)
  166. {
  167. if (!Visible || HostControl?.HasFocus == false || Suggestions.Count == 0) {
  168. LastPopupPos = null;
  169. Visible = false;
  170. return;
  171. }
  172. LastPopupPos = renderAt;
  173. int height, width;
  174. if (PopupInsideContainer) {
  175. // don't overspill vertically
  176. height = Math.Min (HostControl.Bounds.Height - renderAt.Y, MaxHeight);
  177. // There is no space below, lets see if can popup on top
  178. if (height < Suggestions.Count && HostControl.Bounds.Height - renderAt.Y >= height) {
  179. // Verifies that the upper limit available is greater than the lower limit
  180. if (renderAt.Y > HostControl.Bounds.Height - renderAt.Y) {
  181. renderAt.Y = Math.Max (renderAt.Y - Math.Min (Suggestions.Count + 1, MaxHeight + 1), 0);
  182. height = Math.Min (Math.Min (Suggestions.Count, MaxHeight), LastPopupPos.Value.Y - 1);
  183. }
  184. }
  185. } else {
  186. // don't overspill vertically
  187. height = Math.Min (Math.Min (top.Bounds.Height - HostControl.Frame.Bottom, MaxHeight), Suggestions.Count);
  188. // There is no space below, lets see if can popup on top
  189. if (height < Suggestions.Count && HostControl.Frame.Y - top.Frame.Y >= height) {
  190. // Verifies that the upper limit available is greater than the lower limit
  191. if (HostControl.Frame.Y > top.Bounds.Height - HostControl.Frame.Y) {
  192. renderAt.Y = Math.Max (HostControl.Frame.Y - Math.Min (Suggestions.Count, MaxHeight), 0);
  193. height = Math.Min (Math.Min (Suggestions.Count, MaxHeight), HostControl.Frame.Y);
  194. }
  195. } else {
  196. renderAt.Y = HostControl.Frame.Bottom;
  197. }
  198. }
  199. if (ScrollOffset > Suggestions.Count - height) {
  200. ScrollOffset = 0;
  201. }
  202. var toRender = Suggestions.Skip (ScrollOffset).Take (height).ToArray ();
  203. toRenderLength = toRender.Length;
  204. if (toRender.Length == 0) {
  205. return;
  206. }
  207. width = Math.Min (MaxWidth, toRender.Max (s => s.Length));
  208. if (PopupInsideContainer) {
  209. // don't overspill horizontally, let's see if can be displayed on the left
  210. if (width > HostControl.Bounds.Width - renderAt.X) {
  211. // Verifies that the left limit available is greater than the right limit
  212. if (renderAt.X > HostControl.Bounds.Width - renderAt.X) {
  213. renderAt.X -= Math.Min (width, LastPopupPos.Value.X);
  214. width = Math.Min (width, LastPopupPos.Value.X);
  215. } else {
  216. width = Math.Min (width, HostControl.Bounds.Width - renderAt.X);
  217. }
  218. }
  219. } else {
  220. // don't overspill horizontally, let's see if can be displayed on the left
  221. if (width > top.Bounds.Width - (renderAt.X + HostControl.Frame.X)) {
  222. // Verifies that the left limit available is greater than the right limit
  223. if (renderAt.X + HostControl.Frame.X > top.Bounds.Width - (renderAt.X + HostControl.Frame.X)) {
  224. renderAt.X -= Math.Min (width, LastPopupPos.Value.X);
  225. width = Math.Min (width, LastPopupPos.Value.X);
  226. } else {
  227. width = Math.Min (width, top.Bounds.Width - renderAt.X);
  228. }
  229. }
  230. }
  231. if (PopupInsideContainer) {
  232. popup.Frame = new Rect (
  233. new Point (HostControl.Frame.X + renderAt.X, HostControl.Frame.Y + renderAt.Y),
  234. new Size (width, height));
  235. } else {
  236. popup.Frame = new Rect (
  237. new Point (HostControl.Frame.X + renderAt.X, renderAt.Y),
  238. new Size (width, height));
  239. }
  240. popup.Move (0, 0);
  241. for (int i = 0; i < toRender.Length; i++) {
  242. if (i == SelectedIdx - ScrollOffset) {
  243. Application.Driver.SetAttribute (ColorScheme.Focus);
  244. } else {
  245. Application.Driver.SetAttribute (ColorScheme.Normal);
  246. }
  247. popup.Move (0, i);
  248. var text = TextFormatter.ClipOrPad (toRender [i], width);
  249. Application.Driver.AddStr (text);
  250. }
  251. }
  252. /// <summary>
  253. /// Updates <see cref="SelectedIdx"/> to be a valid index within <see cref="Suggestions"/>
  254. /// </summary>
  255. public virtual void EnsureSelectedIdxIsValid ()
  256. {
  257. SelectedIdx = Math.Max (0, Math.Min (Suggestions.Count - 1, SelectedIdx));
  258. // if user moved selection up off top of current scroll window
  259. if (SelectedIdx < ScrollOffset) {
  260. ScrollOffset = SelectedIdx;
  261. }
  262. // if user moved selection down past bottom of current scroll window
  263. while (toRenderLength > 0 && SelectedIdx >= ScrollOffset + toRenderLength) {
  264. ScrollOffset++;
  265. }
  266. }
  267. /// <summary>
  268. /// Handle key events before <see cref="HostControl"/> e.g. to make key events like
  269. /// up/down apply to the autocomplete control instead of changing the cursor position in
  270. /// the underlying text view.
  271. /// </summary>
  272. /// <param name="kb">The key event.</param>
  273. /// <returns><c>true</c>if the key can be handled <c>false</c>otherwise.</returns>
  274. public virtual bool ProcessKey (KeyEvent kb)
  275. {
  276. if (IsWordChar ((char)kb.Key)) {
  277. Visible = true;
  278. ManipulatePopup ();
  279. closed = false;
  280. return false;
  281. }
  282. if (kb.Key == Reopen) {
  283. return ReopenSuggestions ();
  284. }
  285. if (closed || Suggestions.Count == 0) {
  286. Visible = false;
  287. if (!closed) {
  288. Close ();
  289. }
  290. return false;
  291. }
  292. if (kb.Key == Key.CursorDown) {
  293. MoveDown ();
  294. return true;
  295. }
  296. if (kb.Key == Key.CursorUp) {
  297. MoveUp ();
  298. return true;
  299. }
  300. if (kb.Key == Key.CursorLeft || kb.Key == Key.CursorRight) {
  301. GenerateSuggestions (kb.Key == Key.CursorLeft ? -1 : 1);
  302. if (Suggestions.Count == 0) {
  303. Visible = false;
  304. if (!closed) {
  305. Close ();
  306. }
  307. }
  308. return false;
  309. }
  310. if (kb.Key == SelectionKey) {
  311. return Select ();
  312. }
  313. if (kb.Key == CloseKey) {
  314. Close ();
  315. return true;
  316. }
  317. return false;
  318. }
  319. /// <summary>
  320. /// Handle mouse events before <see cref="HostControl"/> e.g. to make mouse events like
  321. /// report/click apply to the autocomplete control instead of changing the cursor position in
  322. /// the underlying text view.
  323. /// </summary>
  324. /// <param name="me">The mouse event.</param>
  325. /// <param name="fromHost">If was called from the popup or from the host.</param>
  326. /// <returns><c>true</c>if the mouse can be handled <c>false</c>otherwise.</returns>
  327. public virtual bool MouseEvent (MouseEvent me, bool fromHost = false)
  328. {
  329. if (fromHost) {
  330. if (!Visible) {
  331. return false;
  332. }
  333. GenerateSuggestions ();
  334. if (Visible && Suggestions.Count == 0) {
  335. Visible = false;
  336. HostControl?.SetNeedsDisplay ();
  337. return true;
  338. } else if (!Visible && Suggestions.Count > 0) {
  339. Visible = true;
  340. HostControl?.SetNeedsDisplay ();
  341. Application.UngrabMouse ();
  342. return false;
  343. } else {
  344. // not in the popup
  345. if (Visible && HostControl != null) {
  346. Visible = false;
  347. closed = false;
  348. }
  349. HostControl?.SetNeedsDisplay ();
  350. }
  351. return false;
  352. }
  353. if (popup == null || Suggestions.Count == 0) {
  354. ManipulatePopup ();
  355. return false;
  356. }
  357. if (me.Flags == MouseFlags.ReportMousePosition) {
  358. RenderSelectedIdxByMouse (me);
  359. return true;
  360. }
  361. if (me.Flags == MouseFlags.Button1Clicked) {
  362. SelectedIdx = me.Y - ScrollOffset;
  363. return Select ();
  364. }
  365. if (me.Flags == MouseFlags.WheeledDown) {
  366. MoveDown ();
  367. return true;
  368. }
  369. if (me.Flags == MouseFlags.WheeledUp) {
  370. MoveUp ();
  371. return true;
  372. }
  373. return false;
  374. }
  375. /// <summary>
  376. /// Render the current selection in the Autocomplete context menu by the mouse reporting.
  377. /// </summary>
  378. /// <param name="me"></param>
  379. protected void RenderSelectedIdxByMouse (MouseEvent me)
  380. {
  381. if (SelectedIdx != me.Y - ScrollOffset) {
  382. SelectedIdx = me.Y - ScrollOffset;
  383. if (LastPopupPos != null) {
  384. RenderOverlay ((Point)LastPopupPos);
  385. }
  386. }
  387. }
  388. /// <summary>
  389. /// Clears <see cref="Suggestions"/>
  390. /// </summary>
  391. public virtual void ClearSuggestions ()
  392. {
  393. Suggestions = Enumerable.Empty<string> ().ToList ().AsReadOnly ();
  394. }
  395. /// <summary>
  396. /// Populates <see cref="Suggestions"/> with all strings in <see cref="AllSuggestions"/> that
  397. /// match with the current cursor position/text in the <see cref="HostControl"/>
  398. /// </summary>
  399. /// <param name="columnOffset">The column offset.</param>
  400. public virtual void GenerateSuggestions (int columnOffset = 0)
  401. {
  402. // if there is nothing to pick from
  403. if (AllSuggestions.Count == 0) {
  404. ClearSuggestions ();
  405. return;
  406. }
  407. var currentWord = GetCurrentWord (columnOffset);
  408. if (string.IsNullOrWhiteSpace (currentWord)) {
  409. ClearSuggestions ();
  410. } else {
  411. Suggestions = AllSuggestions.Where (o =>
  412. o.StartsWith (currentWord, StringComparison.CurrentCultureIgnoreCase) &&
  413. !o.Equals (currentWord, StringComparison.CurrentCultureIgnoreCase)
  414. ).ToList ().AsReadOnly ();
  415. EnsureSelectedIdxIsValid ();
  416. }
  417. }
  418. /// <summary>
  419. /// Return true if the given symbol should be considered part of a word
  420. /// and can be contained in matches. Base behavior is to use <see cref="char.IsLetterOrDigit(char)"/>
  421. /// </summary>
  422. /// <param name="rune"></param>
  423. /// <returns></returns>
  424. public virtual bool IsWordChar (Rune rune)
  425. {
  426. return Char.IsLetterOrDigit ((char)rune);
  427. }
  428. /// <summary>
  429. /// Completes the autocomplete selection process. Called when user hits the <see cref="SelectionKey"/>.
  430. /// </summary>
  431. /// <returns></returns>
  432. protected bool Select ()
  433. {
  434. if (SelectedIdx >= 0 && SelectedIdx < Suggestions.Count) {
  435. var accepted = Suggestions [SelectedIdx];
  436. return InsertSelection (accepted);
  437. }
  438. return false;
  439. }
  440. /// <summary>
  441. /// Called when the user confirms a selection at the current cursor location in
  442. /// the <see cref="HostControl"/>. The <paramref name="accepted"/> string
  443. /// is the full autocomplete word to be inserted. Typically a host will have to
  444. /// remove some characters such that the <paramref name="accepted"/> string
  445. /// completes the word instead of simply being appended.
  446. /// </summary>
  447. /// <param name="accepted"></param>
  448. /// <returns>True if the insertion was possible otherwise false</returns>
  449. protected virtual bool InsertSelection (string accepted)
  450. {
  451. var typedSoFar = GetCurrentWord () ?? "";
  452. if (typedSoFar.Length < accepted.Length) {
  453. // delete the text
  454. for (int i = 0; i < typedSoFar.Length; i++) {
  455. DeleteTextBackwards ();
  456. }
  457. InsertText (accepted);
  458. return true;
  459. }
  460. return false;
  461. }
  462. /// <summary>
  463. /// Returns the currently selected word from the <see cref="HostControl"/>.
  464. /// <para>
  465. /// When overriding this method views can make use of <see cref="IdxToWord(List{Rune}, int, int)"/>
  466. /// </para>
  467. /// </summary>
  468. /// <param name="columnOffset">The column offset.</param>
  469. /// <returns></returns>
  470. protected abstract string GetCurrentWord (int columnOffset = 0);
  471. /// <summary>
  472. /// <para>
  473. /// Given a <paramref name="line"/> of characters, returns the word which ends at <paramref name="idx"/>
  474. /// or null. Also returns null if the <paramref name="idx"/> is positioned in the middle of a word.
  475. /// </para>
  476. ///
  477. /// <para>
  478. /// Use this method to determine whether autocomplete should be shown when the cursor is at
  479. /// a given point in a line and to get the word from which suggestions should be generated.
  480. /// Use the <paramref name="columnOffset"/> to indicate if search the word at left (negative),
  481. /// at right (positive) or at the current column (zero) which is the default.
  482. /// </para>
  483. /// </summary>
  484. /// <param name="line"></param>
  485. /// <param name="idx"></param>
  486. /// <param name="columnOffset"></param>
  487. /// <returns></returns>
  488. protected virtual string IdxToWord (List<Rune> line, int idx, int columnOffset = 0)
  489. {
  490. StringBuilder sb = new StringBuilder ();
  491. var endIdx = idx;
  492. // get the ending word index
  493. while (endIdx < line.Count) {
  494. if (IsWordChar (line [endIdx])) {
  495. endIdx++;
  496. } else {
  497. break;
  498. }
  499. }
  500. // It isn't a word char then there is no way to autocomplete that word
  501. if (endIdx == idx && columnOffset != 0) {
  502. return null;
  503. }
  504. // we are at the end of a word. Work out what has been typed so far
  505. while (endIdx-- > 0) {
  506. if (IsWordChar (line [endIdx])) {
  507. sb.Insert (0, (char)line [endIdx]);
  508. } else {
  509. break;
  510. }
  511. }
  512. return sb.ToString ();
  513. }
  514. /// <summary>
  515. /// Deletes the text backwards before insert the selected text in the <see cref="HostControl"/>.
  516. /// </summary>
  517. protected abstract void DeleteTextBackwards ();
  518. /// <summary>
  519. /// Inser the selected text in the <see cref="HostControl"/>.
  520. /// </summary>
  521. /// <param name="accepted"></param>
  522. protected abstract void InsertText (string accepted);
  523. /// <summary>
  524. /// Closes the Autocomplete context menu if it is showing and <see cref="ClearSuggestions"/>
  525. /// </summary>
  526. protected void Close ()
  527. {
  528. ClearSuggestions ();
  529. Visible = false;
  530. closed = true;
  531. HostControl?.SetNeedsDisplay ();
  532. ManipulatePopup ();
  533. }
  534. /// <summary>
  535. /// Moves the selection in the Autocomplete context menu up one
  536. /// </summary>
  537. protected void MoveUp ()
  538. {
  539. SelectedIdx--;
  540. if (SelectedIdx < 0) {
  541. SelectedIdx = Suggestions.Count - 1;
  542. }
  543. EnsureSelectedIdxIsValid ();
  544. HostControl?.SetNeedsDisplay ();
  545. }
  546. /// <summary>
  547. /// Moves the selection in the Autocomplete context menu down one
  548. /// </summary>
  549. protected void MoveDown ()
  550. {
  551. SelectedIdx++;
  552. if (SelectedIdx > Suggestions.Count - 1) {
  553. SelectedIdx = 0;
  554. }
  555. EnsureSelectedIdxIsValid ();
  556. HostControl?.SetNeedsDisplay ();
  557. }
  558. /// <summary>
  559. /// Reopen the popup after it has been closed.
  560. /// </summary>
  561. /// <returns></returns>
  562. protected bool ReopenSuggestions ()
  563. {
  564. GenerateSuggestions ();
  565. if (Suggestions.Count > 0) {
  566. Visible = true;
  567. closed = false;
  568. HostControl?.SetNeedsDisplay ();
  569. return true;
  570. }
  571. return false;
  572. }
  573. }
  574. }