mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2026-01-02 01:03:29 +01:00
Add AutocompleteContext
This commit is contained in:
@@ -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 ();
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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> ();
|
||||
|
||||
@@ -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/>
|
||||
|
||||
@@ -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/>
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user