-
-
Notifications
You must be signed in to change notification settings - Fork 161
feat: Auto calculate commentstring
#62
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6537549
feat: Auto calculate commentstring
tjdevries ed18b8a
fixup
tjdevries 80dd5f1
fixes from stream
tjdevries 96db471
some fixes
numToStr 964be1c
fix: fix off by one
tjdevries 5e39d46
new logic
numToStr 3412cff
refactor range
numToStr 68e76c1
pass correct range
numToStr 30d2a53
it can be better
numToStr bc37d4a
cleanup
numToStr 4761d44
readme update
numToStr 7932f57
fix
numToStr 1394f6a
typo
numToStr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
local ts = require('Comment.ts') | ||
|
||
--- Comment context | ||
---@class Ctx | ||
---@field lang string: The name of the language where the cursor is | ||
---@field node table: The containing node of where the cursor is | ||
---@field ctype CType | ||
---@field cmode CMode | ||
---@field cmotion CMotion | ||
local Ctx = {} | ||
|
||
--- Create a new Ctx | ||
---@return Ctx | ||
function Ctx:new(opts) | ||
assert(opts.cmode, 'Must have a cmode') | ||
assert(opts.cmotion, 'Must have a cmotion') | ||
assert(opts.ctype, 'Must have a ctype') | ||
|
||
opts.lang = ts.get_lang(opts) | ||
opts.node = ts.get_node(opts) | ||
opts.node_type = opts.node and opts.node:type() | ||
|
||
return setmetatable(opts, self) | ||
end | ||
|
||
return Ctx |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
local ft = require('Comment.ft') | ||
local U = require('Comment.utils') | ||
|
||
local ts = {} | ||
|
||
local get_cursor_line_non_whitespace_col_location = function() | ||
local cursor = vim.api.nvim_win_get_cursor(0) | ||
local first_non_whitespace_col = vim.fn.match(vim.fn.getline('.'), '\\S') | ||
|
||
return { | ||
cursor[1] - 1, | ||
first_non_whitespace_col, | ||
} | ||
end | ||
|
||
--- Get the tree associated with the current comment | ||
---@param ctx Ctx | ||
local get_current_tree = function(ctx, bufnr, range) | ||
if bufnr and not range then | ||
error('If you pass bufnr, you must pass a range as well') | ||
end | ||
|
||
bufnr = bufnr or vim.api.nvim_get_current_buf() | ||
local ok, langtree = pcall(vim.treesitter.get_parser, bufnr) | ||
|
||
if not ok then | ||
return nil | ||
end | ||
|
||
if not range then | ||
local cursor | ||
if ctx.cmotion == U.cmotion.line then | ||
cursor = get_cursor_line_non_whitespace_col_location() | ||
elseif ctx.cmotion == U.cmotion.V then | ||
local region = { U.get_region('V') } | ||
cursor = { region[1] - 1, region[2] } | ||
elseif ctx.cmotion == U.cmotion.v then | ||
local region = { U.get_region('v') } | ||
cursor = { region[1] - 1, region[2] } | ||
else | ||
-- ctx.cmotion == U.cmotion.char | ||
cursor = vim.api.nvim_win_get_cursor(0) | ||
end | ||
|
||
range = { | ||
cursor[1] - 1, | ||
cursor[2], | ||
cursor[1] - 1, | ||
cursor[2], | ||
} | ||
end | ||
|
||
return langtree:language_for_range(range), range | ||
numToStr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
--- Get the current language for bufnr and range | ||
---@param ctx Ctx: The current content | ||
---@param bufnr number: The bufnr | ||
---@param range table: See ranges for treesitter | ||
function ts.get_lang(ctx, bufnr, range) | ||
local current_tree = get_current_tree(ctx, bufnr, range) | ||
if not current_tree then | ||
return | ||
end | ||
|
||
return current_tree:lang() | ||
end | ||
|
||
--- Get corresponding node to the context | ||
---@param ctx Ctx: The current content | ||
---@param bufnr number: The buffer you are interested in. Generally nil | ||
---@param in_range table: The range you are interested in. Generally nil | ||
function ts.get_node(ctx, bufnr, in_range) | ||
local current_tree, range = get_current_tree(ctx, bufnr, in_range) | ||
if not current_tree then | ||
return | ||
end | ||
|
||
local lang = current_tree:lang() | ||
|
||
local config = ft.lang(lang) | ||
if not config then | ||
return | ||
end | ||
|
||
-- Short circuit if this ft doesn't have magical embedded languages | ||
-- inside of itself that randomly change just based on where you are | ||
-- in a syntax tree, instead of like, having the same rules everywhere. | ||
if vim.tbl_islist(config) then | ||
return nil | ||
end | ||
|
||
local root = current_tree:trees()[1]:root() | ||
local contained = root:named_descendant_for_range(unpack(range)) | ||
|
||
local original = contained | ||
while contained and contained:type() and not config[contained:type()] do | ||
contained = contained:parent() | ||
end | ||
|
||
-- Hmmm, might want this back, not sure yet. | ||
-- if not contained then | ||
-- return original | ||
-- end | ||
|
||
return contained | ||
numToStr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
return ts |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.