Remove some items from code completion box

Hi.
I have a SyntaxEdit (with c# roslyn parser).
how can I remove some of items (field, property, method,…) from code completion box? Is it possible to exclude some items by their names?

Thanks

Hi Peyman,

The easiest way is to handle NeedCodeCompletion event and filter out unwanted properties/methods by name from the result list, something along this line.

            public void SyntaxEdit1_NeedCodeCompletion(object sender, CodeCompletionArgs e)
            {
                  var members = e.Provider as IListMembers;
                  if (members == null) return;

                  for (int i = members.Count - 1; i >= 0; i--)
                  {
                        var member = members[i] as IListMember;
                        if (member != null && member.Name == "MyMember"))
                        {
                              members.RemoveAt(i);
                        }
                  }
            }

Regards,
Dmitry

Hi @dmitry.medvedev
Thanks for this solution. It works but can unwanted items be filtered only once (for example, at the beginning of the program)? Because it’s a time consuming task and “NeedCodeCompletion” event is called many times…

Secondary question: different classes may contains the same method/property names. so how can i filter out a member only in specific Class/Type?

Thanks

Hi Peyman,

NeedCodeCompletion method is called after code completion items are already obtained and when code completion listbox is about to be displayed, so doing one loop through items will not have much of impact on the performance.
You can also look at additional information if you typecast

var item = (member as RoslynListMember).Item;

However this CompletionItem might not have all information you need. You can see if it’s a class or property by examining CompletionItem.Tags, but not the parent class this property belongs to.

We used different API before that had all this information, but it’s not available in the new API:

In some cases you might be able to figure out symbol that the property being displayed belongs to (for example if you type myvariable. - list of properties to be displayed here), then you can look at what myvariable is by executing parser.FindDeclaration method and obtaining Symbol from it.

We can provide an example project to demonstrate how this can be done.

Regards,
Dmitry

1 Like

Hi Dmitry,
Thanks a lot. my problem solved

Regards,