-
-
Notifications
You must be signed in to change notification settings - Fork 159
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: Auto calculate commentstring
#62
Conversation
Thanks a ton. I'll have a look at this on the weekend or so. |
Should I be doing: -- TODO: Dunno if this is any good or not.
return config[ctx.ctype] or config[1] Or just return the |
I thought about this before and IMO we should throw an error if the block comment isn't found when doing One question, Is adding context awareness by default a good idea? I mean some (like me) don't use context-commentstring and for some language like rust, this is not necessarily useful to calculate comment string. |
I can make it short circuit for languages that don't have additional configuration. My thought would be is if you know this is definitely something people will use and you can make it in a way that doesn't require any additional configuration, why not just enable it? It might cost a few bonus cpu cycles, but you obviously can afford it. You compile rust code 😂 |
Yeah I guess we can do that 😄 But enter // When only attribute is on the line, then `//%s` is used.
export function Element() {
return (
<div>
<h1
// id="foo"
// class="lsp"
data-telescope="good"
>
Hello, world!
</h1>
</div>
);
}
// When attribute is on the same line as element, then `/*%s*/` is used.
export function Element() {
return (
<div>
<h1 /* id="foo" class="lsp" */ data-telescope="good" >
Hello, world!
</h1>
</div>
);
}
// For element, then `{/*%s*/}` is used.
export function Element() {
return (
<div>
{/*<h1 id="foo" class="lsp" data-telescope="good" >*/}
Hello, world!
</h1>
</div>
);
} What do you think? Should we try to cover all these cases or let the |
I just wanted to leave my 0.02$ and voice that I think the marriage between |
What changes do you mean for
I don't think you have to handle everything, but providing some framework to make things work reasonably well out of the box in what is essentially 70ish lines of code (the other bits are docs, config updates or renames effectively) seems like not a bad tradeoff. Of course, it's not my plugin :) so you can reject any time you want. I was just thinking this works surprisingly well with no dependencies (it only uses things from Another thing is this calculates based on where your cursor currently is, so you can do interesting stuff like if you have multiple filetypes on one line, it will calculate the correct one, but you just get that for free basically from just checking the current node and walking up til you find either an interesting node or the root. |
(Also, people can just do whatever crazy |
He was talking about this JoosepAlviste/nvim-ts-context-commentstring#28 change.
The initial code that you showed on your stream, yeah that was also a total surprise to me as well. For something like vue or lua + ffi, this is all that you might need. But IMO we should hold off adding this now. We can revisit this later :) |
@numToStr if there are changes you want to make, just push directly to this branch and then merge if you want |
So when this pr is going to be merged ? I loving this plugin so far but this will be killer |
I am little caught up with my work right now. It could take a while before I merge this :) |
Fine :) , BTW Great Work man ❤️ |
Anything I can do to help it get merged? I want to merge this before I make my TakeTuesday video about it :) Let me know if there is anything missing still for it. |
@tjdevries I am probably gonna merge it tomorrow morning :) |
<3 excellent. Thanks. Maybe I will try and sneak in a recording session before this tuesday then :) thanks |
I've done the following changes
|
OK, I think I pushed. Checked a bunch of different combos in that file. Feel free to squash and merge or whatever you want to do w/ this after. Excited for it :) working on the video now too |
Thanks a ton ❤️. But, I am a little skeptical about this feature as I am pretty sure there will be some potential issue that we haven't found yet (some I did find). IMO 90% of the issues will always come from But what about react/jsx gang? Ok, for that we could easily say that you should use
To do so I came with this (thanks to ts-playground) function ft.calculate(ctype)
local buf = vim.api.nvim_get_current_buf()
local langtree = vim.treesitter.get_parser(buf)
local win = vim.api.nvim_get_current_win()
local row, col = unpack(vim.api.nvim_win_get_cursor(win))
local range = { row - 1, col, row - 1, col }
local found
langtree:for_each_child(function(tree)
if not found and tree:contains(range) then
local lang = tree:lang()
local cstr = ft.get(lang, ctype)
if cstr then
found = cstr
end
end
end)
return found or ft.get(vim.bo.filetype, ctype)
end This is easy to maintain and reason about. (I am being selfish here 😅). And this is how this works with Lua (ffi), HTML and vue filetype. 2021-11-17.13-00-12.mp4@tjdevries What's your opinion on this? I need your green light on this :) |
Ah, so main difference is just that instead of being able to do for particular nodes, you'll only do it for embedded languages? |
Exactly, only supporting proper languages that are embedded inside one another. No walking nodes or something. No jsx either. |
It still includes the opening tag. But I am pretty sure that this is just how it works, since I can recreate the behaviour with |
Yup It does. If you have a whitespace after the opening tag. |
That's the thing, I don't have a whitespace. If you could try with this, and do a <template>
<div>
<p>Something</p>
<div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
name: 'test',
},
});
</script>
<style>
body {
height: 100vh;
}
</style> |
But this isn't a big deal since it is the same behaviour from other comment-plugins like for example |
@seblj 2021-11-18.16-30-55.mp4EDIT: |
Oh okay, that's good to know! I might check out |
I have now refactored the range thingy. Apart from cleanup, I think everything is good to go. @seblj Care to test (and don't forget to install PS: I might need to do some refactoring after this PR is merged. |
There seems to be some issues when there are whitespaces in the tag-block. (I installed Screen.Recording.2021-11-18.at.12.41.08.mov |
Hmm, I am getting this kinda similar issue in HTML file but vue is fine. I am looking into this issue. |
@seblj I get what's happening. Assuming the following style tag, doing <style>
.comment {
^ ^ `css` starts from here
This whitespace area is `html`
border: 1px solid red;
}
</style>
Now I think @tjdevries was right with this one. |
Apart from this tag issue (which only happens where one language ends and the other starts), everything else looks good to me. We can still say you can use |
Okay so I found out some of the issues and why they happen (I think). It seems to be the case like you say that treesitter is interpreting it as another language, but I could only reproduce this if the newline is between the starting tag and the first line inside the tags. <script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
name: 'test',
},
});
</script> With the newline there, treesitter interprets that as filetype vue, and I don't think there is much we can do about that. One thing I think we can do is calculate the commentstring based on where the cursor is when doing the motion. Right now it looks like the cursor jumps to the start of the motion, and then when finding the language where the cursor is currently, the language is returned as vue. After this the cursor returns to where it was (probably has something to do with the sticky option). If one is holding the cursor over the newline and uncomment, there isn't much we can do (to my knowledge) since treesitter interprets this as vue. However there is also a slight issue with empty lines if the commentstring surrounds the string (commentstrings like <style>
/* body { */
/* height: 100vh; */
/*
/* } */
</style> Note that this will not be an issue with commentstrings like If we add the rhs of the commentstring ( <style>
/* body { */
/* height: 100vh; */
/* */
/* } */
</style> |
Yeah, that certainly a limitation. But as I said we can still rely on
Ahh, yes I also noticed this sometimes. But this a general issue I can fix this later. Anyways other than these I think we are good to go. I would like to merge this. What do you say? |
That is actually also a problem when using
Yes, other than those two, it looks to be working great |
Noice. I'll just cleanup some stuff and update readme then I'll merge this :) |
Just a heads-up: this PR completely broke comment.nvim for filetypes without a parser: you just get
|
Fixed 7627727 |
Not all the way done, but figured I'd shoot this over to you so you can take a look and let me know what you think.