Git Product home page Git Product logo

Comments (17)

alerque avatar alerque commented on May 17, 2024 9

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!

from nerdcommenter.

alerque avatar alerque commented on May 17, 2024 2

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

from nerdcommenter.

tuomastiainen avatar tuomastiainen commented on May 17, 2024 2

A useful feature would be to assume that the region inside <script> tags in html files is javascript.

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.

from nerdcommenter.

alerque avatar alerque commented on May 17, 2024 1

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

from nerdcommenter.

scrooloose avatar scrooloose commented on May 17, 2024

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

from nerdcommenter.

ain avatar ain commented on May 17, 2024

May I suggest reopening this ticket and label as enhancement?

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

from nerdcommenter.

chadfurman avatar chadfurman commented on May 17, 2024
<html>
...
<body>
<script>
<!-- function a() { -->
    <!-- var a = 'a string'; -->
<!-- } -->
</script>
</body>

Please re-open :)

from nerdcommenter.

mblarsen avatar mblarsen commented on May 17, 2024

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

from nerdcommenter.

alerque avatar alerque commented on May 17, 2024

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.

from nerdcommenter.

mblarsen avatar mblarsen commented on May 17, 2024

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

from nerdcommenter.

mblarsen avatar mblarsen commented on May 17, 2024

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.

from nerdcommenter.

alerque avatar alerque commented on May 17, 2024

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

from nerdcommenter.

shammellee avatar shammellee commented on May 17, 2024

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.

from nerdcommenter.

TaDaa avatar TaDaa commented on May 17, 2024

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

from nerdcommenter.

alerque avatar alerque commented on May 17, 2024

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?

from nerdcommenter.

MuhammadSawalhy avatar MuhammadSawalhy commented on May 17, 2024

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:

simplescreenrecorder-2021-08-12_16.51.13-montaged.mp4

from nerdcommenter.

MuhammadSawalhy avatar MuhammadSawalhy commented on May 17, 2024

https://github.com/JoosepAlviste/nvim-ts-context-commentstring

from nerdcommenter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.