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

Split comma-separated parameters #68

Open
firedev opened this issue Mar 19, 2015 · 7 comments
Open

Split comma-separated parameters #68

firedev opened this issue Mar 19, 2015 · 7 comments

Comments

@firedev
Copy link

firedev commented Mar 19, 2015

Hi, me again...

Sorry, I forgot what is the consensus on splitting lists? Is this a valid issue?

Given:

▐"#4d0121", "#660130", "#7f023e", "#930049", "#a80156", "#c30766", "#d21272", "#de2a84", "#e75298", "#eb5d9d"

gS

Expected:

▐"#4d0121",
 "#660130",
 "#7f023e",
 "#930049",
 "#a80156",
 "#c30766",
 "#d21272",
 "#de2a84",
 "#e75298",
 "#eb5d9d"

Actual:

▐"""
      #4d0121
""", "#660130", "#7f023e", "#930049", "#a80156", "#c30766", "#d21272", "#de2a84", "#e75298", "#eb5d9d"
@AndrewRadev
Copy link
Owner

First off, it would be nice to know which language this is :). I'm guessing it's coffeescript, based on the triple quotes.

As for the lists -- the only way that list splitting would work is if there are delimiters. There has to be some way to detect "you're in a list", and checking for commas is by far not enough. If it was ["#4d0121", "#660130"], for example, or maybe function_call("#4d0121", "#660130"), I would consider implementing something that turns this into a multiline format.

The implementation depends on the language, though. Coffeescript, for example, doesn't require commas on every line if it's a multiline list. I'm not sure about argument lists in function calls.

@firedev
Copy link
Author

firedev commented Mar 19, 2015

Sorry about that, yes thats coffeescript. I was converting some old code. I know it's hard to fix everything with just regexps. Are you saying that it would work if I start on the square bracket instead? Didn't even think about that.

Here's how I go about splitting long lines:

[ xxxxxx ]

first I just extract it on a new line using surround-vim

[
  xxxxx
]

If that's not enough I split the long list:

[
  x
  x
  x
]

@AndrewRadev
Copy link
Owner

I don't mean that if you try on the square brackets, it would work. I mean that, in order to implement this, I'd need to have some way to detect that these comma-delimited items are in a list.

In the situation of "lists with strings that are valid hex codes", this is easy enough to do -- just split by commas. But if I just split by commas, this would do very wrong things when the strings contain commas, or when you have nested lists or function calls.

If you're mainly refactoring these kinds of strings, a simple s/,\s\+/,\r/g should do the trick, and you can use history to repeat it, or make a command for it or something. (Though I suppose it took me a while to get to this, so you're probably not working on this problem anymore :)).

Now, if you would like me to implement generic argument/list splitting for coffeescript, I could give it a try. I guess it would work similarly to the ruby one or the python one. I would also have to consider how this would work in combination with object literal splitting.

But bear in mind, this means that splitting would work like this:

foo = ["one", "two", "three"]
foo = [
  "one"
  "two"
  "three"
]

So if you split it like this beforehand:

foo = [
  "one", "two", "three"
]

I'd rather not try to detect this case, since I'd have to search back and forth for whether there are brackets or not. And I would only do this if this "looks like a list", which I'm not sure how to detect either (what if it's function_call("one", "two"), do I look above for brackets? I shouldn't, but how can I decide this?).

What do you think? Should I work on list/argument splitting or does that not help with your use case?

@firedev
Copy link
Author

firedev commented Apr 22, 2015

Hey, thanks for getting back to me on this. Recently the pressure increased so I am accepting things as they are.

I break out of brackets using vim-surroung ysi[^J^J - that sometimes blows the things up but what can you do. Then I do :s/, /,\r/g to break parameters vertically. I have a mapping to re-align the whole file with === and all excessive whitespace is automatically trimmed on save.

To wrap it up, yes it would be great to have this ability, but I understand vim is not very friendly in that regard and it will cause more support headaches down the line. I don't see anybody else commenting, maybe this is a pattern nobody else is using.

On a side note, yesterday vim-switch really helped me to convert a bunch of ruby hashes:

"before" => "Was like this"

---

after: "It's like this"

Thanks.

@AndrewRadev
Copy link
Owner

I should have probably responded a lot earlier, sorry about that. To start off, thanks for your kind words regarding vim-switch, I'm glad it helped you out :).

As for the feature itself, I did have a complaint from someone else as well that it would be nice if a list of items could be split "partially", and I admit I've had similar use cases. I'll leave this issue open for now and maybe I'll come around to implementing this yet.

@firedev firedev changed the title List of strings Split comma-separated parameters May 23, 2016
@firedev
Copy link
Author

firedev commented May 23, 2016

Hey, just wanted to check up if you have any new thoughts about the matter.
Now with ES6 it makes more sense than ever:

import { whatever, and, something } from 'package'

cSBB from vim-surround makes sure the params are on the separate line:

import { 
  whatever, and, something 
} from 'package'

And now if only I could do gS to top it off:

import { 
  whatever, 
  and, 
  something 
} from 'package'

No need to worry about joining the lines, maybe remove the trailing comma, but it's not important.

Please consider. Thanks.

@firedev
Copy link
Author

firedev commented May 29, 2016

Here is what I came up with. bp is for 'Break Parameters':

map <leader>bp f}%cSBBj:s/,/,\r/g<CR>viB==
  • find closing curly brace }
  • % go back to the opening {
  • change Surround blockwise from { to {
  • j down one line
  • :substiture , with ,\r /globally
  • visually select culybraces Block
  • == reindent

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

2 participants