CompletionSession item filtering

SyntaxEditor Python Language Add-on for WPF Forum

The latest build of this product (v22.1.3) was released 20 days ago, which was before this thread was created.
Posted 14 days ago by Kim Jiho - Ansys
Version: 22.1.3
Avatar

I use Python addon as an python expression evaluator.

Imports a functions that I predefined into the syntax editor header to provide the function to the user.

var headerText = string.Join(Environment.NewLine,
    paths
    .SelectMany(path => Directory.GetFiles(path))
    .Select(filePath => Path.GetFileNameWithoutExtension(filePath))
    .Select(fileName => $"from {fileName} import *"));

editor.SetHeaderAndFooterText(headerText, string.Empty);

Auto-complete includes the following items:

  • Function that I predefined
  • Many others(built-in keywords, constants, functions)

I want to exclude "many others". How can I exclude them?

 

Comments (2)

Posted 14 days ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

I would suggest that you make a class that inherits the PythonCompletionProvider class and override its OnSessionOpening method.  In that, you can look at each item and remove the ones you want.  Then call the base OnSessionOpening method and return its result.  Unregister the default PythonCompletionProvider service from the language and register your custom one in its place.
 
Here's a quick example, and of course you could extend the logic a lot more.
private class MyPythonCompletionProvider : PythonCompletionProvider {

	protected override bool OnSessionOpening(ICompletionSession session) {
		for (var index = session.Items.Count - 1; index >= 0; index--) {
			var item = session.Items[index];
			var removeItem = false;

			if (item.Tag is Text.Languages.Python.Resolution.ITypeResolverResult typeResult) {
				// Exclude built-in types
				if (typeResult.Type?.DeclaringModule?.IsBuiltin == true)
					removeItem = true;
			}
			else if (item.Tag is Text.Languages.Python.Resolution.IFunctionResolverResult functionResult) {
				// Exclude built-in functions
				if (functionResult.Function?.DeclaringModule?.IsBuiltin == true)
					removeItem = true;
			}
			else if (item.Tag is Text.Languages.Python.Resolution.IVariableResolverResult variableResult) {
				// Exclude built-in variables
				if (variableResult.Variable?.DeclaringModule?.IsBuiltin == true)
					removeItem = true;
			}
			// NOTE: Examine other resolve result types in conditionals here too
			else if (item.Tag == null) {
				// Keywords
				removeItem = true;
			}
					
			if (removeItem)
				session.Items.RemoveAt(index);
		}

		return base.OnSessionOpening(session);
	}

}
editor.Document.Language.UnregisterService<PythonCompletionProvider>();
editor.Document.Language.RegisterService(new MyPythonCompletionProvider());

I hope that helps!


Actipro Software Support

Posted 14 days ago by Kim Jiho - Ansys
Avatar
protected override bool OnSessionOpening(ICompletionSession session)
{
    var items = session.Items;
    var itemsCount = items.Count;

    for (int index = itemsCount - 1; index >= 0; index--)
    {
        var item = items[index];
        var isBuiltin = item.Tag switch
        {
            ITypeResolverResult typeResult when typeResult.Type?.DeclaringModule?.IsBuiltin ?? false => true,
            IFunctionResolverResult functionResult when functionResult.Function?.DeclaringModule?.IsBuiltin ?? false => true,
            IVariableResolverResult variableResult when variableResult.Variable?.DeclaringModule?.IsBuiltin ?? false => true,
            null => true,
            _ => false,
        };

        if (isBuiltin)
        {
            items.RemoveAt(index);
        }
    }

    return base.OnSessionOpening(session);
}

It works well as you suggested.
Thank you for your help.

Add Comment

Please log in to a validated account to post comments.