AppendAutocomplete.cs 5.0 KB

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