Last week we discussed the concept of prioritized item matching algorithms and yesterday I talked about completion list matched text highlights.
We already have four matching algorithms in place for the default completion list implementation, two of which can be toggled on/off via options. All the matching algorithms simply implement an interface.
Now wouldn’t it be neat if you could write your own customized matching algorithm?
Well you can with SyntaxEditor, and it’s so easy too!
A SQL example
Let’s assume you are showing a list of SQL data objects and you want to implement a custom matcher that uses a regular expression to match
any item that has the typed text appearing after a "." or "_" character. The default starts-with matcher should also be used to match text at the start of each item.
To start you’d make a custom class that inherits our RegexCompletionItemMatcherBase class.
1: public class CustomCompletionItemMatcher : RegexCompletionItemMatcherBase {
2: ...
Next you’d override a GetRegex method to return the Regex to use.
1: protected override Regex GetRegex(string text) {
2: // Make sure the text to highlight is surrounded with parenthesis
3: // so that SyntaxEditor can locate the captures
4: return new Regex(String.Format("[\\._]({0})", Regex.Escape(text)),
5: RegexOptions.IgnoreCase | RegexOptions.Singleline);
6: }
That’s it! The RegexCompletionItemMatcherBase base class has all the code in it to automatically match items and highlight matched text based on your Regex.
Here’s what the list looks like at run-time:
|

The custom item matcher in action, with results highlighted
|
Note that since we typed an L, the LOCATION.ID item was selected. However you can see that CUSTOMER.LAST_NAME and EMPLOYEE.LAST_NAME also had their L characters highlighted that occurred after a period. EMPLOYEE.CELL_NUMBER has L characters in it too but our algorithm didn’t match it because the L came after a letter and not a period or underscore.
More advanced implementations
While it is likely that the Regex-based item matcher will provide all the functionality you need for a custom item matcher implementation, sometimes you may require additional logic. No problem, there is a more low-level CompletionItemMatcherBase base class that lets you implement any sort of logic you desire to match items and return highlighted ranges over item text. All the base classes implement the ICompletionItemMatcher interface.