Продолжение комментария Vim в конце строки - PullRequest
5 голосов
/ 16 марта 2012

Итак, допустим, у меня есть кусок кода

int i = 0; // Do some junk here<cursor is here>

if(i == 0){
  blahblahblahblah;
}

blahblahblah;

Можно ли сказать vim, что когда я нажимаю клавишу ввода, я хочу, чтобы это приводило к следующему:

int i = 0; // Do some junk here
           // <cursor is here>

if(i == 0){
  blahblahblahblah;
}

blahblahblah;

Я знаю, что это будет сделано для комментария, который находится на одной линии, но я не могу понять, таким образом.

1 Ответ

4 голосов
/ 16 марта 2012

Я не знаю, есть ли для этого плагин (но может быть один), но следующее отображение должно помочь при добавлении строки, нажав клавишу ввода (хотя есть гораздо больше способов добавить строку):

" Function that adds new line starting with comment symbol if line does not 
" start with comment, but contains it.
function! s:NewLine(comsymb)
    let line=getline('.')
    " Check whether we are in comment. Assumes syntax highlighting is working 
    " correctly. Remove these lines if you never write “//” in a string literal
    if empty(filter(synstack(line('.'), min([col('.'), col('$')-1])),
                \   'stridx(tolower(synIDattr(v:val, "name")), "comment")!=-1'))
        return "\n"
    endif
    let cidx=stridx(line, a:comsymb)
    if cidx==-1
        " No comments
        return "\n"
    elseif cidx==0 || line[:(cidx-1)]!~#'\S'
        " This assumes that vim own continuation works correctly: do not do work 
        " that can be done by something else
        return "\n"
    endif
    " Preserve tab indentation if any, correctly replace non-indent tabs with 
    " spaces
    let nextline=substitute(line[:(cidx-1)], '\v^(\s*)(\S.*)$',
                \           '\=submatch(1).'.
                \             'repeat(" ", strdisplaywidth(submatch(2), '.
                \                                          indent('.').'))',
                \           'g').a:comsymb
    " Preserve presence of a space after comment start mark
    if line[cidx+len(a:comsymb)] is# ' '
        let nextline.=' '
    endif
    return "\n".((col('.')<col('$'))?("\e\"_c0"):("\<C-\>\<C-o>\"_d0")).nextline
endfunction

inoremap <expr> <CR> <SID>NewLine('//')
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...