Skip to content

Support neorg format#129

Closed
n-shift wants to merge 1 commit intonumToStr:masterfrom
n-shift:neorg
Closed

Support neorg format#129
n-shift wants to merge 1 commit intonumToStr:masterfrom
n-shift:neorg

Conversation

@n-shift
Copy link
Copy Markdown

@n-shift n-shift commented Mar 12, 2022

No description provided.

Comment thread lua/Comment/ft.lua
make = { M.hash },
mbsyncrc = { M.double_hash },
meson = { M.hash },
norg = { "+%s+" }, -- blockwise comments work unexpected
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by blockwise comments work unexpected?

Copy link
Copy Markdown
Author

@n-shift n-shift Mar 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neorg does not support blockwise comments you are used for, instead there's this kind of syntax:

@comment
blockwise comment, you can't nest \@comment on one line
@end

or

#comment
This is a comment

after a blank line it is not a comment

this syntax would not work

+
comment
+

I think that creator of this plugin could answer better, @vhyrro

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I've been summoned :P

Yeah Neorg's syntax doesn't have any true blockwise structure which will probably make it difficult to fully support, only the aforementioned:

@comment
comment contents
@end

Is a valid multiline (or block) comment.

This is invalid, therefore I don't think it'd work with Comment.nvim's implementation:

@comment my comment @end

The + modifiers have strict rules, in that:

+
this
+

and

+ this +

are not valid in Neorg. That probably puts them out of the question if you want to add blockwise support. I dunno if there's any proper way to do this 🤔

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this + comment + (spaces after/before +) is invalid, in that case you have to disable padding option to make it +comment+.

Also, i've read the docs and I am assuming that norg file is only be usable with neorg plugin. Am I missing something?

@numToStr
Copy link
Copy Markdown
Owner

I played a little bit with norg comments trying to find the best way to do commenting in norg files. And I, kinda, found a way to do it with padding and post_hook magic.

  • With a841f73 you can do dynamic padding, which should be enough for linewise commenting using +%s+.
  • But for blockwise comments using @comment%s@comment, you need post_hook.

So, I came up with the following which works for the most part, there might a bug here and there.

local A = vim.api
local U = require('Comment.utils')
local F = require('Comment.ft')

F.set('norg', { '+%s+', '@comment%s@comment' })

require('Comment').setup({
    padding = function()
        -- NOTE: this is important for linewise comment
        return vim.bo.filetype ~= 'norg'
    end,
    post_hook = function(ctx)
        if vim.bo.filetype == 'norg' and ctx.ctype == U.ctype.block then
            if ctx.cmode == U.cmode.comment then
                -- Getting the first and the last line of the commented block
                -- NOTE: this will also include the comment chars
                local first_line = A.nvim_buf_get_lines(0, ctx.range.srow - 1, ctx.range.srow, true)[1]
                local last_line = A.nvim_buf_get_lines(0, ctx.range.erow - 1, ctx.range.erow, true)[1]

                -- Separting the comment chars from the lines, so that we can set them properly
                local s_cstr, e_cstr = U.unwrap_cstr(F.calculate(ctx))
                local s_pad, s_line = first_line:match('(%s*)' .. vim.pesc(s_cstr) .. '(.*)')
                local e_pad, e_line = last_line:match('(%s*)(.-)' .. vim.pesc(e_cstr))

                -- Finally, set the comment and and line into mutliple lines
                A.nvim_buf_set_lines(0, ctx.range.erow - 1, ctx.range.erow, true, {
                    e_pad .. e_line,
                    e_pad .. e_cstr,
                })
                A.nvim_buf_set_lines(0, ctx.range.srow - 1, ctx.range.srow, true, {
                    s_pad .. s_cstr,
                    s_pad .. s_line,
                })
            else
                -- We created the new lines, so we should also delete them
                A.nvim_buf_set_lines(0, ctx.range.erow - 1, ctx.range.erow, true, {})
                A.nvim_buf_set_lines(0, ctx.range.srow - 1, ctx.range.srow, true, {})
            end
        end
    end,
})

See for yourself :)

2022-03-17.12-51-34.mp4

@numToStr
Copy link
Copy Markdown
Owner

I am going to close this as the linewise commentstring i.e +%s+ won't work without the padding configuration. If anyone wants norg support they can copy the above snippets which also handle block comments i.e @comment%s@comment.

@numToStr numToStr closed this Mar 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants