Как игнорировать пробел после комментариев при расчете уровня отступа в Vim

Рассмотрите возможность написания комментария в стиле JavaDoc, который включает список с отступом (когда установлено expandtab и softtabstop=2):

/**
 * First line:
 *   - Indented text
 */

В настоящее время после ввода First line: и нажатия return Vim правильно вставит *<space>. Однако, когда я нажму tab, чтобы сделать отступ во второй строке, будет вставлен только один пробел вместо двух.

Можно ли это исправить, чтобы пробел после * при расчете отступа игнорировался?


person David Wolever    schedule 21.03.2012    source источник
comment
Поскольку вы установили вкладку на 2 пробела, будет учитываться первый. Я предлагаю вам взять текущие настройки, а затем добавить один пробел. Нажмите Enter, вы должны остановиться в нужной позиции (* + 3 пробела).   -  person David.Chu.ca    schedule 12.04.2012


Ответы (1)


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

function AdjustSoftTabStop()
    " Only act if we are in a /* */ comment region
    if match(getline('.'), '\s*\*') == 0
        " Compensate for switching out of insert mode sometimes removing lone
        " final space
        if match(getline('.'), '\*$') != -1
            " Put back in the space that was removed
            substitute/\*$/\* /
            " Adjust position of the cursor accordingly
            normal l
        endif
        " Temporary new value for softtabstop; use the currect column like a
        " base to extend off of the normal distance
        let &softtabstop+=col('.')
    endif
endfunction

function ResetSoftTabStop()
    " Note that you will want to change this if you do not like your tabstop
    " and softtabstop equal.
    let &softtabstop=&tabstop
endfunction

" Create mapping to call the function when <TAB> is pressed. Note that because
" this is mapped with inoremap (mapping in insert mode disallowing remapping of
" character on the RHS), it does not result in infinite recursion.
inoremap <TAB> <ESC>:call AdjustSoftTabStop()<CR>a<TAB><ESC>:call ResetSoftTabStop()<CR>a
person Keith Pinson    schedule 05.09.2012
comment
Жаль, что Prettify не поддерживает VimScript. - person Keith Pinson; 05.09.2012