Skip to content

Use base &indentexpr for non-GraphQL indentation #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions after/indent/javascript.vim
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,22 @@
" Language: GraphQL
" Maintainer: Jon Parise <[email protected]>

runtime! indent/graphql.vim

" Don't redefine our function and also require the standard Javascript indent
" function to exist.
if exists('*GetJavascriptGraphQLIndent') || !exists('*GetJavascriptIndent')
if exists('*GetJavascriptGraphQLIndent') && !empty(&indentexpr)
finish
endif

runtime! indent/graphql.vim

" Set the indentexpr with our own version that will call GetGraphQLIndent when
" we're inside of a GraphQL string and otherwise defer to GetJavascriptIndent.
" we're inside of a GraphQL string and otherwise defer to the base function.
let b:indentexpr_base = &indentexpr
setlocal indentexpr=GetJavascriptGraphQLIndent()

function GetJavascriptGraphQLIndent()
let l:stack = map(synstack(v:lnum, 1), "synIDattr(v:val,'name')")
if !empty(l:stack) && l:stack[0] ==# 'graphqlTemplateString'
let l:stack = map(synstack(v:lnum, 1), "synIDattr(v:val, 'name')")
if get(l:stack, 0) ==# 'graphqlTemplateString'
return GetGraphQLIndent()
endif

return GetJavascriptIndent()
return eval(b:indentexpr_base)
endfunction
17 changes: 8 additions & 9 deletions after/indent/typescript.vim
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,22 @@
" Language: GraphQL
" Maintainer: Jon Parise <[email protected]>

runtime! indent/graphql.vim

" Don't redefine our function and also require the standard Typescript indent
" function to exist.
if exists('*GetTypescriptGraphQLIndent') || !exists('*GetTypescriptIndent')
if exists('*GetTypescriptGraphQLIndent') && !empty(&indentexpr)
finish
endif

runtime! indent/graphql.vim

" Set the indentexpr with our own version that will call GetGraphQLIndent when
" we're inside of a GraphQL string and otherwise defer to GetTypescriptIndent.
" we're inside of a GraphQL string and otherwise defer to the base function.
let b:indentexpr_base = &indentexpr
setlocal indentexpr=GetTypescriptGraphQLIndent()

function GetTypescriptGraphQLIndent()
let l:stack = map(synstack(v:lnum, 1), "synIDattr(v:val,'name')")
if !empty(l:stack) && l:stack[0] ==# 'graphqlTemplateString'
let l:stack = map(synstack(v:lnum, 1), "synIDattr(v:val, 'name')")
if get(l:stack, 0) ==# 'graphqlTemplateString'
return GetGraphQLIndent()
endif

return GetTypescriptIndent()
return eval(b:indentexpr_base)
endfunction
1 change: 0 additions & 1 deletion test/javascript/default.vader
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ Execute (Syntax assertions):

Execute (Indent assertions):
Assert exists('*GetGraphQLIndent')
Assert exists('*GetJavascriptIndent')
Assert exists('*GetJavascriptGraphQLIndent')

Do (re-indent buffer):
Expand Down
1 change: 0 additions & 1 deletion test/javascript/react.vader
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ Execute (Syntax assertions):

Execute (Indent assertions):
Assert exists('*GetGraphQLIndent')
Assert exists('*GetJavascriptIndent')
Assert exists('*GetJavascriptGraphQLIndent')
1 change: 0 additions & 1 deletion test/typescript/default.vader
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ Execute (Syntax assertions):

Execute (Indent assertions):
Assert exists('*GetGraphQLIndent')
Assert exists('*GetTypescriptIndent')
Assert exists('*GetTypescriptGraphQLIndent')

Do (re-indent buffer):
Expand Down