robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
adcd9360df
master
1" Vim indent file 2" Language: graphql 3" Maintainer: Jon Parise <jon@indelible.org> 4" Filenames: *.graphql *.graphqls *.gql 5" URL: https://github.com/jparise/vim-graphql 6" License: MIT <https://opensource.org/license/mit> 7" Last Change: 2024 Dec 21 8 9" Set our local options if indentation hasn't already been set up. 10" This generally means we've been detected as the primary filetype. 11if ! exists ( 'b:did_indent' ) 12setlocal autoindent 13setlocal nocindent 14setlocal nolisp 15setlocal nosmartindent 16 17setlocal indentexpr = GetGraphQLIndent () 18setlocal indentkeys = 0{,0},0),0[,0],0#,!^F,o,O 19 20let b: did_indent = 1 21endif 22 23" If our indentation function already exists, we have nothing more to do. 24if exists ( '*GetGraphQLIndent' ) 25finish 26endif 27 28let s: cpo_save = & cpoptions 29set cpoptions & vim 30 31" searchpair() skip expression that matches in comments and strings. 32let s: pair_skip_expr = 33 \'synIDattr(synID(line("."), col("."), 0), "name") =~? "comment\\|string"' 34 35" Check if the character at lnum:col is inside a string. 36function s: InString ( lnum , col ) 37return synIDattr ( synID ( a: lnum , a: col , 1 ), 'name' ) ==# 'graphqlString' 38endfunction 39 40function GetGraphQLIndent () 41" If this is the first non-blank line, we have nothing more to do because 42" all of our indentation rules are based on matching against earlier lines. 43let l: prevlnum = prevnonblank ( v: lnum - 1 ) 44if l: prevlnum == 0 45return 0 46endif 47 48" If the previous line isn't GraphQL, assume we're part of a template 49" string and indent this new line within it. 50let l: stack = map ( synstack ( l: prevlnum , 1 ), "synIDattr(v:val, 'name')" ) 51if get ( l: stack , -1 ) !~# '^graphql' 52return indent ( l: prevlnum ) + shiftwidth () 53endif 54 55let l: line = getline ( v: lnum ) 56 57" If this line contains just a closing bracket, find its matching opening 58" bracket and indent the closing bracket to match. 59let l: col = matchend ( l: line , '^\s*[]})]' ) 60if l: col > 0 && ! s: InString ( v: lnum , l: col ) 61call cursor ( v: lnum , l: col ) 62 63let l: bracket = l: line [ l: col - 1 ] 64if l: bracket ==# '}' 65let l: matched = searchpair ( '{' , '' , '}' , 'bW' , s: pair_skip_expr ) 66elseif l: bracket ==# ']' 67let l: matched = searchpair ( '\[' , '' , '\]' , 'bW' , s: pair_skip_expr ) 68elseif l: bracket ==# ')' 69let l: matched = searchpair ( '(' , '' , ')' , 'bW' , s: pair_skip_expr ) 70else 71let l: matched = -1 72endif 73 74return l: matched > 0 ? indent ( l: matched ) : virtcol ( '.' ) - 1 75endif 76 77" If we're inside of a multiline string, continue with the same indentation. 78if s: InString ( v: lnum , matchend ( l: line , '^\s*' ) + 1 ) 79return indent ( v: lnum ) 80endif 81 82" If the previous line ended with an opening bracket, indent this line. 83if getline ( l: prevlnum ) =~# '\%(#.*\)\@<![[{(]\s*$' 84return indent ( l: prevlnum ) + shiftwidth () 85endif 86 87" Default to the existing indentation level. 88return indent ( l: prevlnum ) 89endfunction 90 91let & cpoptions = s: cpo_save 92unlet s: cpo_save