Skip to content

Commit

Permalink
feat(ecma): jsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
bennypowers committed Apr 10, 2024
1 parent 21c0e05 commit 1b020d6
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lua/splitjoin/languages/ecmascript/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ return {
join = ECMAScript.join_arrow_function,
},

-- comment = {
-- split = ECMAScript.split_comment,
-- join = ECMAScript.join_comment,
-- }

},

}
28 changes: 28 additions & 0 deletions lua/splitjoin/languages/ecmascript/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,32 @@ function ECMAScript.join_arrow_function(node, options)
Node.goto_node(node)
end

-- function ECMAScript.split_comment(node, options)
-- local text = Node.get_text(node)
-- if String.is_multiline(text) or vim.startwith(text, '//') then return end
-- local indent = options.default_indent or ' '
-- local append, get = String.append('')
-- append(
-- '/**\n',
-- indent,
-- '* ',
-- text.gsub([[(^/**)|(*/$)]], '')
-- '\n */'
-- )
-- Node.replace(node, get())
-- Node.trim_line_end(node)
-- Node.trim_line_end(node, 1)
-- Node.goto_node(node)
-- end
--
-- function ECMAScript.join_comment(node, options)
-- local text = Node.get_text(node)
-- if String.is_multiline(text) or vim.startwith(text, '//') then return end
-- local row, col = node:range()
-- local comment = vim.treesitter.get_node{ pos = { row, col - 1 } }
-- local description = text.gsub([[(^/**)|(*/$)]], '');
-- Node.replace(comment, '/** ' .. description .. ' */')
-- Node.goto_node(comment)
-- end

return ECMAScript
16 changes: 16 additions & 0 deletions lua/splitjoin/languages/jsdoc/defaults.lua
Original file line number Diff line number Diff line change
@@ -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,
},

},

}
39 changes: 39 additions & 0 deletions lua/splitjoin/languages/jsdoc/functions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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)
if String.is_multiline(text) then return end
text = text:gsub([[%*$]], '')
local indent = options.default_indent or ' '
local append, get = String.append('')
append(
'\n',
indent,
'* ',
text,
'\n *'
)
local row, col = unpack(vim.api.nvim_win_get_cursor(0));
Node.replace(node, get())
Node.trim_line_end(node)
Node.trim_line_end(node, 1)
vim.api.nvim_win_set_cursor(0, { row + 1, col - 1 })
end

function JSDoc.join_jsdoc_description(node, options)
local text = Node.get_text(node)
if String.is_multiline(text) then return end
local nrow, ncol = node:range()
local comment = vim.treesitter.get_node { pos = { nrow, ncol - 1 } }
if comment and not String.is_singleline(Node.get_text(comment)) then
local row, col = unpack(vim.api.nvim_win_get_cursor(0));
Node.replace(comment, '/** ' .. text .. ' */')
Node.goto_node(comment)
vim.api.nvim_win_set_cursor(0, { row - 1, col + 1 })
end
end

return JSDoc
5 changes: 3 additions & 2 deletions lua/splitjoin/util/node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions lua/splitjoin/util/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 8 additions & 0 deletions lua/splitjoin/util/string.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ 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.is_singleline(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)
Expand Down
2 changes: 2 additions & 0 deletions queries/javascript/splitjoin.scm
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
; assignment patterns
(array_pattern) @splitjoin.javascript.array
(object_pattern) @splitjoin.javascript.object

(comment) @splitjoin.javascript.comment
1 change: 1 addition & 0 deletions queries/jsdoc/splitjoin.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(description) @splitjoin.jsdoc.description
12 changes: 12 additions & 0 deletions test/fixtures/fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions test/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ function M.get_char_at_cursor()
return char
end

---@param lang string language name
---@param name string suite name
---@param input string code to operate on
---@param expected string expected result
---@param go_to string|number[] go_to result
function M.make_suite(lang, name, input, expected, go_to)
local assert = require 'luassert'
local splitjoin = require'splitjoin'
Expand Down
16 changes: 16 additions & 0 deletions test/javascript_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
]],
'jsdoc'
)
end)

describe('const f = function() { return 0; }', function()
H.make_suite(lang, '',
d[[
Expand Down

0 comments on commit 1b020d6

Please sign in to comment.