nerdcommenter icon indicating copy to clipboard operation
nerdcommenter copied to clipboard

[Feature request] Comment by codes' indent

Open zoumi opened this issue 5 years ago • 2 comments

Assume we have these c codes, and the cursor is at the first line(normal mode)

void UartTxArr(uint8_t *arr,uint8_t len)
{
    uint8_t i=0;
    for(i=0;i<len;i++)
    {
        UartTxChar(*arr);
        arr++;
    }
}

after we trigger an "indent comment",we get

/*
void UartTxArr(uint8_t *arr,uint8_t len)
{
    uint8_t i=0;
    for(i=0;i<len;i++)
    {
        UartTxChar(*arr);
        arr++;
    }
}
*/

zoumi avatar Jan 18 '20 02:01 zoumi

@zoumi I'm not sure that "indent" is actually what you are after here. Indent in particular is a very difficult thing the quantify in most language. It depends on your coding style of course, but I wondering if even your example wouldn't be better off with a different selection method. Have you considered using that the example you give would actually work fine with the existing paragraph text object and even play very nicely with a function closure based text object. For example I use kana/vim-textobj-function and haya14busa/vim-textobj-function-syntax plugins and it is very easy for me to select and comment out and function that I'm inside of.

I'm happy to consider merging any PR that contributes such an indent feature that works reasonably well, but I will likely not be coding such a thing up myself.

alerque avatar Jan 18 '20 09:01 alerque

I have write a pieces of code,but I don't know how to pass the line start and end to nerdcommenter.

func! s:GetIndent(str)
    let l:cur_indent = ""
    let l:cur_indent_end=matchend(a:str,'^\s*\S')
    if l:cur_indent_end==-1
        return -1
    endif
    echo l:cur_indent_end
    if l:cur_indent_end>1
        return strpart(a:str,0,l:cur_indent_end-1)
        "return a:str[0:l:cur_indent_end-1]
    endif
    return l:cur_indent
endfunc

func! IndentComment()
    let l:first_line_num=line('.')
    let l:first_indent=s:GetIndent(getline('.'))
    if l:first_indent==-1
        let l:first_indent=''
    endif
    let l:differ_indent_found=0
    let l:line_num=l:first_line_num+1
    let l:line_content = getline(l:line_num)
    let l:total_line =  line('$')
    while l:line_num < l:total_line
        let l:indent=s:GetIndent(l:line_content)
        if l:indent!=-1
            if l:indent != l:first_indent
                let l:differ_indent_found=1
            else
                if l:differ_indent_found==1
                    "find finished here
                    let l:end_line_num= l:line_num
                    echomsg l:line_num
                    "how to do here
                    "the code between first_line_num and end_line_num should be comment out
                    call NERDComment(1,'sexy')<CR>
                    return
                endif
            endif
        endif
        let l:line_num=l:line_num+1
        let l:line_content = getline(l:line_num)
    endwhile
endfunc

nmap <Leader>ic :call IndentComment()<CR>

zoumi avatar Jan 20 '20 03:01 zoumi