Hallo Marijn!
I’m using CodeMirror for a project that requires editing of expressions - basically, simple one-liners… no funny code syntax - hence why I decided to go with simple mode.
The backend code itself has some integration with CM - type information and variable availability is passed to the editor. This is to allow type-hinting and on-the-fly code validation in the editor (eg; the user will know when a variable is not supported).
For example, let’s say there are 3 available variables: user_id
, name
and address
. The state would look like this:
states.start.push({
regex: new RegExp('user_id|name|address'),
token: 'variable'
});
This works well for scalar types, but when it comes to objects things start getting messy:
regex: new RegExp('user_id|name|address|address\.house|address\.street|address\.city|address\.country'),
For one thing, I don’t know how to make a usable regex for recursive references (eg: variable called “root” of class “node” which which defines two properties: name (string) and parent (node)):
regex: new RegExp('root\.parent|root\.name|parent\.parent|parent\.name'),
Assuming it’s somehow possible, it would be massive though - basically, all possible combinations.
So the question is, what options do I have? Should I go back to writing a “regular” mode?
Edit: One option is to use the correct regex pattern (eg: \([A-Za-z_][\w\.]*)\
) but then also pass the matches through a further filtering mechanism (perhaps based on a callback that returns true/false):
states.start.push({
regex: new RegExp('user_id|name|address'),
token: 'variable',
validator: function(token) { typesys.verifyPath(token); } // or just typesys.verifyPath
});
I think this would make sense since the token matcher itself probably should care about the token being valid or not as long as it is the correct syntax. Also, it would be backward-compatible with the current implementation.