Skip to content

Commit 649a5d1

Browse files
Merge pull request #26 from JoosepAlviste/feature/multiline-comments
2 parents 2317a61 + ef7bdab commit 649a5d1

File tree

2 files changed

+80
-21
lines changed

2 files changed

+80
-21
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,29 @@ rest of the keys refer to the type of the treesitter node. In this example, if
133133
your cursor is inside a `jsx_element`, then the `{/* %s */}` `commentstring`
134134
will be set.
135135

136+
Finally, it is possible to have each `commentstring` configuration be a table
137+
with custom keys. This can be used to configure separate single and multi-line
138+
comment styles (useful when integrating with a commenting plugin):
139+
140+
```lua
141+
require'nvim-treesitter.configs'.setup {
142+
context_commentstring = {
143+
enable = true,
144+
config = {
145+
typescript = { __default = '// %s', __multiline = '/* %s */' }
146+
}
147+
}
148+
}
149+
```
150+
151+
Then, the custom key can be passed to `update_commentstring`:
152+
153+
```lua
154+
require('ts_context_commentstring.internal').update_commentstring({
155+
key = '__multiline',
156+
})
157+
```
158+
136159
Note that the language refers to the *treesitter* language, not the filetype or
137160
the file extension.
138161

@@ -247,6 +270,35 @@ require("nvim_comment").setup({
247270
})
248271
```
249272

273+
#### [`Comment.nvim`](https://github.com/numToStr/Comment.nvim)
274+
275+
First, disable the `CursorHold` autocommand of this plugin:
276+
277+
```lua
278+
require'nvim-treesitter.configs'.setup {
279+
context_commentstring = {
280+
enable = true,
281+
enable_autocmd = false,
282+
}
283+
}
284+
```
285+
286+
Then, configure `Comment.nvim` to trigger the `commentstring` updating logic
287+
with its `pre_hook` configuration:
288+
289+
```lua
290+
require('Comment').setup {
291+
pre_hook = function(ctx)
292+
local U = require 'Comment.utils'
293+
local type = ctx.ctype == U.ctype.line and '__default' or '__multiline'
294+
return require('ts_context_commentstring.internal').calculate_commentstring {
295+
key = type,
296+
}
297+
end,
298+
}
299+
```
300+
301+
250302
## More demos
251303

252304
**React:**

lua/ts_context_commentstring/internal.lua

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,38 @@ local M = {}
1616
-- `:lua print(require'nvim-treesitter.parsers'.get_parser():language_for_range({ line, col, line, col }):lang())`
1717
M.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

@@ -79,7 +81,10 @@ end
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)
9398
end
9499

95100
-- Update the `commentstring` setting based on the current location of the
@@ -98,8 +103,8 @@ end
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)
@@ -114,7 +119,9 @@ end
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)
139146
end
140147

141148
-- Attach the module to the current buffer

0 commit comments

Comments
 (0)