|
| 1 | +local Node = require'splitjoin.util.node' |
| 2 | +local String = require'splitjoin.util.string' |
| 3 | + |
| 4 | +local JSDoc = {} |
| 5 | + |
| 6 | +function JSDoc.split_jsdoc_description(node, options) |
| 7 | + local text = Node.get_text(node) |
| 8 | + if String.is_multiline(text) then return end |
| 9 | + text = text:gsub([[%*$]], '') |
| 10 | + local indent = options.default_indent or ' ' |
| 11 | + local append, get = String.append('') |
| 12 | + append( |
| 13 | + '\n', |
| 14 | + indent, |
| 15 | + '* ', |
| 16 | + text, |
| 17 | + '\n *' |
| 18 | + ) |
| 19 | + local row, col = unpack(vim.api.nvim_win_get_cursor(0)); |
| 20 | + Node.replace(node, get()) |
| 21 | + Node.trim_line_end(node) |
| 22 | + Node.trim_line_end(node, 1) |
| 23 | + vim.api.nvim_win_set_cursor(0, { row + 1, col - 1 }) |
| 24 | +end |
| 25 | + |
| 26 | +function JSDoc.join_jsdoc_description(node, options) |
| 27 | + local text = Node.get_text(node) |
| 28 | + if String.is_multiline(text) then return end |
| 29 | + local nrow, ncol = node:range() |
| 30 | + local comment = vim.treesitter.get_node { pos = { nrow, ncol - 1 } } |
| 31 | + if comment and not String.is_singleline(Node.get_text(comment)) then |
| 32 | + local row, col = unpack(vim.api.nvim_win_get_cursor(0)); |
| 33 | + Node.replace(comment, '/** ' .. text .. ' */') |
| 34 | + Node.goto_node(comment) |
| 35 | + vim.api.nvim_win_set_cursor(0, { row - 1, col + 1 }) |
| 36 | + end |
| 37 | +end |
| 38 | + |
| 39 | +return JSDoc |
0 commit comments