norcalli / snippets.nvim

GNU General Public License v3.0
276 stars 13 forks source link

match_indentation #20

Closed salkin-mada closed 3 years ago

salkin-mada commented 3 years ago

Thanks for a nice snippet plugin! I use it every day in lua and especially on dynamic languages like supercollider. Its really fantastic for wrapping registers in other code etc.

but. I cant get match_indentation to work with the following type of snippet.

local U = require'snippets.utils'
require'snippets'.snippets = {
    lua = {
        ["for"] = U.match_indentation [[
        for ${1:i}, ${2:v} in ipairs(${3:t}) do
        $0
        end]];
    };
}

this always results in a lua for loop placed with the same amount of tabs/spaces that was present in the snippet file where it was defined. hope this makes sense :)

Am I doing something wrong or misunderstanding the use of require'snippets.utils'.match_indentation or is it broken?

bew commented 3 years ago

This is because Lua's multi-line strings retain the leading spaces, to do what you want you have to do:

local U = require'snippets.utils'
require'snippets'.snippets = {
    lua = {
        ["for"] = U.match_indentation [[
for ${1:i}, ${2:v} in ipairs(${3:t}) do
$0
end]];
    };
}

to ensure there are no spaces before the snippet's text.


I personally don't like this way of writing multiline snippets, it would be nice to have a wrapper that removes the leading spaces, so your initial code would work (with that wrapper)

salkin-mada commented 3 years ago

thanks @bew . I see. I thought match_indentation did two things.

  1. It placed the snippet in relation to the indentation of the code surrounding it (when using the snippet)
  2. In addition to removing preline white spaces of the snippet definition (similar to text preline in css or a like)

indeed a leading space removal wrapper would be nice to have. I also write my multiline snippets like this for = "for ${1:i}, ${2:v} in ipairs(${3:t}) do\n\t$0\nend"; at the moment due to the leading space lua multi-line string "issue". And using \t and \n feels very much like home. Spend many hours with strings and println / postln in different langs . But for multi-line snippets i would actually really love to be able to write them like so:

    lua = {
        ["for"] = U.match_indentation [[
        for ${1:i}, ${2:v} in ipairs(${3:t}) do
        $0
        end]];
    };

keeping the indentation of the snippet def file makes it more readable. Especially when there is alot of automatic generated snippets from class trees.