Add AutocompleteContext

This commit is contained in:
tznind
2023-03-30 08:17:44 +01:00
parent 8f6bdca20c
commit 3d9aac8e4c
7 changed files with 29 additions and 13 deletions

View File

@@ -6,6 +6,17 @@ using System.Text;
using Rune = System.Rune;
namespace Terminal.Gui {
public class AutocompleteContext
{
public List<Rune> CurrentLine { get; set; }
public int Idx { get; set; }
public AutocompleteContext (List<Rune> currentLine, int idx)
{
CurrentLine = currentLine;
Idx = idx;
}
}
public abstract class AutocompleteBase : IAutocomplete {
@@ -65,9 +76,9 @@ namespace Terminal.Gui {
/// <inheritdoc/>
public virtual void GenerateSuggestions (List<Rune> currentLine, int idx)
public virtual void GenerateSuggestions (AutocompleteContext context)
{
Suggestions = SuggestionGenerator.GenerateSuggestions (currentLine, idx).ToList ().AsReadOnly ();
Suggestions = SuggestionGenerator.GenerateSuggestions (context).ToList ().AsReadOnly ();
EnsureSelectedIdxIsValid ();
}

View File

@@ -105,9 +105,9 @@ namespace Terminal.Gui {
/// <summary>
/// Populates <see cref="Suggestions"/> with all <see cref="Suggestion"/>
/// proposed by <see cref="SuggestionGenerator"/> at the given <paramref name="idx"/>
/// of <paramref name="currentLine"/>
/// proposed by <see cref="SuggestionGenerator"/> at the given <paramref name="context"/>
/// (cursor position)
/// </summary>
void GenerateSuggestions (List<Rune> currentLine, int idx);
void GenerateSuggestions (AutocompleteContext context);
}
}

View File

@@ -8,10 +8,9 @@ namespace Terminal.Gui {
public interface ISuggestionGenerator {
/// <summary>
/// Generates autocomplete <see cref="Suggestion"/> based on a given cursor location <paramref name="idx"/>
/// within a <paramref name="currentLine"/>
/// Generates autocomplete <see cref="Suggestion"/> based on a given <paramref name="context"/>
/// </summary>
IEnumerable<Suggestion> GenerateSuggestions (List<Rune> currentLine, int idx);
IEnumerable<Suggestion> GenerateSuggestions (AutocompleteContext context);
bool IsWordChar (Rune rune);

View File

@@ -12,14 +12,14 @@ namespace Terminal.Gui {
/// <returns></returns>
public virtual List<string> AllSuggestions { get; set; } = new List<string> ();
public IEnumerable<Suggestion> GenerateSuggestions (List<Rune> currentLine, int idx)
public IEnumerable<Suggestion> GenerateSuggestions (AutocompleteContext context)
{
// if there is nothing to pick from
if (AllSuggestions.Count == 0) {
return Enumerable.Empty<Suggestion> ();
}
var currentWord = IdxToWord (currentLine, idx);
var currentWord = IdxToWord (context.CurrentLine, context.Idx);
if (string.IsNullOrWhiteSpace (currentWord)) {
return Enumerable.Empty<Suggestion> ();

View File

@@ -486,7 +486,9 @@ namespace Terminal.Gui {
var currentLine = Text.ToRuneList ();
var cursorPosition = Math.Min (this.CursorPosition, currentLine.Count);
Autocomplete.GenerateSuggestions(currentLine,cursorPosition);
Autocomplete.GenerateSuggestions(
new AutocompleteContext(currentLine,cursorPosition)
);
}
/// <inheritdoc/>

View File

@@ -2440,7 +2440,9 @@ namespace Terminal.Gui {
{
var currentLine = this.GetCurrentLine ();
var cursorPosition = Math.Min (this.CurrentColumn, currentLine.Count);
Autocomplete.GenerateSuggestions(currentLine,cursorPosition);
Autocomplete.GenerateSuggestions(
new AutocompleteContext(currentLine,cursorPosition)
);
}
/// <inheritdoc/>

View File

@@ -30,7 +30,9 @@ namespace Terminal.Gui.ViewTests {
tv.InsertText ("co");
ac.HostControl = tv;
ac.GenerateSuggestions (tv.Text.ToRuneList(),2);
ac.GenerateSuggestions (
new AutocompleteContext(
tv.Text.ToRuneList(),2));
Assert.Equal (2, ac.Suggestions.Count);
Assert.Equal ("const", ac.Suggestions [0].Title);