File: mlir.vim

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (75 lines) | stat: -rw-r--r-- 2,144 bytes parent folder | download | duplicates (23)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
" Vim indent file
" Language:   mlir
" Maintainer: The MLIR team
" Adapted from the LLVM vim indent file
" What this indent plugin currently does:
"  - If no other rule matches copy indent from previous non-empty,
"    non-commented line.
"  - On '}' align the same as the line containing the matching '{'.
"  - If previous line starts with a block label, increase indentation.
"  - If the current line is a block label and ends with ':' indent at the same
"    level as the enclosing '{'/'}' block.
" Stuff that would be nice to add:
"  - Continue comments on next line.
"  - If there is an opening+unclosed parenthesis on previous line indent to
"    that.
if exists("b:did_indent")
  finish
endif
let b:did_indent = 1

setlocal shiftwidth=2 expandtab

setlocal indentkeys=0{,0},<:>,!^F,o,O,e
setlocal indentexpr=GetMLIRIndent()

if exists("*GetMLIRIndent")
  finish
endif

function! FindOpenBrace(lnum)
  call cursor(a:lnum, 1)
  return searchpair('{', '', '}', 'bW')
endfun

function! GetMLIRIndent()
  " On '}' align the same as the line containing the matching '{'
  let thisline = getline(v:lnum)
  if thisline =~ '^\s*}'
    call cursor(v:lnum, 1)
    silent normal %
    let opening_lnum = line('.')
    if opening_lnum != v:lnum
      return indent(opening_lnum)
    endif
  endif

  " Indent labels the same as the current opening block
  if thisline =~ '\^\h\+.*:\s*$'
    let blockbegin = FindOpenBrace(v:lnum)
    if blockbegin > 0
      return indent(blockbegin)
    endif
  endif

  " Find a non-blank not-completely commented line above the current line.
  let prev_lnum = prevnonblank(v:lnum - 1)
  while prev_lnum > 0 && synIDattr(synID(prev_lnum, 1 + indent(prev_lnum), 0), "name") == "mlirComment"
    let prev_lnum = prevnonblank(prev_lnum-1)
  endwhile
  " Hit the start of the file, use zero indent.
  if prev_lnum == 0
    return 0
  endif

  let ind = indent(prev_lnum)
  let prevline = getline(prev_lnum)

  " Add a 'shiftwidth' after lines that start a function, block/labels, or a
  " region.
  if prevline =~ '{\s*$' || prevline =~ '\^\h\+.*:\s*$'
    let ind = ind + &shiftwidth
  endif

  return ind
endfunction