New to Telerik UI for WinFormsStart a free 30-day trial

Conditional Intelliprompts in SyntaxEditor

Updated over 6 months ago

Environment

Product VersionProductAuthor
2020.2.616RadSyntaxEditor for WinFormsDesislava Yordanova

Description

RadSyntaxEditor allows you to specify the CompletionInfoCollection and add CompletionInfos to it.

By default, the CodeCompletionCommand is being executed by RadSyntaxEditor when pressing Space + Ctrl keys. It shows the CompletionListWindow. An alternative solution is to display the dialog programmatically by simply calling the SyntaxEditorElement.IntelliPrompts.CompletionListWindow.Show method.

conditional-intelliprompts-in-syntaxeditor001

However, the completion items are fixed and the window always shows the same items. A common requirement is to show dynamic items according to the the user's inputs. This tutorial aims to demonstrate a sample approach.

Solution

RadSyntaxEditor handles the user's keyboard input by its SyntaxEditorInputBehavior. You can create a custom behavior and override its ProcessKeyDown method. Thus, you can handle specific keys and execute the desired logic.

The following code snippet demonstrates how to show different suggestions in the CompletionListWindow when Ctrl + Space keys are pressed considering the text just before the caret position:

conditional-intelliprompts-in-syntaxeditor002

C#
 public RadForm1()
{
    InitializeComponent();

    this.radSyntaxEditor1.InputHandler = new CustomSyntaxEditorInputBehavior(this.radSyntaxEditor1.SyntaxEditorElement);
}

public class CustomSyntaxEditorInputBehavior : SyntaxEditorInputBehavior
{ 
    public CustomSyntaxEditorInputBehavior(RadSyntaxEditorElement editor) : base(editor)
    {
    }

    public override void ProcessKeyDown(KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Space && e.Control) // default is CodeCompletionCommand
        {
            CaretPosition start = new CaretPosition(this.SyntaxEditor.CaretPosition);
            CaretPosition end = new CaretPosition(start);
            start.MoveToPreviousWord(); 
            string textBeforeCaret = this.SyntaxEditor.GetText(start, end);
            if (textBeforeCaret == "Dock")
            {
                CompletionInfoCollection completionList = new CompletionInfoCollection()
                {
                    new CompletionInfo("Collapsed", "Indicates that the element is collapsed.", null),
                    new CompletionInfo("Hidden", "Indicates that the element is hidden.",null),
                    new CompletionInfo("Visible", "Indicates that the element is visible." ,null),
                };

                this.SyntaxEditor.IntelliPrompts.CompletionListWindow.Presenter.CompletionListItems = completionList;
            }
            else if (textBeforeCaret == "Telerik")
            {
                CompletionInfoCollection completionList = new CompletionInfoCollection()
                {
                    new CompletionInfo("RadGridView", "Grid component", null),
                    new CompletionInfo("RadSyntaxEditor", "Syntax editor",null),
                    new CompletionInfo("RadButton", "Button" ,null),
                };

                this.SyntaxEditor.IntelliPrompts.CompletionListWindow.Presenter.CompletionListItems = completionList;
            }
            base.PerformCodeCompletion(e);
        }
        else
        {
            base.ProcessKeyDown(e);
        }
    } 
}

 

See Also