Skip to content

Commit

Permalink
Merge pull request #78 from felixnorden/main
Browse files Browse the repository at this point in the history
  • Loading branch information
JoosepAlviste committed Oct 12, 2023
2 parents 7241635 + 4e8efa2 commit 92e688f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
14 changes: 9 additions & 5 deletions doc/nvim-ts-context-commentstring.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Currently, the following languages are supported when they are injected with
language tree (see `lua/ts_context_commentstring/internal.lua`):

- `astro`
- `c`
- `css`
- `glimmer`
- `graphql`
Expand All @@ -28,13 +29,16 @@ language tree (see `lua/ts_context_commentstring/internal.lua`):
- `python`
- `rescript`
- `scss`
- `shell`
- `sql`
- `solidity`
- `svelte`
- `tsx`
- `twig`
- `typescript`
- `vim`
- `vue`
- `zsh`

This means that in any filetype, if the given languages are injected, this
plugin should detect them and correctly set the 'commentstring'. For example,
Expand Down Expand Up @@ -68,13 +72,13 @@ With `nvim-treesitter` >= 1.0, use the `setup` function of this plugin:
The following examples are for `nvim-treesitter` < 1.0, but they can easily be
adapted for `nvim-treesitter` >= 1.0.

Support for more languages can be added quite easily by passing a `config` table
Support for more languages can be added quite easily by passing a `languages` (formerly the now deprecated `config`) table
when configuring the plugin:
>lua
require('nvim-treesitter.configs').setup {
context_commentstring = {
enable = true,
config = {
languages = {
css = '// %s',
},
},
Expand All @@ -84,7 +88,7 @@ when configuring the plugin:
Or with the `setup` function if using `nvim-treesitter` >= 1.0:
>lua
require('ts_context_commentstring').setup {
config = {
languages = {
css = '// %s',
},
}
Expand All @@ -103,7 +107,7 @@ like:
require('nvim-treesitter.configs').setup {
context_commentstring = {
enable = true,
config = {
languages = {
javascript = {
__default = '// %s',
jsx_element = '{/* %s */}',
Expand Down Expand Up @@ -131,7 +135,7 @@ multi-line comment styles (useful when integrating with a commenting plugin):
require('nvim-treesitter.configs').setup {
context_commentstring = {
enable = true,
config = {
languages = {
typescript = { __default = '// %s', __multiline = '/* %s */' },
},
},
Expand Down
40 changes: 23 additions & 17 deletions lua/ts_context_commentstring/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ local M = {}
---@class ts_context_commentstring.Config
---@field enable_autocmd boolean
---@field custom_calculation? fun(node: TSNode, language_tree: LanguageTree): string
---@field languages ts_context_commentstring.LanguagesConfig
---@field config ts_context_commentstring.LanguagesConfig
---@field commentary_integration ts_context_commentstring.CommentaryConfig

Expand All @@ -56,28 +57,30 @@ M.config = {
CommentaryUndo = 'gcu',
},

-- TODO: We should probably rename this as having a "config" key inside
-- "config" is probably confusing. Maybe "languages"?
config = {
languages = {
-- Languages that have a single comment style
typescript = { __default = '// %s', __multiline = '/* %s */' },
css = '/* %s */',
scss = { __default = '// %s', __multiline = '/* %s */' },
php = { __default = '// %s', __multiline = '/* %s */' },
html = '<!-- %s -->',
svelte = '<!-- %s -->',
vue = '<!-- %s -->',
astro = '<!-- %s -->',
handlebars = '{{! %s }}',
c = { __default = '// %s', __multiline = '/* %s */' },
css = '/* %s */',
glimmer = '{{! %s }}',
graphql = '# %s',
handlebars = '{{! %s }}',
html = '<!-- %s -->',
lua = { __default = '-- %s', __multiline = '--[[ %s ]]' },
vim = '" %s',
sql = '-- %s',
twig = '{# %s #}',
python = { __default = '# %s', __multiline = '""" %s """' },
nix = { __default = '# %s', __multiline = '/* %s */' },
php = { __default = '// %s', __multiline = '/* %s */' },
python = { __default = '# %s', __multiline = '""" %s """' },
rescript = { __default = '// %s', __multiline = '/* %s */' },
scss = { __default = '// %s', __multiline = '/* %s */' },
sh = '# %s',
solidity = { __default = '// %s', __multiline = '/* %s */' },
sql = '-- %s',
svelte = '<!-- %s -->',
twig = '{# %s #}',
typescript = { __default = '// %s', __multiline = '/* %s */' },
vim = '" %s',
vue = '<!-- %s -->',
zsh = '# %s',

-- Languages that can have multiple types of comments
tsx = {
Expand All @@ -92,9 +95,12 @@ M.config = {
spread_element = { __default = '// %s', __multiline = '/* %s */' },
},
},

---@deprecated Use the languages configuration instead!
config = {},
}

M.config.config.javascript = M.config.config.tsx
M.config.languages.javascript = M.config.languages.tsx

---@param config? ts_context_commentstring.Config
function M.update(config)
Expand All @@ -113,7 +119,7 @@ end

---@return ts_context_commentstring.LanguagesConfig
function M.get_languages_config()
return M.config.config
return vim.tbl_deep_extend('force', M.config.languages, M.config.config)
end

return M

0 comments on commit 92e688f

Please sign in to comment.