Skip to content

Commit 1b020d6

Browse files
committed
feat(ecma): jsdoc
1 parent 21c0e05 commit 1b020d6

File tree

12 files changed

+136
-2
lines changed

12 files changed

+136
-2
lines changed

lua/splitjoin/languages/ecmascript/defaults.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ return {
6262
join = ECMAScript.join_arrow_function,
6363
},
6464

65+
-- comment = {
66+
-- split = ECMAScript.split_comment,
67+
-- join = ECMAScript.join_comment,
68+
-- }
69+
6570
},
6671

6772
}

lua/splitjoin/languages/ecmascript/functions.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,32 @@ function ECMAScript.join_arrow_function(node, options)
9898
Node.goto_node(node)
9999
end
100100

101+
-- function ECMAScript.split_comment(node, options)
102+
-- local text = Node.get_text(node)
103+
-- if String.is_multiline(text) or vim.startwith(text, '//') then return end
104+
-- local indent = options.default_indent or ' '
105+
-- local append, get = String.append('')
106+
-- append(
107+
-- '/**\n',
108+
-- indent,
109+
-- '* ',
110+
-- text.gsub([[(^/**)|(*/$)]], '')
111+
-- '\n */'
112+
-- )
113+
-- Node.replace(node, get())
114+
-- Node.trim_line_end(node)
115+
-- Node.trim_line_end(node, 1)
116+
-- Node.goto_node(node)
117+
-- end
118+
--
119+
-- function ECMAScript.join_comment(node, options)
120+
-- local text = Node.get_text(node)
121+
-- if String.is_multiline(text) or vim.startwith(text, '//') then return end
122+
-- local row, col = node:range()
123+
-- local comment = vim.treesitter.get_node{ pos = { row, col - 1 } }
124+
-- local description = text.gsub([[(^/**)|(*/$)]], '');
125+
-- Node.replace(comment, '/** ' .. description .. ' */')
126+
-- Node.goto_node(comment)
127+
-- end
128+
101129
return ECMAScript
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
local JSDoc = require'splitjoin.languages.jsdoc.functions'
2+
3+
---@type SplitjoinLanguageConfig
4+
return {
5+
6+
nodes = {
7+
8+
description = {
9+
split = JSDoc.split_jsdoc_description,
10+
join = JSDoc.join_jsdoc_description,
11+
trailing_separator = false,
12+
},
13+
14+
},
15+
16+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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

lua/splitjoin/util/node.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ function Node.get_base_indent(node)
112112
end
113113

114114
---@param node TSNode
115-
function Node.trim_line_end(node)
116-
local row = node:range()
115+
function Node.trim_line_end(node, rowoffset)
116+
local noderow = node:range()
117+
local row = noderow + (rowoffset or 0)
117118
local trimmed = Buffer.get_line(0, row):gsub('%s*$', '')
118119
vim.api.nvim_buf_set_lines(0,
119120
row,

lua/splitjoin/util/options.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ local function get_defaults()
1818
ecmascript = require'splitjoin.languages.ecmascript.defaults',
1919
javascript = require'splitjoin.languages.javascript.defaults',
2020
typescript = require'splitjoin.languages.typescript.defaults',
21+
jsdoc = require'splitjoin.languages.jsdoc.defaults',
2122
json = require'splitjoin.languages.json.defaults',
2223
html = require'splitjoin.languages.html.defaults',
2324
css = require'splitjoin.languages.css.defaults',

lua/splitjoin/util/string.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ function String.is_lengthy(str)
77
return #str > 0
88
end
99

10+
function String.is_multiline(str)
11+
return #vim.split(vim.fn.trim(str), '\n') > 1
12+
end
13+
14+
function String.is_singleline(str)
15+
return #vim.split(vim.fn.trim(str), '\n') == 1
16+
end
17+
1018
function String.split(str, sep, opts)
1119
opts = vim.tbl_extend('keep', opts or {}, { plain = true, trimempty = true })
1220
return vim.split(str, sep, opts)

queries/javascript/splitjoin.scm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@
1515
; assignment patterns
1616
(array_pattern) @splitjoin.javascript.array
1717
(object_pattern) @splitjoin.javascript.object
18+
19+
(comment) @splitjoin.javascript.comment

queries/jsdoc/splitjoin.scm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(description) @splitjoin.jsdoc.description

test/fixtures/fixture.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,15 @@ const g = () => 0
2323
function destruct({ a, b, c }, d) { return { a, b, c, d }; }
2424

2525
destruct = ({ a, b, c }, d) => 0;
26+
27+
/** jsdoc */
28+
const jsdoc = (thing) => thing
29+
30+
/**
31+
* multiline
32+
* jsdoc
33+
*/
34+
const multilineJsdoc = (thing) => thing
35+
36+
/** jsdoc @param {number} thing */
37+
const paramJsdoc = (thing) => thing

0 commit comments

Comments
 (0)