Skip to content

Commit

Permalink
Configure plugin via global variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Slotos committed Nov 25, 2023
1 parent b8ff464 commit 5c86c49
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 24 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ require('lazy').setup {
Defaults work out of the box.
Set `vim.g.skip_ts_context_commentstring_module = true` somewhere in your configuration to skip backwards compatibility routines and speed up loading.

If you want to change the configuration, call the `setup` function of this plugin, e.g.:
If you want to change the configuration, provide custom overrides in `vim.g.ts_context_commentstring_config` before it's loaded

```lua
vim.g.ts_context_commentstring_config = {
enable_autocmd = false,
}
```

Or call the `setup` function of this plugin, e.g.:

```lua
require('ts_context_commentstring').setup {
Expand Down
18 changes: 13 additions & 5 deletions doc/nvim-ts-context-commentstring.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ This plugin initializes using default **plugin** system.
If you need to disable auto-initialization, set
`g:loaded_ts_context_commentstring` to non-zero value.

Custom configuration can be supplied using `setup` function:
Custom configuration can be supplied by populating
`vim.g.ts_context_commentstring_config` before plugin is loaded
>lua
vim.g.ts_context_commentstring_config = {
-- ... configuration here
}
<

Or using `setup` function:
>lua
require('ts_context_commentstring').setup {
-- ... configuration here
Expand All @@ -65,7 +73,7 @@ Custom configuration can be supplied using `setup` function:
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('ts_context_commentstring').setup {
vim.g.ts_context_commentstring_config = {
languages = {
css = '// %s',
},
Expand All @@ -82,7 +90,7 @@ Treesitter nodes. Each node can have its own unique commenting style. For
example, here's how the default configuration for `javascript` would look
like:
>lua
require('ts_context_commentstring').setup {
vim.g.ts_context_commentstring_config = {
languages = {
javascript = {
__default = '// %s',
Expand All @@ -107,7 +115,7 @@ Additionally, it is possible to have each 'commentstring' configuration be a
table with custom keys. This can be used to configure separate single and
multi-line comment styles (useful when integrating with a commenting plugin):
>lua
require('ts_context_commentstring').setup {
vim.g.ts_context_commentstring_config = {
languages = {
typescript = { __default = '// %s', __multiline = '/* %s */' },
},
Expand All @@ -134,7 +142,7 @@ integrations. There are some useful helper functions exported from
If you want to calculate your own 'commentstring' you are able to do so with
the `custom_calculation` option:
>lua
require('ts_context_commentstring').setup {
vim.g.ts_context_commentstring_config = {
custom_calculation = function(node, language_tree)
-- ...
end,
Expand Down
6 changes: 3 additions & 3 deletions lua/ts_context_commentstring.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local M = {}

---Set up non-default configuration
---@param config ts_context_commentstring.Config
---Re-read global configuration or apply explicit override
---@param config ts_context_commentstring.Config|nil
function M.setup(config)
require('ts_context_commentstring.config').update(config)
require('ts_context_commentstring.config').setup(config or vim.g.ts_context_commentstring_config)
end

---Calculate the commentstring based on the current location of the cursor.
Expand Down
45 changes: 36 additions & 9 deletions lua/ts_context_commentstring/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ local M = {}
---@field commentary_integration ts_context_commentstring.CommentaryConfig

---@type ts_context_commentstring.Config
M.config = {
local default_config = {
-- Whether to update the `commentstring` on the `CursorHold` autocmd
enable_autocmd = true,

Expand Down Expand Up @@ -102,12 +102,20 @@ M.config = {
---@deprecated Use the languages configuration instead!
config = {},
}
default_config.languages.javascript = default_config.languages.tsx

M.config.languages.javascript = M.config.languages.tsx
local config, configured_languages = {}, {}

---@param config? ts_context_commentstring.Config
function M.update(config)
M.config = vim.tbl_deep_extend('force', M.config, config or {})
---@param overrides? ts_context_commentstring.Config
function M.setup(overrides)
overrides = overrides or {}
vim.validate {
config = { overrides, 'table' },
}

config = vim.tbl_deep_extend('force', {}, default_config, overrides)
config.languages = vim.tbl_deep_extend('force', config.languages, config.config)
configured_languages = vim.tbl_keys(config.languages)
end

---@return boolean
Expand All @@ -116,13 +124,32 @@ function M.is_autocmd_enabled()
return false
end

local enable_autocmd = M.config.enable_autocmd
local enable_autocmd = config.enable_autocmd
return enable_autocmd == nil and true or enable_autocmd
end

---@return ts_context_commentstring.LanguagesConfig
function M.get_languages_config()
return vim.tbl_deep_extend('force', M.config.languages, M.config.config)
---@return table<string>
function M.configured_languages()
return configured_languages
end

---@param lang string
---@return ts_context_commentstring.LanguageConfig | nil
function M.for_language(lang)
return (config.languages or {})[lang]
end

---@return ts_context_commentstring.CommentaryConfig | table
function M.commentary_integration()
return config.commentary_integration or {}
end

---@return function | nil
function M.custom_calculation()
return config.custom_calculation
end

---Load global configuration on file load
M.setup(vim.g.ts_context_commentstring_config)

return M
13 changes: 7 additions & 6 deletions lua/ts_context_commentstring/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function M.setup_buffer(bufnr)

-- If vim-commentary is installed, set up mappings for it
if vim.g.loaded_commentary == 1 then
require('ts_context_commentstring.integrations.vim_commentary').set_up_maps(config.config.commentary_integration)
require('ts_context_commentstring.integrations.vim_commentary').set_up_maps(config.commentary_integration())
end

if enable_autocmd then
Expand Down Expand Up @@ -53,14 +53,15 @@ function M.calculate_commentstring(args)
local key = args.key or '__default'
local location = args.location or nil

local node, language_tree =
utils.get_node_at_cursor_start_of_line(vim.tbl_keys(config.get_languages_config()), location)
local node, language_tree = utils.get_node_at_cursor_start_of_line(config.configured_languages(), location)

if not node and not language_tree then
---@cast node -nil
---@cast language_tree -nil
return nil
end

local custom_calculation = config.config.custom_calculation
local custom_calculation = config.custom_calculation()
if custom_calculation then
local commentstring = custom_calculation(node, language_tree)
if commentstring then
Expand All @@ -69,7 +70,7 @@ function M.calculate_commentstring(args)
end

local language = language_tree:lang()
local language_config = config.get_languages_config()[language]
local language_config = config.for_language(language)

return M.check_node(node, language_config, key)
end
Expand Down Expand Up @@ -100,7 +101,7 @@ end
---check its parent node.
---
---@param node table
---@param language_config ts_context_commentstring.LanguageConfig
---@param language_config ts_context_commentstring.LanguageConfig|nil
---@param commentstring_key string
---
---@return string | nil
Expand Down

0 comments on commit 5c86c49

Please # to comment.