AutocompleteBase.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  6. using Rune = System.Rune;
  7. namespace Terminal.Gui {
  8. public class AutocompleteContext
  9. {
  10. public List<Rune> CurrentLine { get; set; }
  11. public int Idx { get; set; }
  12. public AutocompleteContext (List<Rune> currentLine, int idx)
  13. {
  14. CurrentLine = currentLine;
  15. Idx = idx;
  16. }
  17. }
  18. public abstract class AutocompleteBase : IAutocomplete {
  19. /// <inheritdoc/>
  20. public abstract View HostControl { get; set; }
  21. /// <inheritdoc/>
  22. public bool PopupInsideContainer { get; set; }
  23. public ISuggestionGenerator SuggestionGenerator { get; set; } = new SingleWordSuggestionGenerator ();
  24. /// <inheritdoc/>
  25. public virtual int MaxWidth { get; set; } = 10;
  26. /// <inheritdoc/>
  27. public virtual int MaxHeight { get; set; } = 6;
  28. /// <inheritdoc/>
  29. /// <inheritdoc/>
  30. public virtual bool Visible { get; set; }
  31. /// <inheritdoc/>
  32. public virtual ReadOnlyCollection<Suggestion> Suggestions { get; set; } = new ReadOnlyCollection<Suggestion> (new Suggestion [0]);
  33. /// <inheritdoc/>
  34. public virtual int SelectedIdx { get; set; }
  35. /// <inheritdoc/>
  36. public abstract ColorScheme ColorScheme { get; set; }
  37. /// <inheritdoc/>
  38. public virtual Key SelectionKey { get; set; } = Key.Enter;
  39. /// <inheritdoc/>
  40. public virtual Key CloseKey { get; set; } = Key.Esc;
  41. /// <inheritdoc/>
  42. public virtual Key Reopen { get; set; } = Key.Space | Key.CtrlMask | Key.AltMask;
  43. /// <inheritdoc/>
  44. public abstract bool MouseEvent (MouseEvent me, bool fromHost = false);
  45. /// <inheritdoc/>
  46. public abstract bool ProcessKey (KeyEvent kb);
  47. /// <inheritdoc/>
  48. public abstract void RenderOverlay (Point renderAt);
  49. /// <inheritdoc/>>
  50. public virtual void ClearSuggestions ()
  51. {
  52. Suggestions = Enumerable.Empty<Suggestion> ().ToList ().AsReadOnly ();
  53. }
  54. /// <inheritdoc/>
  55. public virtual void GenerateSuggestions (AutocompleteContext context)
  56. {
  57. Suggestions = SuggestionGenerator.GenerateSuggestions (context).ToList ().AsReadOnly ();
  58. EnsureSelectedIdxIsValid ();
  59. }
  60. /// <summary>
  61. /// Updates <see cref="SelectedIdx"/> to be a valid index within <see cref="Suggestions"/>
  62. /// </summary>
  63. public virtual void EnsureSelectedIdxIsValid ()
  64. {
  65. SelectedIdx = Math.Max (0, Math.Min (Suggestions.Count - 1, SelectedIdx));
  66. }
  67. }
  68. }