preservim / nerdcommenter

Vim plugin for intensely nerdy commenting powers
Creative Commons Zero v1.0 Universal
4.99k stars 447 forks source link

Different comment style for file regions #17

Open splondike opened 13 years ago

splondike commented 13 years ago

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?

scrooloose commented 13 years ago

Hey splondike, yeah this is non-trivial. I may implement it in the future though as it is something i want as well.

ain commented 10 years ago

May I suggest reopening this ticket and label as enhancement?

I came across this one today myself. Somewhat major request.

chadfurman commented 8 years ago
<html>
...
<body>
<script>
<!-- function a() { -->
    <!-- var a = 'a string'; -->
<!-- } -->
</script>
</body>

Please re-open :)

mblarsen commented 7 years ago

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

alerque commented 7 years ago

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.

mblarsen commented 7 years ago

Couldn't the plugin look for the nearest language syntax instead of using file type?

alerque commented 7 years ago

@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.

mblarsen commented 7 years ago

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.

tuomastiainen commented 6 years ago

A useful feature would be to assume that the region inside Githubissues.

  • Githubissues is a development platform for aggregating issues.