diff --git a/README.md b/README.md index 82be05d..c72e385 100644 --- a/README.md +++ b/README.md @@ -295,6 +295,7 @@ local node_type = { omit = {}, ---Non-bracket nodes (e.g., with 'then|()' ... 'end' instead of { ... }|< ... >|[ ... ]) ---If value is table, should be contains follow keys: { left = 'text', right = 'text' }. Empty string uses by default + ---For indentation-based blocks with no closing delimiter, add `outer_framing = false` to stop framing from extending to the parent's sibling ---@type boolean|table non_bracket_node = false, ---If you need to process only nodes in the range from / to. @@ -638,6 +639,13 @@ specifies what text should wrap the actual base node. E.g., table value: `{ left = 'text', right = 'text' }` +For indentation-based blocks that have no closing delimiter (e.g. Python, +GDScript), also set `outer_framing = false`, e.g. +`{ left = '', right = '', outer_framing = false }`. By default the range +framing climbs to the parent's sibling when the node has none of its own; for +such blocks that sibling is the following statement, so the join range would +otherwise extend over it and swallow it. +
Example of usage diff --git a/lua/treesj/langs/default_preset.lua b/lua/treesj/langs/default_preset.lua index 2db6a00..757530d 100644 --- a/lua/treesj/langs/default_preset.lua +++ b/lua/treesj/langs/default_preset.lua @@ -40,6 +40,7 @@ return { omit = {}, ---Non-bracket nodes (e.g., with 'then|()' ... 'end' instead of { ... }|< ... >|[ ... ]) ---If value is table, should be contains follow keys: { left = 'text', right = 'text' }. Empty string uses by default + ---For indentation-based blocks with no closing delimiter, add `outer_framing = false` to stop framing from extending to the parent's sibling ---@type boolean|table non_bracket_node = false, ---If you need to process only nodes in the range from / to. diff --git a/lua/treesj/search.lua b/lua/treesj/search.lua index af64910..a2a0d74 100644 --- a/lua/treesj/search.lua +++ b/lua/treesj/search.lua @@ -289,9 +289,18 @@ function M.range(tsn, preset) local er, ec = get_last_symbol_range(tsn, to) if preset and (non_bracket_node and not shrink_node) then + -- Indentation-based blocks (e.g. Python) have no closing delimiter token. + -- `outer_framing = false` stops framing from climbing to the parent's sibling + local climb = type(non_bracket_node) ~= 'table' + or non_bracket_node.outer_framing ~= false + local function get_framing_for_non_bracket(n) - local first = n:prev_sibling() or n:parent():prev_sibling() - local last = n:next_sibling() or n:parent():next_sibling() + local first = n:prev_sibling() + local last = n:next_sibling() + if climb then + first = first or n:parent():prev_sibling() + last = last or n:parent():next_sibling() + end return first, last end