SingleWordSuggestionGenerator.cs 2.9 KB

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