Giter Site home page Giter Site logo

Comments (14)

rr0gi avatar rr0gi commented on May 19, 2024 1

seconding support for newlines (it is a personal preference matter ofc)
(thanks for the hook code but would be nice to have it builtin support \n in commentstring)

from comment.nvim.

numToStr avatar numToStr commented on May 19, 2024

To be honest, I won't be adding any special support for any specific language. And I don't think new lines in block comments would work like you showed. I tried the following and it worked for me.

ft.c = { '/*%s*/', '#if 0%s#endif' }
-- or
ft.set('c', { '/*%s*/', '#if 0%s#endif' })

If you need the desired result you should add an empty line above and below the code and then do the comments.

2021-10-16.20-34-24.mp4

from comment.nvim.

cryptomilk avatar cryptomilk commented on May 19, 2024

You don't have to support a special language, but allow to set it in the filetype config, like:

ft.set({
    lang = 'c',
    line = { '/*%s*/' },
    block = {
        comment =  '#if 0%s#endif',
        padding = false,
        newline = true,
 })

from comment.nvim.

numToStr avatar numToStr commented on May 19, 2024

I think you are mixing block comments with something else. And your use case is totally different and have nothing to do with comments. I hope you know that.

from comment.nvim.

cryptomilk avatar cryptomilk commented on May 19, 2024

In the end it comes down to insert a newline instead of some padding. It might be that you want that, that you have:

--[[
ft.set('c', { '/*%s*/', '#if 0%s#endif' })
ft.set('cpp', { '/*%s*/', '#if 0%s#endif' })
--]]

Currently you get:

--[[ ft.set('c', { '/*%s*/', '#if 0%s#endif' })
ft.set('cpp', { '/*%s*/', '#if 0%s#endif' }) --]]

from comment.nvim.

numToStr avatar numToStr commented on May 19, 2024

The problem with new lines is that the plugin is now responsible for adding and removing newlines. And honestly, I don't want to deal with newlines for now 😐 .

from comment.nvim.

numToStr avatar numToStr commented on May 19, 2024

To cover your original use case. I made some helpers for you. This is not perfect but it's the best I could do.

local Op = require('Comment.opfunc')
local U = require('Comment.utils')
local A = vim.api

local if_def = '#if 0%s#endif'

function _G.___comment_ifdef(vmode, uncomment)
    local srow, erow = U.get_region(vmode)

    if uncomment then
        A.nvim_buf_set_lines(0, srow - 1, srow, false, { '' })
        A.nvim_buf_set_lines(0, erow - 1, erow, false, { '' })
        return
    end

    local new_srow = srow - 1 -- moving one line upwords
    local new_erow1, new_erow2 = erow + 1, erow + 2 -- moving one line downwards

    A.nvim_buf_set_lines(0, new_srow, new_srow, false, { '' })
    A.nvim_buf_set_lines(0, new_erow1, new_erow1, false, { '' })

    local lines = {
        A.nvim_buf_get_lines(0, new_srow, srow, false)[1],
        A.nvim_buf_get_lines(0, new_erow1, new_erow2, false)[1],
    }

    local lcs, rcs = U.unwrap_cstr(if_def)

    Op.blockwise({
        cfg = { padding = true },
        cmode = U.cmode.comment,
        lcs = lcs,
        rcs = rcs,
        srow = srow,
        erow = new_erow2,
        lines = lines,
    })
end

local opts = { noremap = true, silent = true }

-- This is only for comment. Supports dot-repeat
-- Example: gla{ (comment around curly bracket)
A.nvim_set_keymap('n', 'gl', '<CMD>set operatorfunc=v:lua.___comment_ifdef<CR>g@', opts)

-- Comment/Uncomment in visual mode
A.nvim_set_keymap('x', 'gl', '<ESC><CMD>lua ___comment_ifdef(vim.fn.visualmode(), false)<CR>', opts)
A.nvim_set_keymap('x', 'gL', '<ESC><CMD>lua ___comment_ifdef(vim.fn.visualmode(), true)<CR>', opts)

from comment.nvim.

cryptomilk avatar cryptomilk commented on May 19, 2024

I've implemented it using a post hook now:

local A = vim.api
local C = require('Comment.config'):new()
local U = require('Comment.utils')
local ft = require('Comment.ft')

ft.set('c', {'/*%s*/', '#if 0%s#endif'})
ft.set('cpp', {'/*%s*/', '#if 0%s#endif'})

local post_hook = function(ctx)
    if vim.bo.filetype ~= 'c' and vim.bo.filetype ~= 'cpp' and vim.bo.filetype ~= 'lua' then
        return
    end

    if ctx.range.srow == -1 then
        return
    end

    if ctx.ctype == 1 then
        return
    end

    local cfg = C:get()
    local cstr = ft.get(vim.bo.filetype, ctx.ctype)
    local lcs, rcs = U.unwrap_cstr(cstr)
    local padding = U.get_padding(cfg.padding)
    local lines = A.nvim_buf_get_lines(0, ctx.range.srow - 1, ctx.range.erow, false)

    if ctx.cmode == 1 then
        -- comment
        local str = lines[1]
        local i, j = string.find(str, lcs .. padding)
        lines[1] = string.sub(str, i, j - #padding)
        table.insert(lines, 2, string.sub(str, 0, i - 1) .. string.sub(str, j + #padding, #str))

        str = lines[#lines]
        i, j = string.find(str, rcs)
        lines[#lines] = string.sub(str, 0, i - #padding - 1)
        table.insert(lines, #lines + 1, string.sub(str, i, j))
    elseif ctx.cmode == 2 then
        -- uncomment
        if #lines[1] == 0 and #lines[#lines] == 0 then
            table.remove(lines, 1)
            table.remove(lines, #lines)
        end
    end

    vim.api.nvim_buf_set_lines(0, ctx.range.srow - 1, ctx.range.erow, false, lines)
end

require('Comment').setup({
    post_hook = post_hook,
})

The result looks like:

#if 0
/* main */
int main(void)
{
    return 0;
}
#endif

from comment.nvim.

numToStr avatar numToStr commented on May 19, 2024

Just curious, Why do you need config in post_hook?

from comment.nvim.

cryptomilk avatar cryptomilk commented on May 19, 2024

I thought I need it to local lcs, rcs = U.parse_cstr(cfg, ctx), but after reading the functions, it isn't needed.

from comment.nvim.

numToStr avatar numToStr commented on May 19, 2024

There is also U.unwrap_cstr() that only deals with commentstring. You might wanna use that instead of U.parse_cstr()

from comment.nvim.

cryptomilk avatar cryptomilk commented on May 19, 2024

I already implemented that, see code above ;-)

from comment.nvim.

cryptomilk avatar cryptomilk commented on May 19, 2024

I need access the config to get cfg.padding.

from comment.nvim.

numToStr avatar numToStr commented on May 19, 2024

Just call the following to get the config

require('Comment').get_config()

from comment.nvim.

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.