Skip to content

Commit 3d69ddd

Browse files
authored
fix: nvim 0.11.2 support (#35)
1 parent c84c4e5 commit 3d69ddd

File tree

14 files changed

+617
-199
lines changed

14 files changed

+617
-199
lines changed

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
test: *
2-
nvim --headless -u test/init.lua -c "lua require'plenary.test_harness'.test_directory('test/', {minimal_init='test/init.lua',sequential=true})"
1+
.PHONY: test
2+
3+
test:
4+
nvim --headless -u test/init.lua -c "lua local file = os.getenv('TEST_FILE') or 'test/'; require'plenary.test_harness'.test_directory(file, {minimal_init='test/init.lua',sequential=true})"

lua/splitjoin.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ local get_parser = vim.treesitter.get_parser
66
local is_in_node_range = vim.treesitter.is_in_node_range
77

88
---@class SplitjoinLanguageOptions
9-
---@field default_indent string
10-
---@field surround string[] tuple of surround strings
11-
---@field separator string=','
12-
---@field separator_is_node boolean=true
13-
---@field padding string
14-
---@field trailing_separator boolean=true true when a trailing separator (e.g. trailing comma) is permitted/desired
9+
---@field default_indent? string
10+
---@field surround? string[] tuple of surround strings
11+
---@field separator? string=','
12+
---@field separator_is_node? boolean=true
13+
---@field padding? string
14+
---@field trailing_separator? boolean=true true when a trailing separator (e.g. trailing comma) is permitted/desired
1515

1616
---@class SplitjoinLanguageConfig
1717
---@field nodes table<string, SplitjoinLanguageOptions>
1818

1919
---@class SplitjoinOptions
20-
---@field default_indent string
20+
---@field default_indent? string
2121
---@field languages table<string, SplitjoinLanguageOptions>
2222

2323
local Splitjoin = {}

lua/splitjoin/languages/ecmascript/defaults.lua

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

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

7070
},
7171

lua/splitjoin/languages/ecmascript/functions.lua

Lines changed: 83 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -98,32 +98,88 @@ 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
101+
function ECMAScript.split_comment(node, options)
102+
local text = Node.get_text(node)
103+
if String.is_multiline(text) or vim.startswith(text, '//') then return end
104+
local indent = options.default_indent or ' '
105+
local append, get = String.append('')
106+
local description = text
107+
:gsub("^/%*%*", "") -- Remove leading /**
108+
:gsub("%*/$", "") -- Remove trailing */
109+
:gsub("^%s+", "") -- Remove leading whitespace
110+
:gsub("%s+$", "") -- Remove trailing whitespace
111+
append(
112+
'/**\n',
113+
indent,
114+
'* ',
115+
description,
116+
'\n */'
117+
)
118+
Node.replace(node, get())
119+
Node.trim_line_end(node)
120+
Node.trim_line_end(node, 1)
121+
Node.goto_node(node)
122+
end
123+
124+
function ECMAScript.join_comment(node, options)
125+
local text = Node.get_text(node)
126+
127+
if vim.startswith(text, '/**') and String.is_multiline(text) then
128+
-- Join JSDoc comment
129+
local description = text
130+
:gsub("^/%*%*", "")
131+
:gsub("%*/$", "")
132+
:gsub("\r", "")
133+
:gsub("^\n*", "")
134+
:gsub("\n%s*%*%s?", "\n") -- Remove leading * on every line
135+
:gsub("^%s*%*%s?", "") -- Remove * at the start if present
136+
:gsub("^%s+", "") -- Trim leading whitespace
137+
:gsub("%s+$", "") -- Trim trailing whitespace
138+
139+
if not description:find("\n") then
140+
Node.replace(node, '/** ' .. description .. ' */')
141+
else
142+
-- Reformat multiline comment
143+
local indent = options and options.default_indent or ' '
144+
local lines = {}
145+
for line in description:gmatch("[^\n]+") do
146+
table.insert(lines, indent .. '* ' .. line)
147+
end
148+
Node.replace(node, '/**\n' .. table.concat(lines, '\n') .. '\n' .. indent .. '*/')
149+
end
150+
Node.goto_node(node)
151+
return
152+
end
153+
154+
if String.is_multiline(text) or vim.startswith(text, '//') then return end
155+
local row, col = node:range()
156+
local comment = vim.treesitter.get_node{ pos = { row, col - 1 } }
157+
158+
-- Remove /**, */, normalize whitespace, remove leading * on each line
159+
local description = text
160+
:gsub("^/%*%*", "")
161+
:gsub("%*/$", "")
162+
:gsub("\r", "")
163+
:gsub("^\n*", "")
164+
:gsub("\n%s*%*%s?", "\n") -- Remove leading * on every line
165+
:gsub("^%s*%*%s?", "") -- Remove * at the start if present
166+
:gsub("^%s+", "") -- Trim leading whitespace
167+
:gsub("%s+$", "") -- Trim trailing whitespace
168+
169+
-- Now check if the description is a single line (no internal newlines)
170+
if not description:find("\n") then
171+
Node.replace(comment, '/** ' .. description .. ' */')
172+
else
173+
local indent = options and options.default_indent or ' '
174+
local lines = {}
175+
for line in description:gmatch("[^\n]+") do
176+
table.insert(lines, indent .. '* ' .. line)
177+
end
178+
Node.replace(comment, '/**\n' .. table.concat(lines, '\n') .. '\n' .. indent .. '*/')
179+
end
180+
Node.goto_node(comment)
181+
end
182+
183+
128184

129185
return ECMAScript

0 commit comments

Comments
 (0)