-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Highlightjs not respecting parent regex of a sub mode #2238
Comments
Please provide output HTML so it’s 100% clear what is happening. I find your description not that clear. |
Looks like your second begin matches dvc pipeline then your third grabs list. |
It’s all strong because your 3rd matches is a child of the 2nd. |
@yyyc514 but the parent regex - |
I don’t think you understand how it works. The first regex matches then it switches to the contains and starts looking for children then matches list |
The first regex only tells it to begin. It can keep matching if there are children to match. |
The parents BEGIN regex has nothing to do with children, it just starts the mode. Well unless you use begin same as end. Lol. Then it COULD potentially |
@yyyc514 so how should I change my configuration to enable that? Basically, I would want to match |
Well depends how picky you want to be... You could probably get a long ways with just a single mode:
And then matching a long list of keywords... it wouldn't be "smart" but it might work for 95% of things... Or you could go all the way the other way and have a single mode per subcommand:
|
@yyyc514 we were trying the second way only. But we don't know how to distinguish between |
Well, that might be impossible in some circumstances. But if you had a separate mode for each then you know how many keyword to expect, right? Do for "dvc add" you'd be done... everything else should be arguments, no? What are the keywords for add? You only need to worry about matching keywords, not non-keywords. |
@yyyc514 dvc add doesn't have any. But "dvc pipeline" has one sub command called 'list'. So basically it can at most two sub-commands after 'dvc' |
Right... but again if you're doing "per subcommand" then it's be a tree... Or you might do better just hard coding them all:
Or any combination. There are only like 20 ways to do the same thing here. :) |
@yyyc514 Yes, I want to do it "per subcommand". I just don't know how can I differentiate between |
Why would there be any problem? per subcommand means writing outa rule PER subcommand. IE: Rules:
They would be ENTIRELy separate matchers. |
So if you had 20 commands you'd have 20 rules. |
@yyyc514 Okay you mean I make different sub-modes for commands with one sub-command and two sub-commands? |
Or a different sub-mode for EVERY command... really at this point you'd probably want to write code to build your syntax, not write it by hand... language_rules = []
language_rules.push(add_command("dvc add"))
language_rules.push(add_command("dvc pipeline", keywords: {...}))
return {
contains: language_rules
} |
@yyyc514 I am sorry. But I am unfamiliar with this |
It's an exercise for the reader, you'd have to write it yourself... I'm just talking about building the language dynamically using code. |
could you point to the docs please? I thought that it's working in a bit different way according to this: https://highlightjs.readthedocs.io/en/latest/reference.html#end
what does |
@yyyc514 I'm agreed in general on sub-commands/sub-modes approach in general! And thank you for the prompt help and response. Just trying to understand better the behavior here. It looked like a bug to us. First, it does not stop (okay, may be I misinterpreted the "immediately" part). Then why did @algomaster99 had to put Also, should it be matching |
It means immediately (and you’d see that for rules with no children) but if there are children/contains they will match. If it worked the way you are thinking you could never have any children or subrules. This may be a docs issues. Clarity could probably be added. |
I have no evidence right now this statement is true. The last bit eats the space which is often desirable. |
I though you can do |
I think a big key is it’s always named “begin”. It BEGINS a mode, not ends it. When there are no more matches a mode ends. And again if you are expecting it to immediately terminate AND also including children your not thinking consistently, because that makes no sense. returnBegin is a huge hack and often (always?) can be avoided with look ahead. It honestly should probably be deprecated. |
If you have a recommendation to update the docs a PR would be welcome. The behavior here (so far) is correct as intended. |
totally, I'm not arguing with that. And again thank you for your help! 🙏 I'm just trying to fit in my head the way you describe it and what we actually see in our case. For example this logic https://github.com/iterative/dvc.org/blob/master/src/Documentation/Markdown/lang/dvc.js#L47:L75 should be matching
is it correct? Or am I still missing something?
As far as remember we had to use it this piece above to actually being able match at least one kid and apply different classes - |
At a glance, yes. Although you could use endsParent if that was undesirable.
Right, to look-ahead. But real look ahead in regex actually works as of 9.16 so that’s a much better way. |
That's not what happening for me. Thus I'm confused. It's very easy to reproduce in our project.
could you please elaborate, share a link? |
Looks at some of the smaller commits in this PR for examples. |
Make a jsfiddle and I’ll look at it. You don’t eat the space. So your rule DOES not match again because there is an extra space, which immediately ends the parent mode. |
You could use end and excludeEnd to eat the space. |
Yep, I see. That's right! |
thanks! I see that we can get rid of returnBegin this way indeed. |
Added: Closing this as asked and answered, though conversation can continue if you have any other questions. |
I need to write a lexer which highlights my command-line tool commands properly.
So the command starts with
dvc
and it may have one or two subcommands -add
orpipeline list
respectively.Therefore, it should highlight
dvc add
anddvc pipeline list
in first and second case respectively.It matches
$ dvc pipeline list
even though the parent regex i.e./^\s*\$\s(dvc|git) [a-z-]+/
and should only match till$ dvc pipeline
. How is it exactly functioning?How does
/dvc [a-z-]+ ?/
override it and continues matching the expression?The text was updated successfully, but these errors were encountered: