ojroques / nvim-osc52

A Neovim plugin to copy text through SSH with OSC52
BSD 2-Clause "Simplified" License
346 stars 12 forks source link

trim with multiple lines #2

Closed zegervdv closed 2 years ago

zegervdv commented 2 years ago

When copying multiple lines with the trim option enabled, only the first whitespace is trimmed. E.g. when copying:

# some class code here
     def foo(self, bar):
         return bar * 2

You will get:

def foo(self, bar):
        return bar * 2

This is of course because the copy function takes the full string, so it cannot know the lines (except if it would split them again, which will end up in concatting, splitting and concatting again in many cases).

I've created a small function that strips the common indent of the copied lines:

local copy = function(lines, _)
  -- Trim indent before copying
  local indent, _ = lines[1]:find '[^ ]'
  for key, line in ipairs(lines) do
    lines[key] = line:sub(indent)
  end
  require('osc52').copy(table.concat(lines, '\n'))
end

I wonder if this would be something worth integrating? Or mentioning in the README?

I still need to handle cases where the first line(s) are empty, probably could be done in a better way.

ojroques commented 2 years ago

Hello! Right now I prefer to keep the trim option simple so it should only trim whitespaces at the start and end of the string. But I've realized that there's no reason it should be the default, especially when there are tricky cases like yours. I've set it to false by default in the latest commit. It makes the plugin behaves the same way as normal yanking, which makes more sense I think.

I like your solution though and I agree this looks better. I'll think about improving the option.

ojroques commented 2 years ago

I've modified the trim option so that it removes common indent on all lines, like you described. You only need to set trim to true now.

zegervdv commented 2 years ago

Nice! Thanks for that.