robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f5d59340a6
master
1" Vim indent file 2" Language: gdscript (Godot game engine) 3" Maintainer: Maxim Kim <habamax@gmail.com> 4" Based on python indent file. 5" 6" This file has been manually translated from Vim9 script. 7 8if exists ( "b:did_indent" ) 9finish 10endif 11let b: did_indent = 1 12 13let s: save_cpo = & cpo 14set cpo & vim 15 16let s: undo_opts = "setl indentexpr< indentkeys< lisp< autoindent<" 17 18if exists ( 'b:undo_indent' ) 19let b: undo_indent ..= "|" .. s: undo_opts 20else 21let b: undo_indent = s: undo_opts 22endif 23 24setlocal nolisp 25setlocal autoindent 26setlocal indentexpr = s :GDScriptIndent() 27setlocal indentkeys += <:>,=elif,=except 28 29 30function s: GDScriptIndent () abort 31" If this line is explicitly joined: If the previous line was also joined, 32" line it up with that one, otherwise add two 'shiftwidth' 33if getline ( v: lnum - 1 ) =~# '\\$' 34if v: lnum > 1 && getline ( v: lnum - 2 ) =~# '\\$' 35return indent ( v: lnum - 1 ) 36endif 37return indent ( v: lnum - 1 ) + ( shiftwidth () * 2 ) 38endif 39 40" If the start of the line is in a string don't change the indent. 41if has ( 'syntax_items' ) && synIDattr ( synID ( v: lnum , 1 , 1 ), "name" ) =~# "String$" 42return -1 43endif 44 45" Search backwards for the previous non-empty line. 46let plnum = prevnonblank ( v: lnum - 1 ) 47 48if plnum == 0 49" This is the first non-empty line, use zero indent. 50return 0 51endif 52 53let plindent = indent ( plnum ) 54let plnumstart = plnum 55 56" Get the line and remove a trailing comment. 57" Use syntax highlighting attributes when possible. 58let pline = getline ( plnum ) 59let pline_len = strlen ( pline ) 60if has ( 'syntax_items' ) 61" If the last character in the line is a comment, do a binary search for 62" the start of the comment. synID() is slow, a linear search would take 63" too long on a long line. 64if synIDattr ( synID ( plnum , pline_len , 1 ), "name" ) =~# "\\(Comment\\|Todo\\)$" 65let min = 1 66let max = pline_len 67while min < max 68let col = ( min + max ) / 2 69if synIDattr ( synID ( plnum , col , 1 ), "name" ) =~# "\\(Comment\\|Todo\\)$" 70let max = col 71else 72let min = col + 1 73endif 74endwhile 75let pline = strpart ( pline , 0 , min - 1 ) 76endif 77else 78let col = 0 79while col < pline_len 80if pline [ col ] ==# '#' 81let pline = strpart ( pline , 0 , col ) 82break 83endif 84let col = col + 1 85endwhile 86endif 87 88 89" When "inside" parenthesis: If at the first line below the parenthesis add 90" one 'shiftwidth' ("inside" is simplified and not really checked) 91" my_var = ( 92" a 93" + b 94" + c 95" ) 96if pline =~# '[({\[]\s*$' 97return indent ( plnum ) + shiftwidth () 98endif 99 100 101" If the previous line ended with a colon, indent this line 102if pline =~# ':\s*$' 103return plindent + shiftwidth () 104endif 105 106" If the previous line was a stop-execution statement... 107if getline ( plnum ) =~# '^\s*\(break\|continue\|raise\|return\|pass\)\>' 108" See if the user has already dedented 109if indent ( v: lnum ) > indent ( plnum ) - shiftwidth () 110" If not, recommend one dedent 111return indent ( plnum ) - shiftwidth () 112endif 113" Otherwise, trust the user 114return -1 115endif 116 117" If the current line begins with a keyword that lines up with "try" 118if getline ( v: lnum ) =~# '^\s*\(except\|finally\)\>' 119let lnum = v: lnum - 1 120while lnum >= 1 121if getline ( lnum ) =~# '^\s*\(try\|except\)\>' 122let ind = indent ( lnum ) 123if ind >= indent ( v: lnum ) 124return -1 " indent is already less than this 125endif 126return ind " line up with previous try or except 127endif 128let lnum = lnum - 1 129endwhile 130return -1 " no matching "try"! 131endif 132 133 134" If the current line begins with a header keyword, dedent 135if getline ( v: lnum ) =~# '^\s*\(elif\|else\)\>' 136 137" Unless the previous line was a one-liner 138if getline ( plnumstart ) =~# '^\s*\(for\|if\|try\)\>' 139return plindent 140endif 141 142" Or the user has already dedented 143if indent ( v: lnum ) <= plindent - shiftwidth () 144return -1 145endif 146 147return plindent - shiftwidth () 148endif 149 150return -1 151endfunction 152 153let & cpo = s: save_cpo 154unlet s: save_cpo