p00f / cphelper.nvim

Neovim helper for competitive programming. Use https://sr.ht/~p00f/cphelper.nvim instead
MIT License
130 stars 9 forks source link

Add code template capabilty on `:CphRecieve` #33

Closed vishalpaudel closed 1 year ago

vishalpaudel commented 1 year ago

The feature of adding snippets, although usually external, can be added to the plugin. The feature would be cool, I think. I use the following snippet inside prepare.lua for copying ~/temp.cpp on :CphRecieve:

-- Creates the folder for the problem: contests_dir/judge/contest/problem
--- @param problem string #The name of the problem
--- @param group string #Group, in the format "Judge - Contest"
--- @return Path #The problem dir
function M.prepare_folders(problem, group)

...

    local file1 = "~/temp.cpp"
    local file2 = problem_dir:joinpath("solution." .. extension):absolute()

    -- Check if file2 exists and has content
    local f2 = io.open(file2, 'r')
    if f2 then
        local content = f2:read('*a')
        f2:close()

        -- Check if file2 is empty
        if content and content ~= '' then
            print('File2 is not empty. Skipping copy operation.')
        else
            -- Open file1 in read mode
            local f1 = io.open(file1, 'r')
            if not f1 then
                error('Failed to open file1')
            end

            -- Read the contents of file1
            local content = f1:read('*a')
            f1:close()

            -- Open file2 in write mode
            local f2 = io.open(file2, 'w')
            if not f2 then
                error('Failed to open file2')
            end

            -- Write the contents of file1 to file2
            f2:write(content)
            f2:close()

            print('File copied successfully.')
        end
    else
        -- If file2 doesn't exist, create it and copy the contents of file1
        local f1 = io.open(file1, 'r')
        if not f1 then
            error('Failed to open file1')
        end

        local content = f1:read('*a')
        f1:close()

        local f2 = io.open(file2, 'w')
        if not f2 then
            error('Failed to create file2')
        end

        f2:write(content)
        f2:close()

        print('File copied successfully.')
    end

    -- Open file2 in Neovim for editing
    vim.api.nvim_command('edit ' .. file2)
end

return M
vishalpaudel commented 1 year ago

competitest.nvim also has this feature.

p00f commented 1 year ago

Sorry, this is out of scope - you can use https://github.com/mattn/vim-sonictemplate (which I contributed cc templates to)

p00f commented 1 year ago

It's not that I don't like it, it's just added complexity which solves a problem already solved by other plugins

vishalpaudel commented 1 year ago

Thanks! I understand that it makes it complex. Are there features to be added in the future?

p00f commented 1 year ago

If you can open a PR which does the template part in a separate file (without doing too much in prepare.lua itself), I can take a look

vishalpaudel commented 1 year ago

No, even I am motivated to use vim-sonictemplate now that I see the ugliness in adding that capability.

Are there other places and capabilities that I can try contributing towards?

vishalpaudel commented 1 year ago

For example, I was thinking of somehow incorporating the plugin with things like harwest.

p00f commented 1 year ago

harwest

Oh I thought CF blocked api submissions? Happy to merge a PR for this

p00f commented 1 year ago

There's https://pypi.org/project/pynvim/ for python - neovim interaction if you want to implement harwest