如何在计算Vim缩进级别时忽略注释后的空格

5

考虑编写一个JavaDoc风格的注释,其中包括一个缩进列表(当expandtab设置为truesofttabstop=2时):

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

目前,在键入 First line: 并按下 return 后,Vim 将正确插入 *<space>。然而,当我按下 tab 缩进第二行时,只会插入一个空格而不是两个。

是否可能修复这个问题,使得缩进计算过程中忽略 * 后面的空格?


由于您将制表符设置为2个空格,因此它将考虑第一个空格。我建议您采用当前设置,然后再添加一个空格。按Enter键,您应该停在所需位置(* + 3个空格)。 - David.Chu.ca
1个回答

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

1
很遗憾,Prettify不支持VimScript。 - Keith Pinson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接