Comments (17)
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.
@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.
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.
@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.
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.
May I suggest reopening this ticket and label as enhancement?
I came across this one today myself. Somewhat major request.
from nerdcommenter.
<html>
...
<body>
<script>
<!-- function a() { -->
<!-- var a = 'a string'; -->
<!-- } -->
</script>
</body>
Please re-open :)
from nerdcommenter.
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.
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.
Couldn't the plugin look for the nearest language syntax instead of using file type?
from nerdcommenter.
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.
@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.
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.
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.
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.
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.
https://github.com/JoosepAlviste/nvim-ts-context-commentstring
from nerdcommenter.
Related Issues (20)
- [Question] Are "interfaces" not available anymore? HOT 1
- The vue hook doesn't work now HOT 5
- `commentstring` value is not updated after first usage in the buffer HOT 8
- If `$` is selected, the following line is commented as well HOT 1
- [Feature Request] Sexy comments with double star (/** */) HOT 1
- How to map the function toggling comment state to a keymap in normal mode and visual mode? HOT 2
- Error on version 2.6.0 HOT 4
- [Bug] Vim file with inline comment and spaces before line can't be uncommented normally. HOT 5
- Uncommenting indented code is broken for vim9script HOT 5
- [Bug] Add left delim comment before leading space
- [Feature Request] Add in place commenting option
- Uncommenting does not work when left delimiter starts with backslash HOT 7
- [Feature Request] Un/Folding comments HOT 2
- Fortran is not supported HOT 3
- Ignore specific lines when comment/uncomment HOT 2
- [Question] how to placte the cursor a line below commented section? HOT 3
- readme.md needs to include autoload vim file
- infinite recursion when trying to comment glsl code HOT 4
- NERDCommenterDelims left switches to leftAlt repeatedly on a new .c buffer open
- Keep selection after commenting HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nerdcommenter.