Skip to content

Commit a234c51

Browse files
committed
Split calculate_commentstring from update_commentstring
1 parent 728f954 commit a234c51

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

lua/ts_context_commentstring/internal.lua

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,44 @@ function M.setup_buffer()
6464
end
6565
end
6666

67-
-- Update the commentstring based on the current location of the cursor
68-
function M.update_commentstring()
67+
-- Calculate the commentstring based on the current location of the cursor.
68+
--
69+
-- **Note:** We should treat this function like a public API, try not to break
70+
-- it!
71+
--
72+
-- @returns the commentstring or nil if not found
73+
function M.calculate_commentstring()
6974
local node, language_tree = utils.get_node_at_cursor_start_of_line(
7075
vim.tbl_keys(M.config)
7176
)
77+
78+
if not node and not language_tree then
79+
return nil
80+
end
81+
7282
local language = language_tree:lang()
7383
local language_config = M.config[language]
7484

75-
local found_commentstring = M.check_node(node, language_config)
85+
return M.check_node(node, language_config)
86+
end
87+
88+
-- Update the `commentstring` setting based on the current location of the
89+
-- cursor. If no `commentstring` can be calculated, will revert to the ofiginal
90+
-- `commentstring` for the current file.
91+
--
92+
-- **Note:** We should treat this function like a public API, try not to break
93+
-- it!
94+
function M.update_commentstring()
95+
local found_commentstring = M.calculate_commentstring()
7696

7797
if found_commentstring then
7898
api.nvim_buf_set_option(0, 'commentstring', found_commentstring)
7999
else
80-
api.nvim_buf_set_option(0, 'commentstring', api.nvim_buf_get_var(0, 'ts_original_commentstring'))
100+
-- No commentstring was found, default to the
101+
local original_commentstring = vim.b.ts_original_commentstring
102+
if original_commentstring then
103+
api.nvim_buf_set_option(0, 'commentstring', vim.b.ts_original_commentstring)
104+
end
81105
end
82106
end
83107

0 commit comments

Comments
 (0)