SingleWordSuggestionGenerator.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Terminal.Gui {
  6. /// <summary>
  7. /// <see cref="ISuggestionGenerator"/> which suggests from a collection
  8. /// of words those that match the <see cref="AutocompleteContext"/>. You
  9. /// can update <see cref="AllSuggestions"/> at any time to change candidates
  10. /// considered for autocomplete.
  11. /// </summary>
  12. public class SingleWordSuggestionGenerator : ISuggestionGenerator {
  13. /// <summary>
  14. /// The full set of all strings that can be suggested.
  15. /// </summary>
  16. /// <returns></returns>
  17. public virtual List<string> AllSuggestions { get; set; } = new List<string> ();
  18. /// <inheritdoc/>
  19. public IEnumerable<Suggestion> GenerateSuggestions (AutocompleteContext context)
  20. {
  21. // if there is nothing to pick from
  22. if (AllSuggestions.Count == 0) {
  23. return Enumerable.Empty<Suggestion> ();
  24. }
  25. var line = context.CurrentLine.Select (c => c.Rune).ToList ();
  26. var currentWord = IdxToWord (line, context.CursorPosition, out int startIdx);
  27. context.CursorPosition = startIdx < 1 ? startIdx : Math.Min (startIdx + 1, line.Count);
  28. if (string.IsNullOrWhiteSpace (currentWord)) {
  29. return Enumerable.Empty<Suggestion> ();
  30. } else {
  31. return AllSuggestions.Where (o =>
  32. o.StartsWith (currentWord, StringComparison.CurrentCultureIgnoreCase) &&
  33. !o.Equals (currentWord, StringComparison.CurrentCultureIgnoreCase)
  34. ).Select (o => new Suggestion (currentWord.Length, o))
  35. .ToList ().AsReadOnly ();
  36. }
  37. }
  38. /// <summary>
  39. /// Return true if the given symbol should be considered part of a word
  40. /// and can be contained in matches. Base behavior is to use <see cref="char.IsLetterOrDigit(char)"/>
  41. /// </summary>
  42. /// <param name="rune">The rune.</param>
  43. /// <returns></returns>
  44. public virtual bool IsWordChar (Rune rune)
  45. {
  46. return Char.IsLetterOrDigit ((char)rune.Value);
  47. }
  48. /// <summary>
  49. /// <para>
  50. /// Given a <paramref name="line"/> of characters, returns the word which ends at <paramref name="idx"/>
  51. /// or null. Also returns null if the <paramref name="idx"/> is positioned in the middle of a word.
  52. /// </para>
  53. ///
  54. /// <para>
  55. /// Use this method to determine whether autocomplete should be shown when the cursor is at
  56. /// a given point in a line and to get the word from which suggestions should be generated.
  57. /// Use the <paramref name="columnOffset"/> to indicate if search the word at left (negative),
  58. /// at right (positive) or at the current column (zero) which is the default.
  59. /// </para>
  60. /// </summary>
  61. /// <param name="line"></param>
  62. /// <param name="idx"></param>
  63. /// <param name="startIdx">The start index of the word.</param>
  64. /// <param name="columnOffset"></param>
  65. /// <returns></returns>
  66. protected virtual string IdxToWord (List<Rune> line, int idx, out int startIdx, int columnOffset = 0)
  67. {
  68. StringBuilder sb = new StringBuilder ();
  69. startIdx = idx;
  70. // get the ending word index
  71. while (startIdx < line.Count) {
  72. if (IsWordChar (line [startIdx])) {
  73. startIdx++;
  74. } else {
  75. break;
  76. }
  77. }
  78. // It isn't a word char then there is no way to autocomplete that word
  79. if (startIdx == idx && columnOffset != 0) {
  80. return null;
  81. }
  82. // we are at the end of a word. Work out what has been typed so far
  83. while (startIdx-- > 0) {
  84. if (IsWordChar (line [startIdx])) {
  85. sb.Insert (0, (char)line [startIdx].Value);
  86. } else {
  87. break;
  88. }
  89. }
  90. startIdx = Math.Max (startIdx, 0);
  91. return sb.ToString ();
  92. }
  93. }
  94. }