Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor utils.get_lines #73

Merged
merged 2 commits into from
Nov 20, 2021
Merged

chore: refactor utils.get_lines #73

merged 2 commits into from
Nov 20, 2021

Conversation

numToStr
Copy link
Owner

@numToStr numToStr commented Nov 20, 2021

Resolves #70

Now you can do this

require('Comment').setup({
    pre_hook = function(ctx)
        local lines = require('Comment.utils').get_lines(ctx.range)
        local srow = ctx.range.erow
        vim.api.nvim_buf_set_lines(0, srow, srow, false, lines)
    end,
    post_hook = function(ctx)
        local U = require('Comment.utils')
        local srow = ctx.range.erow
        local erow = srow + 1
        local line = U.get_lines({ srow = srow, erow = erow })
        local _, col = U.grab_indent(line[1])
        vim.api.nvim_win_set_cursor(0, { erow, col })
    end,
})
2021-11-20.13-20-26.mp4

@numToStr
Copy link
Owner Author

@garymh You might wanna try this :)

@numToStr
Copy link
Owner Author

NOTE: The only downside is this duplication will happen every time. But you can determine when to do this by using ctx argument

@garymh
Copy link

garymh commented Nov 20, 2021

@numToStr Thanks so much!! 😃

@numToStr
Copy link
Owner Author

I suppose this is what you wanted 🙂.

@numToStr numToStr merged commit fac23e7 into master Nov 20, 2021
@numToStr numToStr deleted the chore/get-lines branch November 20, 2021 16:00
@garymh
Copy link

garymh commented Nov 20, 2021

It works great! I'm a little confused by the ctx portion, though. Can you pass a different cmotion or cmode with the API? I ideally wanted to be able to define a command like gdc that would do it.

@numToStr
Copy link
Owner Author

numToStr commented Nov 20, 2021

Ahh, I was also going to suggest making a function for yourself. And yes you can definitely make one for yourself. This plugin is basically a bunch of functions smushed together. So following will work for you

local C = require('Comment')
local U = require('Comment.utils')
local Op = require('Comment.opfunc')
local A = vim.api
local opt = { silent = true, noremap = true }

function _G.___gdc(vmode)
    local range = U.get_region(vmode)
    local cfg = C.get_config()
    local ctx = {
        cmode = U.cmode.comment, -- Always comment the line
        cmotion = U.cmotion.line, -- Line action `gy2j`
        ctype = U.ctype.line, -- Use line style comment
        range = range,
    }
    local lcs, rcs = U.parse_cstr(cfg, ctx)
    local lines = U.get_lines(range)

    -- Copying the block
    local srow = ctx.range.erow
    A.nvim_buf_set_lines(0, srow, srow, false, lines)

    -- Doing the comment
    Op.linewise({
        cfg = cfg,
        cmode = ctx.cmode,
        lines = lines,
        lcs = lcs,
        rcs = rcs,
        range = range,
    })

    -- Move the cursor
    local erow = srow + 1
    local line = U.get_lines({ srow = srow, erow = erow })
    local _, col = U.grab_indent(line[1])
    A.nvim_win_set_cursor(0, { erow, col })
end

A.nvim_set_keymap('x', 'gy', '<ESC><CMD>lua ___gdc(vim.fn.visualmode())<CR>', opt)
A.nvim_set_keymap('n', 'gy', '<CMD>set operatorfunc=v:lua.___gdc<CR>g@', opt)

@garymh
Copy link

garymh commented Nov 20, 2021

Works absolutely perfectly! Thanks so much!!!

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.

Feature: Add "duplicate" (yank, comment, paste) function?
2 participants