Skip to content

Commit

Permalink
feat(snip_expand): make indenting snippets optional
Browse files Browse the repository at this point in the history
  • Loading branch information
TwIStOy authored Dec 2, 2023
1 parent 2a75f4a commit 07f217f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
21 changes: 21 additions & 0 deletions DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -3708,6 +3708,27 @@ These are the settings you can provide to `luasnip.setup()`:
return snip.insert_nodes[0]
end
```
- `indent`: bool?, defaults to `true`. Whether LuaSnip will try to add
additional indents to fit current indent level in snippet expanding. This
option is useful when some LSP server already take indents into
consideration. In such cases, LuaSnip should not try to add additional
indents. If you are using `nvim-cmp`, sample config:

```lua
require("cmp").setup {
snippet = {
expand = function(args)
local indent_nodes = true
if vim.api.nvim_get_option_value("filetype", { buf = 0 }) == "dart" then
indent_nodes = false
end
require("luasnip").lsp_expand(args.body, {
indent = indent_nodes,
})
end,
},
}
```

`opts` and any of its parameters may be nil.

Expand Down
20 changes: 20 additions & 0 deletions doc/luasnip.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3503,6 +3503,26 @@ GENERAL ~
jump_into set the placeholder of the snippet, 1 -- to jump forwards. return
snip:jump_into(1)` while this can be used to only insert the snippet: `lua
function(snip) return snip.insert_nodes[0] end`
- `indent`: bool?, defaults to `true`. Whether LuaSnip will try to add additional
indents to fit current indent level in snippet expanding. This option is useful
when some LSP server already take indents into consideration. In such cases,
LuaSnip should not try to add additional indents. If you are using `nvim-cmp`,
sample config:
>lua
require("cmp").setup {
snippet = {
expand = function(args)
local indent_nodes = true
if vim.api.nvim_get_option_value("filetype", { buf = 0 }) == "dart" then
indent_nodes = false
end
require("luasnip").lsp_expand(args.body, {
indent = indent_nodes,
})
end,
},
}
<
`opts` and any of its parameters may be nil.
- `get_active_snip()`: returns the currently active snippet (not node!).
- `choice_active()`: true if inside a choiceNode.
Expand Down
4 changes: 3 additions & 1 deletion lua/luasnip/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ local function snip_expand(snippet, opts)
-- override with current position if none given.
opts.pos = opts.pos or util.get_cursor_0ind()
opts.jump_into_func = opts.jump_into_func or _jump_into_default
opts.indent = vim.F.if_nil(opts.indent, true)

snip.trigger = opts.expand_params.trigger or snip.trigger
snip.captures = opts.expand_params.captures or {}
Expand Down Expand Up @@ -271,7 +272,8 @@ local function snip_expand(snippet, opts)
local snip_parent_node = snip:trigger_expand(
session.current_nodes[vim.api.nvim_get_current_buf()],
pos_id,
env
env,
opts.indent
)

-- jump_into-callback returns new active node.
Expand Down
14 changes: 8 additions & 6 deletions lua/luasnip/nodes/snippet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ local function insert_into_jumplist(
table.insert(sibling_snippets, own_indx, snippet)
end

function Snippet:trigger_expand(current_node, pos_id, env)
function Snippet:trigger_expand(current_node, pos_id, env, indent_nodes)
local pos = vim.api.nvim_buf_get_extmark_by_id(0, session.ns_id, pos_id, {})

-- find tree-node the snippet should be inserted at (could be before another node).
Expand Down Expand Up @@ -661,12 +661,14 @@ function Snippet:trigger_expand(current_node, pos_id, env)

Environ:override(env, pre_expand_res.env_override or {})

local indentstring = util.line_chars_before(pos):match("^%s*")
-- expand tabs before indenting to keep indentstring unmodified
if vim.bo.expandtab then
self:expand_tabs(util.tab_width(), #indentstring)
if indent_nodes then
local indentstring = util.line_chars_before(pos):match("^%s*")
-- expand tabs before indenting to keep indentstring unmodified
if vim.bo.expandtab then
self:expand_tabs(util.tab_width(), #indentstring)
end
self:indent(indentstring)
end
self:indent(indentstring)

-- (possibly) keep user-set opts.
if self.merge_child_ext_opts then
Expand Down

0 comments on commit 07f217f

Please sign in to comment.