1+ local Node = require (" splitjoin.util.node" )
2+
3+ local M = {}
4+
5+ --- @param node TSNode
6+ --- @param options SplitjoinLanguageOptions
7+ function M .split_struct (node , options )
8+ local indent = options .default_indent or " "
9+ local open , close = unpack (options .surround or {})
10+ local lines = {}
11+
12+ table.insert (lines , open .. " \n " )
13+
14+ for child in node :iter_children () do
15+ if child :type () == " field_declaration" then
16+ local text = vim .trim (Node .get_text (child ))
17+ table.insert (lines , indent .. text .. " \n " )
18+ end
19+ end
20+
21+ table.insert (lines , close )
22+ Node .replace (node , table.concat (lines , " " ))
23+ Node .goto_node (node )
24+ end
25+
26+ --- @param node TSNode
27+ --- @param options SplitjoinLanguageOptions
28+ function M .join_struct (node , options )
29+ local open , close = unpack (options .surround or {})
30+ local padding = options .padding or " "
31+ local parts = {}
32+
33+ for child in node :iter_children () do
34+ if child :type () == " field_declaration" then
35+ local text = vim .trim (Node .get_text (child ))
36+ if text :match (" .*,%s*$" ) then
37+ text = text :gsub (" ,%s*$" , " " )
38+ end
39+ table.insert (parts , text )
40+ end
41+ end
42+
43+ local final_string = open .. padding .. " " .. table.concat (parts , " ; " ) .. " " .. padding .. close
44+ Node .replace (node , final_string )
45+ Node .goto_node (node )
46+ end
47+
48+ return M
0 commit comments