diff --git a/DOC.md b/DOC.md index 7d5358606..dd0a5f874 100644 --- a/DOC.md +++ b/DOC.md @@ -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. diff --git a/doc/luasnip.txt b/doc/luasnip.txt index 0ce0d8b9f..6d6d18a95 100644 --- a/doc/luasnip.txt +++ b/doc/luasnip.txt @@ -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. diff --git a/lua/luasnip/init.lua b/lua/luasnip/init.lua index 7598b5540..5ec2a932d 100644 --- a/lua/luasnip/init.lua +++ b/lua/luasnip/init.lua @@ -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 {} @@ -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. diff --git a/lua/luasnip/nodes/snippet.lua b/lua/luasnip/nodes/snippet.lua index e81fea461..147480b67 100644 --- a/lua/luasnip/nodes/snippet.lua +++ b/lua/luasnip/nodes/snippet.lua @@ -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). @@ -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