robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
ab20681c52
master
1" Vim indent file 2" Language: Clojure 3" Maintainer: Alex Vear <alex@vear.uk> 4" Former Maintainers: Sung Pae <self@sungpae.com> 5" Meikel Brandmeyer <mb@kotka.de> 6" URL: https://github.com/clojure-vim/clojure.vim 7" License: Vim (see :h license) 8" Last Change: 2022-03-24 9 10if exists ( "b:did_indent" ) 11finish 12endif 13let b: did_indent = 1 14 15let s: save_cpo = & cpo 16set cpo & vim 17 18let b: undo_indent = 'setlocal autoindent< smartindent< expandtab< softtabstop< shiftwidth< indentexpr< indentkeys<' 19 20setlocal noautoindent nosmartindent 21setlocal softtabstop = 2 shiftwidth = 2 expandtab 22setlocal indentkeys = !,o,O 23 24if exists ( "*searchpairpos" ) 25 26if ! exists ( 'g:clojure_maxlines' ) 27let g: clojure_maxlines = 300 28endif 29 30if ! exists ( 'g:clojure_fuzzy_indent' ) 31let g: clojure_fuzzy_indent = 1 32endif 33 34if ! exists ( 'g:clojure_fuzzy_indent_patterns' ) 35let g: clojure_fuzzy_indent_patterns = [ '^with' , '^def' , '^let' ] 36endif 37 38if ! exists ( 'g:clojure_fuzzy_indent_blacklist' ) 39let g: clojure_fuzzy_indent_blacklist = [ '-fn$' , '\v^with-%(meta|out-str|loading-context)$' ] 40endif 41 42if ! exists ( 'g:clojure_special_indent_words' ) 43let g: clojure_special_indent_words = 'deftype,defrecord,reify,proxy,extend-type,extend-protocol,letfn' 44endif 45 46if ! exists ( 'g:clojure_align_multiline_strings' ) 47let g: clojure_align_multiline_strings = 0 48endif 49 50if ! exists ( 'g:clojure_align_subforms' ) 51let g: clojure_align_subforms = 0 52endif 53 54function ! s: syn_id_name () 55return synIDattr ( synID ( line ( "." ), col ( "." ), 0 ), "name" ) 56endfunction 57 58function ! s: ignored_region () 59return s: syn_id_name () =~? '\vstring|regex|comment|character' 60endfunction 61 62function ! s: current_char () 63return getline ( '.' )[ col ( '.' ) - 1 ] 64endfunction 65 66function ! s: current_word () 67return getline ( '.' )[ col ( '.' ) - 1 : searchpos ( '\v>' , 'n' , line ( '.' ))[ 1 ] - 2 ] 68endfunction 69 70function ! s: is_paren () 71return s: current_char () =~# '\v[\(\)\[\]\{\}]' && ! s: ignored_region () 72endfunction 73 74" Returns 1 if string matches a pattern in 'patterns', which should be 75" a list of patterns. 76function ! s: match_one ( patterns , string ) 77for pat in a: patterns 78if a: string =~# pat |return 1 |endif 79endfor 80endfunction 81 82function ! s: match_pairs ( open , close , stopat ) 83" Stop only on vector and map [ resp. {. Ignore the ones in strings and 84" comments. 85if a: stopat == 0 && g: clojure_maxlines > 0 86let stopat = max ([ line ( "." ) - g: clojure_maxlines , 0 ]) 87else 88let stopat = a: stopat 89endif 90 91let pos = searchpairpos ( a: open , '' , a: close , 'bWn' , "!s:is_paren()" , stopat ) 92return [ pos [ 0 ], col ( pos )] 93endfunction 94 95function ! s: clojure_check_for_string_worker () 96" Check whether there is the last character of the previous line is 97" highlighted as a string. If so, we check whether it's a ". In this 98" case we have to check also the previous character. The " might be the 99" closing one. In case the we are still in the string, we search for the 100" opening ". If this is not found we take the indent of the line. 101let nb = prevnonblank ( v: lnum - 1 ) 102 103if nb == 0 104return -1 105endif 106 107call cursor ( nb , 0 ) 108call cursor ( 0 , col ( "$" ) - 1 ) 109if s: syn_id_name () !~? "string" 110return -1 111endif 112 113" This will not work for a " in the first column... 114if s: current_char () == '"' 115call cursor ( 0 , col ( "$" ) - 2 ) 116if s: syn_id_name () !~? "string" 117return -1 118endif 119if s: current_char () != '\' 120return -1 121endif 122call cursor ( 0 , col ( "$" ) - 1 ) 123endif 124 125let p = searchpos ( '\(^\|[^\\]\)\zs"' , 'bW' ) 126 127if p != [ 0 , 0 ] 128return p [ 1 ] - 1 129endif 130 131return indent ( "." ) 132endfunction 133 134function ! s: check_for_string () 135let pos = getpos ( '.' ) 136try 137let val = s: clojure_check_for_string_worker () 138finally 139call setpos ( '.' , pos ) 140endtry 141return val 142endfunction 143 144function ! s: strip_namespace_and_macro_chars ( word ) 145return substitute ( a: word , "\\v%(.*/|[#'`~@^,]*)(.*)" , '\1' , '' ) 146endfunction 147 148function ! s: clojure_is_method_special_case_worker ( position ) 149" Find the next enclosing form. 150call search ( '\S' , 'Wb' ) 151 152" Special case: we are at a '(('. 153if s: current_char () == '(' 154return 0 155endif 156call cursor ( a: position ) 157 158let next_paren = s: match_pairs ( '(' , ')' , 0 ) 159 160" Special case: we are now at toplevel. 161if next_paren == [ 0 , 0 ] 162return 0 163endif 164call cursor ( next_paren ) 165 166call search ( '\S' , 'W' ) 167let w = s: strip_namespace_and_macro_chars ( s: current_word ()) 168 169if g: clojure_special_indent_words =~# '\V\<' . w . '\>' 170 171" `letfn` is a special-special-case. 172if w ==# 'letfn' 173" Earlier code left the cursor at: 174" (letfn [...] ...) 175" ^ 176 177" Search and get coordinates of first `[` 178" (letfn [...] ...) 179" ^ 180call search ( '\[' , 'W' ) 181let pos = getcurpos () 182let letfn_bracket = [ pos [ 1 ], pos [ 2 ]] 183 184" Move cursor to start of the form this function was 185" initially called on. Grab the coordinates of the 186" closest outer `[`. 187call cursor ( a: position ) 188let outer_bracket = s: match_pairs ( '\[' , '\]' , 0 ) 189 190" If the located square brackets are not the same, 191" don't use special-case formatting. 192if outer_bracket != letfn_bracket 193return 0 194endif 195endif 196 197return 1 198endif 199 200return 0 201endfunction 202 203function ! s: is_method_special_case ( position ) 204let pos = getpos ( '.' ) 205try 206let val = s: clojure_is_method_special_case_worker ( a: position ) 207finally 208call setpos ( '.' , pos ) 209endtry 210return val 211endfunction 212 213" Check if form is a reader conditional, that is, it is prefixed by #? 214" or #?@ 215function ! s: is_reader_conditional_special_case ( position ) 216return getline ( a: position [ 0 ])[ a: position [ 1 ] - 3 : a: position [ 1 ] - 2 ] == "#?" 217 \|| getline ( a: position [ 0 ])[ a: position [ 1 ] - 4 : a: position [ 1 ] - 2 ] == "#?@" 218endfunction 219 220" Returns 1 for opening brackets, -1 for _anything else_. 221function ! s: bracket_type ( char ) 222return stridx ( '([{' , a: char ) > -1 ? 1 : -1 223endfunction 224 225" Returns: [opening-bracket-lnum, indent] 226function ! s: clojure_indent_pos () 227" Get rid of special case. 228if line ( "." ) == 1 229return [ 0 , 0 ] 230endif 231 232" We have to apply some heuristics here to figure out, whether to use 233" normal lisp indenting or not. 234let i = s: check_for_string () 235if i > -1 236return [ 0 , i + !! g: clojure_align_multiline_strings ] 237endif 238 239call cursor ( 0 , 1 ) 240 241" Find the next enclosing [ or {. We can limit the second search 242" to the line, where the [ was found. If no [ was there this is 243" zero and we search for an enclosing {. 244let paren = s: match_pairs ( '(' , ')' , 0 ) 245let bracket = s: match_pairs ( '\[' , '\]' , paren [ 0 ]) 246let curly = s: match_pairs ( '{' , '}' , bracket [ 0 ]) 247 248" In case the curly brace is on a line later then the [ or - in 249" case they are on the same line - in a higher column, we take the 250" curly indent. 251if curly [ 0 ] > bracket [ 0 ] || curly [ 1 ] > bracket [ 1 ] 252if curly [ 0 ] > paren [ 0 ] || curly [ 1 ] > paren [ 1 ] 253return curly 254endif 255endif 256 257" If the curly was not chosen, we take the bracket indent - if 258" there was one. 259if bracket [ 0 ] > paren [ 0 ] || bracket [ 1 ] > paren [ 1 ] 260return bracket 261endif 262 263" There are neither { nor [ nor (, ie. we are at the toplevel. 264if paren == [ 0 , 0 ] 265return paren 266endif 267 268" Now we have to reimplement lispindent. This is surprisingly easy, as 269" soon as one has access to syntax items. 270" 271" - Check whether we are in a special position after a word in 272" g:clojure_special_indent_words. These are special cases. 273" - Get the next keyword after the (. 274" - If its first character is also a (, we have another sexp and align 275" one column to the right of the unmatched (. 276" - In case it is in lispwords, we indent the next line to the column of 277" the ( + sw. 278" - If not, we check whether it is last word in the line. In that case 279" we again use ( + sw for indent. 280" - In any other case we use the column of the end of the word + 2. 281call cursor ( paren ) 282 283if s: is_method_special_case ( paren ) 284return [ paren [ 0 ], paren [ 1 ] + & shiftwidth - 1 ] 285endif 286 287if s: is_reader_conditional_special_case ( paren ) 288return paren 289endif 290 291" In case we are at the last character, we use the paren position. 292if col ( "$" ) - 1 == paren [ 1 ] 293return paren 294endif 295 296" In case after the paren is a whitespace, we search for the next word. 297call cursor ( 0 , col ( '.' ) + 1 ) 298if s: current_char () == ' ' 299call search ( '\v\S' , 'W' ) 300endif 301 302" If we moved to another line, there is no word after the (. We 303" use the ( position for indent. 304if line ( "." ) > paren [ 0 ] 305return paren 306endif 307 308" We still have to check, whether the keyword starts with a (, [ or {. 309" In that case we use the ( position for indent. 310let w = s: current_word () 311if s: bracket_type ( w [ 0 ]) == 1 312return paren 313endif 314 315" If the keyword begins with #, check if it is an anonymous 316" function or set, in which case we indent by the shiftwidth 317" (minus one if g:clojure_align_subforms = 1), or if it is 318" ignored, in which case we use the ( position for indent. 319if w [ 0 ] == "#" 320" TODO: Handle #=() and other rare reader invocations? 321if w [ 1 ] == '(' || w [ 1 ] == '{' 322return [ paren [ 0 ], paren [ 1 ] + ( g: clojure_align_subforms ? 0 : & shiftwidth - 1 )] 323elseif w [ 1 ] == '_' 324return paren 325endif 326endif 327 328" Test words without namespace qualifiers and leading reader macro 329" metacharacters. 330" 331" e.g. clojure.core/defn and #'defn should both indent like defn. 332let ww = s: strip_namespace_and_macro_chars ( w ) 333 334if & lispwords =~# '\V\<' . ww . '\>' 335return [ paren [ 0 ], paren [ 1 ] + & shiftwidth - 1 ] 336endif 337 338if g: clojure_fuzzy_indent 339 \&& ! s: match_one ( g: clojure_fuzzy_indent_blacklist , ww ) 340 \&& s: match_one ( g: clojure_fuzzy_indent_patterns , ww ) 341return [ paren [ 0 ], paren [ 1 ] + & shiftwidth - 1 ] 342endif 343 344call search ( '\v\_s' , 'cW' ) 345call search ( '\v\S' , 'W' ) 346if paren [ 0 ] < line ( "." ) 347return [ paren [ 0 ], paren [ 1 ] + ( g: clojure_align_subforms ? 0 : & shiftwidth - 1 )] 348endif 349 350call search ( '\v\S' , 'bW' ) 351return [ line ( '.' ), col ( '.' ) + 1 ] 352endfunction 353 354function ! GetClojureIndent () 355let lnum = line ( '.' ) 356let orig_lnum = lnum 357let orig_col = col ( '.' ) 358let [ opening_lnum , indent ] = s: clojure_indent_pos () 359 360" Account for multibyte characters 361if opening_lnum > 0 362let indent -= indent - virtcol ([ opening_lnum , indent ]) 363endif 364 365" Return if there are no previous lines to inherit from 366if opening_lnum < 1 || opening_lnum >= lnum - 1 367call cursor ( orig_lnum , orig_col ) 368return indent 369endif 370 371let bracket_count = 0 372 373" Take the indent of the first previous non-white line that is 374" at the same sexp level. cf. src/misc1.c:get_lisp_indent() 375while 1 376let lnum = prevnonblank ( lnum - 1 ) 377let col = 1 378 379if lnum <= opening_lnum 380break 381endif 382 383call cursor ( lnum , col ) 384 385" Handle bracket counting edge case 386if s: is_paren () 387let bracket_count += s: bracket_type ( s: current_char ()) 388endif 389 390while 1 391if search ( '\v[(\[{}\])]' , '' , lnum ) < 1 392break 393elseif ! s: ignored_region () 394let bracket_count += s: bracket_type ( s: current_char ()) 395endif 396endwhile 397 398if bracket_count == 0 399" Check if this is part of a multiline string 400call cursor ( lnum , 1 ) 401if s: syn_id_name () !~? '\vstring|regex' 402call cursor ( orig_lnum , orig_col ) 403return indent ( lnum ) 404endif 405endif 406endwhile 407 408call cursor ( orig_lnum , orig_col ) 409return indent 410endfunction 411 412setlocal indentexpr = GetClojureIndent () 413 414else 415 416" In case we have searchpairpos not available we fall back to 417" normal lisp indenting. 418setlocal indentexpr = 419setlocal lisp 420let b: undo_indent .= '| setlocal lisp<' 421 422endif 423 424let & cpo = s: save_cpo 425unlet ! s: save_cpo 426 427" vim:sts=8:sw=8:ts=8:noet