Comments (4)
Hey @RaafatTurki!
It does make sense to have this function in this plugin. I created a PR: #45 Could you try it out and see if it works correctly for your use case?
Also, if you don't mind, could you show your final solution as well? 😄
from nvim-ts-context-commentstring.
It worked pretty well, here's the final Comment.nvim pre_hook
I ended up with
pre_hook = function(ctx)
local comment_utils = require 'Comment.utils'
local tsctxcs_utils = require 'ts_context_commentstring.utils'
local tsctxcs_internal = require 'ts_context_commentstring.internal'
local comment_strings = {}
local cs_type = ctx.ctype == comment_utils.ctype.line and '__default' or '__multiline'
local calc_cs = function (posision)
return tsctxcs_internal.calculate_commentstring({
key = cs_type,
location = posision
})
end
local abort_cs = function (msg)
vim.notify(msg)
return '%s'
end
-- visual commenting
if ctx.cmotion == comment_utils.cmotion.v or ctx.cmotion == comment_utils.cmotion.V then
table.insert(comment_strings, calc_cs(tsctxcs_utils.get_visual_start_location()))
table.insert(comment_strings, calc_cs(tsctxcs_utils.get_visual_end_location()))
-- block commenting
elseif ctx.ctype == comment_utils.ctype.block then
table.insert(comment_strings, calc_cs(tsctxcs_utils.get_cursor_location()))
-- linewise commenting
else
table.insert(comment_strings, calc_cs(nil))
end
-- compare comment_strings and return one
local last_cs = nil
for i, cs in ipairs(comment_strings) do
if i > 1 and cs ~= last_cs then
return abort_cs('Commenting aborted due to mismatching comment strings')
end
last_cs = cs
end
return last_cs
end
One thing I'd like it to do better is not register something in the undo history when aborting but that's a Comment.nvim issue that I'll look further into (will post new snippet here when resolved).
from nvim-ts-context-commentstring.
I'm thinking that maybe it would make sense to maintain the pre_hook
integration inside this plugin (or perhaps inside Comment.nvim). Then people wouldn't need to copy-paste a lot of unnecessary code into their configuration and we could maintain the integration in one of the plugins, keeping it always up-to-date, adding features, etc.
Then, the users could import it easily:
require('Comment').setup {
pre_hook = require('ts_context_commentstring.integrations.comment_nvim').pre_hook,
}
Maybe once you're happy with your solution you could make a PR to this plugin or to Comment.nvim?
from nvim-ts-context-commentstring.
I very much appreciate the care you take in user experience!
In order for this to get implemented correctly an aborting mechanism is needed in the pre_hook
(to avoid things like comment string parsing and inserting a change that does nothing in the undo history). Currently there is no such thing in Comment.nvim and its author wouldn't accept a PR to implement it so I'll look into other commenting plugins
Once I come up with a decent solution I'll PR an integration here.
from nvim-ts-context-commentstring.
Related Issues (20)
- Svelte only returns html-comment for all locations
- Need help in debugging strange situation
- Configure without tree-sitter HOT 3
- trouble getting this to work with jsx HOT 3
- mini.comment integration pre hook - require("ts_context_commentstring.internal").update_commentstring() requires 1 argument HOT 5
- Option 'commentstring' is empty in .swift file HOT 3
- Does this work well when nvim-treesitter 1.0 is out? HOT 4
- plugin cause j/k position incorrect HOT 6
- tsx is not properly commenting HOT 8
- Vue comment not working properly HOT 2
- This plugin doesn't set the `comments` var. HOT 2
- BREAKING CHANGES HOT 5
- Incorrent commentstring for inline markdown code blocks HOT 4
- `set ft?` return `filetype=on` and causing losing syntax highlighting HOT 5
- Commenting is slow in large Markdown files HOT 2
- context_commentstring nvim-treesitter module is deprecated HOT 2
- tsx is not properly commenting even when ts-context-commentstring works HOT 1
- Please do not create unnecessary global variables
- Vue comment js/ts using html comment tags HOT 2
- Doesn't work ootb with mini.comment / Comment.nvim (React) HOT 1
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 nvim-ts-context-commentstring.