SingleWordSuggestionGenerator.cs 2.9 KB

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