github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/mlir/utils/vim/indent/mlir.vim (about)

     1  " Vim indent file
     2  " Language:   mlir
     3  " Maintainer: The MLIR team
     4  " Adapted from the LLVM vim indent file
     5  " What this indent plugin currently does:
     6  "  - If no other rule matches copy indent from previous non-empty,
     7  "    non-commented line.
     8  "  - On '}' align the same as the line containing the matching '{'.
     9  "  - If previous line starts with a block label, increase indentation.
    10  "  - If the current line is a block label and ends with ':' indent at the same
    11  "    level as the enclosing '{'/'}' block.
    12  " Stuff that would be nice to add:
    13  "  - Continue comments on next line.
    14  "  - If there is an opening+unclosed parenthesis on previous line indent to
    15  "    that.
    16  if exists("b:did_indent")
    17    finish
    18  endif
    19  let b:did_indent = 1
    20  
    21  setlocal shiftwidth=2 expandtab
    22  
    23  setlocal indentkeys=0{,0},<:>,!^F,o,O,e
    24  setlocal indentexpr=GetMLIRIndent()
    25  
    26  if exists("*GetMLIRIndent")
    27    finish
    28  endif
    29  
    30  function! FindOpenBrace(lnum)
    31    call cursor(a:lnum, 1)
    32    return searchpair('{', '', '}', 'bW')
    33  endfun
    34  
    35  function! GetMLIRIndent()
    36    " On '}' align the same as the line containing the matching '{'
    37    let thisline = getline(v:lnum)
    38    if thisline =~ '^\s*}'
    39      call cursor(v:lnum, 1)
    40      silent normal %
    41      let opening_lnum = line('.')
    42      if opening_lnum != v:lnum
    43        return indent(opening_lnum)
    44      endif
    45    endif
    46  
    47    " Indent labels the same as the current opening block
    48    if thisline =~ '\^\h\+.*:\s*$'
    49      let blockbegin = FindOpenBrace(v:lnum)
    50      if blockbegin > 0
    51        return indent(blockbegin)
    52      endif
    53    endif
    54  
    55    " Find a non-blank not-completely commented line above the current line.
    56    let prev_lnum = prevnonblank(v:lnum - 1)
    57    while prev_lnum > 0 && synIDattr(synID(prev_lnum, 1 + indent(prev_lnum), 0), "name") == "mlirComment"
    58      let prev_lnum = prevnonblank(prev_lnum-1)
    59    endwhile
    60    " Hit the start of the file, use zero indent.
    61    if prev_lnum == 0
    62      return 0
    63    endif
    64  
    65    let ind = indent(prev_lnum)
    66    let prevline = getline(prev_lnum)
    67  
    68    " Add a 'shiftwidth' after lines that start a function, block/labels, or a
    69    " region.
    70    if prevline =~ '{\s*$' || prevline =~ '\^\h\+.*:\s*$'
    71      let ind = ind + &shiftwidth
    72    endif
    73  
    74    return ind
    75  endfunction