splitjoin.vim icon indicating copy to clipboard operation
splitjoin.vim copied to clipboard

General support for joining lines with line continuation characters

Open kiryph opened this issue 7 years ago • 6 comments

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:

  • awk
  • c
  • c++
  • cmake
  • gnuplot
  • python
  • shell scripting languages (e.g. bash)

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.

kiryph avatar Jun 13 '17 12:06 kiryph

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.

AndrewRadev avatar Jun 13 '17 14:06 AndrewRadev

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

gaving avatar Aug 30 '20 19:08 gaving

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.

flwyd avatar Nov 02 '23 04:11 flwyd

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 avatar Nov 05 '23 19:11 AndrewRadev

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

AndrewRadev avatar Nov 18 '23 19:11 AndrewRadev

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

gaving avatar Nov 25 '23 11:11 gaving