Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
},

},

}
42 changes: 42 additions & 0 deletions lua/splitjoin/languages/jsdoc/functions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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 base_indent = Node.get_base_indent(node)
local append, get = String.append('')
append(
'\n',
indent,
base_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
27 changes: 16 additions & 11 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 All @@ -43,19 +48,19 @@ function M.make_suite(lang, name, input, expected, go_to)
local bufnr

local function create ()
if not bufnr then
bufnr = vim.api.nvim_create_buf(true, false)
vim.api.nvim_win_set_buf(0, bufnr)
vim.opt.filetype = lang
local lines = vim.split(input, '\n', { plain = true, trimempty = false })
vim.api.nvim_buf_set_lines(bufnr, 0, 1, false, lines)
if type(go_to) == 'string' then
vim.cmd.norm('f'..go_to)
elseif type(go_to) == 'table' then
vim.api.nvim_win_set_cursor(0, go_to)
if not bufnr then
bufnr = vim.api.nvim_create_buf(true, false)
vim.api.nvim_win_set_buf(0, bufnr)
vim.opt.filetype = lang
local lines = vim.split(input, '\n', { plain = true, trimempty = false })
vim.api.nvim_buf_set_lines(bufnr, 0, 1, false, lines)
if type(go_to) == 'string' then
vim.fn.search(go_to)
elseif type(go_to) == 'table' then
vim.api.nvim_win_set_cursor(0, go_to)
end
end
end
end

local function destroy()
vim.api.nvim_buf_delete(bufnr, { force = true })
Expand Down
1 change: 1 addition & 0 deletions test/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function M.setup()
'lua',
'css',
'json',
'jsdoc',
'javascript',
'typescript',
'html',
Expand Down
46 changes: 46 additions & 0 deletions test/javascript_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,52 @@ describe(lang, function()
)
end)

describe('jsdoc', function()
H.make_suite(lang, 'single line jsdoc',
d[[
/** jsdoc */
const x = y => y
]],
d[[
/**
* jsdoc
*/
const x = y => y
]],
'jsdoc'
)
H.make_suite(lang, 'indented jsdoc',
d[[
/** indented jsdoc */
const x = y => y
]],
d[[
/**
* indented jsdoc
*/
const x = y => y
]],
'jsdoc'
)
H.make_suite(lang, 'multiline jsdoc',
d[[
/**
* multiline
* jsdoc
*/
const x = y => y
]],
d[[
/**
* multiline
* jsdoc
*/
const x = y => y
]],
'jsdoc'
)
end)

describe('const f = function() { return 0; }', function()
H.make_suite(lang, '',
d[[
Expand Down
6 changes: 1 addition & 5 deletions test/lua_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,7 @@ describe(lang, function()
end)
H.make_suite(lang, 'params',
d[[
local function call(
a,
b,
c
) end
local function call(a, b, c) end
]],
d[[
local function call(
Expand Down
2 changes: 1 addition & 1 deletion test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ set -o errexit -o pipefail -o noclobber -o nounset

# -allow a command to fail with !’s side effect on errexit
# -use return value from ${PIPESTATUS[0]}, because ! hosed $?
! getopt --test > /dev/null
! getopt --test > /dev/null
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
echo 'I’m sorry, `getopt --test` failed in this environment.'
exit 1
Expand Down