AndrewRadev / splitjoin.vim

Switch between single-line and multiline forms of code
http://www.vim.org/scripts/script.php?script_id=3613
MIT License
1.91k stars 89 forks source link

General support for joining lines with line continuation characters #114

Open kiryph opened 7 years ago

kiryph commented 7 years ago

I miss support for the commonly found line continuation style with a trailing backslash when joining lines. For example in cmake:

message("\
This is the first line of a quoted argument. \
In fact it is the only line but since it is long \
the source code uses line continuation.\
")

I would like to have following behavior: when joining those lines, the backslashes are automatically removed.

Many languages use this style:

and most likely many more which I am however not familiar with.

I know this does not apply to all languages, e.g. Lua which uses \z:

The escape sequence '\z' skips the following span of white-space characters, including line breaks; it is particularly useful to break and indent a long literal string into multiple lines without adding the newlines and spaces into the string contents.

AndrewRadev commented 7 years ago

Yeah, that makes sense, I've thought about it myself. I guess it should be a global definition, of which there's currently none. I'll see what I can do about it.

gaving commented 4 years ago

I'd also be quite keen on this kind of functionality for shell scripts e.g. transforming:-

codeclimate() {
  docker run --interactive --tty --rm --env CODE_PATH="$PWD" --volume "$PWD":/code --volume /var/run/docker.sock:/var/run/docker.sock --volume /tmp/cc:/tmp/cc codeclimate/codeclimate "$@"
}

To:-

codeclimate() {
  docker run \
    --interactive \
    --tty \
    --rm \
    --env CODE_PATH="$PWD" \
    --volume "$PWD":/code \
    --volume /var/run/docker.sock:/var/run/docker.sock \
    --volume /tmp/cc:/tmp/cc codeclimate/codeclimate "$@"
}

And back.

Currently just do a '<,'>snomag/--/\ ^M--/g but would be nice if it was a suitable feature of splitjoin (assuming it made sense)!

flwyd commented 10 months ago

I wrote https://github.com/flwyd/vim-conjoin to remove line continuation characters for several dozen filetypes (though I wasn't aware of Lua's \z and backslash-newline continuations inside strings). Conjoin cooperates with splitjoin's gJ as long as the splitjoin plugin is loaded before conjoin.

Conjoin only removes characters when joining lines, it doesn't add line continuations when splitting, so it doesn't help the shell script example in comment 3.

AndrewRadev commented 10 months ago

Sorry for ignoring this issue for quite a while. I think I kept postponing it, because I wanted to implement a general "global" setup. Instead, to provide at least something that's helpful, I've pushed a branch, line-continuations which implement splitting for the sh filetype in particular.

It only splits one at a time, so for large command-lines, it could take some f-., but the alternative might get complicated fast, since ---style flags have varying conventions, there's |-separated parts... Formatting this stuff might be tricky. We ended up with a similar arrangement for Vimscript in https://github.com/AndrewRadev/splitjoin.vim/issues/15.

@gaving, if you're still around, I'd appreciate your thoughts on your use case.

@kiryph The implementation might be reusable, since the way the plugin is implemented, it just triggers certain callbacks. If the bash example works well, I could manually add support for several other filetypes rather than generalizing. What do you think?

AndrewRadev commented 10 months ago

Update: I've merged the branch, since I've found it pretty useful for bash myself.

gaving commented 10 months ago

@AndrewRadev Just seen this, look forward to trying it out!