diff --git a/lua/splitjoin/languages/ecmascript/defaults.lua b/lua/splitjoin/languages/ecmascript/defaults.lua index 4c4d6b7..d6e9316 100644 --- a/lua/splitjoin/languages/ecmascript/defaults.lua +++ b/lua/splitjoin/languages/ecmascript/defaults.lua @@ -62,6 +62,11 @@ return { join = ECMAScript.join_arrow_function, }, + comment = { + split = ECMAScript.split_jsdoc_comment, + join = ECMAScript.join_jsdoc_comment, + } + }, } diff --git a/lua/splitjoin/languages/jsdoc/defaults.lua b/lua/splitjoin/languages/jsdoc/defaults.lua new file mode 100644 index 0000000..8dab990 --- /dev/null +++ b/lua/splitjoin/languages/jsdoc/defaults.lua @@ -0,0 +1,16 @@ +local JSDoc = require'splitjoin.languages.jsdoc.functions' + +---@type SplitjoinLanguageConfig +return { + + nodes = { + + description = { + split = JSDoc.split_jsdoc_description, + join = JSDoc.join_jsdoc_description, + trailing_separator = false, + }, + + }, + +} diff --git a/lua/splitjoin/languages/jsdoc/functions.lua b/lua/splitjoin/languages/jsdoc/functions.lua new file mode 100644 index 0000000..9f6735b --- /dev/null +++ b/lua/splitjoin/languages/jsdoc/functions.lua @@ -0,0 +1,33 @@ +local Node = require'splitjoin.util.node' +local String = require'splitjoin.util.string' + +local JSDoc = {} + +function JSDoc.split_jsdoc_description(node, options) + local text = Node.get_text(node):gsub([[%*$]], '') + if String.is_multiline(text) then return end + local indent = options.default_indent or ' ' + local append, get = String.append('') + append( + '\n', + indent, + '* ', + text, + '\n *' + ) + Node.replace(node, get()) + Node.trim_line_end(node) + Node.trim_line_end(node, 1) + Node.goto_node(node) +end + +function JSDoc.join_jsdoc_description(node, options) + local text = Node.get_text(node) + if String.is_multiline(text) then return end + local row, col = node:range() + local comment = vim.treesitter.get_node{ pos = { row, col - 1 } } + Node.replace(comment, '/** ' .. text .. ' */') + Node.goto_node(comment) +end + +return JSDoc diff --git a/lua/splitjoin/util/node.lua b/lua/splitjoin/util/node.lua index 55f7df3..6ed98bb 100644 --- a/lua/splitjoin/util/node.lua +++ b/lua/splitjoin/util/node.lua @@ -112,8 +112,9 @@ function Node.get_base_indent(node) end ---@param node TSNode -function Node.trim_line_end(node) - local row = node:range() +function Node.trim_line_end(node, rowoffset) + local noderow = node:range() + local row = noderow + (rowoffset or 0) local trimmed = Buffer.get_line(0, row):gsub('%s*$', '') vim.api.nvim_buf_set_lines(0, row, diff --git a/lua/splitjoin/util/options.lua b/lua/splitjoin/util/options.lua index 956df89..4781737 100644 --- a/lua/splitjoin/util/options.lua +++ b/lua/splitjoin/util/options.lua @@ -18,6 +18,7 @@ local function get_defaults() ecmascript = require'splitjoin.languages.ecmascript.defaults', javascript = require'splitjoin.languages.javascript.defaults', typescript = require'splitjoin.languages.typescript.defaults', + jsdoc = require'splitjoin.languages.jsdoc.defaults', json = require'splitjoin.languages.json.defaults', html = require'splitjoin.languages.html.defaults', css = require'splitjoin.languages.css.defaults', diff --git a/lua/splitjoin/util/string.lua b/lua/splitjoin/util/string.lua index e44570f..705895c 100644 --- a/lua/splitjoin/util/string.lua +++ b/lua/splitjoin/util/string.lua @@ -7,6 +7,10 @@ function String.is_lengthy(str) return #str > 0 end +function String.is_multiline(str) + return #vim.split(vim.fn.trim(str), '\n') > 1 +end + function String.split(str, sep, opts) opts = vim.tbl_extend('keep', opts or {}, { plain = true, trimempty = true }) return vim.split(str, sep, opts) diff --git a/queries/javascript/splitjoin.scm b/queries/javascript/splitjoin.scm index 71049a2..aefa86d 100644 --- a/queries/javascript/splitjoin.scm +++ b/queries/javascript/splitjoin.scm @@ -15,3 +15,5 @@ ; assignment patterns (array_pattern) @splitjoin.javascript.array (object_pattern) @splitjoin.javascript.object + +(comment) @splitjoin.javascript.comment diff --git a/queries/jsdoc/splitjoin.scm b/queries/jsdoc/splitjoin.scm new file mode 100644 index 0000000..e807a60 --- /dev/null +++ b/queries/jsdoc/splitjoin.scm @@ -0,0 +1 @@ +(description) @splitjoin.jsdoc.description diff --git a/test/fixtures/fixture.js b/test/fixtures/fixture.js index 31178d4..d30bf1a 100644 --- a/test/fixtures/fixture.js +++ b/test/fixtures/fixture.js @@ -23,3 +23,15 @@ const g = () => 0 function destruct({ a, b, c }, d) { return { a, b, c, d }; } destruct = ({ a, b, c }, d) => 0; + +/** jsdoc */ +const jsdoc = (thing) => thing + +/** + * multiline + * jsdoc + */ +const multilineJsdoc = (thing) => thing + +/** jsdoc @param {number} thing */ +const paramJsdoc = (thing) => thing diff --git a/test/javascript_spec.lua b/test/javascript_spec.lua index da6d976..a043b52 100644 --- a/test/javascript_spec.lua +++ b/test/javascript_spec.lua @@ -313,6 +313,22 @@ describe(lang, function() ) end) + describe('jsdoc', function() + H.make_suite(lang, '', + d[[ + /** jsdoc */ + const x = y => y + ]], + d[[ + /** + * jsdoc + */ + const x = y => y + ]], + 'j' + ) + end) + describe('const f = function() { return 0; }', function() H.make_suite(lang, '', d[[