Custom Match Implementation¶
Tip
You may check how built-in Matches are implemented here.
You may create your own custom Match implementation by following steps.
- Run
AutoSetSyntax: Create New Matchfrom the command palette1. -
It will create a template like
Example
from AutoSetSyntax.plugin import UNFOLDED from AutoSetSyntax.plugin import AbstractMatch from AutoSetSyntax.plugin import Fold from AutoSetSyntax.plugin import MatchableRule from AutoSetSyntax.plugin import ViewSnapshot class MyOwnMatch(AbstractMatch): """Your custom `Match` must inherit `AbstractMatch` and implement the `test` method.""" def fold(self, rules: tuple[MatchableRule, ...]) -> Fold: # Optionally, you can implement `fold` to tell the optimizer that this combinator's # parameters and `rules` count already settle the answer, so it needn't be tested on # every view: return `Fold(True/False, "why")` for the constant `test` would always # return, or `UNFOLDED` when the result still depends on the view. # The reason is shown to the user in the dropped-rules report, so phrase it for them, # e.g. `Fold(False, 'an "any" with no sub-rule matches nothing')`. return UNFOLDED def test(self, view_snapshot: ViewSnapshot, rules: tuple[MatchableRule, ...]) -> bool: # Your job is to implement this function, at least. # This function tests `rules` (mix of `ConstraintRule`s and `MatchRule`s). return False -
Decide the match name of your
Match.Say, if your class name is
MyOwnMatch, the match name is decided by- Remove
Matchsuffix from the class name. (MyOwnMatch»MyOwn) - Convert it into snake case. (
MyOwn»my_own)
That is, you can use it via
"match": "my_own"in a match rule. - Remove
-
At least, implement the
testmethod. -
Save your implementation in
Packages/AutoSetSyntax-Custom/matches/. Conventionally, the file name used is the match name,my_own.py. -
Restart ST and check whether your implementation is loaded via Debug Information.
-
Command palette: Ctrl+P for Windows/Linux. Cmd+P for macOS. ↩