-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: relay on languagetree commentstring as much as possible #86
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,10 +51,8 @@ end | |
function M.calculate_commentstring(args) | ||
args = args or {} | ||
local key = args.key or '__default' | ||
local location = args.location or nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will need to keep allowing to pass in a custom |
||
|
||
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() | ||
|
||
if not node and not language_tree then | ||
return nil | ||
|
@@ -71,9 +69,24 @@ function M.calculate_commentstring(args) | |
local language = language_tree:lang() | ||
local language_config = config.get_languages_config()[language] | ||
|
||
-- Use ts node commentstring if no language is configured | ||
if not language_config then | ||
local cs = M.check_ts_node(language_tree) | ||
if cs then | ||
return cs | ||
end | ||
end | ||
|
||
return M.check_node(node, language_config, key) | ||
end | ||
|
||
function M.check_ts_node(language_tree) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name for this isn't very indicative of what the function does, I'd rename it to something like "get commentstring from language tree". Also, it would be nice to have type hints for the parameter and return type. |
||
local cs = vim.filetype.get_option(language_tree:lang(), 'commentstring') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Language of a tree doesn't always equal to the filetype it is usually used. See There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah what i'm doing already is to obtain the language under the cursor and skip the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even if this variable is used in a very small scope, I think that it still makes sense to avoid abbreviations in variable names. "cs" makes me think of Counter Strike 😄 Same thing above |
||
if type(cs) == 'string' and cs ~= '' then | ||
return cs | ||
end | ||
end | ||
|
||
---Update the `commentstring` setting based on the current location of the | ||
---cursor. If no `commentstring` can be calculated, will revert to the ofiginal | ||
---`commentstring` for the current file. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,20 +74,14 @@ end | |
---Returns the node at the cursor's line and the language tree for that | ||
---injection. | ||
--- | ||
---@param only_languages string[] List of languages to filter for, all | ||
--- other languages will be ignored. | ||
---@param location? ts_context_commentstring.Location location Line, column | ||
--- where to start traversing the tree. Defaults to cursor start of line. | ||
--- This usually makes the most sense when commenting the whole line. | ||
--- | ||
---@return table|nil node, table|nil language_tree Node and language tree for the | ||
--- location | ||
function M.get_node_at_cursor_start_of_line(only_languages, location) | ||
function M.get_node_at_cursor_start_of_line() | ||
if not M.is_treesitter_active() then | ||
return | ||
end | ||
|
||
location = location or M.get_cursor_line_non_whitespace_col_location() | ||
local location = M.get_cursor_line_non_whitespace_col_location() | ||
local range = { | ||
location[1], | ||
location[2], | ||
|
@@ -99,7 +93,9 @@ function M.get_node_at_cursor_start_of_line(only_languages, location) | |
local language_tree = vim.treesitter.get_parser() | ||
-- Get the smallest supported language's tree with nodes inside the given range | ||
language_tree:for_each_tree(function(_, ltree) | ||
if ltree:contains(range) and vim.tbl_contains(only_languages, ltree:lang()) then | ||
-- always exclude the comment language as it's returning invalid | ||
-- commentstring | ||
if ltree:contains(range) and ltree:lang() ~= 'comment' then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's also |
||
language_tree = ltree | ||
end | ||
end) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd keep the language configs that:
as removing those would result in breaking changes and would require people to change their configs after updating this plugin. I would very much like to avoid any breaking changes, we could group those together with other breaking changes in the future (e.g., removing the
config
option for good).Here are the language configs I would keep:
The others can be deleted, I think.
This at least makes it so that new languages with a single commenting style would work out of the box, and wouldn't need to be configured in this plugin.
We can figure out how to get rid of the multiline comment configs in the future (maybe using
:h comments
somehow?).