Skip to content
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

Support class option hash for eruby #52

Open
jdsutherland opened this issue Jul 4, 2021 · 4 comments
Open

Support class option hash for eruby #52

jdsutherland opened this issue Jul 4, 2021 · 4 comments

Comments

@jdsutherland
Copy link
Contributor

jdsutherland commented Jul 4, 2021

It'd be nice if #41 worked for option hashes in eruby:

<li><%= link_to 'Home', '#', class: 'one two three' %></li>

I tried implementing this myself:

let b:sideways_definitions = [
      \   {
      \     'start':     '<%=\=\s*\%(\k\|\.\|::\)*\k\{1,} ',
      \     'end':       '\s*%>',
      \     'delimiter': ',\s*',
      \     'brackets':  ['([''"', ')]''"'],
      \   },
      \   {
        \     'skip_syntax':             [],
        \     'start':                   "\v<class:\s?'",
        \     'end':                     "'",
        \     'delimited_by_whitespace': 1,
        \     'brackets':                ['', ''],
        \   },
      \   sideways#html#Definition('tag_attributes',      { 'brackets': ['"''<', '"''>'] }),
      \   sideways#html#Definition('double_quoted_class', { 'brackets': ['<', '>'] }),
      \   sideways#html#Definition('single_quoted_class', { 'brackets': ['<', '>'] }),
      \   sideways#html#Definition('double_quoted_style', { 'brackets': ['<', '>'] }),
      \   sideways#html#Definition('single_quoted_style', { 'brackets': ['<', '>'] }),
      \ ]

but the existing seemed to take precedence. I'm guessing the regex:

'start': '<%=\=\s*\%(\k\|\.\|::\)*\k\{1,}`

would have to negative lookahead for class:?

@AndrewRadev
Copy link
Owner

No, that's fine -- the definitions are all attempted, and the "smallest" match wins. The problem is that the pattern is surrounded by double-quotes, and those evaluate backslashes. If you echo b:sideways_definitions[1], you'll get:

{'skip_syntax': [], 'end': '''', 'brackets': ['', ''], 'start': 'v<class:s?''', 'delimited_by_whitespace': 1}

And you can see the start pattern starts with v instead of \v. One option would be to use \\ in the double-quotes, another is to use single quotes and escape apostrophes by doubling them: '\v<class:\s?'''. This is, admittedly, a bit obscure :). You could learn more details from :help string and :help literal-string.

I'm not sure if I it makes sense to keep this in the default definitions, since it's specific to the rails helper, and not a feature of ERB itself. Still, I guess it wouldn't be a practical problem, and it's true that most ERB is rails... Would you like to see this in the defaults, or are you happy to use this in your own override? If you'd like, you could create a PR with the change, and I can probably be persuaded to merge it.

@jdsutherland
Copy link
Contributor Author

jdsutherland commented Jul 5, 2021

I'm fine with having this be an override. I can imagine encountering a gem that use some variation of the name class that I'd want to add in.

Thanks for clearing that up! I've learned some things that should useful for future changes. Actually the customization section in help covers this pretty well, sorry!

@pooriajr
Copy link

pooriajr commented May 31, 2023

I find myself having the same problem constantly with classes in option hashes in ERB. I've gotten used to stopping myself from using this plugin's functionality in that one context, but it's a constant point of tiny friction

I tried using @jdsutherland implementation, then modifying it based on @AndrewRadev's response, but couldn't get it to work... I know it's an old issue but if anyone can help I would be really grateful 🙏

AndrewRadev added a commit that referenced this issue Jun 4, 2023
@AndrewRadev AndrewRadev reopened this Jun 4, 2023
@AndrewRadev
Copy link
Owner

@pooriar I've added support to the repo in the eruby-classes branch. Could you try it out and see if the patterns in that form work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants