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 6b8ca32 commit 4999dc5
Show file tree
Hide file tree
Showing 10 changed files with 93 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_jsdoc_comment,
join = ECMAScript.join_jsdoc_comment,
}

},

}
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,
},

},

}
33 changes: 33 additions & 0 deletions lua/splitjoin/languages/jsdoc/functions.lua
Original file line number Diff line number Diff line change
@@ -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
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
4 changes: 4 additions & 0 deletions lua/splitjoin/util/string.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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
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
]],
'j'
)
end)

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

0 comments on commit 4999dc5

Please sign in to comment.