Code Monkey home page Code Monkey logo

splitjoin.vim's Introduction

GitHub version Build Status

Usage

This plugin is meant to simplify a task I've found too common in my workflow: switching between a single-line statement and a multi-line one. It offers the following default keybindings, which can be customized:

  • gS to split a one-liner into multiple lines
  • gJ (with the cursor on the first line of a block) to join a block into a single-line statement.

Demo

I usually work with ruby and a lot of expressions can be written very concisely on a single line. A good example is the "if" statement:

puts "foo" if bar?

This is a great feature of the language, but when you need to add more statements to the body of the "if", you need to rewrite it:

if bar?
  puts "foo"
  puts "baz"
end

The idea of this plugin is to introduce a single key binding (default: gS) for transforming a line like this:

<div id="foo">bar</div>

Into this:

<div id="foo">
  bar
</div>

And another binding (default: gJ) for the opposite transformation.

This currently works for various constructs in the following languages:

  • C
  • CSS
  • Clojure
  • Coffeescript
  • Elixir
  • Elm
  • Eruby
  • Go
  • HAML
  • HTML (and HTML-like markup)
  • Handlebars
  • JSON
  • Java
  • Javascript (within JSX, TSX, Vue.js templates as well)
  • Lua
  • PHP
  • Perl
  • Python
  • R
  • Ruby
  • Rust
  • SCSS and Less
  • Shell (sh, bash, zsh)
  • Tex
  • Vimscript
  • YAML

For more information, including examples for all of those languages, try :help splitjoin, or take a look at the full help file online at doc/splitjoin.txt

Installation

The easiest way to install the plugin is with a plugin manager:

If you use one, just follow the instructions in its documentation.

You can install the plugin yourself using Vim's "packages" functionality by cloning the project (or adding it as a submodule) under ~/.vim/pack/<any-name>/start/. For example:

git clone https://github.com/AndrewRadev/splitjoin.vim ~/.vim/pack/_/start/splitjoin

This should automatically load the plugin for you on Vim start. Alternatively, you can add it to ~/.vim/pack/<any-name>/opt/ instead and load it in your .vimrc manually with:

packadd splitjoin

If you'd rather not use git, you can download the files from the "releases" tab and unzip them in the relevant directory: https://github.com/AndrewRadev/splitjoin.vim/releases.

Contributing

If you'd like to hack on the plugin, please see CONTRIBUTING.md first.

Issues

Any issues and suggestions are very welcome on the github bugtracker.

splitjoin.vim's People

Contributors

andrewradev avatar andyjack avatar bastes avatar benwoodward avatar cehoffman avatar deris avatar dgkf avatar frm avatar hardenedapple avatar hiberabyss avatar jonleighton avatar josa42 avatar kien avatar krzystof avatar lfdm avatar lucerion avatar mhanberg avatar mhinz avatar mutewinter avatar naquad avatar neki avatar noahtheduke avatar opavader avatar perelo avatar rgbd avatar rrhubenov avatar sietse avatar sktocha avatar sobolevn avatar strogiyotec avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

splitjoin.vim's Issues

Ternary support for ruby

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.

Support for ruby symbol-to-block cast with `&`

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...

LESS/SCSS Support

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.

Boldly provide default maps?

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!

Splitting curved braces adds an extra comma

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
    }

Vimscript splitjoining

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?

Ruby array support?

Is there a plan to support Ruby arrays and their literals (%w{}, %i{}, %W{}, %I{})?

Enhance Python syntax support

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 !

Operator pending and visual mode

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).

Issue joining a PHP array

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.

gS<char> command

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
)

Splitting method invocation in PHP (fluent interfaces)

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();

Make number of spaces configurable on ruby condition suffixes

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.

splitjoin with one line if and string

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.

Support for switching ruby lambda literals

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 😄)

Add array support to JavaScript

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).

Perl hash support broken?

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]};

splitjoin doesn't handle indentation properly

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.

Spacing around braces

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!

Remove key whitespace on join?

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?

Is it possible to add perl?

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, ...

Split comma-separated parameters

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"

Ruby: Option to not add hash brackets

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.

Perl list/array support broken

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);

Ruby inline block conversion assumes conversion as hash

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.

PHP oneliners

It'd be cool if splitjoin could convert the following:

<?php $whatever = 'stuff'; ?>

into the following:

<?php
    $whatever = 'stuff';
?>

Split/join doesn't work for console VIM on Mac

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).

PHP argument lists

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.

Splitting arg list for Ruby method

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?

It starts from the wrong split

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

Not working when open a new buffer

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.

Method Params

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

Split inline `if` statements.

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!

Split and join by semicolon / generic command

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.

Add LaTeX support

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

HTML attributes splitting

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.

Support Ruby 1.9 Hash Syntax

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.

[ruby] (lack of) handling of comments within blocks breaks code

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.