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

[Feature request] Comment by codes' indent #413

Open
zoumi opened this issue Jan 18, 2020 · 2 comments
Open

[Feature request] Comment by codes' indent #413

zoumi opened this issue Jan 18, 2020 · 2 comments
Labels

Comments

@zoumi
Copy link

zoumi commented Jan 18, 2020

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 zoumi changed the title [Feature request] Comment by codes's indent [Feature request] Comment by codes' indent Jan 18, 2020
@alerque
Copy link
Member

alerque commented Jan 18, 2020

@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.

@zoumi
Copy link
Author

zoumi commented Jan 20, 2020

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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants