robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
ab20681c52
master
1"------------------------------------------------------------------------------ 2" Description: Vim Ada indent file 3" Language: Ada (2005) 4" $Id: ada.vim 887 2008-07-08 14:29:01Z krischik $ 5" Copyright: Copyright (C) 2006 Martin Krischik 6" Maintainer: Martin Krischik <krischik@users.sourceforge.net> 7" Neil Bird <neil@fnxweb.com> 8" Ned Okie <nokie@radford.edu> 9" $Author: krischik $ 10" $Date: 2008-07-08 16:29:01 +0200 (Di, 08 Jul 2008) $ 11" Version: 4.6 12" $Revision: 887 $ 13" $HeadURL: https://gnuada.svn.sourceforge.net/svnroot/gnuada/trunk/tools/vim/indent/ada.vim $ 14" History: 24.05.2006 MK Unified Headers 15" 16.07.2006 MK Ada-Mode as vim-ball 16" 15.10.2006 MK Bram's suggestion for runtime integration 17" 05.11.2006 MK Bram suggested to save on spaces 18" 19.09.2007 NO g: missing before ada#Comment 19" 2022 April: b:undo_indent added by Doug Kearns 20" Help Page: ft-vim-indent 21"------------------------------------------------------------------------------ 22" ToDo: 23" Verify handling of multi-line exprs. and recovery upon the final ';'. 24" Correctly find comments given '"' and "" ==> " syntax. 25" Combine the two large block-indent functions into one? 26"------------------------------------------------------------------------------ 27 28" Only load this indent file when no other was loaded. 29if exists ( "b:did_indent" ) || version < 700 30finish 31endif 32 33let b: did_indent = 45 34 35setlocal indentexpr = GetAdaIndent () 36setlocal indentkeys -= 0{,0} 37setlocal indentkeys += 0=~then,0=~end,0=~elsif,0=~when,0=~exception,0=~begin,0=~is,0=~record 38 39let b: undo_indent = "setl inde< indk<" 40 41" Only define the functions once. 42if exists ( "*GetAdaIndent" ) 43finish 44endif 45let s: keepcpo = & cpo 46set cpo & vim 47 48if exists ( "g:ada_with_gnat_project_files" ) 49let s: AdaBlockStart = '^\s*\(if\>\|while\>\|else\>\|elsif\>\|loop\>\|for\>.*\<\(loop\|use\)\>\|declare\>\|begin\>\|type\>.*\<is\>[^;]*$\|\(type\>.*\)\=\<record\>\|procedure\>\|function\>\|accept\>\|do\>\|task\>\|package\>\|project\>\|then\>\|when\>\|is\>\)' 50else 51let s: AdaBlockStart = '^\s*\(if\>\|while\>\|else\>\|elsif\>\|loop\>\|for\>.*\<\(loop\|use\)\>\|declare\>\|begin\>\|type\>.*\<is\>[^;]*$\|\(type\>.*\)\=\<record\>\|procedure\>\|function\>\|accept\>\|do\>\|task\>\|package\>\|then\>\|when\>\|is\>\)' 52endif 53 54" Section: s:MainBlockIndent {{{1 55" 56" Try to find indent of the block we're in 57" prev_indent = the previous line's indent 58" prev_lnum = previous line (to start looking on) 59" blockstart = expr. that indicates a possible start of this block 60" stop_at = if non-null, if a matching line is found, gives up! 61" No recursive previous block analysis: simply look for a valid line 62" with a lesser or equal indent than we currently (on prev_lnum) have. 63" This shouldn't work as well as it appears to with lines that are currently 64" nowhere near the correct indent (e.g., start of line)! 65" Seems to work OK as it 'starts' with the indent of the /previous/ line. 66function s: MainBlockIndent ( prev_indent , prev_lnum , blockstart , stop_at ) 67let lnum = a: prev_lnum 68let line = substitute ( getline ( lnum ), g: ada#Comment , '' , '' ) 69while lnum > 1 70if a: stop_at != '' && line =~ '^\s*' . a: stop_at && indent ( lnum ) < a: prev_indent 71return a: prev_indent 72elseif line =~ '^\s*' . a: blockstart 73let ind = indent ( lnum ) 74if ind < a: prev_indent 75return ind 76endif 77endif 78 79let lnum = prevnonblank ( lnum - 1 ) 80" Get previous non-blank/non-comment-only line 81while 1 82let line = substitute ( getline ( lnum ), g: ada#Comment , '' , '' ) 83if line !~ '^\s*$' && line !~ '^\s*#' 84break 85endif 86let lnum = prevnonblank ( lnum - 1 ) 87if lnum <= 0 88return a: prev_indent 89endif 90endwhile 91endwhile 92" Fallback - just move back one 93return a: prev_indent - shiftwidth () 94endfunction MainBlockIndent 95 96" Section: s:EndBlockIndent {{{1 97" 98" Try to find indent of the block we're in (and about to complete), 99" including handling of nested blocks. Works on the 'end' of a block. 100" prev_indent = the previous line's indent 101" prev_lnum = previous line (to start looking on) 102" blockstart = expr. that indicates a possible start of this block 103" blockend = expr. that indicates a possible end of this block 104function s: EndBlockIndent ( prev_indent , prev_lnum , blockstart , blockend ) 105let lnum = a: prev_lnum 106let line = getline ( lnum ) 107let ends = 0 108while lnum > 1 109if getline ( lnum ) =~ '^\s*' . a: blockstart 110let ind = indent ( lnum ) 111if ends <= 0 112if ind < a: prev_indent 113return ind 114endif 115else 116let ends = ends - 1 117endif 118elseif getline ( lnum ) =~ '^\s*' . a: blockend 119let ends = ends + 1 120endif 121 122let lnum = prevnonblank ( lnum - 1 ) 123" Get previous non-blank/non-comment-only line 124while 1 125let line = getline ( lnum ) 126let line = substitute ( line , g: ada#Comment , '' , '' ) 127if line !~ '^\s*$' 128break 129endif 130let lnum = prevnonblank ( lnum - 1 ) 131if lnum <= 0 132return a: prev_indent 133endif 134endwhile 135endwhile 136" Fallback - just move back one 137return a: prev_indent - shiftwidth () 138endfunction EndBlockIndent 139 140" Section: s:StatementIndent {{{1 141" 142" Return indent of previous statement-start 143" (after we've indented due to multi-line statements). 144" This time, we start searching on the line *before* the one given (which is 145" the end of a statement - we want the previous beginning). 146function s: StatementIndent ( current_indent , prev_lnum ) 147let lnum = a: prev_lnum 148while lnum > 0 149let prev_lnum = lnum 150let lnum = prevnonblank ( lnum - 1 ) 151" Get previous non-blank/non-comment-only line 152while 1 153let line = substitute ( getline ( lnum ), g: ada#Comment , '' , '' ) 154 155if line !~ '^\s*$' && line !~ '^\s*#' 156break 157endif 158let lnum = prevnonblank ( lnum - 1 ) 159if lnum <= 0 160return a: current_indent 161endif 162endwhile 163" Leave indent alone if our ';' line is part of a ';'-delineated 164" aggregate (e.g., procedure args.) or first line after a block start. 165if line =~ s: AdaBlockStart || line =~ '(\s*$' 166return a: current_indent 167endif 168if line !~ '[.=(]\s*$' 169let ind = indent ( prev_lnum ) 170if ind < a: current_indent 171return ind 172endif 173endif 174endwhile 175" Fallback - just use current one 176return a: current_indent 177endfunction StatementIndent 178 179 180" Section: GetAdaIndent {{{1 181" 182" Find correct indent of a new line based upon what went before 183" 184function GetAdaIndent () 185" Find a non-blank line above the current line. 186let lnum = prevnonblank ( v: lnum - 1 ) 187let ind = indent ( lnum ) 188let package_line = 0 189 190" Get previous non-blank/non-comment-only/non-cpp line 191while 1 192let line = substitute ( getline ( lnum ), g: ada#Comment , '' , '' ) 193if line !~ '^\s*$' && line !~ '^\s*#' 194break 195endif 196let lnum = prevnonblank ( lnum - 1 ) 197if lnum <= 0 198return ind 199endif 200endwhile 201 202" Get default indent (from prev. line) 203let ind = indent ( lnum ) 204let initind = ind 205 206" Now check what's on the previous line 207if line =~ s: AdaBlockStart || line =~ '(\s*$' 208" Check for false matches to AdaBlockStart 209let false_match = 0 210if line =~ '^\s*\(procedure\|function\|package\)\>.*\<is\s*new\>' 211" Generic instantiation 212let false_match = 1 213elseif line =~ ')\s*;\s*$' || line =~ '^\([^(]*([^)]*)\)*[^(]*;\s*$' 214" forward declaration 215let false_match = 1 216endif 217" Move indent in 218if ! false_match 219let ind = ind + shiftwidth () 220endif 221elseif line =~ '^\s*\(case\|exception\)\>' 222" Move indent in twice (next 'when' will move back) 223let ind = ind + 2 * shiftwidth () 224elseif line =~ '^\s*end\s*record\>' 225" Move indent back to tallying 'type' preceding the 'record'. 226" Allow indent to be equal to 'end record's. 227let ind = s: MainBlockIndent ( ind + shiftwidth (), lnum , 'type\>' , '' ) 228elseif line =~ '\(^\s*new\>.*\)\@<!)\s*[;,]\s*$' 229" Revert to indent of line that started this parenthesis pair 230exe lnum 231exe 'normal! $F)%' 232if getline ( '.' ) =~ '^\s*(' 233" Dire layout - use previous indent (could check for g:ada#Comment here) 234let ind = indent ( prevnonblank ( line ( '.' ) - 1 ) ) 235else 236let ind = indent ( '.' ) 237endif 238exe v: lnum 239elseif line =~ '[.=(]\s*$' 240" A statement continuation - move in one 241let ind = ind + shiftwidth () 242elseif line =~ '^\s*new\>' 243" Multiple line generic instantiation ('package blah is\nnew thingy') 244let ind = s: StatementIndent ( ind - shiftwidth (), lnum ) 245elseif line =~ ';\s*$' 246" Statement end (but not 'end' ) - try to find current statement-start indent 247let ind = s: StatementIndent ( ind , lnum ) 248endif 249 250" Check for potential argument list on next line 251let continuation = ( line =~ '[A-Za-z0-9_]\s*$' ) 252 253 254" Check current line; search for simplistic matching start-of-block 255let line = getline ( v: lnum ) 256if line =~ '^\s*#' 257" Start of line for ada-pp 258let ind = 0 259elseif continuation && line =~ '^\s*(' 260" Don't do this if we've already indented due to the previous line 261if ind == initind 262let ind = ind + shiftwidth () 263endif 264elseif line =~ '^\s*\(begin\|is\)\>' 265let ind = s: MainBlockIndent ( ind , lnum , '\(procedure\|function\|declare\|package\|task\)\>' , 'begin\>' ) 266elseif line =~ '^\s*record\>' 267let ind = s: MainBlockIndent ( ind , lnum , 'type\>\|for\>.*\<use\>' , '' ) + shiftwidth () 268elseif line =~ '^\s*\(else\|elsif\)\>' 269let ind = s: MainBlockIndent ( ind , lnum , 'if\>' , '' ) 270elseif line =~ '^\s*when\>' 271" Align 'when' one /in/ from matching block start 272let ind = s: MainBlockIndent ( ind , lnum , '\(case\|exception\)\>' , '' ) + shiftwidth () 273elseif line =~ '^\s*end\>\s*\<if\>' 274" End of if statements 275let ind = s: EndBlockIndent ( ind , lnum , 'if\>' , 'end\>\s*\<if\>' ) 276elseif line =~ '^\s*end\>\s*\<loop\>' 277" End of loops 278let ind = s: EndBlockIndent ( ind , lnum , '\(\(while\|for\)\>.*\)\?\<loop\>' , 'end\>\s*\<loop\>' ) 279elseif line =~ '^\s*end\>\s*\<record\>' 280" End of records 281let ind = s: EndBlockIndent ( ind , lnum , '\(type\>.*\)\=\<record\>' , 'end\>\s*\<record\>' ) 282elseif line =~ '^\s*end\>\s*\<procedure\>' 283" End of procedures 284let ind = s: EndBlockIndent ( ind , lnum , 'procedure\>.*\<is\>' , 'end\>\s*\<procedure\>' ) 285elseif line =~ '^\s*end\>\s*\<case\>' 286" End of case statement 287let ind = s: EndBlockIndent ( ind , lnum , 'case\>.*\<is\>' , 'end\>\s*\<case\>' ) 288elseif line =~ '^\s*end\>' 289" General case for end 290let ind = s: MainBlockIndent ( ind , lnum , '\(if\|while\|for\|loop\|accept\|begin\|record\|case\|exception\|package\)\>' , '' ) 291elseif line =~ '^\s*exception\>' 292let ind = s: MainBlockIndent ( ind , lnum , 'begin\>' , '' ) 293elseif line =~ '^\s*then\>' 294let ind = s: MainBlockIndent ( ind , lnum , 'if\>' , '' ) 295endif 296 297return ind 298endfunction GetAdaIndent 299 300let & cpo = s: keepcpo 301unlet s: keepcpo 302 303finish " 1}}} 304 305"------------------------------------------------------------------------------ 306" Copyright (C) 2006 Martin Krischik 307" 308" Vim is Charityware - see ":help license" or uganda.txt for licence details. 309"------------------------------------------------------------------------------ 310" vim: textwidth=78 wrap tabstop=8 shiftwidth=3 softtabstop=3 noexpandtab 311" vim: foldmethod=marker