如何基于花括号在vim中定义缩进? [英] How do I define indents in vim based on curly braces?

查看:148
本文介绍了如何基于花括号在vim中定义缩进?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 https://github.com/cakebaker/scss-syntax.vim用于在vim上突出显示 SCSS (或 SASS )文件,这对于语法突出显示非常有效.但是,该插件没有附带缩进文件,因此无法编写一个缩进文件.

I use https://github.com/cakebaker/scss-syntax.vim for syntax highlighting SCSS (or SASS) files on vim, which works very well for syntax highlighting. However, the plugin does not come with an indent file and am having trouble writing one.

我想将缩进设置如下:

但是,如果我执行gg=G,我会得到:

However, if i do gg=G, I get:

我怀疑它不理解基于花括号的嵌套缩进.我尝试了

I suspect that it does not understand nested indent based on braces. I tried all the different combinations of

set cindent

set nocindent

set autoindent

set smartindent

并尝试使用 set tabstop=2

set shiftwidth=2

set expandtab

...但是嵌套大括号缩进似乎无法正常工作.

...but nested braces indent never seems to work.

我相信我可能想编写一个自定义的缩进文件,而我所需要的只是基于带有嵌套级别的花括号的缩进.我应该怎么做呢?如果某人有一个语法相似的文件类型的缩进文件,那也很好.

I believe that I might want to write a custom indent file, and all I need is indentation based on braces with nested levels. How should I go about this? If someone has an indentation file for filetypes with similar syntax, that will be great as well.

推荐答案

这是一个快速的技巧,基于内置的perl缩进代码(在indent/perl.vim中).希望您可以使用它来完成您想做的事情.有关更多详细信息,请参见perl缩进代码或indent目录中的另一个文件中的更详细的注释.

This is a quick hack, based on the built-in perl indentation code (in indent/perl.vim). Hopefully you can use it to get what you want to do. See the more detailed comments in either the perl indentation code or another one of the files in the indent directory for more details.

setlocal indentexpr=GetMyIndent()
function! GetMyIndent()
    let cline = getline(v:lnum)

    " Find a non-blank line above the current line.
    let lnum = prevnonblank(v:lnum - 1)
    " Hit the start of the file, use zero indent.
    if lnum == 0
        return 0
    endif
    let line = getline(lnum)
    let ind = indent(lnum)

    " Indent blocks enclosed by {}, (), or []
    " Find a real opening brace
    let bracepos = match(line, '[(){}\[\]]', matchend(line, '^\s*[)}\]]'))
    while bracepos != -1
        let brace = strpart(line, bracepos, 1)
        if brace == '(' || brace == '{' || brace == '['
            let ind = ind + &sw
        else
            let ind = ind - &sw
        endif
        let bracepos = match(line, '[(){}\[\]]', bracepos + 1)
    endwhile
    let bracepos = matchend(cline, '^\s*[)}\]]')
    if bracepos != -1
        let ind = ind - &sw
    endif

    return ind
endfunction

将该文件另存为~/.vim/indent/something.vim,其中something是您的文件类型(如果在Windows上,请将~/.vim替换为vimfiles的路径.

Save that file as ~/.vim/indent/something.vim where something is your file type (replace ~/.vim with the path to vimfiles if you're on Windows.

您可能还希望将其粘贴在文件的开头(但前提是没有其他可能首先加载的缩进声明):

You might also want to stick this at the start of the file (but only if there isn't some other indent declaration that might be loaded first):

" Only load this indent file when no other was loaded.
if exists("b:did_indent")
    finish
endif
let b:did_indent = 1

这篇关于如何基于花括号在vim中定义缩进?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆