AppendAutocomplete.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 void GenerateSuggestions (int columnOffset = 0)
  20. {
  21. // TODO: testing only
  22. AllSuggestions = new List<string> { "fish", "flipper", "fun" };
  23. base.GenerateSuggestions();
  24. }
  25. public override bool MouseEvent (MouseEvent me, bool fromHost = false)
  26. {
  27. return false;
  28. }
  29. public override bool ProcessKey (KeyEvent kb)
  30. {
  31. var key = kb.Key;
  32. if (key == SelectionKey) {
  33. return this.AcceptSelectionIfAny ();
  34. } else
  35. if (key == Key.CursorUp) {
  36. return this.CycleSuggestion (1);
  37. } else
  38. if (key == Key.CursorDown) {
  39. return this.CycleSuggestion (-1);
  40. }
  41. return false;
  42. }
  43. public override void RenderOverlay (Point renderAt)
  44. {
  45. if (!this.MakingSuggestion ()) {
  46. return;
  47. }
  48. // draw it like its selected even though its not
  49. Application.Driver.SetAttribute (new Attribute (Color.DarkGray, textField.ColorScheme.Focus.Background));
  50. textField.Move (textField.Text.Length, 0);
  51. Application.Driver.AddStr (this.Suggestions.ElementAt(this.SelectedIdx));
  52. }
  53. public AppendAutocomplete (TextField textField)
  54. {
  55. this.textField = textField;
  56. }
  57. /// <summary>
  58. /// Accepts the current autocomplete suggestion displaying in the text box.
  59. /// Returns true if a valid suggestion was being rendered and acceptable or
  60. /// false if no suggestion was showing.
  61. /// </summary>
  62. /// <returns></returns>
  63. internal bool AcceptSelectionIfAny ()
  64. {
  65. if (this.MakingSuggestion ()) {
  66. textField.Text += this.Suggestions.ElementAt(this.SelectedIdx);
  67. this.MoveCursorToEnd ();
  68. this.ClearSuggestions ();
  69. return true;
  70. }
  71. return false;
  72. }
  73. internal void MoveCursorToEnd ()
  74. {
  75. textField.ClearAllSelection ();
  76. textField.CursorPosition = textField.Text.Length;
  77. }
  78. internal void SetTextTo (FileSystemInfo fileSystemInfo)
  79. {
  80. var newText = fileSystemInfo.FullName;
  81. if (fileSystemInfo is DirectoryInfo) {
  82. newText += System.IO.Path.DirectorySeparatorChar;
  83. }
  84. textField.Text = newText;
  85. this.MoveCursorToEnd ();
  86. }
  87. internal bool CursorIsAtEnd ()
  88. {
  89. return textField.CursorPosition == textField.Text.Length;
  90. }
  91. /// <summary>
  92. /// Returns true if there is a suggestion that can be made and the control
  93. /// is in a state where user would expect to see auto-complete (i.e. focused and
  94. /// cursor in right place).
  95. /// </summary>
  96. /// <returns></returns>
  97. private bool MakingSuggestion ()
  98. {
  99. return Suggestions.Any() && this.SelectedIdx != -1 && textField.HasFocus && this.CursorIsAtEnd ();
  100. }
  101. private bool CycleSuggestion (int direction)
  102. {
  103. if (this.Suggestions.Count <= 1) {
  104. return false;
  105. }
  106. this.SelectedIdx = (this.SelectedIdx + direction) % this.Suggestions.Count;
  107. if (this.SelectedIdx < 0) {
  108. this.SelectedIdx = this.Suggestions.Count() - 1;
  109. }
  110. textField.SetNeedsDisplay ();
  111. return true;
  112. }
  113. protected override string GetCurrentWord (int columnOffset = 0)
  114. {
  115. // TODO: Make real
  116. return textField.Text.ToString();
  117. }
  118. }
  119. }