如何在 vim 中基于大括号定义缩进?

发布于 2024-10-15 02:04:43 字数 1032 浏览 1 评论 0原文

我使用 https://github.com/cakebaker/scss-syntax.vim vim 上的语法高亮 SCSS (或 SASS)文件,这对于语法高亮非常有效。但是,该插件没有附带缩进文件,并且我在编写缩进文件时遇到了麻烦。

我想将缩进设置为如下所示:

在此处输入图像描述

但是,如果我这样做 gg= G,我得到:

在此处输入图像描述

我怀疑它不理解基于大括号的嵌套缩进。 的所有不同组合

我尝试了set cindent

set nocindent

set autoindent

set smartindent

,并尝试使用以下代码Tab 键 == 4 个空格和 auto -Vim 中大括号后缩进 ,包括

set tabstop=2

set shiftwidth=2

set Expandtab

...但嵌套大括号缩进似乎永远不起作用。

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

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.

I would like to set the indent to look like this:

enter image description here

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

enter image description here

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

and tried to use the code from Tab key == 4 spaces and auto-indent after curly braces in Vim , including

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷爱 2024-10-22 02:04:43

这是一个基于内置 perl 缩进代码(在 indent/perl.vim 中)的快速破解。希望你能用它来实现你想做的事。有关更多详细信息,请参阅 perl 缩进代码或缩进​​目录中的另一个文件中的更详细注释。

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 是您的文件类型(将 ~/.vim 替换为路径如果您在 Windows 上,

您可能还想将其粘贴到 vimfiles 中(但前提是没有可能首先加载的其他缩进声明):

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

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文