andrewradev / splitjoin.vim Goto Github PK
View Code? Open in Web Editor NEWSwitch between single-line and multiline forms of code
Home Page: http://www.vim.org/scripts/script.php?script_id=3613
License: MIT License
Switch between single-line and multiline forms of code
Home Page: http://www.vim.org/scripts/script.php?script_id=3613
License: MIT License
Hi, I was wondering if it would be possible to add support for splitting inline if
statements.
For example:
if (something) do_something();
To be split into:
if (something)
do_something();
Right now what happens is this:
if (
something
) do_something();
At least in PHP does. Thanks in advance!
Given:
facilities = facilities.flatten.map { |item| item.is_a?(Facility) ? item.id : item.to_i }
After pressing gS
I expect it would start from the outside and convert it to:
facilities = facilities.flatten.map do |item|
item.is_a?(Facility) ? item.id : item.to_i
end
Instead i get this, which is wrong and not even ruby:
facilities = if facilities.flatten.map { |item| item.is_a?Facility
item.id
else
item.to_i }
end
I might be missing something, but there appears to be no (default) support for operator pending and visual mode to apply the splitting/joining across a given motion / region.
E.g. for gJap
to join a paragraph (which would require the default to be moved to gJJ
though).
I've tried to use visual mode as a workaround, but that seems to also get not handled/mapped (e.g. vapgJ
).
It'd be cool if we could switch between
-> (a, b) { a.do_stuff_with(b) }
and
lambda do |a, b|
a.do_stuff_with(b)
end
Though there's gonna have to be some fiagling to make it not overlap with the block-syntax splitjoiner
(thanks for the awesome plugin, btw π)
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"
I'm not sure how easy/hard this is but when using fluent interfaces you can end upp with long lines looking like the following
$entries = $objects->method1()->method2()->method3();
it would be nice if you could do gS and end up with
$entries = $objects
->method1()
->method2()
->method3();
One would have to deal with things like this though
$entries = $object->method1()->method2($person->name)->method3();
which I guess one would still like to be on the same line like
$entries = $objects
->method1()
->method2($person->name)
->method3();
foo = if test then 'bar' else 'baz'
to
if test
foo = 'bar'
else
foo = 'baz'
The current behavior with Python does not seem to be configurable:
When using gS
on a function call like:
some_function(a, b, c, d, e)
If get:
some_function(a,
b,
c,
d,
e)
Which may be fine for some people, but the default Python syntax I get when I type the line-endings manually gives me:
some_function(
a,
b,
c,
d,
e,
)
Is there a way to use the default syntax rules configured by the user to get the output I expect ?
Thanks for the good work !
Splitting/Joining arrays in Perl does not function at all. Here are example of the two most common perl array syntaxes. The first is very simiar to PHP. The second is an array that's space separated.
note leading qw()
.
The first requires quotes around items, and is comma separated. The second does not require quotes, and is whitespace separated. Would it be possible to add support for these also?
my @bar = ('one','two','three','four','five','six');
my @foo = qw(one two three four five six);
Is there a plan to support Ruby arrays and their literals (%w{}, %i{}, %W{}, %I{})?
It'd be super cool if I could convert
[1, 2, 3, 4].map(&:to_s)
to
[1, 2, 3, 4].map do |i|
i.to_s
end
This is a bit ambiguous because of the name of the block parameter - it could just always be i
, or maybe the singularization of the variable if a variable is used instead of a literal? But then there's localization...
I just noticed that splitting/joining doesn't honor the spaces/tabs used in the current buffer. For example
foo = (bar) ->
--->if foo # ---> is a tab
--->--->bar() # two tabs here
produces
foo = (bar) ->
bar() if foo # spaces in front of bar()
This is a coffeescript example, and 2 spaces is the canonical indentation, but now all projects honor it, so using the values in sw/ts/sts/expandtab to determine what kind of indentation to use makes sense.
So splitjoin.vim is pretty slick, and I feel like it deserves to claim a couple of maps. This would of course reduce friction for new users, and also make it easier for "distributions" like Janus to ship it by default. As a frequent pair programmer, I care about both of these greatly.
I'd imagine you'd agree that sj
and sk
are nonstarters (take it from the guy that used to map s
in surround.vim). And leader maps are pretty lame. But there's still plenty of good options. You could pick a pair like cS
and cJ
(or cJ
and cK
, if that feels more natural), and have virtually zero chance of stepping on anyone, ever.
Think about it!
When I split join
var x = {
something: 'asdf',
anotherThing: 'asdf',
anotherThing: 'asdf'
};
I get:
var x = { something: 'asdf', anotherThing: 'asdf', anotherThing: 'asdf' };
Most style guides forbid the additional spacing that pads the "hugging" of the braces. Can JavaScript be configured to join them as follows:
var x = {something: 'asdf', anotherThing: 'asdf', anotherThing: 'asdf'};
Excellent plugin otherwise!
As requested in #47 (comment). The way this would work would probably be something like:
var foo = bar(one, two, three)
var foo = bar(
one,
two,
three
)
It would be great if split join could do something like extracting params:
File.join(Rails.root, 'db/seed', "#{class_name.to_s.underscore}.yml")
File.join(
Rails.root, 'db/seed', "#{class_name.to_s.underscore}.yml"
)
File.join(
Rails.root,
'db/seed',
"#{class_name.to_s.underscore}.yml"
)
Related to tpope/vim-surround#140
It works on various type of files if their filetype is available.
But when I create a new buffer, i.e. tabenew
or macvim CMD+N
after I type
<div id="foo">bar</div>
_______________^
:gS
^ is the cursor position
Nothing happens, it would be better if can load automatically after a new buffer. Thanks.
foo = """
ala bala
"""
and
foo = "ala bala"
Tested to be working perfectly on vim in Ubuntu 12.04, but command doesn't do anything on Mac 10.8 VIM version 7.3. Issue localized to "function! sj#ReplaceMotion(motion, text)" in sj.vim (pattern matching and replacement generation is being done correctly, however, the actual replacement doesn't happen).
Been writing lots and lots of angular lately - directives and templating let you often end up with something like this:
<span token="token" colorize="true" click="true" hover="true" highlight="true"></span>
Mind that such a line can be even longer and gets therefore very hard to read. I prefer a formatting like this then:
<span
token="token"
colorize="true"
click="true"
hover="true"
highlight="true">
</span>
Would you be interested in adding something like this? Question is how this would collide with the general routine of HTML splitjoining.
I am also not sure if this is compliant with any real official HTML styleguide - I just know that styling it like that seems much more readable to me.
Splitting arg list for Ruby method creates additional set of parentheses.
However, this only happens when there is space after opening parens or before closing parens.
If this behavior is known, not so much a problem.
Perhaps this could be addressed by just updating the section on method arguments in splitjoin.txt?
The following Perl code
if ($foo) { $a = 'bar'; }
splits to this:
if ($foo) {
$a = 'bar';,
}
Note the trailing comma
Is it possible to have this? Thanks,
Is it possible to make this "splitable":
if ($foo) { $a = 'bar'; }
to this
if ($foo) {
$a = 'bar';
}
The addon seems to be cool. But there is no perl support 'till now. Is it possible to add perl?
blabla if foo; -> if (foo)\n{\n blabla;\n} -- foo could be in brakes but do not have to in single line.
Same with while, until, unless, ...
gS
on the following (where the cursor is on the opening {
):
things.each { |thing| thing.do_stuff if thing.can_do_stuff? }
results in:
if thing.can_do_stuff? }
things.each { |thing| thing.do_stuff
end
where I would expect it would result in:
things.each do |thing|
thing.do_stuff if thing.can_do_stuff?
end
console.log "foo" if bar?
when expanded expands to
console.log """
foo
""" if bar?
string if the cursor is after the first "
, even when it is after the end of the string. This is a bug I think -- that is, strings should be expanded only if you are inside the string.
Please do not include doc/tags as it would override a local one and should be created.
I used example files under splitjoin.vim fold to test, but the SplitjoinSplit command doesn't work. My configure is here.
Hi,
Me again. I don't know if this is intended, but seems a bit wrong to me:
Given:
render locals:β{ found: found }
Expected:
render locals: {
found: found
}
Actual:
render locals: {
found: found, # <- extra comma
}
Implementable via opening a new buffer, performing the operation and writing it back in. Proof of concept in the visual-mode branch.
It would be cool if splitjoin would not add hash brackets for ruby method options arguments:
validate :title, :presence => true
# into
validate :title,
:presence => true
And vice versa.
Can be done with an option or as default behavior like: keep brackets if they where and don't add if they wasn't.
any plan for supporting css??
It'd be cool if splitjoin could convert the following:
<?php $whatever = 'stuff'; ?>
into the following:
<?php
$whatever = 'stuff';
?>
I would like to split and join lines on ";", e.g. when editing sublists in the shell.
This could also apply make sense for languages where you end a command with semicolon, like PHP, Javascript, etc.
What do you think it the best approach to this?
It seems like there could be a generic helper method for this in autoload/sj.vim (split and join on a specific pattern), and/or something like a generic mode, where something like that would be easily possible without being bound to a specific filetype.
I could imagine a command where you provide the pattern to use, and this could be easily mapped then and/or applied to modes where it makes sense.
This is just a quick idea, without me having looked really at the source (apart from your presentation yesterday).
btw: while there are references to the commands (:SplitjoinJoin
, ...) in doc/splitjoin.txt, there is no documentation for it.
Following previous discussions I am suggesting to extend the split shortcut and alow user to specify a character for splitting:
%w(
βwhat ever goes here
)
gS<space>
%w(
βgets
split
by
space
)
If one tries to convert a single statement inline block into a multiline block, the conversion assumes the inline block is a hash.
Original:
this { block gets, horribly: broken }
After :SplitjoinSplit
this {
horribly: broken,
}
It's not too easy to detect these, but one good idea would be to test if the {} is preceded with a word, then it's most likely a block. If there is a = or no word, then it's most likely a hash. I bet there are better ways to detect this, but I can't think of them at the moment.
In Ruby, when joining:
if true
puts 'hello'
end
You get this:
puts 'hello' if true
Personally, I'm a fan of using two spaces to add a little visual separation between the condition suffix and the main statement, like so:
puts 'hello' if true
I've manually hacked this for my setup, but it would be great if there were a configuration option baked in for this.
Hey @AndrewRadev, would you be interesting in adding ternary support for Ruby to the built in feature set?
Here's what it does: https://gist.github.com/LFDM/8721142
And here's how its implemented: https://github.com/LFDM/splitjoin.vim/blob/ternary_support/autoload/sj/ruby.vim#L53-160
A slight change to the IfClause functions was needed, but all for the better: They now return false when there is an else case present - tests of course still green.
I'll add a PR and tests if you're interested.
In languages with associative arrays I often use another program to line them up for easy reading like:
my $info = {
name => $map_name,
admin_status => lc($parts[1]),
operational_status => lc($parts[2]),
evc => $parts[3],
};
When I use splitjoin to join this in to one line I end up with:
my $info = {name => $map_name, admin_status => lc($parts[1]), operational_status => lc($parts[2]), evc => $parts[3]};
Would it be possible to remove whitespace from the keys?
JavaScript can split/join Object literals and functions. It would also be nice to split/join array literals as well:
var foo = [ 'baz', 'bar', 'froboz', 'gnusto' ];
var foo = [
'baz',
'bar',
'froboz',
'gnusto'
];
I wouldn't mind making a pull request but I'm not so sure where to start (code wise).
Hi,
I just installed this plugin which looks great, good work!
I'f got an issue with PHP arrays, e.g from:
$a = array(1,2);
If i press gS on the line i get:
$a = array(
1,
2
);
which is perfect, but then, selecting those lines in visual mode and pression gJ i get:
$a = array( 1, 2);
With those extra spaces between the elements.
If i press gJ on the first line instead of selecting all the array lines with visual mode i get:
$a = array( 1,
2
);
Any hints? Thanks.
Consider following example
\documentclass[]{article}
\usepackage[]{amsmath}
\begin{document}
\begin{center}Hello World\end{center}
\begin{center}
Hello World
\end{center}
\begin{align*} x = y\\ y = z \end{align*}
\begin{align*}
x = y\\
y = z
\end{align*}
\begin{tabular}[]{cc} row1 col1 & row1 col2 \\ row2 col1 & row col2\end{tabular}
\begin{tabular}[]{cc}
row1 col1 & row1 col2 \\
row2 col1 & row col2
\end{tabular}
\end{document}
Is it possible to add support for all these examples generically?
outer blocks are \begin/\end
, line delimiters are \\
, words center, align, tabular are only examples
if true
1
else
2
This doesn't want to be joined correctly in any way.
I have the following Perl code which is ripe for split, but it doesn't seem to work. I can split PHP and CSS, but it seems like Perl support is broken?
my $info = {name => $map_name, admin_status => lc($parts[1]), operational_status => lc($parts[2]), evc => $parts[3]};
I see that there is CSS support for your plugin, and I think it would only be natural to extend support for LESS and SCSS. As far as I know (I haven't tested this yet) the syntaxes are similar enough that adding support would only require changing what file types the plugin registers itself with.
Right now Splitjoin doesn't detect the 1.9 hash syntax, e.g.
{ a: b, c: d }
# splits to
do
a: b, c: d
end
I tried looking at where to modify this but it looks like I'll need to spend a little more time to understand how the Process method in the ruby argparser works.
Something I find myself wanting to do a lot is split and join PHP argument lists, as per PSR-2.
,
separated argument lists are handled nicely for arrays in PHP (and no doubt other languages); if I were to make changes for argument lists, is the preference to add on functionality or generalise the array implementation? Thanks.
Here's my feelings on the VimL support. Do with them what you will.
Most of the time when splitting, I'm doing so at whitespace. This leaves me with either leading or trailing whitespace I have to clean up. Is it possible that could be trimmed automatically?
On joining, it's pretty common for me to want to join just two lines in a several line sequence, but splitjoin gives me all or nothing. Can anything be done about that?
Joining a block of code with comments inside will break the resulting code. I know this is a bit of a corner case and that comments can be a bit tricky to handle.
if true
method! # comment
end
becomes
method! # comment if true
instead of
method! if true # comment
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.