Skip to content

Commit

Permalink
Fixed: error when node is comment, cursor outside buffer (#18)
Browse files Browse the repository at this point in the history
* Fixed error of no preset when node is comment

* Added safe call of 'nvim_win_set_cursor'

* Lint and format code
  • Loading branch information
Wansmer authored Nov 25, 2022
1 parent 9917d5a commit 992683e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
16 changes: 8 additions & 8 deletions lua/treesj/chold/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ function M.increase_row(chold, tsj)

if type(text) == 'table' then
if M.in_node_range(chold, tsj) then
local pos = M.new_col_pos(chold, tsj, 'split')
local len = 0
local line_count = 0
while pos > len do
line_count = line_count + 1
len = len + #text[line_count]
end
term = line_count
local pos = M.new_col_pos(chold, tsj, 'split')
local len = 0
local line_count = 0
while pos > len do
line_count = line_count + 1
len = len + #text[line_count]
end
term = line_count
else
term = #vim.tbl_flatten(text)
end
Expand Down
10 changes: 3 additions & 7 deletions lua/treesj/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,8 @@ function M._format(mode)

if u.has_disabled_descendants(node, MODE) then
local p = u.get_preset(node, MODE)
notify.info(
msg.no_format_with,
MODE,
node:type(),
vim.inspect(p.no_format_with)
)
local no_format_with = p and vim.inspect(p.no_format_with) or ''
notify.info(msg.no_format_with, MODE, node:type(), no_format_with)
return
end

Expand All @@ -64,7 +60,7 @@ function M._format(mode)
local new_cursor = cursor:get_cursor()

vim.api.nvim_buf_set_text(0, sr, sc, er, ec, replacement)
vim.api.nvim_win_set_cursor(0, new_cursor)
pcall(vim.api.nvim_win_set_cursor, 0, new_cursor)
end

return M
2 changes: 1 addition & 1 deletion lua/treesj/treesj/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function M._split(tsj)
if child:is_omit() then
merge_text_to_prev_line(lines, text)
if child:is_last() then
local indent = u.calc_indent(child);
local indent = u.calc_indent(child)
lines[#lines] = (' '):rep(indent) .. vim.trim(lines[#lines])
end
else
Expand Down
14 changes: 12 additions & 2 deletions lua/treesj/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,19 @@ end
---If mode passed, return preset for specified mode
---@param node userdata TSNode instance
---@param mode? string Current mode (split|join)
---@return table
---@return table|nil
function M.get_preset(node, mode)
local lang = M.get_node_lang(node)
if not M.is_lang_support(lang) then
return nil
end
local preset = langs[lang]
local type = node:type()
return mode and preset[type][mode] or preset[type]
if preset[type] then
return mode and preset[type][mode] or preset[type]
else
return nil
end
end

---Return the preset for current node if it no contains field 'target_nodes'
Expand Down Expand Up @@ -181,6 +188,9 @@ end
---@return boolean
function M.has_disabled_descendants(tsnode, mode)
local p = M.get_preset(tsnode, mode)
if not p then
return false
end
local function contains_in_no_format_with(tsn)
return vim.tbl_contains(p.no_format_with, tsn:type())
end
Expand Down

0 comments on commit 992683e

Please sign in to comment.