11local api = vim .api
22
3- local utils = require ( ' ts_context_commentstring.utils' )
4- local configs = require ( ' nvim-treesitter.configs' )
3+ local utils = require ' ts_context_commentstring.utils'
4+ local configs = require ' nvim-treesitter.configs'
55
66local M = {}
77
8- -- The configuration object keys should be **treesitter** languages, NOT
8+ -- The configuration object keys should be **treesitter** languages, NOT
99-- filetypes or file extensions.
1010--
11- -- You can get the treesitter language for the current file by running this
11+ -- You can get the treesitter language for the current file by running this
1212-- command:
1313-- `:lua print(require'nvim-treesitter.parsers'.get_buf_lang(0))`
1414--
@@ -65,24 +65,22 @@ function M.setup_buffer()
6565 end
6666
6767 if enable_autocmd then
68- utils .create_augroups ( {
68+ utils .create_augroups {
6969 context_commentstring_ft = {
70- {' CursorHold' , ' <buffer>' , [[ lua require('ts_context_commentstring.internal').update_commentstring()]] },
70+ { ' CursorHold' , ' <buffer>' , [[ lua require('ts_context_commentstring.internal').update_commentstring()]] },
7171 },
72- })
72+ }
7373 end
7474end
7575
7676-- Calculate the commentstring based on the current location of the cursor.
7777--
78- -- **Note:** We should treat this function like a public API, try not to break
78+ -- **Note:** We should treat this function like a public API, try not to break
7979-- it!
8080--
8181-- @returns the commentstring or nil if not found
8282function M .calculate_commentstring ()
83- local node , language_tree = utils .get_node_at_cursor_start_of_line (
84- vim .tbl_keys (M .config )
85- )
83+ local node , language_tree = utils .get_node_at_cursor_start_of_line (vim .tbl_keys (M .config ))
8684
8785 if not node and not language_tree then
8886 return nil
@@ -94,11 +92,11 @@ function M.calculate_commentstring()
9492 return M .check_node (node , language_config )
9593end
9694
97- -- Update the `commentstring` setting based on the current location of the
98- -- cursor. If no `commentstring` can be calculated, will revert to the ofiginal
95+ -- Update the `commentstring` setting based on the current location of the
96+ -- cursor. If no `commentstring` can be calculated, will revert to the ofiginal
9997-- `commentstring` for the current file.
10098--
101- -- **Note:** We should treat this function like a public API, try not to break
99+ -- **Note:** We should treat this function like a public API, try not to break
102100-- it!
103101function M .update_commentstring ()
104102 local found_commentstring = M .calculate_commentstring ()
@@ -117,11 +115,13 @@ end
117115-- Check if the given node matches any of the given types. If not, recursively
118116-- check its parent node.
119117function M .check_node (node , language_config )
120- -- There is no commentstring configuration for this language, use the
118+ -- There is no commentstring configuration for this language, use the
121119 -- `ts_original_commentstring`
122- if not language_config then return nil end
120+ if not language_config then
121+ return nil
122+ end
123123
124- -- There is no node, we have reached the top-most node, use the default
124+ -- There is no node, we have reached the top-most node, use the default
125125 -- commentstring from language config
126126 if not node then
127127 return language_config .__default or language_config
@@ -130,30 +130,31 @@ function M.check_node(node, language_config)
130130 local type = node :type ()
131131 local match = language_config [type ]
132132
133- if match then return match end
133+ if match then
134+ return match
135+ end
134136
135137 -- Recursively check the parent node
136138 return M .check_node (node :parent (), language_config )
137139end
138140
139141-- Attach the module to the current buffer
140142function M .attach ()
141- M .config = vim .tbl_deep_extend (
142- ' force' , M .config ,
143- configs .get_module (' context_commentstring' ).config or {}
144- )
143+ M .config = vim .tbl_deep_extend (' force' , M .config , configs .get_module (' context_commentstring' ).config or {})
145144
146145 return M .setup_buffer ()
147146end
148147
149- function M .detach () return end
148+ function M .detach ()
149+ return
150+ end
150151
151152_G .context_commentstring = {}
152153
153- -- Trigger re-calculation of the `commentstring` and trigger the given <Plug>
154+ -- Trigger re-calculation of the `commentstring` and trigger the given <Plug>
154155-- mapping right after that.
155156--
156- -- This is in the global scope because
157+ -- This is in the global scope because
157158-- `v:lua.require('ts_context_commentstring')` does not work for some reason.
158159function _G .context_commentstring .update_commentstring_and_run (mapping )
159160 M .update_commentstring ()
0 commit comments