@@ -98,32 +98,88 @@ function ECMAScript.join_arrow_function(node, options)
9898 Node .goto_node (node )
9999end
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
129185return ECMAScript
0 commit comments