How can Auto completion work properly when it is not "Word"?

SyntaxEditor .NET Languages Add-on for WPF Forum

Posted 2 years ago by Kim Jiho - Ansys
Version: 22.1.2
Avatar
private readonly CustomCompletionProvider customCompletionProvider = new CustomCompletionProvider();
private readonly EntityNameCompletionProvider entityNameCompletionProvider = new EntityNameCompletionProvider();

.
.
.

this.editor.DocumentTextChanged += (sender, e) =>
{
    if (e.TextChange.Source == this.editor.ActiveView)
    {
        if (e.IsTypedWordStart)
        {
            // If no completion session is currently open, show a completion list
            if (!this.editor.IntelliPrompt.Sessions.Contains(IntelliPromptSessionTypes.Completion))
            {
                this.editor.Document.Language.RegisterService<ICompletionProvider>(this.customCompletionProvider);

                EditorCommands.RequestIntelliPromptCompletionSession.Execute(this.editor.ActiveView);
            }
        }
        else
        {
            if (e.TypedText == "/")
            {
                this.editor.Document.Language.RegisterService<ICompletionProvider>(this.entityNameCompletionProvider);

                EditorCommands.RequestIntelliPromptCompletionSession.Execute(this.editor.ActiveView);
            }
        }
    }
};
public class EntityNameCompletionProvider : CompletionProviderBase
{
	public override bool RequestSession(IEditorView view, bool canCommitWithoutPopup)
	{
		var completionSession = new CompletionSession
		{
			CanCommitWithoutPopup = canCommitWithoutPopup,
			CanFilterUnmatchedItems = true,
            MatchOptions = CompletionMatchOptions.UseAcronyms | CompletionMatchOptions.UseShorthand,
        };

		var highlightingStyleRegistry = view.HighlightingStyleRegistry;
		completionSession.Items.Add(new CompletionItem("/BD_01", new CommonImageSourceProvider(CommonImageKind.Keyword), new CustomContentProvider(highlightingStyleRegistry, typeof(bool))));
		completionSession.Items.Add(new CompletionItem("/BD_01/BaseMarker", new CommonImageSourceProvider(CommonImageKind.Keyword), new CustomContentProvider(highlightingStyleRegistry, typeof(byte))));
		completionSession.Items.Add(new CompletionItem("/BD_01/ActionMarker", new CommonImageSourceProvider(CommonImageKind.Keyword), new CustomContentProvider(highlightingStyleRegistry, typeof(char))));
		completionSession.Items.Add(new CompletionItem("/Ground", new CommonImageSourceProvider(CommonImageKind.Keyword), new CustomContentProvider(highlightingStyleRegistry, typeof(double))));
		completionSession.Items.Add(new CompletionItem("/RJ_01", new CommonImageSourceProvider(CommonImageKind.Keyword), new CustomContentProvider(highlightingStyleRegistry, typeof(float))));
		completionSession.Items.Add(new CompletionItem("/RJ_01/BaseMarker", new CommonImageSourceProvider(CommonImageKind.Keyword), new CustomContentProvider(highlightingStyleRegistry, typeof(int))));
		completionSession.Items.Add(new CompletionItem("/RJ_01/ActionMarker", new CommonImageSourceProvider(CommonImageKind.Keyword), new CustomContentProvider(highlightingStyleRegistry, typeof(long))));

		if (completionSession.Items.Count > 0)
		{
			// Ensure the caret is visible
			view.Scroller.ScrollToCaret();

			// Ensure the items are sorted and open the session
			completionSession.SortItems();
			completionSession.Open(view);
			return true;
		}

		return false;
	}
}

Environment

- Windows 10 Enterprise 20H2

- Visual studio 2019 16.11.16

- net48

- ActiproSoftware.Controls.WPF.SyntaxEditor 22.1.2 nuget

I'm working on example of auto completion, and the text of "CompletionItem" starts with a slash.

Enter "/" in Syntex Editor and select an item from the list to enter "//Value". (eg. "//BD_01", expect: "/BD_01")

Enter "/BD_01/ActionMarker", press Ctrl+Space, and select "/BD_01/ActionMarker", and enter "/BD_01//BD_01/ActionMarker". (expect: "/BD_01/ActionMarker")

How can Auto completion work properly when it is not "Word"?

[Modified 2 years ago]

Comments (2)

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

I'd recommend doing some text scanning around the caret at the start of RequestSession to determine the path expression that might already be in the document.  For instance, you might see that a "/" is already typed before the caret.  Or you might see that "/BD_01" is there, or "/BD_01/".  You need that kind of contextual information to determine what text to insert via completion items.

Say you see a "/" is the context before the caret.  Then your completion item's text to insert should not begin with a "/" character.

You might decide to just do one path entry at a time (e.g. "/BD_01") instead of showing "/BD_01/ActionMarker" as an option.  Typing "/" would first show the "BD_01" option.  If the user highlights that and types "/", it would auto-complete "BD_01" then start a new completion session that would see "/BD_01/" is now typed and would show the "ActionMarker" entry next.  That's generally how we would approach path completion.

I hope that helps!


Actipro Software Support

Answer - Posted 2 years ago by Kim Jiho - Ansys
Avatar
var charIntervals = new CharInterval[] { new CharInterval('_'), new CharInterval('/'), };
var completionSession = new CompletionSession
{
	AllowedCharacters = new CharClass(charIntervals),
	CanCommitWithoutPopup = canCommitWithoutPopup,
	CanFilterUnmatchedItems = true,
	MatchOptions = CompletionMatchOptions.UseAcronyms | CompletionMatchOptions.UseShorthand,
};

I added characters to the "AllowedCharacters" property to make it work. Thanks for help.

The latest build of this product (v24.1.1) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.