AndrewRadev / sideways.vim

A Vim plugin to move function arguments (and other delimited-by-something items) left and right.
http://www.vim.org/scripts/script.php?script_id=4171
MIT License
481 stars 9 forks source link

Support class option hash for eruby #52

Open jdsutherland opened 3 years ago

jdsutherland commented 3 years ago

It'd be nice if https://github.com/AndrewRadev/sideways.vim/pull/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 commented 3 years ago

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 commented 3 years ago

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 commented 1 year ago

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 commented 1 year ago

@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?