SingleWordSuggestionGenerator.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 currentWord = IdxToWord (context.CurrentLine, context.CursorPosition);
  26. if (string.IsNullOrWhiteSpace (currentWord)) {
  27. return Enumerable.Empty<Suggestion> ();
  28. } else {
  29. return AllSuggestions.Where (o =>
  30. o.StartsWith (currentWord, StringComparison.CurrentCultureIgnoreCase) &&
  31. !o.Equals (currentWord, StringComparison.CurrentCultureIgnoreCase)
  32. ).Select (o => new Suggestion (currentWord.Length, o))
  33. .ToList ().AsReadOnly ();
  34. }
  35. }
  36. /// <summary>
  37. /// Return true if the given symbol should be considered part of a word
  38. /// and can be contained in matches. Base behavior is to use <see cref="char.IsLetterOrDigit(char)"/>
  39. /// </summary>
  40. /// <param name="rune"></param>
  41. /// <returns></returns>
  42. public virtual bool IsWordChar (Rune rune)
  43. {
  44. return Char.IsLetterOrDigit ((char)rune.Value);
  45. }
  46. /// <summary>
  47. /// <para>
  48. /// Given a <paramref name="line"/> of characters, returns the word which ends at <paramref name="idx"/>
  49. /// or null. Also returns null if the <paramref name="idx"/> is positioned in the middle of a word.
  50. /// </para>
  51. ///
  52. /// <para>
  53. /// Use this method to determine whether autocomplete should be shown when the cursor is at
  54. /// a given point in a line and to get the word from which suggestions should be generated.
  55. /// Use the <paramref name="columnOffset"/> to indicate if search the word at left (negative),
  56. /// at right (positive) or at the current column (zero) which is the default.
  57. /// </para>
  58. /// </summary>
  59. /// <param name="line"></param>
  60. /// <param name="idx"></param>
  61. /// <param name="columnOffset"></param>
  62. /// <returns></returns>
  63. protected virtual string IdxToWord (List<Rune> line, int idx, int columnOffset = 0)
  64. {
  65. StringBuilder sb = new StringBuilder ();
  66. var endIdx = idx;
  67. // get the ending word index
  68. while (endIdx < line.Count) {
  69. if (IsWordChar (line [endIdx])) {
  70. endIdx++;
  71. } else {
  72. break;
  73. }
  74. }
  75. // It isn't a word char then there is no way to autocomplete that word
  76. if (endIdx == idx && columnOffset != 0) {
  77. return null;
  78. }
  79. // we are at the end of a word. Work out what has been typed so far
  80. while (endIdx-- > 0) {
  81. if (IsWordChar (line [endIdx])) {
  82. sb.Insert (0, (char)line [endIdx].Value);
  83. } else {
  84. break;
  85. }
  86. }
  87. return sb.ToString ();
  88. }
  89. }
  90. }