AppendAutocomplete.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.IO;
  4. using System.Linq;
  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. public override View HostControl { get => textField; set => textField = (TextField)value; }
  13. public override ColorScheme ColorScheme { get; set; }
  14. public override void ClearSuggestions ()
  15. {
  16. base.ClearSuggestions ();
  17. textField.SetNeedsDisplay ();
  18. }
  19. public override bool MouseEvent (MouseEvent me, bool fromHost = false)
  20. {
  21. return false;
  22. }
  23. public override bool ProcessKey (KeyEvent kb)
  24. {
  25. var key = kb.Key;
  26. if (key == SelectionKey) {
  27. return this.AcceptSelectionIfAny ();
  28. } else
  29. if (key == Key.CursorUp) {
  30. return this.CycleSuggestion (1);
  31. } else
  32. if (key == Key.CursorDown) {
  33. return this.CycleSuggestion (-1);
  34. }
  35. else if(key == CloseKey && Suggestions.Any())
  36. {
  37. ClearSuggestions();
  38. _suspendSuggestions = true;
  39. return true;
  40. }
  41. if(char.IsLetterOrDigit((char)kb.KeyValue))
  42. {
  43. _suspendSuggestions = false;
  44. }
  45. return false;
  46. }
  47. bool _suspendSuggestions = false;
  48. public override void GenerateSuggestions (AutocompleteContext context)
  49. {
  50. if(_suspendSuggestions)
  51. {
  52. return;
  53. }
  54. base.GenerateSuggestions (context);
  55. }
  56. public override void RenderOverlay (Point renderAt)
  57. {
  58. if (!this.MakingSuggestion ()) {
  59. return;
  60. }
  61. // draw it like its selected even though its not
  62. Application.Driver.SetAttribute (new Attribute (Color.DarkGray, textField.ColorScheme.Focus.Background));
  63. textField.Move (textField.Text.Length, 0);
  64. var suggestion = this.Suggestions.ElementAt (this.SelectedIdx);
  65. var fragment = suggestion.Replacement.Substring (suggestion.Remove);
  66. Application.Driver.AddStr (fragment);
  67. }
  68. public AppendAutocomplete (TextField textField)
  69. {
  70. this.textField = textField;
  71. SelectionKey = Key.Tab;
  72. }
  73. /// <summary>
  74. /// Accepts the current autocomplete suggestion displaying in the text box.
  75. /// Returns true if a valid suggestion was being rendered and acceptable or
  76. /// false if no suggestion was showing.
  77. /// </summary>
  78. /// <returns></returns>
  79. internal bool AcceptSelectionIfAny ()
  80. {
  81. if (this.MakingSuggestion ()) {
  82. var insert = this.Suggestions.ElementAt (this.SelectedIdx);
  83. var newText = textField.Text.ToString ();
  84. newText = newText.Substring (0, newText.Length - insert.Remove);
  85. newText += insert.Replacement;
  86. textField.Text = newText;
  87. this.MoveCursorToEnd ();
  88. this.ClearSuggestions ();
  89. return true;
  90. }
  91. return false;
  92. }
  93. internal void MoveCursorToEnd ()
  94. {
  95. textField.ClearAllSelection ();
  96. textField.CursorPosition = textField.Text.Length;
  97. }
  98. internal void SetTextTo (FileSystemInfo fileSystemInfo)
  99. {
  100. var newText = fileSystemInfo.FullName;
  101. if (fileSystemInfo is DirectoryInfo) {
  102. newText += System.IO.Path.DirectorySeparatorChar;
  103. }
  104. textField.Text = newText;
  105. this.MoveCursorToEnd ();
  106. }
  107. internal bool CursorIsAtEnd ()
  108. {
  109. return textField.CursorPosition == textField.Text.Length;
  110. }
  111. /// <summary>
  112. /// Returns true if there is a suggestion that can be made and the control
  113. /// is in a state where user would expect to see auto-complete (i.e. focused and
  114. /// cursor in right place).
  115. /// </summary>
  116. /// <returns></returns>
  117. private bool MakingSuggestion ()
  118. {
  119. return Suggestions.Any () && this.SelectedIdx != -1 && textField.HasFocus && this.CursorIsAtEnd ();
  120. }
  121. private bool CycleSuggestion (int direction)
  122. {
  123. if (this.Suggestions.Count <= 1) {
  124. return false;
  125. }
  126. this.SelectedIdx = (this.SelectedIdx + direction) % this.Suggestions.Count;
  127. if (this.SelectedIdx < 0) {
  128. this.SelectedIdx = this.Suggestions.Count () - 1;
  129. }
  130. textField.SetNeedsDisplay ();
  131. return true;
  132. }
  133. }
  134. }