AppendAutocomplete.cs 5.8 KB

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