AppendAutocomplete.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// Autocomplete for a <see cref="TextField"/> which shows suggestions within the box. Displayed suggestions can
  4. /// be completed using the tab key.
  5. /// </summary>
  6. public class AppendAutocomplete : AutocompleteBase
  7. {
  8. private bool _suspendSuggestions;
  9. private TextField textField;
  10. /// <summary>Creates a new instance of the <see cref="AppendAutocomplete"/> class.</summary>
  11. public AppendAutocomplete (TextField textField)
  12. {
  13. this.textField = textField;
  14. SelectionKey = KeyCode.Tab;
  15. ColorScheme = new ColorScheme
  16. {
  17. Normal = new Attribute (Color.DarkGray, Color.Black),
  18. Focus = new Attribute (Color.DarkGray, Color.Black),
  19. HotNormal = new Attribute (Color.DarkGray, Color.Black),
  20. HotFocus = new Attribute (Color.DarkGray, Color.Black),
  21. Disabled = new Attribute (Color.DarkGray, Color.Black)
  22. };
  23. }
  24. /// <summary>
  25. /// The color used for rendering the appended text. Note that only <see cref="ColorScheme.Normal"/> is used and
  26. /// then only <see cref="Attribute.Foreground"/> (Background comes from <see cref="HostControl"/>).
  27. /// </summary>
  28. public override ColorScheme ColorScheme { get; set; }
  29. /// <inheritdoc/>
  30. public override View HostControl
  31. {
  32. get => textField;
  33. set => textField = (TextField)value;
  34. }
  35. /// <inheritdoc/>
  36. public override void ClearSuggestions ()
  37. {
  38. base.ClearSuggestions ();
  39. textField.SetNeedsDraw ();
  40. }
  41. /// <inheritdoc/>
  42. public override void GenerateSuggestions (AutocompleteContext context)
  43. {
  44. if (_suspendSuggestions)
  45. {
  46. _suspendSuggestions = false;
  47. return;
  48. }
  49. base.GenerateSuggestions (context);
  50. }
  51. /// <inheritdoc/>
  52. public override bool OnMouseEvent (MouseEventArgs me, bool fromHost = false) { return false; }
  53. /// <inheritdoc/>
  54. public override bool ProcessKey (Key a)
  55. {
  56. Key key = a.KeyCode;
  57. if (key == SelectionKey)
  58. {
  59. return AcceptSelectionIfAny ();
  60. }
  61. if (key == Key.CursorUp)
  62. {
  63. return CycleSuggestion (1);
  64. }
  65. if (key == Key.CursorDown)
  66. {
  67. return CycleSuggestion (-1);
  68. }
  69. if (key == CloseKey && Suggestions.Any ())
  70. {
  71. ClearSuggestions ();
  72. _suspendSuggestions = true;
  73. return true;
  74. }
  75. if (char.IsLetterOrDigit ((char)a))
  76. {
  77. _suspendSuggestions = false;
  78. }
  79. return false;
  80. }
  81. /// <summary>Renders the current suggestion into the <see cref="TextField"/></summary>
  82. public override void RenderOverlay (Point renderAt)
  83. {
  84. if (!MakingSuggestion ())
  85. {
  86. return;
  87. }
  88. // draw it like it's selected, even though it's not
  89. textField.SetAttribute (
  90. new Attribute (
  91. ColorScheme.Normal.Foreground,
  92. textField.ColorScheme.Focus.Background
  93. )
  94. );
  95. textField.Move (textField.Text.Length, 0);
  96. Suggestion suggestion = Suggestions.ElementAt (SelectedIdx);
  97. string fragment = suggestion.Replacement.Substring (suggestion.Remove);
  98. int spaceAvailable = textField.Viewport.Width - textField.Text.GetColumns ();
  99. int spaceRequired = fragment.EnumerateRunes ().Sum (c => c.GetColumns ());
  100. if (spaceAvailable < spaceRequired)
  101. {
  102. fragment = new string (
  103. fragment.TakeWhile (c => (spaceAvailable -= ((Rune)c).GetColumns ()) >= 0)
  104. .ToArray ()
  105. );
  106. }
  107. Application.Driver?.AddStr (fragment);
  108. }
  109. /// <summary>
  110. /// Accepts the current autocomplete suggestion displaying in the text box. Returns true if a valid suggestion was
  111. /// being rendered and acceptable or false if no suggestion was showing.
  112. /// </summary>
  113. /// <returns></returns>
  114. internal bool AcceptSelectionIfAny ()
  115. {
  116. if (MakingSuggestion ())
  117. {
  118. Suggestion insert = Suggestions.ElementAt (SelectedIdx);
  119. string newText = textField.Text;
  120. newText = newText.Substring (0, newText.Length - insert.Remove);
  121. newText += insert.Replacement;
  122. textField.Text = newText;
  123. textField.MoveEnd ();
  124. ClearSuggestions ();
  125. return true;
  126. }
  127. return false;
  128. }
  129. internal void SetTextTo (FileSystemInfo fileSystemInfo)
  130. {
  131. string newText = fileSystemInfo.FullName;
  132. if (fileSystemInfo is DirectoryInfo)
  133. {
  134. newText += Path.DirectorySeparatorChar;
  135. }
  136. textField.Text = newText;
  137. textField.MoveEnd ();
  138. }
  139. private bool CycleSuggestion (int direction)
  140. {
  141. if (Suggestions.Count <= 1)
  142. {
  143. return false;
  144. }
  145. SelectedIdx = (SelectedIdx + direction) % Suggestions.Count;
  146. if (SelectedIdx < 0)
  147. {
  148. SelectedIdx = Suggestions.Count () - 1;
  149. }
  150. textField.SetNeedsDraw ();
  151. return true;
  152. }
  153. /// <summary>
  154. /// Returns true if there is a suggestion that can be made and the control is in a state where user would expect
  155. /// to see auto-complete (i.e. focused and cursor in right place).
  156. /// </summary>
  157. /// <returns></returns>
  158. private bool MakingSuggestion () { return Suggestions.Any () && SelectedIdx != -1 && textField.HasFocus && textField.CursorIsAtEnd (); }
  159. }