neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

cdl.vim (4366B)


      1 " Description:	Comshare Dimension Definition Language (CDL)
      2 " Maintainer:	Raul Segura Acevedo <raulseguraaceved@netscape.net> (Invalid email address)
      3 " 		Doug Kearns <dougkearns@gmail.com>
      4 " Last Change:	2022 Apr 06
      5 
      6 if exists("b:did_indent")
      7    "finish
      8 endif
      9 let b:did_indent = 1
     10 
     11 setlocal indentexpr=CdlGetIndent(v:lnum)
     12 setlocal indentkeys&
     13 setlocal indentkeys+==~else,=~endif,=~then,;,),=
     14 
     15 let b:undo_indent = "setl inde< indk<"
     16 
     17 " Only define the function once.
     18 if exists("*CdlGetIndent")
     19    "finish
     20 endif
     21 
     22 " find out if an "...=..." expression is an assignment (or a conditional)
     23 " it scans 'line' first, and then the previous lines
     24 fun! CdlAssignment(lnum, line)
     25  let f = -1
     26  let lnum = a:lnum
     27  let line = a:line
     28  while lnum > 0 && f == -1
     29    " line without members [a] of [b]:[c]...
     30    let inicio = 0
     31    while 1
     32      " keywords that help to decide
     33      let inicio = matchend(line, '\c\<\(expr\|\a*if\|and\|or\|not\|else\|then\|memberis\|\k\+of\)\>\|[<>;]', inicio)
     34      if inicio < 0
     35 break
     36      endif
     37      " it's formula if there's a ';', 'elsE', 'theN', 'enDif' or 'expr'
     38      " conditional if there's a '<', '>', 'elseif', 'if', 'and', 'or', 'not',
     39      " 'memberis', 'childrenof' and other \k\+of functions
     40      let f = line[inicio-1] =~? '[en;]' || strpart(line, inicio-4, 4) =~? 'ndif\|expr'
     41    endw
     42    let lnum = prevnonblank(lnum-1)
     43    let line = substitute(getline(lnum), '\c\(\[[^]]*]\(\s*of\s*\|:\)*\)\+', ' ', 'g')
     44  endw
     45  " if we hit the start of the file then f = -1, return 1 (formula)
     46  return f != 0
     47 endf
     48 
     49 fun! CdlGetIndent(lnum)
     50  let thisline = getline(a:lnum)
     51  if match(thisline, '^\s*\(\k\+\|\[[^]]*]\)\s*\(,\|;\s*$\)') >= 0
     52    " it's an attributes line
     53    return shiftwidth()
     54  elseif match(thisline, '^\c\s*\([{}]\|\/[*/]\|dimension\|schedule\|group\|hierarchy\|class\)') >= 0
     55    " it's a header or '{' or '}' or a comment
     56    return 0
     57  end
     58 
     59  let lnum = prevnonblank(a:lnum-1)
     60  " Hit the start of the file, use zero indent.
     61  if lnum == 0
     62    return 0
     63  endif
     64 
     65  " PREVIOUS LINE
     66  let ind = indent(lnum)
     67  let line = getline(lnum)
     68 
     69  " Whether a '=' is a conditional or an assignment. -1 means we don't know
     70  " yet.
     71  " One 'closing' element at the beginning of the line has already reduced the
     72  " indent, but 'else', 'elseif' & 'then' increment it for the next line.
     73  " '=' at the beginning already has the right indent (increased for
     74  " asignments).
     75  let f = -1
     76  let inicio = matchend(line, '^\c\s*\(else\a*\|then\|endif\|/[*/]\|[);={]\)')
     77  if inicio > 0
     78    let c = line[inicio-1]
     79    " ')' and '=' don't change indent and are useless to set 'f'
     80    if c == '{'
     81      return shiftwidth()
     82    elseif c != ')' && c != '='
     83      let f = 1 " all but 'elseif' are followed by a formula
     84      if c ==? 'n' || c ==? 'e' " 'then', 'else'
     85 let ind = ind + shiftwidth()
     86      elseif strpart(line, inicio-6, 6) ==? 'elseif' " elseif, set f to conditional
     87 let ind = ind + shiftwidth()
     88 let f = 0
     89      end
     90    end
     91  end
     92 
     93  " remove members [a] of [b]:[c]... (inicio remains valid)
     94  let line = substitute(line, '\c\(\[[^]]*]\(\s*of\s*\|:\)*\)\+', ' ', 'g')
     95  while 1
     96    " search for the next interesting element
     97    let inicio=matchend(line, '\c\<if\|endif\|[()=;]', inicio)
     98    if inicio < 0
     99      break
    100    end
    101 
    102    let c = line[inicio-1]
    103    " 'expr(...)' containing the formula
    104    if strpart(line, inicio-5, 5) ==? 'expr('
    105      let ind = 0
    106      let f = 1
    107    elseif c == ')' || c== ';' || strpart(line, inicio-5, 5) ==? 'endif'
    108      let ind = ind - shiftwidth()
    109    elseif c == '(' || c ==? 'f' " '(' or 'if'
    110      let ind = ind + shiftwidth()
    111    else " c == '='
    112      " if it is an assignment increase indent
    113      if f == -1 " we don't know yet, find out
    114 let f = CdlAssignment(lnum, strpart(line, 0, inicio))
    115      end
    116      if f == 1 " formula increase it
    117 let ind = ind + shiftwidth()
    118      end
    119    end
    120  endw
    121 
    122  " CURRENT LINE, if it starts with a closing element, decrease indent
    123  " or if it starts with '=' (assignment), increase indent
    124  if match(thisline, '^\c\s*\(else\|then\|endif\|[);]\)') >= 0
    125    let ind = ind - shiftwidth()
    126  elseif match(thisline, '^\s*=') >= 0
    127    if f == -1 " we don't know yet if is an assignment, find out
    128      let f = CdlAssignment(lnum, "")
    129    end
    130    if f == 1 " formula increase it
    131      let ind = ind + shiftwidth()
    132    end
    133  end
    134 
    135  return ind
    136 endfun