Code Completion

AvaloniaEdit supports a completion window (Intellisense) that floats over the text area.

Creating a Completion Window

You typically trigger completion on the TextInput event of the TextArea.

using Avalonia.Input;
using AvaloniaEdit.CodeCompletion;

// Define the window variable
CompletionWindow _completionWindow;

public MainWindow()
{
    InitializeComponent();
    var editor = this.FindControl<TextEditor>("Editor");

    // Subscribe to text input
    editor.TextArea.TextEntered += TextArea_TextEntered;
}

private void TextArea_TextEntered(object sender, TextInputEventArgs e)
{
    if (e.Text == ".")
    {
        // Open completion window
        _completionWindow = new CompletionWindow(editor.TextArea);
        _completionWindow.Closed += (o, args) => _completionWindow = null;

        var data = _completionWindow.CompletionList.CompletionData;

        // Add items to the list
        data.Add(new MyCompletionData("ToString"));
        data.Add(new MyCompletionData("GetHashCode"));

        _completionWindow.Show();
    }
}

Implementing ICompletionData

You must implement the ICompletionData interface to define what appears in the list.

public class MyCompletionData : ICompletionData
{
    public MyCompletionData(string text)
    {
        Text = text;
    }

    public IImage Image => null; // Optional image
    public string Text { get; }
    public object Content => Text; // What shows in the list
    public object Description => "Description for " + Text; // Tooltip description
    public double Priority => 0;

    public void Complete(TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs)
    {
        // Perform the actual replacement
        textArea.Document.Replace(completionSegment, Text);
    }
}

Insight Window (Overloads)

Similar to completion, you can show an OverloadInsightWindow (often used for method signatures).

if (e.Text == "(")
{
    var insightWindow = new OverloadInsightWindow(editor.TextArea);
    insightWindow.Provider = new MyOverloadProvider(); // Implement IOverloadProvider
    insightWindow.Show();
}