nerdcommenter
nerdcommenter copied to clipboard
Different comment style for file regions
Would it be possible to have the comment style change depending on the cursor's position within a multi-language file? The specific case I'm interested in is Javascript and CSS embeded in a HTML document. Within the script tags i'd like to use // or // style, // within the style tags and for the HTML:
<html>
<style>
html{
/*background:green;*/
}
</style>
<script>
//var foo = "bar";
</script>
<!--<body></body>-->
</html>
I had a look around and this doesn't seem to be possible in a 'nice' way (i.e. there isn't a 'current_region_code_type' variable set). I've never done any vim programming though, is this correct?
Hey splondike, yeah this is non-trivial. I may implement it in the future though as it is something i want as well.
May I suggest reopening this ticket and label as enhancement?
I came across this one today myself. Somewhat major request.
<html>
...
<body>
<script>
<!-- function a() { -->
<!-- var a = 'a string'; -->
<!-- } -->
</script>
</body>
Please re-open :)
This seems to work well https://github.com/posva/vim-vue#how-can-i-use-nerdcommenter-in-vue-files, but would be nice with a generic solution
Thanks for pointing out that implementation @mblarsen. That's exactly the kind of thing we're going to have to do to make this feature work. In fact I think it will have to be implemented somewhat differently for detecting some embedded language pairs. I guess what this issue needs is some sort of framework for adding such hook based checks to certain languages, e.g. in HTML files by default have hooks that look for JS and CSS blocks, in SILE files look for LUA blocks, in Markdown look for code blocks with language tags, etc.
Couldn't the plugin look for the nearest language syntax instead of using file type?
@mblarsen If you know of a universal way to do quick, reliable detection of languages across the board from small fragments of code, then by all means lets hear it. As far as I'm aware the Unicorns of Neverland still have a corner on that market.
Looking for syntax of specific languages that are likely to be found in specific other ones might just be feasible, but the reason file type settings exist in editors in the first place and why they are typically based on file extension is because there isn't a practical implementation of any other method.
Keep in mind that the fragments may be very small:
<a href="<?php if (JS) { echo "javascript:myPopup('<p>Text</p>'); } else { echo '#'; } ?>" style="margin-left: 1em; <?= $link_css ?>">Link</a>
Think about all the places your cursor could be in that line and what would be involved in detecting the "nearest" language and trying to interpret a comment command. Even given these language pairs include explicit clues, untangling this kind of mess via syntax detection would require accurate syntax parsers for every language involved.
Furthermore any such detection assumes correct syntax. Maybe the reason you're commenting something out is because it has a syntax problem or is in another language. Maybe my comment is a Lua file includes notes about how a similar thing was implemented in Haskell. If I pasted a line of code and then wanted to comment it out, I would want the Lua comment syntax, not the Haskell one that would be detected.
I see, thanks for explaining this. Unfortunately I haven nowhere near the knowledge of the Unicorns of Neverland.
I didn't take into account that the cursor could be in other locations.
let stack = synstack(line('.'), col('.'))
So this isn't much help.
A useful feature would be to assume that the region inside
I acknowlegde that this assumption will always not be correct, but it will work often enough to be very useful when commenting js/html files.
@tuomastiainen Of course that would be useful (we all generally agree on this) but implementing it is not as easy as it sounds. I'd love to accept a contribution that actually accomplishes this, but so far the closest I've seen are hack jobs that come with significant side effects disabling other functionality that would otherwise work. You can check out one of them from this comment. Maybe that will work for your use case, but it isn't in shape that it could be added to the core of this plugin yet.
How about just using a different key mapping for a specific type of comment? For example, <leader>ch for HTML comments at the current cursor position.
@shammellee Obviously you could implement some keybindings yourself for any combination of comment styles you want to use and manually trigger the one you wanted, but that doesn't solve the issue that is being tracked here which is automatic detection of scope.
I think at a minimum the delimiters just need to not cache the commentstring -- this at least allows plugins like vim-vue to change the commentstring and have it respected in NERDCommenter.
Have a fork that does this, but I hacked it together to just call the s:SetUpForNewFiletype every time NERDComment is called. This doesn't handle the sexy comments though
I know that some syntax highlighting plugins are able to detect regions and embed another languages syntax highlighting into their rules. It must be possible to look at the syntax highlighter and figure out what kind of zone we are in and use that to override the filetype defaults.
Anybody want to take a stab at this?
See also tyru/caw.vim which has implemented this using Shougo/context_filetype. We could probably do something similar.
Also call me a humbug, but I wish people would consider contributing to existing projects before starting up new ones just to implement one or two features. This project wouldn't refuse a PR that implemented this!
suy/vim-context-commentstring fixes the issue of Lue in VimScript and this issue of HTML by changing the commentstring value, but another issue appeared after using this, I think nerdcommenter stores the first use of commentstring in the current buffer and never read it again:
https://user-images.githubusercontent.com/42011920/129219251-a0862097-5831-4f09-960b-e1a070a43999.mp4
https://github.com/JoosepAlviste/nvim-ts-context-commentstring