@@ -16,36 +16,38 @@ local M = {}
1616-- `:lua print(require'nvim-treesitter.parsers'.get_parser():language_for_range({ line, col, line, col }):lang())`
1717M .config = {
1818 -- Languages that have a single comment style
19- typescript = ' // %s' ,
19+ typescript = { __default = ' // %s' , __multiline = ' /* %s */ ' } ,
2020 css = ' /* %s */' ,
2121 scss = ' /* %s */' ,
22- php = ' // %s' ,
22+ php = { __default = ' // %s' , __multiline = ' /* %s */ ' } ,
2323 html = ' <!-- %s -->' ,
2424 svelte = ' <!-- %s -->' ,
2525 vue = ' <!-- %s -->' ,
2626 handlebars = ' {{! %s }}' ,
2727 glimmer = ' {{! %s }}' ,
2828 graphql = ' # %s' ,
29- lua = ' -- %s' ,
29+ lua = { __default = ' -- %s' , __multiline = ' --[[ %s ]] ' } ,
3030
3131 -- Languages that can have multiple types of comments
3232 tsx = {
3333 __default = ' // %s' ,
34+ __multiline = ' /* %s */' ,
3435 jsx_element = ' {/* %s */}' ,
3536 jsx_fragment = ' {/* %s */}' ,
36- jsx_attribute = ' // %s' ,
37- comment = ' // %s' ,
38- call_expression = ' // %s' ,
39- statement_block = ' // %s' ,
37+ jsx_attribute = { __default = ' // %s' , __multiline = ' /* %s */ ' } ,
38+ comment = { __default = ' // %s' , __multiline = ' /* %s */ ' } ,
39+ call_expression = { __default = ' // %s' , __multiline = ' /* %s */ ' } ,
40+ statement_block = { __default = ' // %s' , __multiline = ' /* %s */ ' } ,
4041 },
4142 javascript = {
4243 __default = ' // %s' ,
44+ __multiline = ' /* %s */' ,
4345 jsx_element = ' {/* %s */}' ,
4446 jsx_fragment = ' {/* %s */}' ,
45- jsx_attribute = ' // %s' ,
46- comment = ' // %s' ,
47- call_expression = ' // %s' ,
48- statement_block = ' // %s' ,
47+ jsx_attribute = { __default = ' // %s' , __multiline = ' /* %s */ ' } ,
48+ comment = { __default = ' // %s' , __multiline = ' /* %s */ ' } ,
49+ call_expression = { __default = ' // %s' , __multiline = ' /* %s */ ' } ,
50+ statement_block = { __default = ' // %s' , __multiline = ' /* %s */ ' } ,
4951 },
5052}
5153
7981-- it!
8082--
8183-- @returns the commentstring or nil if not found
82- function M .calculate_commentstring ()
84+ function M .calculate_commentstring (args )
85+ args = args or {}
86+ local key = args .key or ' __default'
87+
8388 local node , language_tree = utils .get_node_at_cursor_start_of_line (vim .tbl_keys (M .config ))
8489
8590 if not node and not language_tree then
@@ -89,7 +94,7 @@ function M.calculate_commentstring()
8994 local language = language_tree :lang ()
9095 local language_config = M .config [language ]
9196
92- return M .check_node (node , language_config )
97+ return M .check_node (node , language_config , key )
9398end
9499
95100-- Update the `commentstring` setting based on the current location of the
98103--
99104-- **Note:** We should treat this function like a public API, try not to break
100105-- it!
101- function M .update_commentstring ()
102- local found_commentstring = M .calculate_commentstring ()
106+ function M .update_commentstring (args )
107+ local found_commentstring = M .calculate_commentstring (args )
103108
104109 if found_commentstring then
105110 api .nvim_buf_set_option (0 , ' commentstring' , found_commentstring )
114119
115120-- Check if the given node matches any of the given types. If not, recursively
116121-- check its parent node.
117- function M .check_node (node , language_config )
122+ function M .check_node (node , language_config , commentstring_key )
123+ commentstring_key = commentstring_key or ' __default'
124+
118125 -- There is no commentstring configuration for this language, use the
119126 -- `ts_original_commentstring`
120127 if not language_config then
@@ -124,18 +131,18 @@ function M.check_node(node, language_config)
124131 -- There is no node, we have reached the top-most node, use the default
125132 -- commentstring from language config
126133 if not node then
127- return language_config .__default or language_config
134+ return language_config [ commentstring_key ] or language_config .__default or language_config
128135 end
129136
130- local type = node :type ()
131- local match = language_config [type ]
137+ local node_type = node :type ()
138+ local match = language_config [node_type ]
132139
133140 if match then
134- return match
141+ return match [ commentstring_key ] or match . __default or match
135142 end
136143
137144 -- Recursively check the parent node
138- return M .check_node (node :parent (), language_config )
145+ return M .check_node (node :parent (), language_config , commentstring_key )
139146end
140147
141148-- Attach the module to the current buffer
0 commit comments